Skip to content

Commit

Permalink
Feature/enabled only zap file format2/zapp 1113 (project-chip#1153)
Browse files Browse the repository at this point in the history
* Fixing endpoint type command table schema

- incoming and outgoing used to be in the endpoint_type_command table as a single record. However this is an association that is related to the command source and the cluster side. When the cluster side is the same as the command source then it is an outgoing command. If the cluster side and command source are not the same then it is an incoming command. Now we have 2 entries instead of 1 in the endpoint_type_command based on this logic.
- The above allows us to implement the enabled only .zap file correctly or else we lose the incoming commands when the .zap file is saved for the enabled only content since the way the commands were stored before this commit is that the client sourced commands were stored in the client side cluster and the server sourced command is stored in the server cluster which was wrong.
- Updating the endpoint_type_command queries based on the schema changes to the table
- Fixing the import/export for the above schema changes
- Updating the UI code to capture the new schema correctly
- In the insert command for ENDPOINT_TYPE_COMMAND table in query-impexp.js there is an ignore because the .zap files have been hand edited where there are multiple references of the same command in it.
- Adding generation tests for the enabled only file format
- Updating the default zap file format version to 2
- JIRA: ZAPP-1113
  • Loading branch information
brdandu authored Oct 4, 2023
1 parent 949918d commit 69accf8
Show file tree
Hide file tree
Showing 18 changed files with 20,906 additions and 82 deletions.
2 changes: 2 additions & 0 deletions src-electron/db/db-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ exports.map = {
commandRef: x.COMMAND_REF,
incoming: dbApi.fromDbBool(x.INCOMING),
outgoing: dbApi.fromDbBool(x.OUTGOING),
isIncoming: dbApi.fromDbBool(x.IS_INCOMING),
isEnabled: dbApi.fromDbBool(x.IS_ENABLED),
}
},

