Skip to content

Commit 28f4483

Browse files
authored
Endpoint composition xml update and loader enhancements for Conformance and Constraints (#1406)
* update XMl to be consistent with the Matter spec * loading conformance and constraint of Matter Device Composition and inserting into the database * cleanup * update schema diagram * fixing formatting with prettier update
1 parent 33637fe commit 28f4483

File tree

6 files changed

+2567
-2531
lines changed

6 files changed

+2567
-2531
lines changed

docs/zap-schema.svg

+2,451-2,451
Loading

src-electron/db/query-loader.js

+54-28
Original file line numberDiff line numberDiff line change
@@ -938,54 +938,80 @@ async function insertAtomics(db, packageId, data) {
938938
* @param {*} context - The context containing the mandatory device type to check against.
939939
* @returns A promise resolved with the result of the database insert operation.
940940
*/
941-
function insertEndpointComposition(db, composition, context) {
942-
if (parseInt(context.mandatoryDeviceTypes, 16) === composition.code) {
943-
return dbApi.dbInsert(
944-
db,
945-
'INSERT INTO ENDPOINT_COMPOSITION (TYPE, CODE) VALUES (?, ?)',
946-
[dbEnum.composition.mandatoryEndpoint, composition.code],
947-
)
948-
} else {
949-
return dbApi.dbInsert(
950-
db,
951-
'INSERT INTO ENDPOINT_COMPOSITION (TYPE, CODE) VALUES (?, ?)',
952-
[composition.compositionType, composition.code],
953-
)
941+
async function insertEndpointComposition(db, composition, context) {
942+
try {
943+
if (parseInt(context.mandatoryDeviceTypes, 16) === composition.code) {
944+
return await dbApi.dbInsert(
945+
db,
946+
'INSERT INTO ENDPOINT_COMPOSITION (TYPE, CODE) VALUES (?, ?)',
947+
[dbEnum.composition.mandatoryEndpoint, composition.code],
948+
)
949+
} else {
950+
return await dbApi.dbInsert(
951+
db,
952+
'INSERT INTO ENDPOINT_COMPOSITION (TYPE, CODE) VALUES (?, ?)',
953+
[composition.compositionType, composition.code],
954+
)
955+
}
956+
} catch (error) {
957+
console.error('Error inserting endpoint composition:', error)
958+
throw error // Re-throw the error after logging it
954959
}
955960
}
956961

957962
/**
958-
* Asynchronously retrieves the ID of an endpoint composition based on its code.
963+
* Retrieves the endpoint composition ID by device code.
964+
*
965+
* This function executes a SQL query to fetch the endpoint composition ID
966+
* associated with a given device code. If the query fails, an error is logged.
959967
*
960968
* @param {Object} db - The database connection object.
961-
* @param {Object} deviceType - An object representing the device type, which contains the 'code' property.
962-
* @returns {Promise<number|null>} A promise that resolves with the ID of the endpoint composition if found, or null otherwise.
969+
* @param {Object} deviceType - The device type object containing the device code.
970+
* @returns {Promise<number|null>} The endpoint composition ID or null if not found.
963971
*/
964972
async function getEndpointCompositionIdByCode(db, deviceType) {
965973
const query =
966974
'SELECT ENDPOINT_COMPOSITION_ID FROM ENDPOINT_COMPOSITION WHERE CODE = ?'
967-
const result = await dbApi.dbGet(db, query, [deviceType.code])
968-
return result ? result.ENDPOINT_COMPOSITION_ID : null
975+
try {
976+
const result = await dbApi.dbGet(db, query, [deviceType.code])
977+
return result ? result.ENDPOINT_COMPOSITION_ID : null
978+
} catch (error) {
979+
console.error('Error retrieving endpoint composition ID:', error)
980+
return null
981+
}
969982
}
970983

971984
/**
972-
* Inserts a new device composition record into the database.
985+
* Inserts a device composition record into the DEVICE_COMPOSITION table.
986+
*
987+
* This function constructs an SQL INSERT query to add a new record to the
988+
* DEVICE_COMPOSITION table. It handles the insertion of the device code,
989+
* endpoint composition reference, conformance, and constraint values.
990+
* Note that the "CONSTRAINT" column name is escaped with double quotes
991+
* to avoid conflicts with the SQL reserved keyword.
973992
*
974993
* @param {Object} db - The database connection object.
975-
* @param {Object} deviceType - An object representing the device type, which contains the 'childDeviceId' property.
976-
* @param {number} endpointCompositionId - The ID of the endpoint composition associated with this device composition.
977-
* @returns {Promise} A promise that resolves with the result of the database insertion operation.
994+
* @param {Object} deviceType - The device type object containing the data to be inserted.
995+
* @param {number} endpointCompositionId - The ID of the endpoint composition.
996+
* @returns {Promise} A promise that resolves when the insertion is complete.
978997
*/
979998

980999
function insertDeviceComposition(db, deviceType, endpointCompositionId) {
9811000
const insertQuery = `
982-
INSERT INTO DEVICE_COMPOSITION (CODE, ENDPOINT_COMPOSITION_REF)
983-
VALUES (?, ?)
1001+
INSERT INTO DEVICE_COMPOSITION (CODE, ENDPOINT_COMPOSITION_REF, CONFORMANCE, DEVICE_CONSTRAINT)
1002+
VALUES (?, ?, ?, ?)
9841003
`
985-
return dbApi.dbInsert(db, insertQuery, [
986-
parseInt(deviceType.childDeviceId, 16),
987-
endpointCompositionId,
988-
])
1004+
try {
1005+
return dbApi.dbInsert(db, insertQuery, [
1006+
parseInt(deviceType.childDeviceId, 16),
1007+
endpointCompositionId,
1008+
deviceType.conformance,
1009+
deviceType.constraint,
1010+
])
1011+
} catch (error) {
1012+
console.error('Error inserting device composition:', error)
1013+
throw error // Re-throw the error after logging it
1014+
}
9891015
}
9901016

9911017
/**

src-electron/db/zap-schema.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,11 @@ CREATE TABLE IF NOT EXISTS "ENDPOINT_COMPOSITION" (
394394
395395
Columns:
396396
DEVICE_COMPOSITION_ID: The primary key of the table, auto-incremented for each new record.
397+
CODE: An integer representing the device code.
397398
DEVICE_TYPE_REF: An integer that acts as a foreign key to reference a specific device type.
398399
ENDPOINT_COMPOSITION_REF: A foreign key linking to the ENDPOINT_COMPOSITION table to specify the endpoint composition associated with this device.
399400
CONFORMANCE: A text field describing the conformance level of the device composition.
400-
CONSTRAINT: An integer representing any constraints applied to the device composition.
401+
DEVICE_CONSTRAINT: An integer representing any constraints applied to the device composition.
401402
402403
Foreign Key Constraints:
403404
The DEVICE_TYPE_REF column references the DEVICE_TYPE_ID column of the DEVICE_TYPE table. On deletion of a device type, corresponding records in this table are deleted (CASCADE).
@@ -409,7 +410,7 @@ CREATE TABLE IF NOT EXISTS "DEVICE_COMPOSITION" (
409410
"DEVICE_TYPE_REF" integer,
410411
"ENDPOINT_COMPOSITION_REF" integer,
411412
"CONFORMANCE" text,
412-
"CONSTRAINT" integer,
413+
"DEVICE_CONSTRAINT" integer,
413414
FOREIGN KEY ("ENDPOINT_COMPOSITION_REF") REFERENCES "ENDPOINT_COMPOSITION"("ENDPOINT_COMPOSITION_ID") ON DELETE CASCADE
414415
FOREIGN KEY ("DEVICE_TYPE_REF") REFERENCES "DEVICE_TYPE"("DEVICE_TYPE_ID") ON DELETE CASCADE
415416
);

0 commit comments

Comments
 (0)