Skip to content

Commit

Permalink
getting device type ref after the device types are inserted to mainta…
Browse files Browse the repository at this point in the history
…in db integrity
  • Loading branch information
paulr34 committed Aug 2, 2024
1 parent 99b678b commit 08c1ed0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 49 deletions.
74 changes: 46 additions & 28 deletions src-electron/db/query-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -934,65 +934,56 @@ async function insertAtomics(db, packageId, data) {
* If they do not match, it performs an insert with the composition's type.
*
* @param {*} db - The database connection object.
* @param {*} packageId - The package ID to associate with the endpoint composition.
* @param {*} composition - The composition data to be inserted.
* @param {*} context - The context containing the mandatory device type to check against.
* @returns A promise resolved with the result of the database insert operation.
*/
function insertEndpointComposition(db, packageId, composition, context) {
function insertEndpointComposition(db, composition, context) {
if (parseInt(context.mandatoryDeviceTypes, 16) === composition.code) {
return dbApi.dbInsert(
db,
'INSERT INTO ENDPOINT_COMPOSITION (PACKAGE_REF, TYPE, CODE) VALUES (?, ?, ?)',
[packageId, dbEnum.composition.rootNode, composition.code]
'INSERT INTO ENDPOINT_COMPOSITION (TYPE, CODE) VALUES (?, ?)',
[dbEnum.composition.mandatoryEndpoint, composition.code]
)
} else {
return dbApi.dbInsert(
db,
'INSERT INTO ENDPOINT_COMPOSITION (PACKAGE_REF, TYPE, CODE) VALUES (?, ?, ?)',
[packageId, composition.compositionType, composition.code]
'INSERT INTO ENDPOINT_COMPOSITION (TYPE, CODE) VALUES (?, ?)',
[composition.compositionType, composition.code]
)
}
}

/**
* Asynchronously retrieves the ID of an endpoint composition based on its code and associated package ID.
* Asynchronously retrieves the ID of an endpoint composition based on its code.
*
* @param {Object} db - The database connection object.
* @param {Object} deviceType - An object representing the device type, which contains the 'code' property.
* @param {number} packageId - The ID of the package associated with the endpoint composition.
* @returns {Promise<number|null>} A promise that resolves with the ID of the endpoint composition if found, or null otherwise.
*/
async function getEndpointCompositionIdByCode(db, deviceType, packageId) {
async function getEndpointCompositionIdByCode(db, deviceType) {
const query =
'SELECT ENDPOINT_COMPOSITION_ID FROM ENDPOINT_COMPOSITION WHERE CODE = ? AND PACKAGE_REF = ?'
const result = await dbApi.dbGet(db, query, [deviceType.code, packageId])
'SELECT ENDPOINT_COMPOSITION_ID FROM ENDPOINT_COMPOSITION WHERE CODE = ?'
const result = await dbApi.dbGet(db, query, [deviceType.code])
return result ? result.ENDPOINT_COMPOSITION_ID : null
}

/**
* Inserts a new device composition record into the database.
*
* @param {Object} db - The database connection object.
* @param {number} packageId - The ID of the package associated with this device composition.
* @param {Object} deviceType - An object representing the device type, which contains the 'childDeviceId' property.
* @param {number} endpointCompositionId - The ID of the endpoint composition associated with this device composition.
* @returns {Promise} A promise that resolves with the result of the database insertion operation.
*/

function insertDeviceComposition(
db,
packageId,
deviceType,
endpointCompositionId
) {
function insertDeviceComposition(db, deviceType, endpointCompositionId) {
const insertQuery = `
INSERT INTO DEVICE_COMPOSITION (PACKAGE_REF, CODE, ENDPOINT_COMPOSITION_REF)
INSERT INTO DEVICE_COMPOSITION (CODE, ENDPOINT_COMPOSITION_REF)
VALUES (?, ?, ?)
`
return dbApi.dbInsert(db, insertQuery, [
packageId,
deviceType.childDeviceId,
parseInt(deviceType.childDeviceId, 16),
endpointCompositionId,
])
}
Expand Down Expand Up @@ -1055,13 +1046,40 @@ async function insertDeviceTypes(db, packageId, data) {
}
})
)
).then((dtClusterRefDataPairs) => {
let promises = []
promises.push(insertDeviceTypeAttributes(db, dtClusterRefDataPairs))
promises.push(insertDeviceTypeCommands(db, dtClusterRefDataPairs))
promises.push(insertDeviceTypeFeatures(db, dtClusterRefDataPairs))
return Promise.all(promises)
})
)
.then((dtClusterRefDataPairs) => {
let promises = []
promises.push(
insertDeviceTypeAttributes(db, dtClusterRefDataPairs)
)
promises.push(insertDeviceTypeCommands(db, dtClusterRefDataPairs))
promises.push(insertDeviceTypeFeatures(db, dtClusterRefDataPairs))
return Promise.all(promises)
})
.then(() => {
// Update ENDPOINT_COMPOSITION with DEVICE_TYPE_REF
const updateEndpointComposition = `
UPDATE ENDPOINT_COMPOSITION
SET DEVICE_TYPE_REF = (
SELECT DEVICE_TYPE_ID
FROM DEVICE_TYPE
WHERE DEVICE_TYPE.CODE = ENDPOINT_COMPOSITION.CODE
)
`
return dbApi.dbAll(db, updateEndpointComposition)
})
.then(() => {
// Update DEVICE_COMPOSITION with DEVICE_TYPE_REF
const updateDeviceComposition = `
UPDATE DEVICE_COMPOSITION
SET DEVICE_TYPE_REF = (
SELECT DEVICE_TYPE_ID
FROM DEVICE_TYPE
WHERE DEVICE_TYPE.CODE = DEVICE_COMPOSITION.CODE
)
`
return dbApi.dbAll(db, updateDeviceComposition)
})
}
}
return zclIdsPromises
Expand Down
4 changes: 2 additions & 2 deletions src-electron/db/zap-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ CREATE TABLE IF NOT EXISTS "DEVICE_TYPE" (
*/
CREATE TABLE IF NOT EXISTS "ENDPOINT_COMPOSITION" (
"ENDPOINT_COMPOSITION_ID" integer PRIMARY KEY AUTOINCREMENT,
"PACKAGE_REF" integer,
"DEVICE_TYPE_REF" integer,
"TYPE" text,
"CODE" integer,
FOREIGN KEY ("PACKAGE_REF") REFERENCES "PACKAGE"("PACKAGE_ID") ON DELETE CASCADE ON UPDATE CASCADE
Expand All @@ -394,8 +394,8 @@ CREATE TABLE IF NOT EXISTS "ENDPOINT_COMPOSITION" (
*/
CREATE TABLE IF NOT EXISTS "DEVICE_COMPOSITION" (
"DEVICE_COMPOSITION_ID" integer PRIMARY KEY AUTOINCREMENT,
"PACKAGE_REF" integer,
"CODE" integer,
"DEVICE_TYPE_REF" integer,
"ENDPOINT_COMPOSITION_REF" integer,
"CONFORMANCE" text,
"CONSTRAINT" integer,
Expand Down
24 changes: 8 additions & 16 deletions src-electron/zcl/zcl-loader-silabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1674,26 +1674,18 @@ async function processDeviceTypes(db, filePath, packageId, data, context) {
for (let deviceType of deviceTypes) {
if (
deviceType.compositionType != null ||
deviceType.code === parseInt(context.mandatoryDeviceTypes, 16)
deviceType.code == parseInt(context.mandatoryDeviceTypes, 16)
) {
await queryLoader.insertEndpointComposition(
db,
packageId,
deviceType,
context
)
let endpointCompositionId =
await queryLoader.getEndpointCompositionIdByCode(
await queryLoader.insertEndpointComposition(db, deviceType, context)
if (deviceType.code !== parseInt(context.mandatoryDeviceTypes, 16)) {
let endpointCompositionId =
await queryLoader.getEndpointCompositionIdByCode(db, deviceType)
await queryLoader.insertDeviceComposition(
db,
deviceType,
packageId
endpointCompositionId
)
await queryLoader.insertDeviceComposition(
db,
packageId,
deviceType,
endpointCompositionId
)
}
}
}
return queryLoader.insertDeviceTypes(db, packageId, deviceTypes)
Expand Down
2 changes: 1 addition & 1 deletion src-shared/db-enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ exports.storageOption = {
exports.composition = {
fullFamily: 'fullFamily',
tree: 'tree',
rootNode: 'rootNode',
mandatoryEndpoint: 'mandatoryEndpoint',
}

exports.zclType = {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/ZapConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -713,12 +713,12 @@ export default {
}
if (
this.zclPropertiesRow.length == currentZapFileZclPacakges.length ||
this.zclPropertiesRow.length == currentZapFileZclPackages.length ||
this.zclPropertiesRow.length == 1
) {
// We shortcut this page, if the number of packages in the zap file
// and the number of packages loaded in the backend are the same.
if (this.zclGenRow.length == currentZapFileTemplatePacakges.length) {
if (this.zclGenRow.length == currentZapFileTemplatePackages.length) {
this.selectedZclPropertiesDataIds = this.zclPropertiesRow.map(
(zpr) => zpr.id
)
Expand Down

0 comments on commit 08c1ed0

Please sign in to comment.