Skip to content

Commit

Permalink
Copy commands and events when duplicating endpoint (project-chip#1461)
Browse files Browse the repository at this point in the history
* Copy commands and events when duplicating endpoint

- When duplicating an endpoint, only attribute states are copied. This PR extends to copy command and event states.

- fix issue project-chip#1441

- Jira: GHM_ZAP-457
  • Loading branch information
ethanzhouyc authored Oct 22, 2024
1 parent 3ef94ba commit 5b45cff
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src-electron/db/query-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,83 @@
const dbApi = require('./db-api.js')
const dbMapping = require('./db-mapping.js')

/**
* Promises to select all endpoint type commands filtered by EndpointTypeRef and ClusterRef.
*
* @export
* @param {*} db
* @param {*} endpointTypeRef
* @param {*} endpointTypeClusterRef
* @returns Records of selected Endpoint Type Commands.
*/
async function selectEndpointTypeCommandsByEndpointTypeRefAndClusterRef(
db,
endpointTypeRef,
endpointTypeClusterRef
) {
let rows = await dbApi.dbAll(
db,
`
SELECT
ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_COMMAND_ID,
ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_REF,
ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_CLUSTER_REF AS 'CLUSTER_REF',
ENDPOINT_TYPE_COMMAND.COMMAND_REF,
ENDPOINT_TYPE_COMMAND.IS_INCOMING,
ENDPOINT_TYPE_COMMAND.IS_ENABLED
FROM
ENDPOINT_TYPE_COMMAND
INNER JOIN
ENDPOINT_TYPE_CLUSTER
ON
ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_CLUSTER_REF = ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_CLUSTER_ID
WHERE
ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_REF = ?
AND
ENDPOINT_TYPE_COMMAND.ENDPOINT_TYPE_CLUSTER_REF = ?`,
[endpointTypeRef, endpointTypeClusterRef]
)
return rows.map(dbMapping.map.endpointTypeCommand)
}

/**
* Promises to duplicate endpoint type commands.
*
* @export
* @param {*} db
* @param {*} newEndpointTypeClusterRef
* @param {*} command
* @returns Promise duplicated endpoint type command's id.
*/
async function duplicateEndpointTypeCommand(
db,
newEndpointTypeClusterRef,
command
) {
return await dbApi.dbInsert(
db,
`INSERT INTO
ENDPOINT_TYPE_COMMAND (
ENDPOINT_TYPE_CLUSTER_REF,
COMMAND_REF,
IS_INCOMING,
IS_ENABLED
)
VALUES (
?,
?,
?,
?
)`,
[
newEndpointTypeClusterRef,
command.commandRef,
command.isIncoming,
command.isEnabled
]
)
}

/**
* Returns the count of the number of cluster commands with cli for a cluster
* @param {*} db
Expand Down Expand Up @@ -2008,3 +2085,6 @@ exports.selectAllClustersWithOutgoingCommands =
selectAllClustersWithOutgoingCommands
exports.selectAllOutgoingCommandsForCluster =
selectAllOutgoingCommandsForCluster
exports.selectEndpointTypeCommandsByEndpointTypeRefAndClusterRef =
selectEndpointTypeCommandsByEndpointTypeRefAndClusterRef
exports.duplicateEndpointTypeCommand = duplicateEndpointTypeCommand
72 changes: 72 additions & 0 deletions src-electron/db/query-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,75 @@
const dbApi = require('./db-api.js')
const dbMapping = require('./db-mapping.js')

/**
* Promises to select all endpoint type events filtered by EndpointTypeRef and ClusterRef.
*
* @export
* @param {*} db
* @param {*} endpointTypeRef
* @param {*} endpointTypeClusterRef
* @returns Records of selected Endpoint Type Events.
*/
async function selectEndpointTypeEventsByEndpointTypeRefAndClusterRef(
db,
endpointTypeRef,
endpointTypeClusterRef
) {
let rows = await dbApi.dbAll(
db,
`
SELECT
ENDPOINT_TYPE_EVENT.ENDPOINT_TYPE_EVENT_ID,
ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_REF,
ENDPOINT_TYPE_EVENT.ENDPOINT_TYPE_CLUSTER_REF AS 'CLUSTER_REF',
ENDPOINT_TYPE_EVENT.EVENT_REF,
ENDPOINT_TYPE_EVENT.INCLUDED
FROM
ENDPOINT_TYPE_EVENT
INNER JOIN
ENDPOINT_TYPE_CLUSTER
ON
ENDPOINT_TYPE_EVENT.ENDPOINT_TYPE_CLUSTER_REF = ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_CLUSTER_ID
WHERE
ENDPOINT_TYPE_CLUSTER.ENDPOINT_TYPE_REF = ?
AND
ENDPOINT_TYPE_EVENT.ENDPOINT_TYPE_CLUSTER_REF = ?`,
[endpointTypeRef, endpointTypeClusterRef]
)
return rows.map(dbMapping.map.endpointTypeEvent)
}

/**
* Promises to duplicate endpoint type events.
*
* @export
* @param {*} db
* @param {*} newEndpointTypeClusterRef
* @param {*} event
* @returns Promise duplicated endpoint type event's id.
*/
async function duplicateEndpointTypeEvent(
db,
newEndpointTypeClusterRef,
event
) {
return await dbApi.dbInsert(
db,
`INSERT INTO
ENDPOINT_TYPE_EVENT (
ENDPOINT_TYPE_CLUSTER_REF,
EVENT_REF,
INCLUDED
)
VALUES (
?,
?,
?
)`,
[newEndpointTypeClusterRef, event.eventRef, event.included]
)
}

/**
* Retrieves events for a given cluster Id.
*
Expand Down Expand Up @@ -166,3 +235,6 @@ exports.selectEventsByClusterId = selectEventsByClusterId
exports.selectAllEvents = selectAllEvents
exports.selectAllEventFields = selectAllEventFields
exports.selectEventFieldsByEventId = selectEventFieldsByEventId
exports.selectEndpointTypeEventsByEndpointTypeRefAndClusterRef =
selectEndpointTypeEventsByEndpointTypeRefAndClusterRef
exports.duplicateEndpointTypeEvent = duplicateEndpointTypeEvent
27 changes: 27 additions & 0 deletions src-electron/rest/user-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const env = require('../util/env')
const queryZcl = require('../db/query-zcl.js')
const queryAttribute = require('../db/query-attribute.js')
const queryCommand = require('../db/query-command.js')
const queryEvent = require('../db/query-event.js')
const queryFeature = require('../db/query-feature')
const queryConfig = require('../db/query-config.js')
const upgrade = require('../sdk/matter.js')
Expand Down Expand Up @@ -1045,6 +1046,32 @@ async function duplicateEndpointTypeClusters(
attrubute
)
})
let oldCommands =
await queryCommand.selectEndpointTypeCommandsByEndpointTypeRefAndClusterRef(
db,
oldEndpointTypeId,
endpointTypeCluster.endpointTypeClusterId
)
oldCommands.forEach(async (command) => {
await queryCommand.duplicateEndpointTypeCommand(
db,
newEndpointTypeClusterId,
command
)
})
let oldEvents =
await queryEvent.selectEndpointTypeEventsByEndpointTypeRefAndClusterRef(
db,
oldEndpointTypeId,
endpointTypeCluster.endpointTypeClusterId
)
oldEvents.forEach(async (event) => {
await queryEvent.duplicateEndpointTypeEvent(
db,
newEndpointTypeClusterId,
event
)
})
})
}

Expand Down

0 comments on commit 5b45cff

Please sign in to comment.