@@ -8,7 +8,8 @@ import { IoTHelper } from '../Helper/IoTHelper';
88import { IotResult , StatusResult } from '../IotResult' ;
99
1010export abstract class EntityBase < T extends EntityBaseAttribute > {
11- protected _entityIntLabel :string ; //for understandable log
11+ protected readonly _entityLabel :string ; //for understandable log
12+ protected readonly _entitiesLabel :string ; //for understandable log
1213 private _yamlFilePath :string = "" ; //YAML file
1314 public get YAMLFilePath ( ) : string {
1415 return this . _yamlFilePath ; }
@@ -39,11 +40,12 @@ export abstract class EntityBase<T extends EntityBaseAttribute> {
3940
4041 protected _pathFolderSchemas : string ;
4142
42- constructor ( entityIntLabel :string ,
43+ constructor ( entityLabel : string , entitiesLabel :string ,
4344 TCreator : new ( pathFolderSchemas ?: string ) => T ,
4445 pathFolderSchemas : string
4546 ) {
46- this . _entityIntLabel = entityIntLabel ;
47+ this . _entityLabel = entityLabel ;
48+ this . _entitiesLabel = entitiesLabel ;
4749 this . Attributes = new TCreator ( pathFolderSchemas ) ;
4850 //
4951 this . _pathFolderSchemas = pathFolderSchemas ;
@@ -52,24 +54,28 @@ export abstract class EntityBase<T extends EntityBaseAttribute> {
5254
5355 public Init ( type :EntityType , yamlFilePath :string , recoverySourcePath ?:string )
5456 {
55- this . Type = type ;
56- this . _recoverySourcePath = recoverySourcePath ;
57- this . _yamlFilePath = yamlFilePath ;
58- this . ValidateEntityBase ( ) ;
59- if ( ! this . IsValid ) return ;
60- //if(this.IsValid) this.Parse(path);
61- let attributes = this . Attributes as any ;
62- attributes . Init ( this . YAMLFilePath ) ;
63- if ( attributes . IsValid ) {
64- //ok
65- //check if folder matches entity and id
66- if ( this . RootNameDir != attributes . Id )
67- this . _validationErrors . push ( `${ this . _entityIntLabel } folder name ${ this . RootNameDir } ` +
68- `does not match id value ${ attributes . Id } .` +
69- `You need to rename the folder or change the ${ this . _entityIntLabel } id.` ) ;
70- } else {
71- //error
72- this . _validationErrors = attributes . ValidationErrors . slice ( ) ;
57+ try {
58+ this . Type = type ;
59+ this . _recoverySourcePath = recoverySourcePath ;
60+ this . _yamlFilePath = yamlFilePath ;
61+ this . ValidateEntityBase ( ) ;
62+ if ( ! this . IsValid ) return ;
63+ //if(this.IsValid) this.Parse(path);
64+ let attributes = this . Attributes as any ;
65+ attributes . Init ( this . YAMLFilePath ) ;
66+ if ( attributes . IsValid ) {
67+ //ok
68+ //check if folder matches entity and id
69+ if ( this . RootNameDir != attributes . Id )
70+ this . _validationErrors . push ( `${ this . _entityLabel } folder name ${ this . RootNameDir } ` +
71+ `does not match id value ${ attributes . Id } .` +
72+ `You need to rename the folder or change the ${ this . _entityLabel } id.` ) ;
73+ } else {
74+ //error
75+ this . _validationErrors = attributes . ValidationErrors . slice ( ) ;
76+ }
77+ } catch ( err : any ) {
78+ this . _validationErrors . push ( `Error Init ${ this . _entitiesLabel } . Error: ${ err } ` ) ;
7379 }
7480 }
7581
@@ -92,9 +98,9 @@ export abstract class EntityBase<T extends EntityBaseAttribute> {
9298 //replace fields
9399 const fileName = this . YAMLFilePath . substring ( this . RootDir . length + 1 ) ;
94100 this . _yamlFilePath = path . join ( destDir , fileName ) ;
95- result = new IotResult ( StatusResult . Ok , `${ this . _entityIntLabel } . ${ this . RootDir } folder successfully moved to ${ destDir } folder` ) ;
101+ result = new IotResult ( StatusResult . Ok , `${ this . _entityLabel } . ${ this . RootDir } folder successfully moved to ${ destDir } folder` ) ;
96102 } catch ( err : any ) {
97- result = new IotResult ( StatusResult . Error , `Unable to move ${ this . _entityIntLabel } from folder ${ this . RootDir } to folder ${ destDir } ` , err ) ;
103+ result = new IotResult ( StatusResult . Error , `Unable to move ${ this . _entityLabel } from folder ${ this . RootDir } to folder ${ destDir } ` , err ) ;
98104 }
99105 //result
100106 return result ;
@@ -104,14 +110,13 @@ export abstract class EntityBase<T extends EntityBaseAttribute> {
104110 let result :IotResult ;
105111 try {
106112 //delete
107- if ( fs . existsSync ( this . RootDir ) )
108- {
113+ if ( fs . existsSync ( this . RootDir ) ) {
109114 fs . emptyDirSync ( this . RootDir ) ;
110115 fs . removeSync ( this . RootDir ) ;
111116 }
112117 result = new IotResult ( StatusResult . Ok , `Folder has been deleted: ${ this . RootDir } ` ) ;
113118 } catch ( err : any ) {
114- result = new IotResult ( StatusResult . Error , `Unable to delete ${ this . _entityIntLabel } folder: ${ this . RootDir } ` , err ) ;
119+ result = new IotResult ( StatusResult . Error , `Unable to delete ${ this . _entityLabel } folder: ${ this . RootDir } ` , err ) ;
115120 }
116121 //result
117122 return result ;
@@ -136,14 +141,13 @@ export abstract class EntityBase<T extends EntityBaseAttribute> {
136141 let result :IotResult ;
137142 const fileZipPath = `${ this . RecoverySourcePath } \\${ this . RootNameDir } .zip` ;
138143 result = IoTHelper . UnpackFromZip ( fileZipPath , path . dirname ( this . RootDir ) ) ;
139- if ( result . Status == StatusResult . Error ) result . AddMessage ( `${ this . _entityIntLabel } restore error` ) ;
144+ if ( result . Status == StatusResult . Error ) result . AddMessage ( `${ this . _entityLabel } restore error` ) ;
140145 //result
141146 return result ;
142147 }
143148
144- public IsCompatibleByEndDeviceArchitecture ( endDeviceArchitecture ? :string ) :boolean
149+ public IsCompatibleByEndDeviceArchitecture ( endDeviceArchitecture :string ) :boolean
145150 {
146- if ( ! endDeviceArchitecture ) return false ;
147151 const result = this . Attributes . EndDeviceArchitecture . find ( x => x == endDeviceArchitecture ) ;
148152 if ( result ) return true ; else return false ;
149153 }
0 commit comments