Skip to content

Commit

Permalink
Clean up the IDE integration code. (project-chip#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
tecimovic authored Dec 5, 2023
1 parent 6e8156d commit 5f1f3a6
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 32 deletions.
16 changes: 14 additions & 2 deletions src-electron/ide-integration/studio-rest-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ const queryNotification = require('../db/query-session-notification.js')
const wsServer = require('../server/ws-server.js')
const dbEnum = require('../../src-shared/db-enum.js')
const { StatusCodes } = require('http-status-codes')
const zcl = require('./zcl.js')
const zclComponents = require('./zcl-components.js')
import WebSocket from 'ws'
import { projectName } from '../util/studio-util'

const StudioRestAPI = {
GetProjectInfo: '/rest/clic/components/all/project/',
AddComponent: '/rest/clic/component/add/project/',
RemoveComponent: '/rest/clic/component/remove/project/',
DependsComponent: '/rest/clic/component/depends/project/',
}

const StudioWsAPI = {
WsServerNotification: '/ws/clic/server/notifications/project/',
}

const localhost = 'http://127.0.0.1:'
const wsLocalhost = 'ws://127.0.0.1:'

Expand Down Expand Up @@ -198,7 +209,7 @@ async function updateComponentByClusterIdAndComponentId(
// retrieve components to enable
let promises = []
if (clusterId) {
let ids = zcl
let ids = zclComponents
.getComponentIdsByCluster(db, sessionId, clusterId, side)
.then((response) => Promise.resolve(response.componentIds))
promises.push(ids)
Expand Down Expand Up @@ -516,3 +527,4 @@ exports.initIdeIntegration = initIdeIntegration
exports.deinitIdeIntegration = deinitIdeIntegration
exports.sendSessionCreationErrorStatus = sendSessionCreationErrorStatus
exports.sendComponentUpdateStatus = sendComponentUpdateStatus
exports.isComponentTogglingDisabled = isComponentTogglingDisabled
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
* @module REST API: user data
*/

const queryPackage = require('../db/query-package.js')
const queryZcl = require('../db/query-zcl.js')
const dbEnum = require('../../src-shared/db-enum.js')
const util = require('../util/util.js')
const queryPackage = require('../db/query-package')
const queryZcl = require('../db/query-zcl')
const dbEnum = require('../../src-shared/db-enum')
const util = require('../util/util')
const env = require('../util/env')

/**
* Promise that return a list of component Ids required by a specific cluster
Expand All @@ -34,27 +35,29 @@ const util = require('../util/util.js')
* @param {*} side
* @returns {*} - {componentIds, clusterId, clusterLabel, side}
*/
function getComponentIdsByCluster(db, sessionId, clusterId, side) {
async function getComponentIdsByCluster(db, sessionId, clusterId, side) {
// enable components
let extensions = undefined
return queryPackage
.getSessionPackagesByType(
db,
sessionId,
dbEnum.packageType.genTemplatesJson
)
.then((pkgs) => (pkgs.length == 0 ? null : pkgs[0].id))
.then((id) => {
return queryPackage.selectPackageExtension(
db,
id,
dbEnum.packageExtensionEntity.cluster
)
})
.then((ext) => {
extensions = ext
return queryZcl.selectClusterById(db, clusterId)
})
let pkgs = await queryPackage.getSessionPackagesByType(
db,
sessionId,
dbEnum.packageType.genTemplatesJson
)
let id = pkgs.length == 0 ? null : pkgs[0].id
if (id == null) {
return {
componentIds: [],
clusterId,
side,
}
}

let extensions = await queryPackage.selectPackageExtension(
db,
id,
dbEnum.packageExtensionEntity.cluster
)
return queryZcl
.selectClusterById(db, clusterId)
.then((cluster) => {
let componentIds = []
if (cluster) {
Expand All @@ -71,27 +74,29 @@ function getComponentIdsByCluster(db, sessionId, clusterId, side) {
}
})
} else {
console.log(`Failed to retrieve cluster via clusterId(${clusterId}).`)
env.logWarning(
`Failed to retrieve cluster via clusterId(${clusterId}).`
)
}

return Promise.resolve({
return {
componentIds,
clusterId,
clusterLabel: cluster.label.toLowerCase(),
side,
})
}
})
.catch((err) => {
console.log(
env.logWarning(
`Failed to retrieve component ids required by clusterId(${clusterId}) from cluster extension mapping.`,
err
)

return Promise.resolve({
return {
componentIds: [],
clusterId,
side,
})
}
})
}

Expand Down
104 changes: 104 additions & 0 deletions test/ide-integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
*
* Copyright (c) 2023 Silicon Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* @jest-environment node
*/
const genEngine = require('../src-electron/generator/generation-engine')
const env = require('../src-electron/util/env')
const dbApi = require('../src-electron/db/db-api')
const queryPackage = require('../src-electron/db/query-package')
const zclLoader = require('../src-electron/zcl/zcl-loader')
const importJs = require('../src-electron/importexport/import')
const testUtil = require('./test-util')
const studioRestApi = require('../src-electron/ide-integration/studio-rest-api')
const zclComponents = require('../src-electron/ide-integration/zcl-components')

let db
const templateCount = testUtil.testTemplate.zigbeeCount
const testFile = testUtil.zigbeeTestFile.threeEp

beforeAll(async () => {
env.setDevelopmentEnv()
let file = env.sqliteTestFile('ide-integration')
db = await dbApi.initDatabaseAndLoadSchema(
file,
env.schemaFile(),
env.zapVersion()
)
return zclLoader.loadZcl(db, env.builtinSilabsZclMetafile())
}, testUtil.timeout.medium())

afterAll(() => dbApi.closeDatabase(db), testUtil.timeout.short())

let templateContext

test(
'Load templates',
async () => {
let context = await genEngine.loadTemplates(
db,
testUtil.testTemplate.zigbee
)
expect(context.crc).not.toBeNull()
expect(context.templateData).not.toBeNull()
expect(context.templateData.name).toEqual('Test templates')
expect(context.templateData.version).toEqual('test-v1')
expect(context.templateData.templates.length).toEqual(templateCount)
expect(context.packageId).not.toBeNull()
templateContext = context
},
testUtil.timeout.medium()
)

test(
'Validate package loading',
async () => {
templateContext.packages = await queryPackage.getPackageByParent(
templateContext.db,
templateContext.packageId
)
expect(templateContext.packages.length).toBe(templateCount - 1 + 2) // -1 for ignored one, one for helper and one for overridable
},
testUtil.timeout.short()
)

test(
`Ide integration`,
async () => {
let { sessionId, errors, warnings } = await importJs.importDataFromFile(
db,
testFile
)
expect(errors.length).toBe(0)
expect(warnings.length).toBe(0)
expect(sessionId).not.toBeNull()

let x = await studioRestApi.integrationEnabled(db, sessionId)
let y = await studioRestApi.isComponentTogglingDisabled(db, sessionId)
expect(x).toBeFalsy()
expect(y).toBeFalsy()

let res = await zclComponents.getComponentIdsByCluster(
db,
sessionId,
1,
'client'
)
expect(res.componentIds.length).toBe(0)
},
testUtil.timeout.long()
)

0 comments on commit 5f1f3a6

Please sign in to comment.