Skip to content

Commit 3879080

Browse files
authored
Enable Multiple Endpoints and Device Types in Device Composition Insertion (#1489)
* enabling ZAP to load multiple devices for an endpoint composition * fixing code so there can be multiple endpoints with different consraints and conformance if necessary * remove debug test * PR review cleanup
1 parent 8a3df83 commit 3879080

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

docs/api.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4066,16 +4066,17 @@ Retrieves the endpoint composition ID for a given device type code.
40664066
<a name="module_DB API_ zcl loading queries..insertDeviceComposition"></a>
40674067

40684068
### DB API: zcl loading queries~insertDeviceComposition(db, deviceType, endpointCompositionId) ⇒ <code>Promise</code>
4069-
Inserts a device composition record into the DEVICE_COMPOSITION table.
4069+
Inserts device composition records for each deviceType into the DEVICE_COMPOSITION table
4070+
for all endpoints in the deviceType, including endpoint-specific constraint and conformance values.
40704071

40714072
This function constructs an SQL INSERT query to add a new record to the
4072-
DEVICE_COMPOSITION table. It handles the insertion of the device code,
4073-
endpoint composition reference, conformance, and constraint values.
4073+
DEVICE_COMPOSITION table for each deviceType in each endpoint. It handles the insertion
4074+
of the device code, endpoint composition reference, conformance, and constraint values.
40744075
Note that the "CONSTRAINT" column name is escaped with double quotes
40754076
to avoid conflicts with the SQL reserved keyword.
40764077

40774078
**Kind**: inner method of [<code>DB API: zcl loading queries</code>](#module_DB API_ zcl loading queries)
4078-
**Returns**: <code>Promise</code> - A promise that resolves when the insertion is complete.
4079+
**Returns**: <code>Promise</code> - A promise that resolves when all insertions are complete.
40794080

40804081
| Param | Type | Description |
40814082
| --- | --- | --- |

src-electron/db/query-loader.js

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,35 +1151,73 @@ async function getEndpointCompositionIdByCode(db, deviceType) {
11511151
}
11521152

11531153
/**
1154-
* Inserts a device composition record into the DEVICE_COMPOSITION table.
1154+
* Inserts device composition records for each deviceType into the DEVICE_COMPOSITION table
1155+
* for all endpoints in the deviceType, including endpoint-specific constraint and conformance values.
11551156
*
11561157
* This function constructs an SQL INSERT query to add a new record to the
1157-
* DEVICE_COMPOSITION table. It handles the insertion of the device code,
1158-
* endpoint composition reference, conformance, and constraint values.
1158+
* DEVICE_COMPOSITION table for each deviceType in each endpoint. It handles the insertion
1159+
* of the device code, endpoint composition reference, conformance, and constraint values.
11591160
* Note that the "CONSTRAINT" column name is escaped with double quotes
11601161
* to avoid conflicts with the SQL reserved keyword.
11611162
*
11621163
* @param {Object} db - The database connection object.
11631164
* @param {Object} deviceType - The device type object containing the data to be inserted.
11641165
* @param {number} endpointCompositionId - The ID of the endpoint composition.
1165-
* @returns {Promise} A promise that resolves when the insertion is complete.
1166+
* @returns {Promise} A promise that resolves when all insertions are complete.
11661167
*/
11671168
function insertDeviceComposition(db, deviceType, endpointCompositionId) {
1168-
const insertQuery = `
1169-
INSERT INTO DEVICE_COMPOSITION (CODE, ENDPOINT_COMPOSITION_REF, CONFORMANCE, DEVICE_CONSTRAINT)
1170-
VALUES (?, ?, ?, ?)
1171-
`
1172-
try {
1173-
return dbApi.dbInsert(db, insertQuery, [
1174-
parseInt(deviceType.childDeviceId, 16),
1175-
endpointCompositionId,
1176-
deviceType.conformance,
1169+
// Ensure that deviceType and its necessary properties are defined
1170+
if (!deviceType?.composition?.endpoint) {
1171+
throw new Error('Invalid deviceType object or endpoint data')
1172+
}
1173+
1174+
// Make sure 'deviceType.composition.endpoint' is always an array, even if there's only one endpoint
1175+
const endpoints = Array.isArray(deviceType.composition.endpoint)
1176+
? deviceType.composition.endpoint
1177+
: [deviceType.composition.endpoint]
1178+
1179+
// Prepare an array to hold all insert queries
1180+
const insertQueries = []
1181+
1182+
// Iterate over all endpoints in the deviceType and their respective deviceTypes
1183+
for (let endpoint of endpoints) {
1184+
// Ensure deviceType is present and handle both single value or array
1185+
const deviceTypes = Array.isArray(endpoint.deviceType)
1186+
? endpoint.deviceType
1187+
: endpoint.deviceType
1188+
? [endpoint.deviceType]
1189+
: [] // Default to empty array if undefined
1190+
1191+
// Use the $ to get the endpoint-specific conformance and constraint values
1192+
const endpointConformance =
1193+
endpoint.endpointComposition?.endpoint?.$.conformance ||
1194+
deviceType.conformance
1195+
const endpointConstraint =
1196+
endpoint.endpointComposition?.endpoint?.$.constraint ||
11771197
deviceType.constraint
1178-
])
1179-
} catch (error) {
1180-
console.error('Error inserting device composition:', error)
1181-
throw error // Re-throw the error after logging it
1198+
1199+
// Create insert queries for each deviceType in this endpoint and add them to the insertQueries array
1200+
for (let device of deviceTypes) {
1201+
insertQueries.push(
1202+
dbApi.dbInsert(
1203+
db,
1204+
`
1205+
INSERT INTO DEVICE_COMPOSITION (CODE, ENDPOINT_COMPOSITION_REF, CONFORMANCE, DEVICE_CONSTRAINT)
1206+
VALUES (?, ?, ?, ?)
1207+
`,
1208+
[
1209+
parseInt(device, 16), // Convert deviceType to integer, assuming it is hex
1210+
endpointCompositionId,
1211+
endpointConformance, // Use the endpoint's specific conformance if available
1212+
endpointConstraint // Use the endpoint's specific constraint if available
1213+
]
1214+
)
1215+
)
1216+
}
11821217
}
1218+
1219+
// Return the promise for executing all queries concurrently
1220+
return Promise.all(insertQueries)
11831221
}
11841222

11851223
/**

src-electron/zcl/zcl-loader-silabs.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,12 +1665,7 @@ function prepareDeviceType(deviceType) {
16651665
if ('endpointComposition' in deviceType) {
16661666
try {
16671667
ret.compositionType = deviceType.endpointComposition[0].compositionType[0]
1668-
ret.conformance =
1669-
deviceType.endpointComposition[0].endpoint[0].$.conformance
1670-
ret.constraint =
1671-
deviceType.endpointComposition[0].endpoint[0].$.constraint
1672-
ret.childDeviceId =
1673-
deviceType.endpointComposition[0].endpoint[0].deviceType[0]
1668+
ret.composition = deviceType.endpointComposition[0]
16741669
} catch (error) {
16751670
console.error('Error processing endpoint composition:', error)
16761671
}

0 commit comments

Comments
 (0)