Expand Down
147 changes: 110 additions & 37 deletions src-electron/db/query-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,22 @@ SELECT
COMMAND.COMMAND_ID,
COMMAND.SOURCE AS COMMAND_SOURCE,
COMMAND.CODE AS COMMAND_CODE,
ENDPOINT_TYPE_COMMAND.INCOMING AS INCOMING,
ENDPOINT_TYPE_COMMAND.OUTGOING AS OUTGOING,
CASE
WHEN
(COUNT(CASE WHEN ENDPOINT_TYPE_COMMAND.IS_INCOMING=1 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED THEN 1 ELSE NULL END) OVER (PARTITION BY COMMAND.COMMAND_ID)) >= 1
THEN
1
ELSE
0
END AS INCOMING,
CASE
WHEN
(COUNT(CASE WHEN ENDPOINT_TYPE_COMMAND.IS_INCOMING=0 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED THEN 1 ELSE NULL END) OVER (PARTITION BY COMMAND.COMMAND_ID)) >= 1
THEN
1
ELSE
0
END AS OUTGOING,
COUNT(COMMAND.MANUFACTURER_CODE) OVER () AS MANUFACTURING_SPECIFIC_COMMAND_COUNT,
COMMAND.RESPONSE_REF,
COMMAND.RESPONSE_NAME,
Expand All @@ -181,11 +195,11 @@ INNER JOIN
ON
ENDPOINT_TYPE_COMMAND.COMMAND_REF = COMMAND.COMMAND_ID
INNER JOIN
CLUSTER
ENDPOINT_TYPE_CLUSTER
ON
CLUSTER.CLUSTER_ID = COMMAND.CLUSTER_REF
ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_CLUSTER_ID = ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_CLUSTER_REF
INNER JOIN
ENDPOINT_TYPE_CLUSTER
CLUSTER
ON
ENDPOINT_TYPE_CLUSTER.CLUSTER_REF = CLUSTER.CLUSTER_ID
LEFT JOIN
Expand All @@ -197,8 +211,8 @@ WHERE
AND ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_REF = ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_REF
AND ENDPOINT_TYPE_CLUSTER.SIDE IN ("client", "server") AND ENDPOINT_TYPE_CLUSTER.ENABLED=1
AND (
(ENDPOINT_TYPE_COMMAND.INCOMING=1 AND COMMAND.SOURCE!=ENDPOINT_TYPE_CLUSTER.SIDE) OR
(ENDPOINT_TYPE_COMMAND.OUTGOING=1 AND COMMAND.SOURCE=ENDPOINT_TYPE_CLUSTER.SIDE)
(ENDPOINT_TYPE_COMMAND.IS_INCOMING=1 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED=1) OR
(ENDPOINT_TYPE_COMMAND.IS_INCOMING=0 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED=1)
)
AND COMMAND.PACKAGE_REF IN (${dbApi.toInClause(packageIds)})
GROUP BY
Expand Down Expand Up @@ -245,8 +259,8 @@ async function selectAllClustersWithIncomingOrOutgoingCommands(
let endpointTypeIds = endpointTypes.map((ep) => ep.endpointTypeId).toString()
let sqlGroupBy = uniqueClusterCodes ? 'CLUSTER.CODE' : 'CLUSTER.NAME'
let isIncomingOrOutgoingSql = isIncoming
? `ENDPOINT_TYPE_COMMAND.INCOMING = 1 AND COMMAND.SOURCE != ENDPOINT_TYPE_CLUSTER.SIDE`
: `ENDPOINT_TYPE_COMMAND.OUTGOING = 1 AND COMMAND.SOURCE == ENDPOINT_TYPE_CLUSTER.SIDE`
? `ENDPOINT_TYPE_COMMAND.IS_INCOMING = 1 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED=1`
: `ENDPOINT_TYPE_COMMAND.IS_INCOMING = 0 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED=1`
let mapFunction = (x) => {
return {
id: x.CLUSTER_ID,
Expand Down Expand Up @@ -279,16 +293,15 @@ ENDPOINT_TYPE_COMMAND
ON
ENDPOINT_TYPE_COMMAND.COMMAND_REF = COMMAND.COMMAND_ID
INNER JOIN
CLUSTER
ENDPOINT_TYPE_CLUSTER
ON
CLUSTER.CLUSTER_ID = COMMAND.CLUSTER_REF
ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_CLUSTER_ID = ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_CLUSTER_REF
INNER JOIN
ENDPOINT_TYPE_CLUSTER
CLUSTER
ON
ENDPOINT_TYPE_CLUSTER.CLUSTER_REF = CLUSTER.CLUSTER_ID
WHERE
ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_REF IN (${endpointTypeIds})
AND ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_REF = ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_REF
AND ENDPOINT_TYPE_CLUSTER.SIDE IN ("client", "server")
AND ENDPOINT_TYPE_CLUSTER.ENABLED = 1 AND ${isIncomingOrOutgoingSql}
AND COMMAND.PACKAGE_REF IN (${dbApi.toInClause(packageIds)})
Expand Down Expand Up @@ -410,19 +423,20 @@ INNER JOIN
ON
ENDPOINT_TYPE_COMMAND.COMMAND_REF = COMMAND.COMMAND_ID
INNER JOIN
CLUSTER
ENDPOINT_TYPE_CLUSTER
ON
CLUSTER.CLUSTER_ID = COMMAND.CLUSTER_REF
ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_CLUSTER_ID = ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_CLUSTER_REF
INNER JOIN
ENDPOINT_TYPE_CLUSTER
CLUSTER
ON
ENDPOINT_TYPE_CLUSTER.CLUSTER_REF = CLUSTER.CLUSTER_ID
WHERE
ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_REF IN (${endpointTypeIds})
AND ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_REF = ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_REF
AND ENDPOINT_TYPE_CLUSTER.SIDE IN ("client", "server")
AND ENDPOINT_TYPE_CLUSTER.ENABLED = 1
AND ENDPOINT_TYPE_COMMAND.INCOMING = 1
AND ENDPOINT_TYPE_COMMAND.IS_INCOMING = 1
AND ENDPOINT_TYPE_COMMAND.IS_ENABLED = 1
AND COMMAND.SOURCE != ENDPOINT_TYPE_CLUSTER.SIDE
AND CLUSTER.CODE = ${clusterCode}
AND COMMAND.PACKAGE_REF IN (${dbApi.toInClause(packageIds)})
Expand Down Expand Up @@ -569,8 +583,8 @@ async function selectAllIncomingOrOutgoingCommandsForCluster(
? ` AND COMMAND.MANUFACTURER_CODE IS NOT NULL `
: ` AND COMMAND.MANUFACTURER_CODE IS NULL `
let isIncomingOrOutgoingSql = isIncoming
? `ENDPOINT_TYPE_COMMAND.INCOMING=1 AND COMMAND.SOURCE!=ENDPOINT_TYPE_CLUSTER.SIDE`
: `ENDPOINT_TYPE_COMMAND.OUTGOING=1 AND COMMAND.SOURCE==ENDPOINT_TYPE_CLUSTER.SIDE`
? `ENDPOINT_TYPE_COMMAND.IS_INCOMING = 1 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED=1`
: `ENDPOINT_TYPE_COMMAND.IS_INCOMING = 0 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED=1`
let mapFunction = (x) => {
return {
clusterId: x.CLUSTER_ID,
Expand Down Expand Up @@ -617,15 +631,29 @@ SELECT
COMMAND.IS_FABRIC_SCOPED AS IS_FABRIC_SCOPED,
COMMAND.RESPONSE_NAME AS RESPONSE_NAME,
COMMAND.RESPONSE_REF AS RESPONSE_REF,
ENDPOINT_TYPE_COMMAND.INCOMING AS INCOMING,
ENDPOINT_TYPE_COMMAND.OUTGOING AS OUTGOING,
CASE
WHEN
(COUNT(CASE WHEN ENDPOINT_TYPE_COMMAND.IS_INCOMING=1 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED THEN 1 ELSE NULL END) OVER (PARTITION BY COMMAND.COMMAND_ID)) >= 1
THEN
1
ELSE
0
END AS INCOMING,
CASE
WHEN
(COUNT(CASE WHEN ENDPOINT_TYPE_COMMAND.IS_INCOMING=0 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED THEN 1 ELSE NULL END) OVER (PARTITION BY COMMAND.COMMAND_ID)) >= 1
THEN
1
ELSE
0
END AS OUTGOING,
COUNT(COMMAND.MANUFACTURER_CODE) OVER () AS MANUFACTURING_SPECIFIC_COMMAND_COUNT
FROM COMMAND
INNER JOIN ENDPOINT_TYPE_COMMAND
ON ENDPOINT_TYPE_COMMAND.COMMAND_REF = COMMAND.COMMAND_ID
INNER JOIN CLUSTER
ON CLUSTER.CLUSTER_ID = COMMAND.CLUSTER_REF
INNER JOIN ENDPOINT_TYPE_CLUSTER
ON ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_CLUSTER_ID = ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_CLUSTER_REF
INNER JOIN CLUSTER
ON ENDPOINT_TYPE_CLUSTER.CLUSTER_REF = CLUSTER.CLUSTER_ID
WHERE ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_REF IN (${endpointTypeIds})
AND ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_REF = ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_REF
Expand Down Expand Up @@ -760,8 +788,22 @@ SELECT
COMMAND.CODE AS COMMAND_CODE,
COMMAND.MUST_USE_TIMED_INVOKE AS MUST_USE_TIMED_INVOKE,
COMMAND.IS_FABRIC_SCOPED AS IS_FABRIC_SCOPED,
ENDPOINT_TYPE_COMMAND.INCOMING AS INCOMING,
ENDPOINT_TYPE_COMMAND.OUTGOING AS OUTGOING,
CASE
WHEN
(COUNT(CASE WHEN ENDPOINT_TYPE_COMMAND.IS_INCOMING=1 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED THEN 1 ELSE NULL END) OVER (PARTITION BY COMMAND.COMMAND_ID)) >= 1
THEN
1
ELSE
0
END AS INCOMING,
CASE
WHEN
(COUNT(CASE WHEN ENDPOINT_TYPE_COMMAND.IS_INCOMING=0 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED THEN 1 ELSE NULL END) OVER (PARTITION BY COMMAND.COMMAND_ID)) >= 1
THEN
1
ELSE
0
END AS OUTGOING,
COUNT(COMMAND.MANUFACTURER_CODE) OVER () AS MANUFACTURING_SPECIFIC_COMMAND_COUNT
FROM
COMMAND
Expand All @@ -770,18 +812,18 @@ INNER JOIN
ON
ENDPOINT_TYPE_COMMAND.COMMAND_REF = COMMAND.COMMAND_ID
INNER JOIN
CLUSTER
ENDPOINT_TYPE_CLUSTER
ON
CLUSTER.CLUSTER_ID = COMMAND.CLUSTER_REF
ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_CLUSTER_ID = ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_CLUSTER_REF
INNER JOIN
ENDPOINT_TYPE_CLUSTER
CLUSTER
ON
ENDPOINT_TYPE_CLUSTER.CLUSTER_REF = CLUSTER.CLUSTER_ID
WHERE
ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_REF IN (${endpointTypeIds})
AND ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_REF = ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_REF
AND ENDPOINT_TYPE_CLUSTER.SIDE IN ("client", "server") AND ENDPOINT_TYPE_CLUSTER.ENABLED=1
AND ENDPOINT_TYPE_COMMAND.INCOMING=1 AND COMMAND.SOURCE!=ENDPOINT_TYPE_CLUSTER.SIDE
AND ENDPOINT_TYPE_COMMAND.IS_INCOMING=1 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED = 1
AND COMMAND.PACKAGE_REF IN (${dbApi.toInClause(packageIds)})
${mfgSpecificString} GROUP BY CLUSTER.NAME, COMMAND.NAME
ORDER BY CLUSTER.NAME, COMMAND.NAME`
Expand Down Expand Up @@ -1512,6 +1554,8 @@ function commandMapFunction(x) {
mfgCode: x.MANUFACTURER_CODE,
incoming: x.INCOMING,
outgoing: x.OUTGOING,
isIncoming: x.IS_INCOMING,
isEnabled: x.COMMAND_ENABLED,
description: x.DESCRIPTION,
clusterSide: x.SIDE,
clusterId: x.CLUSTER_ID,
Expand Down Expand Up @@ -1668,8 +1712,22 @@ async function selectCommandDetailsFromAllEndpointTypesAndClusters(
C.RESPONSE_NAME,
C.MUST_USE_TIMED_INVOKE,
C.IS_FABRIC_SCOPED,
ETC.INCOMING,
ETC.OUTGOING,
CASE
WHEN
(COUNT(CASE WHEN ETC.IS_INCOMING=1 AND ETC.IS_ENABLED THEN 1 ELSE NULL END) OVER (PARTITION BY C.COMMAND_ID)) >= 1
THEN
1
ELSE
0
END AS INCOMING,
CASE
WHEN
(COUNT(CASE WHEN ETC.IS_INCOMING=0 AND ETC.IS_ENABLED THEN 1 ELSE NULL END) OVER (PARTITION BY C.COMMAND_ID)) >= 1
THEN
1
ELSE
0
END AS OUTGOING,
ETCL.SIDE,
ETCL.ENABLED,
CLUSTER.NAME AS CLUSTER_NAME
Expand All @@ -1694,11 +1752,11 @@ async function selectCommandDetailsFromAllEndpointTypesAndClusters(

if (doGroupBy) {
// See: https://github.com/project-chip/zap/issues/192
query = query + ' GROUP BY C.NAME, C.COMMAND_ID'
query = 'SELECT * FROM (' + query + ') GROUP BY NAME, COMMAND_ID'
}

// Ordering of the results:
query = query + ' ORDER BY C.MANUFACTURER_CODE, C.CODE, C.NAME'
query = query + ' ORDER BY MANUFACTURER_CODE, CODE, NAME'

return dbApi.dbAll(db, query).then((rows) => rows.map(commandMapFunction))
}
Expand Down Expand Up @@ -1730,14 +1788,29 @@ async function selectCommandDetailsFromAllEndpointTypesAndClustersUtil(
.dbAll(
db,
`
SELECT * FROM (
SELECT
COMMAND.COMMAND_ID,
COMMAND.NAME,
COMMAND.CODE,
COMMAND.SOURCE,
COMMAND.MANUFACTURER_CODE,
ENDPOINT_TYPE_COMMAND.INCOMING,
ENDPOINT_TYPE_COMMAND.OUTGOING,
CASE
WHEN
(COUNT(CASE WHEN ENDPOINT_TYPE_COMMAND.IS_INCOMING=1 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED THEN 1 ELSE NULL END) OVER (PARTITION BY COMMAND.COMMAND_ID)) >= 1
THEN
1
ELSE
0
END AS INCOMING,
CASE
WHEN
(COUNT(CASE WHEN ENDPOINT_TYPE_COMMAND.IS_INCOMING=0 AND ENDPOINT_TYPE_COMMAND.IS_ENABLED THEN 1 ELSE NULL END) OVER (PARTITION BY COMMAND.COMMAND_ID)) >= 1
THEN
1
ELSE
0
END AS OUTGOING,
COMMAND.DESCRIPTION,
COMMAND.RESPONSE_REF,
COMMAND.RESPONSE_NAME,
Expand Down Expand Up @@ -1766,8 +1839,8 @@ async function selectCommandDetailsFromAllEndpointTypesAndClustersUtil(
AND COMMAND.MANUFACTURER_CODE IS ${
isManufacturingSpecific ? `NOT` : ``
} NULL
AND COMMAND.PACKAGE_REF IN (${dbApi.toInClause(packageIds)})
GROUP BY COMMAND.NAME
AND COMMAND.PACKAGE_REF IN (${dbApi.toInClause(packageIds)}))
GROUP BY NAME
`
)
.then((rows) => rows.map(commandMapFunction))
Expand Down
37 changes: 30 additions & 7 deletions src-electron/db/query-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,21 @@ async function insertOrUpdateCommandState(
value,
isIncoming
) {
/*
Retrieve the cluster side and command source and based on that add incoming/outgoing, incoming is 1 and outgoing is 0
Once that is done put the value into enabled clause. See the schema changes.
For backwards compatibility. The existing inserts into the db should continue to work the same way.
*/

let cluster = await insertOrSelectDefaultEndpointTypeCluster(
db,
endpointTypeId,
clusterRef,
side
!isIncoming
? side
: dbEnum.source.client == side
? dbEnum.source.server
: dbEnum.source.client
)

await dbApi.dbInsert(
Expand All @@ -414,20 +424,33 @@ INSERT OR IGNORE
INTO ENDPOINT_TYPE_COMMAND (
ENDPOINT_TYPE_REF,
ENDPOINT_TYPE_CLUSTER_REF,
COMMAND_REF
) VALUES( ?, ?, ? )
COMMAND_REF,
IS_INCOMING
) VALUES( ?, ?, ?, ? )
`,
[endpointTypeId, cluster.endpointTypeClusterId, commandId]
[
endpointTypeId,
cluster.endpointTypeClusterId,
commandId,
dbApi.toDbBool(isIncoming),
]
)
return dbApi.dbUpdate(
db,
`
UPDATE ENDPOINT_TYPE_COMMAND
SET ${isIncoming ? 'INCOMING' : 'OUTGOING'} = ?
SET IS_ENABLED = ?
WHERE ENDPOINT_TYPE_REF = ?
AND ENDPOINT_TYPE_CLUSTER_REF = ?
AND COMMAND_REF = ? `,
[value, endpointTypeId, cluster.endpointTypeClusterId, commandId]
AND COMMAND_REF = ?
AND IS_INCOMING = ? `,
[
value,
endpointTypeId,
cluster.endpointTypeClusterId,
commandId,
dbApi.toDbBool(isIncoming),
]
)
}

Expand Down
8 changes: 4 additions & 4 deletions src-electron/db/query-endpoint-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,8 @@ async function selectCommandDetailsFromAllEndpointTypeCluster(
name: x.NAME,
code: x.CODE,
mfgCode: x.MANUFACTURER_CODE,
incoming: x.INCOMING,
outgoing: x.OUTGOING,
isIncoming: x.IS_INCOMING,
isEnabled: x.IS_ENABLED,
description: x.DESCRIPTION,
}
}
Expand All @@ -563,8 +563,8 @@ async function selectCommandDetailsFromAllEndpointTypeCluster(
COMMAND.NAME,
COMMAND.CODE,
COMMAND.MANUFACTURER_CODE,
ENDPOINT_TYPE_COMMAND.INCOMING,
ENDPOINT_TYPE_COMMAND.OUTGOING,
ENDPOINT_TYPE_COMMAND.IS_INCOMING,
ENDPOINT_TYPE_COMMAND.IS_ENABLED,
COMMAND.DESCRIPTION
FROM COMMAND
INNER JOIN ENDPOINT_TYPE_COMMAND
Expand Down
Loading

0 comments on commit 69accf8

Please sign in to comment.