Skip to content

Commit cfdf9e1

Browse files
authored
Category-based package matching. (#830)
* Proper renaming of a completely confusing function. * Clean up the code a bit. * Consider category as a most important criteria in the matching of packages.
1 parent 758816a commit cfdf9e1

File tree

11 files changed

+115
-45
lines changed

11 files changed

+115
-45
lines changed

docs/api.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,14 @@ NOTE: This function does NOT initialize session packages.</p>
791791
<dt><a href="#importSessionKeyValues">importSessionKeyValues(db, sessionId, keyValuePairs)</a></dt>
792792
<dd><p>Resolves with a promise that imports session key values.</p>
793793
</dd>
794+
<dt><a href="#autoLoadPackage">autoLoadPackage(db, pkg, absPath)</a> ⇒</dt>
795+
<dd><p>Auto-load package. If succesful it returns an object.
796+
Otherwise it throws an exception.</p>
797+
</dd>
798+
<dt><a href="#convertPackageResult">convertPackageResult(data)</a> ⇒</dt>
799+
<dd><p>Convert the array of results into a more palatable value.
800+
Resolves an array of { packageId:, packageType:} objects into { zclPackageId: id, templateIds: [] }</p>
801+
</dd>
794802
<dt><a href="#jsonDataLoader">jsonDataLoader(db, state, sessionId, packageMatch)</a> ⇒</dt>
795803
<dd><p>Given a state object, this method returns a promise that resolves
796804
with the succesfull writing into the database.</p>
@@ -12190,7 +12198,7 @@ Checks if type is a two-byte lengh string.
1219012198

1219112199
- [JS API: random utilities](#module*JS API* random utilities)
1219212200
- [~checksum(data)](#module*JS API* random utilities..checksum) ⇒
12193-
- [~initializeSessionPackage(db, sessionId, options:, selectedZclPropertyPackage, selectedGenTemplatePackages)](#module*JS API* random utilities..initializeSessionPackage) ⇒
12201+
- [~ensurePackagesAndPopulateSessionOptions(db, sessionId, options:, selectedZclPropertyPackage, selectedGenTemplatePackages)](#module*JS API* random utilities..ensurePackagesAndPopulateSessionOptions) ⇒
1219412202
- [~createBackupFile(filePath)](#module*JS API* random utilities..createBackupFile)
1219512203
- [~matchFeatureLevel(featureLevel)](#module*JS API* random utilities..matchFeatureLevel)
1219612204
- [~sessionReport(db, sessionId)](#module*JS API* random utilities..sessionReport) ⇒
@@ -12222,11 +12230,12 @@ Returns the CRC of the data that is passed.
1222212230
| ----- | --------------- |
1222312231
| data | <code>\*</code> |
1222412232

12225-
<a name="module_JS API_ random utilities..initializeSessionPackage"></a>
12233+
<a name="module_JS API_ random utilities..ensurePackagesAndPopulateSessionOptions"></a>
1222612234

12227-
### JS API: random utilities~initializeSessionPackage(db, sessionId, options:, selectedZclPropertyPackage, selectedGenTemplatePackages) ⇒
12235+
### JS API: random utilities~ensurePackagesAndPopulateSessionOptions(db, sessionId, options:, selectedZclPropertyPackage, selectedGenTemplatePackages) ⇒
1222812236

12229-
This function assigns a proper package ID to the session.
12237+
This function assigns a proper package ID to the session if there
12238+
are no packages present. It will also populate session options.
1223012239

1223112240
**Kind**: inner method of [<code>JS API: random utilities</code>](#module*JS API* random utilities)
1223212241
**Returns**: Promise that resolves with the packages array.
@@ -14208,6 +14217,36 @@ Resolves with a promise that imports session key values.
1420814217
| sessionId | <code>\*</code> |
1420914218
| keyValuePairs | <code>\*</code> |
1421014219

14220+
<a name="autoLoadPackage"></a>
14221+
14222+
## autoLoadPackage(db, pkg, absPath) ⇒
14223+
14224+
Auto-load package. If succesful it returns an object.
14225+
Otherwise it throws an exception.
14226+
14227+
**Kind**: global function
14228+
**Returns**: object containing packageId and packageType.
14229+
14230+
| Param | Type |
14231+
| ------- | --------------- |
14232+
| db | <code>\*</code> |
14233+
| pkg | <code>\*</code> |
14234+
| absPath | <code>\*</code> |
14235+
14236+
<a name="convertPackageResult"></a>
14237+
14238+
## convertPackageResult(data) ⇒
14239+
14240+
Convert the array of results into a more palatable value.
14241+
Resolves an array of { packageId:, packageType:} objects into { zclPackageId: id, templateIds: [] }
14242+
14243+
**Kind**: global function
14244+
**Returns**: an object that contains session ids.
14245+
14246+
| Param | Type |
14247+
| ----- | --------------- |
14248+
| data | <code>\*</code> |
14249+
1421114250
<a name="jsonDataLoader"></a>
1421214251

1421314252
## jsonDataLoader(db, state, sessionId, packageMatch) ⇒

src-electron/importexport/import-isc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ async function iscDataLoader(db, state, sessionId) {
651651

652652
// We don't have the package info inside ISC file, so we
653653
// do our best here.
654-
await util.initializeSessionPackage(db, sessionId, {
654+
await util.ensurePackagesAndPopulateSessionOptions(db, sessionId, {
655655
zcl: state.zclMetafile,
656656
template: null,
657657
})

src-electron/importexport/import-json.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,26 @@ async function importSinglePackage(db, pkg, zapFilePath, packageMatch) {
148148
}
149149
}
150150

151-
// We have only one. Use it and be done
152-
if (packages.length == 1) {
151+
// Moving on. We have more than one package, so we have to
152+
// make a smart decision.
153+
//
154+
155+
// First narrow down to the category.
156+
let categoryMatch = packages.filter((p) => p.category == pkg.category)
157+
if (categoryMatch.length == 1) {
153158
env.logDebug(
154-
`Only one package of given type ${pkg.type} present. Using it.`
159+
`Only one package of given type ${pkg.type} and category ${pkg.category} present. Using it.`
155160
)
156161
return {
157-
packageId: packages[0].id,
162+
packageId: categoryMatch[0].id,
158163
packageType: pkg.type,
159164
}
160165
}
161166

162167
// Filter to just the ones that match the version
163-
packages = packages.filter((p) => p.version == pkg.version)
168+
let versionMatch = packages.filter((p) => p.version == pkg.version)
164169
// If there isn't any abort, if there is only one, use it.
165-
if (packages.length == 0) {
170+
if (versionMatch.length == 0) {
166171
let msg = `No packages of type ${pkg.type} that match version ${pkg.version} found in the database.`
167172
if (pkg.type == dbEnum.packageType.genTemplatesJson) {
168173
// We don't throw exception for genTemplatesJson, we can survive without.
@@ -171,16 +176,18 @@ async function importSinglePackage(db, pkg, zapFilePath, packageMatch) {
171176
} else {
172177
throw new Error(msg)
173178
}
174-
} else if (packages.length == 1) {
179+
} else if (versionMatch.length == 1) {
175180
env.logDebug(
176181
`Only one package of given type ${pkg.type} and version ${pkg.version} present. Using it.`
177182
)
178183
return {
179-
packageId: packages[0].id,
184+
packageId: versionMatch[0].id,
180185
packageType: pkg.type,
181186
}
182187
}
183188

189+
// Neither category match, nor version match had only one version.
190+
184191
// We now know we have more than 1 matching package. Find best bet.
185192
let existingPackages = packages.filter(
186193
(p) => fs.existsSync(p.path) && p.path === absPath
@@ -217,7 +224,13 @@ async function importSinglePackage(db, pkg, zapFilePath, packageMatch) {
217224
}
218225
}
219226

220-
// Resolves an array of { packageId:, packageType:} objects into { zclPackageId: id, templateIds: [] }
227+
/**
228+
* Convert the array of results into a more palatable value.
229+
* Resolves an array of { packageId:, packageType:} objects into { zclPackageId: id, templateIds: [] }
230+
*
231+
* @param {*} data
232+
* @returns an object that contains session ids.
233+
*/
221234
function convertPackageResult(data) {
222235
let ret = {
223236
zclPackageId: null,

src-electron/importexport/import.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
* and imports it into a database.
2121
*/
2222
const fsp = require('fs').promises
23-
const path = require('path')
24-
2523
const importIsc = require('./import-isc.js')
2624
const importJson = require('./import-json.js')
2725
const dbApi = require('../db/db-api.js')

src-electron/main-process/startup.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ async function startConvert(argv, options) {
210210

211211
let sessionId = importResult.sessionId
212212

213-
await util.initializeSessionPackage(db, sessionId, {
213+
await util.ensurePackagesAndPopulateSessionOptions(db, sessionId, {
214214
zcl: argv.zclProperties,
215215
template: argv.generationTemplate,
216216
})
@@ -525,7 +525,11 @@ async function generateSingleFile(
525525
}
526526
options.logger(`👉 using output destination: ${output}`)
527527

528-
let sessPkg = await util.initializeSessionPackage(db, sessionId, options)
528+
let sessPkg = await util.ensurePackagesAndPopulateSessionOptions(
529+
db,
530+
sessionId,
531+
options
532+
)
529533
let usedTemplatePackageId = templatePackageId
530534
for (let pkg of sessPkg) {
531535
if (pkg.type === dbEnum.packageType.genTemplatesJson) {

src-electron/rest/initialize.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const restApi = require('../../src-shared/rest-api.js')
55
const util = require('../util/util.js')
66

77
/**
8-
* This function returns Properties, Templates and Dirty-Sessions
9-
* @param {*} db
10-
* @returns Properties, Templates and Dirty-Sessions.
11-
*/
8+
* This function returns Properties, Templates and Dirty-Sessions
9+
* @param {*} db
10+
* @returns Properties, Templates and Dirty-Sessions.
11+
*/
1212
module.exports.packagesAndSessions = (db) => {
1313
return async (req, res) => {
1414
const zclProperties = await queryPackage.getPackagesByType(
@@ -29,19 +29,19 @@ module.exports.packagesAndSessions = (db) => {
2929
}
3030

3131
/**
32-
* This function creates a new session with its packages according to selected Properties and Templates
33-
* @param {*} db
34-
* @param {*} options: object containing 'zcl' and 'template'
35-
* @returns A success message.
36-
*/
32+
* This function creates a new session with its packages according to selected Properties and Templates
33+
* @param {*} db
34+
* @param {*} options: object containing 'zcl' and 'template'
35+
* @returns A success message.
36+
*/
3737
module.exports.initializeSession = (db, options) => {
3838
return async (req, res) => {
3939
let sessionUuid = req.query[restApi.param.sessionId]
4040
let userKey = req.session.id
4141
let user = await querySession.ensureUser(db, userKey)
4242
let sessionId = await querySession.ensureBlankSession(db, sessionUuid)
4343
await querySession.linkSessionToUser(db, sessionId, user.userId)
44-
await util.initializeSessionPackage(
44+
await util.ensurePackagesAndPopulateSessionOptions(
4545
db,
4646
sessionId,
4747
options,
@@ -55,10 +55,10 @@ module.exports.initializeSession = (db, options) => {
5555
}
5656

5757
/**
58-
* This function reloads previous session by user selected session's id
59-
* @param {*} db
60-
* @returns A success message.
61-
*/
58+
* This function reloads previous session by user selected session's id
59+
* @param {*} db
60+
* @returns A success message.
61+
*/
6262
module.exports.loadPreviousSessions = (db) => {
6363
return async (req, res) => {
6464
let sessionUuid = req.query[restApi.param.sessionId]

src-electron/server/http-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function userSessionHandler(db, options) {
220220
})
221221
.then((result) => {
222222
if (result.newSession) {
223-
return util.initializeSessionPackage(
223+
return util.ensurePackagesAndPopulateSessionOptions(
224224
db,
225225
result.sessionId,
226226
options,

src-electron/util/util.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ function checksum(data) {
4848
}
4949

5050
/**
51-
* This function assigns a proper package ID to the session.
51+
* This function assigns a proper package ID to the session if there
52+
* are no packages present. It will also populate session options.
5253
*
5354
* @param {*} db
5455
* @param {*} sessionId
@@ -57,7 +58,7 @@ function checksum(data) {
5758
* @param {*} selectedGenTemplatePackages
5859
* @returns Promise that resolves with the packages array.
5960
*/
60-
async function initializeSessionPackage(
61+
async function ensurePackagesAndPopulateSessionOptions(
6162
db,
6263
sessionId,
6364
options,
@@ -187,9 +188,20 @@ async function initializeSessionPackage(
187188
promises.push(genTemplateJsonPromise)
188189
}
189190

190-
await Promise.all(promises)
191+
if (promises.length > 0) await Promise.all(promises)
191192

193+
// We read all the packages.
192194
let packages = await queryPackage.getSessionPackagesWithTypes(db, sessionId)
195+
196+
// Now we create promises with the queries that populate the
197+
// session key/value pairs from package options.
198+
199+
await populateSessionPackageOptions(db, sessionId, packages)
200+
201+
return packages
202+
}
203+
204+
async function populateSessionPackageOptions(db, sessionId, packages) {
193205
let p = packages.map((pkg) =>
194206
queryPackage
195207
.selectAllDefaultOptions(db, pkg.packageRef)
@@ -211,8 +223,7 @@ async function initializeSessionPackage(
211223
)
212224
)
213225

214-
await Promise.all(p)
215-
return packages
226+
return Promise.all(p)
216227
}
217228

218229
/**
@@ -680,7 +691,8 @@ async function collectJsonData(jsonFile, recursiveLevel = 0) {
680691

681692
exports.createBackupFile = createBackupFile
682693
exports.checksum = checksum
683-
exports.initializeSessionPackage = initializeSessionPackage
694+
exports.ensurePackagesAndPopulateSessionOptions =
695+
ensurePackagesAndPopulateSessionOptions
684696
exports.matchFeatureLevel = matchFeatureLevel
685697
exports.sessionReport = sessionReport
686698
exports.sessionDump = sessionDump

test/gen-zigbee-1.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ test(
9696
test(
9797
'Initialize session packages',
9898
async () => {
99-
let packages = await utilJs.initializeSessionPackage(
99+
let packages = await utilJs.ensurePackagesAndPopulateSessionOptions(
100100
templateContext.db,
101101
templateContext.sessionId,
102102
{

test/query.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe('Session specific queries', () => {
187187
'SESSION'
188188
)
189189
sid = userSession.sessionId
190-
await util.initializeSessionPackage(
190+
await util.ensurePackagesAndPopulateSessionOptions(
191191
db,
192192
sid,
193193
{

0 commit comments

Comments
 (0)