Skip to content

Commit

Permalink
cleanup, add try/catch and better comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paulr34 committed Aug 13, 2024
1 parent 64a22e6 commit 608734d
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 89 deletions.
56 changes: 30 additions & 26 deletions src-electron/db/query-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,34 +704,38 @@ async function getAllPackages(db) {
* @returns {Promise<Array>} A promise that resolves to an array of option objects, each containing the option category, code, and label.
*/
async function getAttributeAccessInterface(db, code, packageId) {
const extendedQuery = `
SELECT
po.OPTION_CATEGORY,
po.OPTION_CODE,
po.OPTION_LABEL
FROM
PACKAGE_OPTION po
WHERE
po.OPTION_CODE = ?
AND po.PACKAGE_REF = ?
try {
const extendedQuery = `
SELECT
po.OPTION_CATEGORY,
po.OPTION_CODE,
po.OPTION_LABEL
FROM
PACKAGE_OPTION po
WHERE
po.OPTION_CODE = ?
AND po.PACKAGE_REF = ?
UNION
UNION
SELECT
c.NAME AS OPTION_CATEGORY,
a.STORAGE_POLICY AS OPTION_CODE,
a.NAME AS OPTION_LABEL
FROM
ATTRIBUTE a
LEFT JOIN CLUSTER c ON a.CLUSTER_REF = c.CLUSTER_ID
WHERE
a.STORAGE_POLICY = ?
AND a.PACKAGE_REF = ?
`
return dbApi
.dbAll(db, extendedQuery, [code, packageId, code, packageId]) // Note the [code, code] to match both placeholders
.then((rows) => rows.map(dbMapping.map.options))
SELECT
c.NAME AS OPTION_CATEGORY,
a.STORAGE_POLICY AS OPTION_CODE,
a.NAME AS OPTION_LABEL
FROM
ATTRIBUTE a
LEFT JOIN CLUSTER c ON a.CLUSTER_REF = c.CLUSTER_ID
WHERE
a.STORAGE_POLICY = ?
AND a.PACKAGE_REF = ?
`
return dbApi
.dbAll(db, extendedQuery, [code, packageId, code, packageId]) // Note the [code, packageId, code, packageId] to match both placeholders
.then((rows) => rows.map(dbMapping.map.options))
} catch (error) {
console.error('Error fetching attribute access interface:', error)
throw error // Rethrow the error for further handling if necessary
}
}

/**
Expand Down
153 changes: 91 additions & 62 deletions src-electron/sdk/matter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,33 @@ const dbEnum = require('../../src-shared/db-enum.js')
*/

async function getForcedExternalStorage(db, packageId) {
let forcedExternal = await queryPackage.getAttributeAccessInterface(
db,
dbEnum.storagePolicy.attributeAccessInterface,
packageId
)
return forcedExternal
try {
let forcedExternal = await queryPackage.getAttributeAccessInterface(
db,
dbEnum.storagePolicy.attributeAccessInterface,
packageId
)
return forcedExternal
} catch (error) {
console.error('Error fetching forced external storage:', error)
throw error // Optionally re-throw the error for further handling
}
}

/**
* This function takes a clusterId (the database ID, not the specification-defined ID) and an array of attributes (associated with the database defined clusterID)
* and changes the global attributes (attributes with specification defined clusterId = null) to represent storage policy
* based on the cluster/attribute pair in zcl.json
* This function takes a clusterId (the database ID, not the specification-defined ID), an array of attributes (associated with the database defined clusterID),
* and a packageId to identify the specific package the attributes belong to. It changes the global attributes (attributes with specification defined clusterId = null) to represent storage policy
* based on the cluster/attribute pair in zcl.json.
*
* Although the specification defined clusterID of the attriibute is null indicating it is a global attribute, we know what the database defined clusterID is by what is passed in as a parameter.
* Although the specification defined clusterID of the attribute is null indicating it is a global attribute, we know what the database defined clusterID is by what is passed in as a parameter.
*
* That database defined clusterID is used to query the name of the cluster which is in turn used to compute the storage policy for that cluster/attribute pair.
* That database defined clusterID is used to query the name of the cluster which is in turn used to compute the storage policy for that cluster/attribute pair based on the packageId.
*
* @export
* @param {*} db
* @param {*} clusterId (database defined) the clusterId representing a cluster from the database being used in the application
* @param {*} attributes an array of objects representing the attributes associated with the cluster
* @param {*} packageId the ID of the package to which the attributes belong, used to determine storage policies specific to the package
* @returns an array of objects representing attributes in the database
*/

