Skip to content

Commit 52e04e5

Browse files
committed
Updating labels for a EntityCollection
1 parent 0bb1210 commit 52e04e5

File tree

2 files changed

+46
-45
lines changed

2 files changed

+46
-45
lines changed

src/Entity/EntityCollection.ts

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import { IotBuiltInConfig } from '../Configuration/IotBuiltInConfig';
1616
export abstract class EntityCollection <A extends EntityBaseAttribute, T extends EntityBase<A>> {
1717

1818
protected _items:Map<string,T>;
19-
protected readonly _entityIntLabel:string; //for understandable log
19+
protected readonly _entityLabel:string; //for understandable log
20+
protected readonly _entitiesLabel:string; //for understandable log
2021
private readonly TCreator: new(pathFolderSchemas: string) => T;
2122
protected readonly Config:IConfigEntityCollection;
2223
protected GetDirEntitiesCallback:(type:EntityType) =>string;
@@ -40,13 +41,13 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
4041
this.ChangedStateDispatcher.Fire(event);
4142
}
4243
//-------------------------------------------
43-
4444
constructor(
45-
entityIntLabel:string, TCreator: new(pathFolderSchemas: string) => T,
45+
entityLabel:string, entitiesLabel:string, TCreator: new(pathFolderSchemas: string) => T,
4646
getDirEntitiesCallback:(type:EntityType) =>string, config:IConfigEntityCollection
4747
){
4848
this._items = new Map<string,T>();
49-
this._entityIntLabel=entityIntLabel;
49+
this._entityLabel=entityLabel;
50+
this._entitiesLabel=entitiesLabel;
5051
this.TCreator=TCreator;
5152
this.GetDirEntitiesCallback=getDirEntitiesCallback;
5253
this.Config=config;
@@ -151,17 +152,17 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
151152
const listFolders=IoTHelper.GetListDir(pathFolder);
152153
//ckeck
153154
if (listFolders.length==0) {
154-
result=new IotResult(StatusResult.Ok,`No ${type} ${this._entityIntLabel}s to load. ${pathFolder} folder is empty`,undefined,LogLevel.Debug);
155+
result=new IotResult(StatusResult.Ok,`No ${type} ${this._entitiesLabel} to load. ${pathFolder} folder is empty`,undefined,LogLevel.Debug);
155156
return Promise.resolve(result);
156157
}
157158
//checking all folders
158159
listFolders.forEach(dir => {
159-
const filePath=`${dir}\\${this._entityIntLabel.toLowerCase()}.fastiot.yaml`;
160+
const filePath=`${dir}\\${this._entityLabel.toLowerCase()}.fastiot.yaml`;
160161
let entity = new this.TCreator(this.Config.schemasFolderPath);
161162
entity.Init(type,filePath,this.Config.recoverySourcePath);
162163
if(!entity.IsValid&&type==EntityType.system) {
163164
//Recovery
164-
this.CreateEvent(`${this._entityIntLabel} recovery: ${path.dirname(filePath)}`,LogLevel.Debug);
165+
this.CreateEvent(`${this._entityLabel} recovery: ${path.dirname(filePath)}`,LogLevel.Debug);
165166
result= entity.Recovery();
166167
if(result.Status==StatusResult.Ok) {
167168
entity.Init(type,filePath,this.Config.recoverySourcePath);}
@@ -171,32 +172,32 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
171172
}
172173
//main
173174
if(entity.IsValid) {
174-
this.CreateEvent(`${this._entityIntLabel} is valid: [${entity.Attributes.Id}]`,LogLevel.Debug);
175+
this.CreateEvent(`${this._entityLabel} is valid: [${entity.Attributes.Id}]`,LogLevel.Debug);
175176
if(this.IsCompatibleByVersionExtAndPlatform(entity)) {
176177
const isContains=this.Contains(entity);
177178
switch(isContains) {
178179
case ContainsType.no: {
179180
this.Add(entity);
180-
this.CreateEvent(`${this._entityIntLabel} added: [${entity.Attributes.Id}] ${entity.RootDir}`,LogLevel.Debug);
181+
this.CreateEvent(`${this._entityLabel} added: [${entity.Attributes.Id}] ${entity.RootDir}`,LogLevel.Debug);
181182
break;
182183
}
183184
case ContainsType.yesVersionSmaller: {
184185
this.Update(entity);
185-
this.CreateEvent(`${this._entityIntLabel} updated: [${entity.Attributes.Id}] ${entity.RootDir}`,LogLevel.Debug);
186+
this.CreateEvent(`${this._entityLabel} updated: [${entity.Attributes.Id}] ${entity.RootDir}`,LogLevel.Debug);
186187
break;
187188
}
188189
default: {
189-
this.CreateEvent(`Adding a ${this._entityIntLabel} was skipped because already in the collection: [${entity.Attributes.Id}] ${entity.RootDir}`,LogLevel.Debug);
190+
this.CreateEvent(`Adding a ${this._entityLabel} was skipped because already in the collection: [${entity.Attributes.Id}] ${entity.RootDir}`,LogLevel.Debug);
190191
break;
191192
}
192193
}
193194
}else{
194-
result = new IotResult(StatusResult.Error,`The ${this._entityIntLabel} ${entity.RootDir} is for a newer version of the extension. ` +
195+
result = new IotResult(StatusResult.Error,`The ${this._entityLabel} ${entity.RootDir} is for a newer version of the extension. ` +
195196
`Update the extension.`);
196197
this.CreateEvent(result);
197198
}
198199
}else{
199-
result = new IotResult(StatusResult.Error,`The ${this._entityIntLabel} ${entity.RootDir} has not been validated.`);
200+
result = new IotResult(StatusResult.Error,`The ${this._entityLabel} ${entity.RootDir} has not been validated.`);
200201
this.CreateEvent(result);
201202
this.CreateEvent(entity.ValidationErrorsToString,LogLevel.Debug);
202203
//delete system entity
@@ -208,14 +209,14 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
208209
});
209210
//result
210211
if((this.Count-entitysCountBegin)>0) {
211-
result= new IotResult(StatusResult.Ok,`Loading ${type} ${this._entityIntLabel}s from ${pathFolder} folder successfully completed`);
212+
result= new IotResult(StatusResult.Ok,`Loading ${type} ${this._entitiesLabel} from ${pathFolder} folder successfully completed`);
212213
result.logLevel=LogLevel.Debug;
213214
}else{
214-
result= new IotResult(StatusResult.Ok,`No ${type} ${this._entityIntLabel} was loaded from the ${pathFolder} folder`);
215+
result= new IotResult(StatusResult.Ok,`No ${type} ${this._entityLabel} was loaded from the ${pathFolder} folder`);
215216
result.logLevel=LogLevel.Debug;
216217
}
217218
} catch (err: any){
218-
result= new IotResult(StatusResult.Error,`Error loading ${type} ${this._entityIntLabel}s`,err);
219+
result= new IotResult(StatusResult.Error,`Error loading ${type} ${this._entitiesLabel}`,err);
219220
}
220221
return Promise.resolve(result);
221222
}
@@ -225,16 +226,16 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
225226
const destPath= this.GetDirEntitiesCallback(type);
226227
let downloader = new EntityDownloader();
227228
let result:IotResult;
228-
this.CreateEvent(`🔗 Downloading a list of ${this._entityIntLabel}s to update: ${url}`,LogLevel.Debug);
229+
this.CreateEvent(`🔗 Downloading a list of ${this._entitiesLabel} to update: ${url}`,LogLevel.Debug);
229230
result= await downloader.GetDownloadListEntity(url);
230231
if(result.Status==StatusResult.Error) {
231-
result.AddMessage(`Error updating ${type} ${this._entityIntLabel}s`);
232+
result.AddMessage(`Error updating ${type} ${this._entitiesLabel}`);
232233
return Promise.resolve(result);
233234
}
234-
this.CreateEvent(`🔗 List of ${this._entityIntLabel}s loaded ${url}`,LogLevel.Debug);
235+
this.CreateEvent(`🔗 List of ${this._entitiesLabel} loaded ${url}`,LogLevel.Debug);
235236
let listDownload:Array<EntityDownload>=result.returnObject;
236237
if(listDownload.length==0) {
237-
result= new IotResult(StatusResult.Ok,`Url: ${url}. No ${this._entityIntLabel}s to download`);
238+
result= new IotResult(StatusResult.Ok,`Url: ${url}. No ${this._entitiesLabel} to download`);
238239
return Promise.resolve(result);
239240
}
240241
//next
@@ -250,7 +251,7 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
250251
result= await downloader.DownloadEntity(item,this.Config.tempFolderPath);
251252
if(result.Status==StatusResult.Ok) {
252253
const unpackPath= <string> result.returnObject;
253-
const filePath = path.join(unpackPath, `${this._entityIntLabel.toLowerCase()}.fastiot.yaml`);
254+
const filePath = path.join(unpackPath, `${this._entityLabel.toLowerCase()}.fastiot.yaml`);
254255
let entity= new this.TCreator(this.Config.schemasFolderPath);
255256
entity.Init(EntityType.system,filePath);
256257
if(entity.IsValid) {
@@ -260,9 +261,9 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
260261
break;
261262
}
262263
this.Add(entity);
263-
this.CreateEvent(`${this._entityIntLabel} added/updated: [${entity.Attributes.Id}] ${entity.RootDir}`,LogLevel.Debug);
264+
this.CreateEvent(`${this._entityLabel} added/updated: [${entity.Attributes.Id}] ${entity.RootDir}`,LogLevel.Debug);
264265
} else {
265-
result = new IotResult(StatusResult.Error,`The ${this._entityIntLabel} ${entity.YAMLFilePath} has not been validated`)
266+
result = new IotResult(StatusResult.Error,`The ${this._entityLabel} ${entity.YAMLFilePath} has not been validated`)
266267
this.CreateEvent(result,LogLevel.Debug);
267268
this.CreateEvent(entity.ValidationErrorsToString,LogLevel.Debug);
268269
}
@@ -273,7 +274,7 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
273274
result= await downloader.DownloadEntity(item,this.Config.tempFolderPath);
274275
if(result.Status==StatusResult.Ok) {
275276
const unpackPath= <string> result.returnObject;
276-
const filePath = path.join(unpackPath, ` ${this._entityIntLabel.toLowerCase()}.fastiot.yaml`);
277+
const filePath = path.join(unpackPath, ` ${this._entityLabel.toLowerCase()}.fastiot.yaml`);
277278
let entity= new this.TCreator(this.Config.schemasFolderPath);
278279
entity.Init(EntityType.system,filePath);
279280
if(entity.IsValid) {
@@ -283,9 +284,9 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
283284
break;
284285
}
285286
this.Update(entity);
286-
this.CreateEvent(`${this._entityIntLabel} added/updated: [${entity.Attributes.Id}] ${entity.RootDir}`,LogLevel.Debug);
287+
this.CreateEvent(`${this._entityLabel} added/updated: [${entity.Attributes.Id}] ${entity.RootDir}`,LogLevel.Debug);
287288
} else {
288-
result = new IotResult(StatusResult.Error,`The ${this._entityIntLabel} ${entity.YAMLFilePath} has not been validated`)
289+
result = new IotResult(StatusResult.Error,`The ${this._entityLabel} ${entity.YAMLFilePath} has not been validated`)
289290
this.CreateEvent(result,LogLevel.Debug);
290291
this.CreateEvent(entity.ValidationErrorsToString,LogLevel.Debug);
291292
}
@@ -297,7 +298,7 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
297298
}
298299
}
299300
}else{
300-
result = new IotResult(StatusResult.Error,`Error. The ${this._entityIntLabel} ${item.Url} is for a newer version of the extension. ` +
301+
result = new IotResult(StatusResult.Error,`Error. The ${this._entityLabel} ${item.Url} is for a newer version of the extension. ` +
301302
`Update the extension.`);
302303
this.CreateEvent(result,LogLevel.Debug);
303304
}
@@ -306,7 +307,7 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
306307
index++;
307308
}while(true)
308309
//result
309-
result= new IotResult(StatusResult.Ok,`Update of ${type} ${this._entityIntLabel}s completed successfully`,undefined,LogLevel.Debug);
310+
result= new IotResult(StatusResult.Ok,`Update of ${type} ${this._entitiesLabel} completed successfully`,undefined,LogLevel.Debug);
310311
return Promise.resolve(result);
311312
}
312313

