@@ -28,6 +28,59 @@ const restApi = require('../../src-shared/rest-api.js')
2828const util = require ( '../util/util.js' )
2929const fs = require ( 'fs' )
3030const fsp = fs . promises
31+ const path = require ( 'path' )
32+ const zclLoader = require ( '../zcl/zcl-loader.js' )
33+ const genEngine = require ( '../generator/generation-engine.js' )
34+
35+ /**
36+ * Helper function to check if a package exists in database and load it if not
37+ * @param {* } db - Database connection
38+ * @param {string } packagePath - Path to the package file
39+ * @param {string } packageType - Type of package (zclProperties or genTemplatesJson)
40+ * @returns {Promise<Object|null> } - Package object from database or null if failed
41+ */
42+ async function ensurePackageLoaded ( db , packagePath , packageType ) {
43+ if ( ! packagePath ) return null
44+
45+ try {
46+ // Check if package already exists in database
47+ let existingPackage = await queryPackage . getPackageByPathAndType (
48+ db ,
49+ packagePath ,
50+ packageType
51+ )
52+
53+ if ( ! existingPackage ) {
54+ console . log ( `Loading package: ${ packagePath } ` )
55+
56+ if ( packageType === dbEnum . packageType . zclProperties ) {
57+ // Load ZCL properties package
58+ let packageContext = await zclLoader . loadZcl ( db , packagePath )
59+ existingPackage = await queryPackage . getPackageByPackageId (
60+ db ,
61+ packageContext . packageId
62+ )
63+ } else if ( packageType === dbEnum . packageType . genTemplatesJson ) {
64+ // Load template package using generation engine
65+ let templateContext = await genEngine . loadTemplates ( db , [ packagePath ] )
66+ if (
67+ templateContext . packageIds &&
68+ templateContext . packageIds . length > 0
69+ ) {
70+ existingPackage = await queryPackage . getPackageByPackageId (
71+ db ,
72+ templateContext . packageIds [ 0 ]
73+ )
74+ }
75+ }
76+ }
77+
78+ return existingPackage
79+ } catch ( error ) {
80+ env . logWarning ( `Failed to load package ${ packagePath } : ${ error . message } ` )
81+ return null
82+ }
83+ }
3184
3285/**
3386 * This function returns Properties, Templates and Dirty-Sessions
@@ -43,6 +96,16 @@ function sessionAttempt(db) {
4396 let filePathExtension = query . get ( 'zapFileExtensions' )
4497 if ( filePath ) {
4598 if ( filePath . includes ( '.zap' ) ) {
99+ let slcArgsFile = path . join ( path . dirname ( filePath ) , 'slc_args.json' )
100+ let slcArgs = null
101+ try {
102+ if ( fs . existsSync ( slcArgsFile ) ) {
103+ const slcArgsData = await fsp . readFile ( slcArgsFile , 'utf8' )
104+ slcArgs = JSON . parse ( slcArgsData )
105+ }
106+ } catch ( error ) {
107+ env . logWarning ( `Failed to read slc_args.json: ${ error . message } ` )
108+ }
46109 let data = await fsp . readFile ( filePath )
47110 let obj = JSON . parse ( data )
48111 let category = [ ]
@@ -53,6 +116,68 @@ function sessionAttempt(db) {
53116 category . push ( pkgCategory )
54117 }
55118 } )
119+
120+ if ( slcArgs ) {
121+ let open = true
122+ let zclProperties = [ ]
123+ let zclGenTemplates = [ ]
124+
125+ if ( category . includes ( `'${ dbEnum . helperCategory . zigbee } '` ) ) {
126+ // Load Zigbee ZCL properties if specified
127+ let zclPackage = await ensurePackageLoaded (
128+ db ,
129+ slcArgs [ dbEnum . slcArgs . zigbeeZclJsonFile ] ,
130+ dbEnum . packageType . zclProperties
131+ )
132+ if ( zclPackage ) {
133+ zclProperties . push ( zclPackage )
134+ }
135+
136+ // Load Zigbee templates if specified
137+ let templatePackage = await ensurePackageLoaded (
138+ db ,
139+ slcArgs [ dbEnum . slcArgs . zigbeeTemplateJsonFile ] ,
140+ dbEnum . packageType . genTemplatesJson
141+ )
142+ if ( templatePackage ) {
143+ zclGenTemplates . push ( templatePackage )
144+ }
145+ }
146+
147+ if ( category . includes ( `'${ dbEnum . helperCategory . matter } '` ) ) {
148+ // Load Matter ZCL properties if specified
149+ let zclPackage = await ensurePackageLoaded (
150+ db ,
151+ slcArgs [ dbEnum . slcArgs . matterZclJsonFile ] ,
152+ dbEnum . packageType . zclProperties
153+ )
154+ if ( zclPackage ) {
155+ zclProperties . push ( zclPackage )
156+ }
157+
158+ // Load Matter templates if specified
159+ let templatePackage = await ensurePackageLoaded (
160+ db ,
161+ slcArgs [ dbEnum . slcArgs . matterTemplateJsonFile ] ,
162+ dbEnum . packageType . genTemplatesJson
163+ )
164+ if ( templatePackage ) {
165+ zclGenTemplates . push ( templatePackage )
166+ }
167+ }
168+
169+ const sessions = await querySession . getDirtySessionsWithPackages ( db )
170+ return res . send ( {
171+ zclGenTemplates,
172+ zclProperties,
173+ sessions,
174+ filePath,
175+ zapFilePackages,
176+ open,
177+ filePathExtension
178+ } )
179+ }
180+
56181 if ( category . length > 0 ) {
57182 let open = true
58183 const zclProperties = await queryPackage . getPackagesByCategoryAndType (
@@ -251,6 +376,13 @@ function init(db) {
251376 }
252377}
253378
379+ // Export individual functions for testing
380+ exports . sessionAttempt = sessionAttempt
381+ exports . sessionCreate = sessionCreate
382+ exports . initializeSession = initializeSession
383+ exports . loadPreviousSessions = loadPreviousSessions
384+ exports . init = init
385+
254386exports . post = [
255387 {
256388 uri : restApi . uri . reloadSession ,
0 commit comments