Skip to content

Commit 5d1df1c

Browse files
authored
Adding search by enum/bitmap name and Cluster name (#1590)
Github: ZAP#1586
1 parent e74dd35 commit 5d1df1c

File tree

8 files changed

+294
-16
lines changed

8 files changed

+294
-16
lines changed

docs/api.md

Lines changed: 145 additions & 9 deletions
Large diffs are not rendered by default.

src-electron/db/query-bitmap.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
/**
19-
* This module provides queries for enums.
19+
* This module provides queries for bitmaps.
2020
*
2121
* @module DB API: zcl database access
2222
*/
@@ -25,6 +25,7 @@ const dbApi = require('./db-api')
2525
const dbCache = require('./db-cache')
2626
const dbMapping = require('./db-mapping')
2727
const queryUtil = require('./query-util')
28+
const dbEnum = require('../../src-shared/db-enum')
2829

2930
/**
3031
* Retrieves all the bitmaps in the database.
@@ -89,12 +90,12 @@ WHERE (DATA_TYPE.NAME = ? OR DATA_TYPE.NAME = ?) AND DATA_TYPE.PACKAGE_REF IN ($
8990
*/
9091
async function selectBitmapByNameAndClusterId(db, name, clusterId, packageIds) {
9192
let queryWithoutClusterId = queryUtil.sqlQueryForDataTypeByNameAndClusterId(
92-
'bitmap',
93+
dbEnum.zclType.bitmap,
9394
null,
9495
packageIds
9596
)
9697
let queryWithClusterId = queryUtil.sqlQueryForDataTypeByNameAndClusterId(
97-
'bitmap',
98+
dbEnum.zclType.bitmap,
9899
clusterId,
99100
packageIds
100101
)
@@ -112,6 +113,48 @@ async function selectBitmapByNameAndClusterId(db, name, clusterId, packageIds) {
112113
}
113114
}
114115

116+
/**
117+
* Select a bitmap matched by name and cluster name
118+
* Note: Use selectBitmapByNameAndClusterId but this was needed for backwards compatibility.
119+
* @param {*} db
120+
* @param {*} name
121+
* @param {*} clusterName
122+
* @param {*} packageIds
123+
* @returns bitmap information or undefined
124+
*/
125+
async function selectBitmapByNameAndClusterName(
126+
db,
127+
name,
128+
clusterName,
129+
packageIds
130+
) {
131+
let queryWithClusterName = queryUtil.sqlQueryForDataTypeByNameAndClusterName(
132+
dbEnum.zclType.bitmap,
133+
name,
134+
clusterName,
135+
packageIds
136+
)
137+
let res = await dbApi
138+
.dbAll(db, queryWithClusterName)
139+
.then((rows) => rows.map(dbMapping.map.bitmap))
140+
if (res && res.length == 1) {
141+
return res[0]
142+
} else if (res && res.length > 1) {
143+
throw new Error(
144+
`More than one bitmap ${name} exists with same name for ${clusterName} cluster.`
145+
)
146+
} else {
147+
queryWithClusterName = queryUtil.sqlQueryForDataTypeByNameAndClusterName(
148+
dbEnum.zclType.bitmap,
149+
name,
150+
null, // Retrieving global data types since cluster specific ones were not found.
151+
packageIds
152+
)
153+
res = await dbApi.dbGet(db, queryWithClusterName).then(dbMapping.map.bitmap)
154+
return res
155+
}
156+
}
157+
115158
/**
116159
* Get Bitmap information by Bitmap ID.
117160
* @param {*} db
@@ -141,3 +184,4 @@ exports.selectBitmapByName = dbCache.cacheQuery(selectBitmapByName)
141184
exports.selectBitmapByNameAndClusterId = dbCache.cacheQuery(
142185
selectBitmapByNameAndClusterId
143186
)
187+
exports.selectBitmapByNameAndClusterName = selectBitmapByNameAndClusterName

src-electron/db/query-enum.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const dbApi = require('./db-api')
2525
const dbCache = require('./db-cache')
2626
const dbMapping = require('./db-mapping')
2727
const queryUtil = require('./query-util')
28+
const dbEnum = require('../../src-shared/db-enum')
2829

2930
/**
3031
* Retrieves all the enums in the database.
@@ -226,12 +227,12 @@ ORDER BY NAME`,
226227
*/
227228
async function selectEnumByNameAndClusterId(db, name, clusterId, packageIds) {
228229
let queryWithoutClusterId = queryUtil.sqlQueryForDataTypeByNameAndClusterId(
229-
'enum',
230+
dbEnum.zclType.enum,
230231
null,
231232
packageIds
232233
)
233234
let queryWithClusterId = queryUtil.sqlQueryForDataTypeByNameAndClusterId(
234-
'enum',
235+
dbEnum.zclType.enum,
235236
clusterId,
236237
packageIds
237238
)
@@ -248,6 +249,48 @@ async function selectEnumByNameAndClusterId(db, name, clusterId, packageIds) {
248249
}
249250
}
250251

252+
/**
253+
* Select a enum matched by name and cluster name
254+
* Note: Use selectEnumByNameAndClusterId but this was needed for backwards compatibility.
255+
* @param {*} db
256+
* @param {*} name
257+
* @param {*} clusterName
258+
* @param {*} packageIds
259+
* @returns enum information or undefined
260+
*/
261+
async function selectEnumByNameAndClusterName(
262+
db,
263+
name,
264+
clusterName,
265+
packageIds
266+
) {
267+
let queryWithClusterName = queryUtil.sqlQueryForDataTypeByNameAndClusterName(
268+
dbEnum.zclType.enum,
269+
name,
270+
clusterName,
271+
packageIds
272+
)
273+
let res = await dbApi
274+
.dbAll(db, queryWithClusterName)
275+
.then((rows) => rows.map(dbMapping.map.enum))
276+
if (res && res.length == 1) {
277+
return res[0]
278+
} else if (res && res.length > 1) {
279+
throw new Error(
280+
`More than one enum ${name} exists with same name for ${clusterName} cluster.`
281+
)
282+
} else {
283+
queryWithClusterName = queryUtil.sqlQueryForDataTypeByNameAndClusterName(
284+
dbEnum.zclType.enum,
285+
name,
286+
null, // Retrieving global data types since cluster specific ones were not found.
287+
packageIds
288+
)
289+
res = await dbApi.dbGet(db, queryWithClusterName).then(dbMapping.map.enum)
290+
return res
291+
}
292+
}
293+
251294
// exports
252295
exports.selectAllEnums = selectAllEnums
253296
exports.selectEnumByName = dbCache.cacheQuery(selectEnumByName)
@@ -258,3 +301,4 @@ exports.selectEnumById = selectEnumById
258301
exports.selectClusterEnums = selectClusterEnums
259302
exports.selectAllEnumItemsById = selectAllEnumItemsById
260303
exports.selectAllEnumItems = selectAllEnumItems
304+
exports.selectEnumByNameAndClusterName = selectEnumByNameAndClusterName

src-electron/db/query-struct.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const dbApi = require('./db-api')
2525
const dbCache = require('./db-cache')
2626
const dbMapping = require('./db-mapping')
2727
const queryUtil = require('./query-util')
28-
const dbEnum = require('../../src-shared/db-enum.js')
28+
const dbEnum = require('../../src-shared/db-enum')
2929

3030
/**
3131
* Get all structs from a given package ID.
@@ -157,7 +157,7 @@ async function selectStructByNameAndClusterId(db, name, clusterId, packageIds) {
157157

158158
/**
159159
* Select a struct matched by name and cluster name
160-
* Note: Use selectStructByNameAndClusterName but this was needed for backwards compatibility.
160+
* Note: Use selectStructByNameAndClusterId but this was needed for backwards compatibility.
161161
* @param {*} db
162162
* @param {*} name
163163
* @param {*} clusterName

src-electron/db/query-zcl.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,10 @@ exports.selectStructByNameAndClusterId =
13361336
queryStruct.selectStructByNameAndClusterId
13371337
exports.selectStructByNameAndClusterName =
13381338
queryStruct.selectStructByNameAndClusterName
1339+
exports.selectEnumByNameAndClusterName =
1340+
queryEnum.selectEnumByNameAndClusterName
1341+
exports.selectBitmapByNameAndClusterName =
1342+
queryBitmap.selectBitmapByNameAndClusterName
13391343

13401344
exports.selectBitmapById = queryBitmap.selectBitmapById
13411345
exports.selectAllBitmaps = queryBitmap.selectAllBitmaps

test/gen-matter-3-1.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,36 @@ test(
537537
zclPackageId
538538
)
539539
expect(globalStruct.id).not.toEqual(clusterStruct.id)
540+
541+
// Testing selectEnumByNameAndClusterName for enum names
542+
let globalEnum = await queryZcl.selectEnumByNameAndClusterName(
543+
db,
544+
'enumTest',
545+
'Descriptor',
546+
zclPackageId
547+
)
548+
let clusterEnum = await queryZcl.selectEnumByNameAndClusterName(
549+
db,
550+
'enumTest',
551+
'Mode Select',
552+
zclPackageId
553+
)
554+
expect(globalEnum.id).not.toEqual(clusterEnum.id)
555+
556+
// Testing selectBitmapByNameAndClusterName for bitmap names
557+
let globalBitmap = await queryZcl.selectBitmapByNameAndClusterName(
558+
db,
559+
'bitmapTest',
560+
'Descriptor',
561+
zclPackageId
562+
)
563+
let clusterBitmap = await queryZcl.selectBitmapByNameAndClusterName(
564+
db,
565+
'bitmapTest',
566+
'Mode Select',
567+
zclPackageId
568+
)
569+
expect(globalBitmap.id).not.toEqual(clusterBitmap.id)
540570
},
541571
testUtil.timeout.long()
542572
)

zcl-builtin/matter/data-model/chip/descriptor-cluster.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ limitations under the License.
3030
<item name="Revision" type="INT16U"/>
3131
</struct>
3232

33+
<enum name="enumTest" type="ENUM8">
34+
<item name="enumTest1" value="0x0"/>
35+
<item name="enumTest2" value="0x1"/>
36+
</enum>
37+
38+
<bitmap name="bitmapTest" type="BITMAP8">
39+
<field name="bitmapFieldTest1" mask="0x1"/>
40+
<field name="bitmapFieldTest2" mask="0x2"/>
41+
</bitmap>
42+
3343
<cluster>
3444
<domain>General</domain>
3545
<name>Descriptor</name>

zcl-builtin/matter/data-model/chip/mode-select-cluster.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ limitations under the License.
3030
<item name="SemanticTags" type="SemanticTagStruct" array="true" optional="false"/>
3131
</struct>
3232

33+
<enum name="enumTest" type="ENUM8">
34+
<cluster code="0x0050"/>
35+
<item name="enumTest1" value="0x0"/>
36+
</enum>
37+
38+
<bitmap name="bitmapTest" type="BITMAP8">
39+
<cluster code="0x0050"/>
40+
<field name="bitmapFieldTest1" mask="0x1"/>
41+
</bitmap>
42+
3343
<cluster>
3444
<domain>General</domain>
3545
<name>Mode Select</name>

0 commit comments

Comments
 (0)