@@ -315,7 +316,7 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
315316
let result:IotResult;
316317
//Check urls
317318
if(urls.length==0) {
318-
result= new IotResult(StatusResult.No,`No update sources for community ${this._entityIntLabel}s`);
319+
result= new IotResult(StatusResult.No,`No update sources for community ${this._entitiesLabel}`);
319320
return Promise.resolve(result);
320321
}
321322
//list
@@ -331,13 +332,13 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
331332
index++;
332333
}while(true)
333334
//result
334-
result= new IotResult(StatusResult.Ok,`Update of ${type} ${this._entityIntLabel}s completed successfully`,undefined,LogLevel.Debug);
335+
result= new IotResult(StatusResult.Ok,`Update of ${type} ${this._entitiesLabel} completed successfully`,undefined,LogLevel.Debug);
335336
return Promise.resolve(result);
336337
}
337338

338339
protected async LoadEntities(type:EntityType):Promise<void>
339340
{
340-
this.CreateEvent(`☑️ Loading ${type} ${this._entityIntLabel}s`,LogLevel.Debug);
341+
this.CreateEvent(`☑️ Loading ${type} ${this._entitiesLabel}`,LogLevel.Debug);
341342
const path=this.GetDirEntitiesCallback(type);
342343
const result = await this.LoadFromFolder(path,type);
343344
this.CreateEvent(result,LogLevel.Debug);
@@ -355,16 +356,16 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
355356
const TimeHasPassedHours=dateNow-this.Config.builtInConfig.LastUpdateTemplatesHours;
356357
const isNeedUpdate = ()=>this.Config.isUpdate&&(TimeHasPassedHours>=this.Config.updateIntervalHours);
357358
//main code
358-
this.CreateEvent(`-------- Loading ${this._entityIntLabel}s -------`,
359+
this.CreateEvent(`-------- Loading ${this._entitiesLabel} -------`,
359360
LogLevel.Information);
360361
//Loading system entities
361-
this.CreateEvent(`Loading system ${this._entityIntLabel}s`,undefined,15); //15
362+
this.CreateEvent(`Loading system ${this._entitiesLabel}`,undefined,15); //15
362363
await this.LoadEntities(EntityType.system);
363364
//Updating system entities
364-
this.CreateEvent(`Updating system ${this._entityIntLabel}s`,undefined,15); //30
365+
this.CreateEvent(`Updating system ${this._entitiesLabel}`,undefined,15); //30
365366
if(force||isNeedUpdate()){
366367
//system
367-
this.CreateEvent(`☑️ Updating system ${this._entityIntLabel}s`,LogLevel.Debug);
368+
this.CreateEvent(`☑️ Updating system ${this._entitiesLabel}`,LogLevel.Debug);
368369
result=await this.UpdateEntitiesFromUrl(this.Config.urlUpdateEntitiesSystem,EntityType.system);
369370
this.CreateEvent(result,LogLevel.Debug);
370371
//timestamp of last update
@@ -373,16 +374,16 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
373374
this.Config.builtInConfig.Save();
374375
}
375376
} else {
376-
this.CreateEvent(`📥 ${this._entityIntLabel} update: disabled or less than ${this.Config.updateIntervalHours} hour(s) have passed since the last update.`,LogLevel.Debug);
377+
this.CreateEvent(`📥 ${this._entityLabel} update: disabled or less than ${this.Config.updateIntervalHours} hour(s) have passed since the last update.`,LogLevel.Debug);
377378
}
378379
//Loading community entities
379-
this.CreateEvent(`Loading community ${this._entityIntLabel}s`,undefined,15); //45
380+
this.CreateEvent(`Loading community ${this._entitiesLabel}`,undefined,15); //45
380381
await this.LoadEntities(EntityType.community);
381382
//Updating community entities
382-
this.CreateEvent(`Updating community ${this._entityIntLabel}s`,undefined,15); //60
383+
this.CreateEvent(`Updating community ${this._entitiesLabel}`,undefined,15); //60
383384
if(force||isNeedUpdate()){
384385
//community
385-
this.CreateEvent(`☑️ Updating community ${this._entityIntLabel}s`,LogLevel.Debug);
386+
this.CreateEvent(`☑️ Updating community ${this._entitiesLabel}`,LogLevel.Debug);
386387
result=await this.UpdateEntitiesFromUrls(this.Config.urlsUpdateEntitiesCommunity,EntityType.community);
387388
this.CreateEvent(result,LogLevel.Debug);
388389
//timestamp of last update
@@ -392,16 +393,16 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
392393
}
393394
}
394395
//Loading custom entities
395-
this.CreateEvent(`Loading custom ${this._entityIntLabel}s templates loaded `,undefined,15); //75
396+
this.CreateEvent(`Loading custom ${this._entitiesLabel} templates loaded `,undefined,15); //75
396397
await this.LoadEntities(EntityType.user);
397398
//result
398-
this.CreateEvent(new IotResult(StatusResult.Ok, `${this._entityIntLabel}s loaded`));
399-
const endMsg=`📚 ${this.Count} ${this._entityIntLabel}(s) available.`;
399+
this.CreateEvent(new IotResult(StatusResult.Ok, `${this._entitiesLabel} loaded`));
400+
const endMsg=`📚 ${this.Count} ${this._entityLabel}(s) available.`;
400401
this.CreateEvent(endMsg,LogLevel.Information);
401402
this.CreateEvent(`----------------------------------`,LogLevel.Information);
402-
this.CreateEvent(`${this._entityIntLabel}s loaded`,undefined,15); //90
403+
this.CreateEvent(`${this._entitiesLabel} loaded`,undefined,15); //90
403404
} catch (err: any){
404-
result= new IotResult(StatusResult.Error, `Error loading ${this._entityIntLabel} collection`,err);
405+
result= new IotResult(StatusResult.Error, `Error loading ${this._entityLabel} collection`,err);
405406
this.CreateEvent(result,LogLevel.Information);
406407
}
407408
}

src/Templates/IotTemplateCollection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { IotTemplateAttribute } from './IotTemplateAttribute';
99
export class IotTemplateCollection extends EntityCollection<IotTemplateAttribute,IotTemplate> {
1010

1111
constructor(getDirEntitiesCallback:(type:EntityType) =>string,config:IConfigEntityCollection){
12-
super("template",IotTemplate,getDirEntitiesCallback,config);
12+
super("template","templates",IotTemplate,getDirEntitiesCallback,config);
1313
}
1414

1515
}

0 commit comments

Comments
 (0)