|
23 | 23 | const env = require('../util/env') |
24 | 24 | const dbApi = require('./db-api.js') |
25 | 25 | const queryNotification = require('../db/query-package-notification') |
| 26 | +const dbEnum = require('../../src-shared/db-enum.js') |
26 | 27 |
|
27 | 28 | // Some loading queries that are reused few times. |
28 | 29 |
|
@@ -926,6 +927,67 @@ async function insertAtomics(db, packageId, data) { |
926 | 927 | ) |
927 | 928 | } |
928 | 929 |
|
| 930 | +/** |
| 931 | + * Inserts endpoint composition data into the database based on the context's mandatory device type. |
| 932 | + * This function checks if the context's mandatory device type matches the composition code. |
| 933 | + * If they match, it performs an insert operation with a specific type from `dbEnum.mandatoryDeviceType`. |
| 934 | + * If they do not match, it performs an insert with the composition's type. |
| 935 | + * |
| 936 | + * @param {*} db - The database connection object. |
| 937 | + * @param {*} composition - The composition data to be inserted. |
| 938 | + * @param {*} context - The context containing the mandatory device type to check against. |
| 939 | + * @returns A promise resolved with the result of the database insert operation. |
| 940 | + */ |
| 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 | + ) |
| 954 | + } |
| 955 | +} |
| 956 | + |
| 957 | +/** |
| 958 | + * Asynchronously retrieves the ID of an endpoint composition based on its code. |
| 959 | + * |
| 960 | + * @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. |
| 963 | + */ |
| 964 | +async function getEndpointCompositionIdByCode(db, deviceType) { |
| 965 | + const query = |
| 966 | + '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 |
| 969 | +} |
| 970 | + |
| 971 | +/** |
| 972 | + * Inserts a new device composition record into the database. |
| 973 | + * |
| 974 | + * @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. |
| 978 | + */ |
| 979 | + |
| 980 | +function insertDeviceComposition(db, deviceType, endpointCompositionId) { |
| 981 | + const insertQuery = ` |
| 982 | + INSERT INTO DEVICE_COMPOSITION (CODE, ENDPOINT_COMPOSITION_REF) |
| 983 | + VALUES (?, ?) |
| 984 | + ` |
| 985 | + return dbApi.dbInsert(db, insertQuery, [ |
| 986 | + parseInt(deviceType.childDeviceId, 16), |
| 987 | + endpointCompositionId, |
| 988 | + ]) |
| 989 | +} |
| 990 | + |
929 | 991 | /** |
930 | 992 | * Inserts device types into the database. |
931 | 993 | * |
@@ -984,13 +1046,40 @@ async function insertDeviceTypes(db, packageId, data) { |
984 | 1046 | } |
985 | 1047 | }) |
986 | 1048 | ) |
987 | | - ).then((dtClusterRefDataPairs) => { |
988 | | - let promises = [] |
989 | | - promises.push(insertDeviceTypeAttributes(db, dtClusterRefDataPairs)) |
990 | | - promises.push(insertDeviceTypeCommands(db, dtClusterRefDataPairs)) |
991 | | - promises.push(insertDeviceTypeFeatures(db, dtClusterRefDataPairs)) |
992 | | - return Promise.all(promises) |
993 | | - }) |
| 1049 | + ) |
| 1050 | + .then((dtClusterRefDataPairs) => { |
| 1051 | + let promises = [] |
| 1052 | + promises.push( |
| 1053 | + insertDeviceTypeAttributes(db, dtClusterRefDataPairs) |
| 1054 | + ) |
| 1055 | + promises.push(insertDeviceTypeCommands(db, dtClusterRefDataPairs)) |
| 1056 | + promises.push(insertDeviceTypeFeatures(db, dtClusterRefDataPairs)) |
| 1057 | + return Promise.all(promises) |
| 1058 | + }) |
| 1059 | + .then(() => { |
| 1060 | + // Update ENDPOINT_COMPOSITION with DEVICE_TYPE_REF |
| 1061 | + const updateEndpointComposition = ` |
| 1062 | + UPDATE ENDPOINT_COMPOSITION |
| 1063 | + SET DEVICE_TYPE_REF = ( |
| 1064 | + SELECT DEVICE_TYPE_ID |
| 1065 | + FROM DEVICE_TYPE |
| 1066 | + WHERE DEVICE_TYPE.CODE = ENDPOINT_COMPOSITION.CODE |
| 1067 | + ) |
| 1068 | + ` |
| 1069 | + return dbApi.dbAll(db, updateEndpointComposition) |
| 1070 | + }) |
| 1071 | + .then(() => { |
| 1072 | + // Update DEVICE_COMPOSITION with DEVICE_TYPE_REF |
| 1073 | + const updateDeviceComposition = ` |
| 1074 | + UPDATE DEVICE_COMPOSITION |
| 1075 | + SET DEVICE_TYPE_REF = ( |
| 1076 | + SELECT DEVICE_TYPE_ID |
| 1077 | + FROM DEVICE_TYPE |
| 1078 | + WHERE DEVICE_TYPE.CODE = DEVICE_COMPOSITION.CODE |
| 1079 | + ) |
| 1080 | + ` |
| 1081 | + return dbApi.dbAll(db, updateDeviceComposition) |
| 1082 | + }) |
994 | 1083 | } |
995 | 1084 | } |
996 | 1085 | return zclIdsPromises |
@@ -1997,3 +2086,6 @@ exports.insertStruct = insertStruct |
1997 | 2086 | exports.insertStructItems = insertStructItems |
1998 | 2087 | exports.updateDataTypeClusterReferences = updateDataTypeClusterReferences |
1999 | 2088 | exports.insertAttributeMappings = insertAttributeMappings |
| 2089 | +exports.insertEndpointComposition = insertEndpointComposition |
| 2090 | +exports.insertDeviceComposition = insertDeviceComposition |
| 2091 | +exports.getEndpointCompositionIdByCode = getEndpointCompositionIdByCode |
0 commit comments