@@ -141,57 +141,11 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
141141 return Promise . resolve ( result ) ;
142142 }
143143 //checking all folders
144- listFolders . forEach ( dir => {
145- const filePath = `${ dir } \\${ this . _entityLabel . toLowerCase ( ) } .fastiot.yaml` ;
146- let entity = new this . TCreator ( this . Config . schemasFolderPath ) ;
147- entity . Init ( type , filePath , this . Config . recoverySourcePath ) ;
148- if ( ! entity . IsValid && type == EntityType . system ) {
149- //Recovery
150- this . CreateEvent ( `${ this . _entityLabel } recovery: ${ path . dirname ( filePath ) } ` , LogLevel . Debug ) ;
151- result = entity . Recovery ( ) ;
152- if ( result . Status == StatusResult . Ok ) {
153- entity . Init ( type , filePath , this . Config . recoverySourcePath ) ; }
154- else {
155- this . CreateEvent ( result , LogLevel . Debug ) ;
156- }
157- }
158- //main
159- if ( entity . IsValid ) {
160- this . CreateEvent ( `${ this . _entityLabel } is valid: [${ entity . Attributes . Id } ]` , LogLevel . Debug ) ;
161- if ( this . IsCompatibleByVersionExtAndPlatform ( entity ) ) {
162- const isContains = this . Contains ( entity ) ;
163- switch ( isContains ) {
164- case ContainsType . no : {
165- this . Add ( entity ) ;
166- this . CreateEvent ( `${ this . _entityLabel } added: [${ entity . Attributes . Id } ] ${ entity . RootDir } ` , LogLevel . Debug ) ;
167- break ;
168- }
169- case ContainsType . yesVersionSmaller : {
170- this . Update ( entity ) ;
171- this . CreateEvent ( `${ this . _entityLabel } updated: [${ entity . Attributes . Id } ] ${ entity . RootDir } ` , LogLevel . Debug ) ;
172- break ;
173- }
174- default : {
175- this . CreateEvent ( `Adding a ${ this . _entityLabel } was skipped because already in the collection: [${ entity . Attributes . Id } ] ${ entity . RootDir } ` , LogLevel . Debug ) ;
176- break ;
177- }
178- }
179- } else {
180- result = new IotResult ( StatusResult . Error , `The ${ this . _entityLabel } ${ entity . RootDir } is for a newer version of the extension. ` +
181- `Update the extension.` ) ;
182- this . CreateEvent ( result ) ;
183- }
184- } else {
185- result = new IotResult ( StatusResult . Error , `The ${ this . _entityLabel } ${ entity . RootDir } has not been validated.` ) ;
186- this . CreateEvent ( result ) ;
187- this . CreateEvent ( entity . ValidationErrorsToString , LogLevel . Debug ) ;
188- //delete system entity
189- if ( type == EntityType . system ) {
190- result = entity . Remove ( ) ;
191- this . CreateEvent ( result , LogLevel . Debug ) ;
192- }
193- }
194- } ) ;
144+ let dir :string ;
145+ for ( let i = 0 ; i < listFolders . length ; i ++ ) {
146+ dir = listFolders [ i ] ;
147+ await this . ImportEntity ( dir , type ) ;
148+ }
195149 //result
196150 if ( ( this . Count - entitysCountBegin ) > 0 ) {
197151 result = new IotResult ( StatusResult . Ok , `Loading ${ type } ${ this . _entitiesLabel } from ${ pathFolder } folder successfully completed` ) ;
@@ -206,6 +160,68 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
206160 return Promise . resolve ( result ) ;
207161 }
208162
163+ protected async ImportEntity ( dir :string , type :EntityType ) :Promise < IotResult >
164+ {
165+ let result :IotResult ;
166+ try {
167+ const filePath = `${ dir } \\${ this . _entityLabel . toLowerCase ( ) } .fastiot.yaml` ;
168+ let entity = new this . TCreator ( this . Config . schemasFolderPath ) ;
169+ entity . Init ( type , filePath , this . Config . recoverySourcePath ) ;
170+ if ( ! entity . IsValid && type == EntityType . system ) {
171+ //Recovery
172+ this . CreateEvent ( `${ this . _entityLabel } recovery: ${ path . dirname ( filePath ) } ` , LogLevel . Debug ) ;
173+ result = entity . Recovery ( ) ;
174+ if ( result . Status == StatusResult . Ok ) {
175+ entity . Init ( type , filePath , this . Config . recoverySourcePath ) ; }
176+ else {
177+ this . CreateEvent ( result , LogLevel . Debug ) ;
178+ }
179+ }
180+ //main
181+ if ( entity . IsValid ) {
182+ this . CreateEvent ( `${ this . _entityLabel } is valid: [${ entity . Attributes . Id } ]` , LogLevel . Debug ) ;
183+ if ( this . IsCompatibleByVersionExtAndPlatform ( entity ) ) {
184+ const isContains = this . Contains ( entity ) ;
185+ switch ( isContains ) {
186+ case ContainsType . no : {
187+ this . Add ( entity ) ;
188+ this . CreateEvent ( `${ this . _entityLabel } added: [${ entity . Attributes . Id } ] ${ entity . RootDir } ` , LogLevel . Debug ) ;
189+ result = new IotResult ( StatusResult . Ok , `${ this . _entityLabel } added: [${ entity . Attributes . Id } ] ${ entity . RootDir } ` ) ;
190+ break ;
191+ }
192+ case ContainsType . yesVersionSmaller : {
193+ this . Update ( entity ) ;
194+ this . CreateEvent ( `${ this . _entityLabel } updated: [${ entity . Attributes . Id } ] ${ entity . RootDir } ` , LogLevel . Debug ) ;
195+ result = new IotResult ( StatusResult . Ok , `${ this . _entityLabel } updated: [${ entity . Attributes . Id } ] ${ entity . RootDir } ` ) ;
196+ break ;
197+ }
198+ default : {
199+ this . CreateEvent ( `Adding a ${ this . _entityLabel } was skipped because already in the collection: [${ entity . Attributes . Id } ] ${ entity . RootDir } ` , LogLevel . Debug ) ;
200+ result = new IotResult ( StatusResult . Error , `Adding a ${ this . _entityLabel } was skipped because already in the collection: [${ entity . Attributes . Id } ] ${ entity . RootDir } ` ) ;
201+ break ;
202+ }
203+ }
204+ } else {
205+ result = new IotResult ( StatusResult . Error , `The ${ this . _entityLabel } ${ entity . RootDir } is for a newer version of the extension. ` +
206+ `Update the extension.` ) ;
207+ this . CreateEvent ( result ) ;
208+ }
209+ } else {
210+ result = new IotResult ( StatusResult . Error , `The ${ this . _entityLabel } ${ entity . RootDir } has not been validated.` ) ;
211+ this . CreateEvent ( result ) ;
212+ this . CreateEvent ( entity . ValidationErrorsToString , LogLevel . Debug ) ;
213+ //delete system entity
214+ if ( type == EntityType . system ) {
215+ result = entity . Remove ( ) ;
216+ this . CreateEvent ( result , LogLevel . Debug ) ;
217+ }
218+ }
219+ } catch ( err : any ) {
220+ result = new IotResult ( StatusResult . Error , `Error import ${ type } ${ this . _entitiesLabel } dir ${ dir } ` , err ) ;
221+ }
222+ return Promise . resolve ( result ) ;
223+ }
224+
209225 protected async UpdateEntitiesFromUrl ( url :string , type :EntityType ) :Promise < IotResult >
210226 {
211227 const destPath = this . GetDirEntitiesCallback ( type ) ;
@@ -336,10 +352,6 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
336352 //Preparing
337353 result = new IotResult ( StatusResult . None ) ;
338354 this . Clear ( ) ;
339- //To get the number of hours since Unix epoch, i.e. Unix timestamp:
340- const dateNow = Math . floor ( Date . now ( ) / 1000 / 3600 ) ;
341- const TimeHasPassedHours = dateNow - this . Config . builtInConfig . LastUpdateTemplatesHours ;
342- const isNeedUpdate = ( ) => this . Config . isUpdate && ( TimeHasPassedHours >= this . Config . updateIntervalHours ) ;
343355 //main code
344356 this . CreateEvent ( `-------- Loading ${ this . _entitiesLabel } -------` ,
345357 LogLevel . Information ) ;
@@ -348,6 +360,10 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
348360 await this . LoadEntities ( EntityType . system ) ;
349361 //Updating system entities
350362 this . CreateEvent ( `Updating system ${ this . _entitiesLabel } ` , undefined , 15 ) ; //30
363+ //To get the number of hours since Unix epoch, i.e. Unix timestamp:
364+ const dateNow = Math . floor ( Date . now ( ) / 1000 / 3600 ) ;
365+ const TimeHasPassedHours = dateNow - this . Config . builtInConfig . LastUpdateTemplatesHours ;
366+ const isNeedUpdate = ( ) => this . Config . isUpdate && ( TimeHasPassedHours >= this . Config . updateIntervalHours ) ;
351367 if ( force || isNeedUpdate ( ) ) {
352368 //system
353369 this . CreateEvent ( `☑️ Updating system ${ this . _entitiesLabel } ` , LogLevel . Debug ) ;
0 commit comments