@@ -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 */
11671168function 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/**
0 commit comments