Expand All @@ -61,26 +67,34 @@ async function computeStoragePolicyForGlobalAttributes(
attributes,
packageId
) {
let forcedExternal
let clusterName = await queryCluster.selectClusterName(db, clusterId)
return Promise.all(
attributes.map(async (attribute) => {
if (attribute.clusterId == null) {
forcedExternal = await getForcedExternalStorage(db, packageId)
forcedExternal.some((option) => {
if (
option.optionCategory == clusterName &&
option.optionLabel == attribute.name
) {
attribute.storagePolicy =
dbEnum.storagePolicy.attributeAccessInterface
return true
}
})
}
return attribute
})
)
try {
let forcedExternal
let clusterName = await queryCluster.selectClusterName(db, clusterId)
return Promise.all(
attributes.map(async (attribute) => {
if (attribute.clusterId == null) {
forcedExternal = await getForcedExternalStorage(db, packageId)
forcedExternal.some((option) => {
if (
option.optionCategory == clusterName &&
option.optionLabel == attribute.name
) {
attribute.storagePolicy =
dbEnum.storagePolicy.attributeAccessInterface
return true
}
})
}
return attribute
})
)
} catch (error) {
console.error(
'Failed to compute storage policy for global attributes:',
error
)
throw error // Rethrow the error if you want to handle it further up the call stack
}
}
/**
* This asynchronous function computes and returns the new configuration for a storage option.
Expand All @@ -95,15 +109,20 @@ async function computeStoragePolicyForGlobalAttributes(
*/

async function computeStorageOptionNewConfig(storagePolicy) {
let storageOption
if (storagePolicy == dbEnum.storagePolicy.attributeAccessInterface) {
storageOption = dbEnum.storageOption.external
} else if (storagePolicy == dbEnum.storagePolicy.any) {
storageOption = dbEnum.storageOption.ram
} else {
throw 'check storage policy'
try {
let storageOption
if (storagePolicy == dbEnum.storagePolicy.attributeAccessInterface) {
storageOption = dbEnum.storageOption.external
} else if (storagePolicy == dbEnum.storagePolicy.any) {
storageOption = dbEnum.storageOption.ram
} else {
throw new Error('Invalid storage policy')
}
return storageOption
} catch (error) {
console.error('Error computing new storage option config:', error)
throw error // Rethrow the error for further handling if necessary
}
return storageOption
}
/**
* This asynchronous function computes and returns the new configuration for a storage policy.
Expand All @@ -126,17 +145,22 @@ async function computeStoragePolicyNewConfig(
forcedExternal,
attributeName
) {
let clusterName = await queryCluster.selectClusterName(db, clusterRef)
forcedExternal.some((option) => {
if (
option.optionCategory == clusterName &&
option.optionLabel == attributeName
) {
storagePolicy = dbEnum.storagePolicy.attributeAccessInterface
return true
}
})
return storagePolicy
try {
let clusterName = await queryCluster.selectClusterName(db, clusterRef)
forcedExternal.some((option) => {
if (
option.optionCategory == clusterName &&
option.optionLabel == attributeName
) {
storagePolicy = dbEnum.storagePolicy.attributeAccessInterface
return true
}
})
return storagePolicy
} catch (error) {
console.error('Error computing storage policy new config:', error)
throw error // Rethrow the error for further handling if necessary
}
}

/**
Expand All @@ -162,18 +186,23 @@ async function computeStorageImport(
forcedExternal,
attributeName
) {
let updatedStoragePolicy = storagePolicy
forcedExternal.some((option) => {
if (
option.optionCategory == clusterName &&
option.optionLabel == attributeName
) {
updatedStoragePolicy = dbEnum.storagePolicy.attributeAccessInterface
return true
}
return false
})
return updatedStoragePolicy
try {
let updatedStoragePolicy = storagePolicy
forcedExternal.some((option) => {
if (
option.optionCategory == clusterName &&
option.optionLabel == attributeName
) {
updatedStoragePolicy = dbEnum.storagePolicy.attributeAccessInterface
return true
}
return false
})
return updatedStoragePolicy
} catch (error) {
console.error('Error computing storage import:', error)
throw error // Rethrow the error for further handling if necessary
}
}

exports.getForcedExternalStorage = getForcedExternalStorage
Expand Down
2 changes: 1 addition & 1 deletion src-electron/zcl/zcl-loader-silabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,7 @@ async function parseBoolOptions(db, pkgRef, booleanCategories) {
* by mapping its values to a specific structure and then inserting them into the database using
* the insertOptionsKeyValues function.
*
* The main purpose of this function is to store cluster/attribute pairs to include global attributes and their cluster pair
* The main purpose of this function is to store cluster/attribute pairs including global attributes and their cluster pair
* The ATTRIBUTE table has cluster_ref as null for global attributes so this second method was necessary
*
* @param {*} db - The database connection object.
Expand Down

0 comments on commit 608734d

Please sign in to comment.