Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy commands and events when duplicating endpoint #1461

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading