Skip to content

Commit

Permalink
set conform notification on import
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanzhouyc committed Dec 11, 2024
1 parent dc336e6 commit eeaa431
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 15 deletions.
15 changes: 0 additions & 15 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3480,7 +3480,6 @@ This module provides queries for device types.
* [~selectDeviceTypeByCodeAndName(db, packageId, code, name)](#module_DB API_ device type database access..selectDeviceTypeByCodeAndName) ⇒
* [~selectDeviceTypeByCode(db, packageId, code, name)](#module_DB API_ device type database access..selectDeviceTypeByCode) ⇒
* [~selectDeviceTypeClustersByDeviceTypeRef(db, deviceTypeRef)](#module_DB API_ device type database access..selectDeviceTypeClustersByDeviceTypeRef) ⇒
* [~selectDeviceTypeClusterIdByDeviceTypeRefAndClusterCode(db, deviceTypeRef, clusterCode)](#module_DB API_ device type database access..selectDeviceTypeClusterIdByDeviceTypeRefAndClusterCode) ⇒
* [~selectDeviceTypeClusterByDeviceTypeClusterId(db, deviceTypeClusterId)](#module_DB API_ device type database access..selectDeviceTypeClusterByDeviceTypeClusterId) ⇒
* [~selectDeviceTypeAttributesByDeviceTypeRef(db, deviceTypeRef)](#module_DB API_ device type database access..selectDeviceTypeAttributesByDeviceTypeRef) ⇒
* [~selectDeviceTypeCommandsByDeviceTypeRef(db, deviceTypeRef)](#module_DB API_ device type database access..selectDeviceTypeCommandsByDeviceTypeRef) ⇒
Expand Down Expand Up @@ -3562,20 +3561,6 @@ Get all device type clusters from a given device type ID.
| db | <code>\*</code> |
| deviceTypeRef | <code>\*</code> |

<a name="module_DB API_ device type database access..selectDeviceTypeClusterIdByDeviceTypeRefAndClusterCode"></a>

### DB API: device type database access~selectDeviceTypeClusterIdByDeviceTypeRefAndClusterCode(db, deviceTypeRef, clusterCode) ⇒
Get device type cluster ID from device type reference and cluster code.

**Kind**: inner method of [<code>DB API: device type database access</code>](#module_DB API_ device type database access)
**Returns**: Promise of device type cluster ID, or null if not found

| Param | Type |
| --- | --- |
| db | <code>\*</code> |
| deviceTypeRef | <code>\*</code> |
| clusterCode | <code>\*</code> |

<a name="module_DB API_ device type database access..selectDeviceTypeClusterByDeviceTypeClusterId"></a>

### DB API: device type database access~selectDeviceTypeClusterByDeviceTypeClusterId(db, deviceTypeClusterId) ⇒
Expand Down
131 changes: 131 additions & 0 deletions src-electron/importexport/import-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const querySessionNotice = require('../db/query-session-notification.js')
const queryDeviceType = require('../db/query-device-type.js')
const queryCommand = require('../db/query-command.js')
const queryConfig = require('../db/query-config.js')
const queryFeature = require('../db/query-feature.js')
const zclLoader = require('../zcl/zcl-loader.js')
const generationEngine = require('../generator/generation-engine')

Expand Down Expand Up @@ -499,6 +500,16 @@ async function importClusters(
endpointId,
sessionId
)

await setElementConformWarning(
db,
endpointId,
endpointTypeId,
endpointClusterId,
deviceTypeRefs,
clusters[k],
sessionId
)
}
}
}
Expand Down Expand Up @@ -2057,6 +2068,126 @@ async function jsonDataLoader(
}
}

/**
* Adds warnings to the session notification table for elements
* that do not conform correctly when importing a ZAP file.
*
* @param {*} db
* @param {*} endpointId
* @param {*} endpointTypeId
* @param {*} endpointClusterId
* @param {*} deviceTypeRefs
* @param {*} cluster
* @param {*} sessionId
* @returns true if there are warnings set, false otherwise
*/
async function setElementConformWarning(
db,
endpointId,
endpointTypeId,
endpointClusterId,
deviceTypeRefs,
cluster,
sessionId
) {
let deviceTypeFeatures = await queryFeature.getFeaturesByDeviceTypeRefs(
db,
deviceTypeRefs,
endpointTypeId
)
let clusterFeatures = deviceTypeFeatures.filter(
(feature) => feature.endpointTypeClusterId == endpointClusterId
)

if (clusterFeatures.length > 0) {
let deviceTypeClusterId = clusterFeatures[0].deviceTypeClusterId
let endpointTypeElements = await queryZcl.getEndpointTypeElements(
db,
endpointClusterId,
deviceTypeClusterId
)

let featureMapVal = clusterFeatures[0].featureMapValue
let featureMap = {}
for (let feature of clusterFeatures) {
let bit = feature.bit
let bitVal = (featureMapVal & (1 << bit)) >> bit
featureMap[feature.code] = bitVal
}

// get elements that should be mandatory or unsupported based on conformance
let requiredElements = queryFeature.checkElementConformance(
endpointTypeElements,
featureMap
)

let contextMessage = `On endpoint ${endpointId}, cluster: ${cluster.name}, `
let warnings = []

/* If unsupported elements are enabled or required elements are disabled,
they are considered non-conforming. A corresponding warning message will be
generated and added to the warnings array. */
const filterNonConformElements = (
elementType,
requiredMap,
notSupportedMap,
elements
) => {
let elementMap = {}
elements.forEach((element) => {
elementType == 'command'
? (elementMap[element.id] = element.isEnabled)
: (elementMap[element.id] = element.included)
})
Object.entries(requiredMap).forEach(([id, message]) => {
if (!(id in elementMap) || !elementMap[id]) {
warnings.push(contextMessage + elementType + ': ' + message)
}
})
Object.entries(notSupportedMap).forEach(([id, message]) => {
if (id in elementMap && elementMap[id]) {
warnings.push(contextMessage + elementType + ': ' + message)
}
})
}

filterNonConformElements(
'attribute',
requiredElements.attributesToUpdate.required,
requiredElements.attributesToUpdate.notSupported,
endpointTypeElements.attributes
)
filterNonConformElements(
'command',
requiredElements.commandsToUpdate.required,
requiredElements.commandsToUpdate.notSupported,
endpointTypeElements.commands
)
filterNonConformElements(
'event',
requiredElements.eventsToUpdate.required,
requiredElements.eventsToUpdate.notSupported,
endpointTypeElements.events
)

// set warnings in the session notification table
if (warnings.length > 0) {
for (const warning of warnings) {
await querySessionNotice.setNotification(
db,
'WARNING',
warning,
sessionId,
1,
0
)
}
return true
}
}
return false
}

/**
* This function cleans up some backwards-compatible problems in zap.
* files.
Expand Down

0 comments on commit eeaa431

Please sign in to comment.