@@ -107,6 +107,8 @@ const newEntity: ImodbusEntityWithName = {
107
107
modbusAddress : 0 ,
108
108
id : - 1 ,
109
109
} ;
110
+
111
+
110
112
@Component ( {
111
113
selector : "app-entity" ,
112
114
templateUrl : "./entity.component.html" ,
@@ -141,6 +143,7 @@ const newEntity: ImodbusEntityWithName = {
141
143
AsyncPipe ,
142
144
] ,
143
145
} )
146
+
144
147
export class EntityComponent
145
148
extends SessionStorage
146
149
implements AfterViewInit , OnChanges , OnDestroy
@@ -159,7 +162,15 @@ export class EntityComponent
159
162
}
160
163
return true ;
161
164
}
162
-
165
+ variableTypes : { id :VariableTargetParameters , name :string } [ ] = [
166
+ { id : VariableTargetParameters . noParam , name : "" } ,
167
+ { id : VariableTargetParameters . deviceIdentifiers , name : "Identification for Discovery" } ,
168
+ { id : VariableTargetParameters . deviceSerialNumber , name : "Serial Number" } ,
169
+ { id : VariableTargetParameters . deviceSWversion , name : "Software Version" } ,
170
+ { id : VariableTargetParameters . entityMultiplier , name : "Multiplier" } ,
171
+ { id : VariableTargetParameters . entityOffset , name : "Offset" } ,
172
+ { id : VariableTargetParameters . entityUom , name : "Unit of Measurement" } ,
173
+ ] ;
163
174
@Input ( { required : true } )
164
175
specificationMethods : ISpecificationMethods ;
165
176
@@ -334,7 +345,42 @@ export class EntityComponent
334
345
if ( dc && dc . uom ) return true ;
335
346
return false ;
336
347
}
348
+ private isVariableType ( ) {
349
+ let vt = this . variableFormGroup . get ( variableTypeFormControlName ) ! . value ;
350
+ return ( vt && vt != 0 && Number . parseInt ( vt . value ) != 0 )
351
+ }
352
+ private enableEntityFieldsVariableType ( ) {
353
+ let vt = this . variableFormGroup . get ( variableTypeFormControlName ) ! . value ;
337
354
355
+ let disableVtFields :{ form : AbstractControl , value :any } [ ] = [
356
+ { form : this . entityFormGroup . get ( "name" ) ! , value :null } ,
357
+ { form : this . entityFormGroup . get ( mqttNameFormControlName ) ! , value :null } ,
358
+ { form : this . entityFormGroup . get ( "icon" ) ! , value :null } ,
359
+ { form : this . entityFormGroup . get ( "forceUpdate" ) ! , value : false } ,
360
+ { form : this . entityFormGroup . get ( "entityCategory" ) ! , value : null } ,
361
+ { form : this . entityFormGroup . get ( "readonly" ) ! , value :true } ,
362
+ ] ;
363
+ if ( this . isVariableType ( ) ) {
364
+ disableVtFields . forEach ( f => {
365
+ f . form . disable ( ) ;
366
+ f . form . setValue ( f . value )
367
+ } )
368
+ if (
369
+ isDeviceVariable (
370
+ this . variableFormGroup . get ( variableTypeFormControlName ) ! . value
371
+ )
372
+ )
373
+ this . variableFormGroup . get ( variableEntityFormControlName ) ! . disable ( ) ;
374
+ else
375
+ this . variableFormGroup . get ( variableEntityFormControlName ) ! . enable ( ) ;
376
+ }
377
+ else {
378
+ disableVtFields . forEach ( f => f . form . enable ( ) )
379
+ this . variableFormGroup . get ( variableEntityFormControlName ) ! . disable ( ) ;
380
+ }
381
+ this . entityFormGroup . updateValueAndValidity ( )
382
+ this . variableFormGroup . updateValueAndValidity ( )
383
+ }
338
384
private copyEntityToForm ( entity : ImodbusEntityWithName ) {
339
385
if ( ! this . entityFormGroup ) return ;
340
386
let converterFormControl = this . entityFormGroup . get ( "converter" ) ! ;
@@ -354,9 +400,7 @@ export class EntityComponent
354
400
this . selectPropertiesFormGroup . enable ( ) ;
355
401
this . entityFormGroup . get ( mqttNameFormControlName ) ?. setErrors ( null ) ;
356
402
let entityFormGroup = this . entityFormGroup ! ;
357
- let vt = this . variableFormGroup . get ( variableTypeFormControlName ) ! . value ;
358
- if ( vt && vt != 0 ) entityFormGroup . get ( "name" ) ?. disable ( ) ;
359
- entityFormGroup . get ( mqttNameFormControlName ) ?. setErrors ( null ) ;
403
+ this . enableEntityFieldsVariableType ( )
360
404
}
361
405
this . variableFormGroup
362
406
. get ( variableTypeFormControlName ) !
@@ -373,23 +417,10 @@ export class EntityComponent
373
417
: null ,
374
418
) ;
375
419
if (
376
- this . variableFormGroup . get ( variableTypeFormControlName ) ! . value != null &&
377
- this . variableFormGroup . get ( variableTypeFormControlName ) ! . value ! = 0
420
+ this . variableFormGroup . get ( variableTypeFormControlName ) ! . value == null ||
421
+ this . variableFormGroup . get ( variableTypeFormControlName ) ! . value = = 0
378
422
) {
379
- this . entityFormGroup . get ( nameFormControlName ) ! . disable ( ) ;
380
- this . entityFormGroup . get ( nameFormControlName ) ! . setValue ( null ) ;
381
-
382
- if (
383
- isDeviceVariable (
384
- this . variableFormGroup . get ( variableTypeFormControlName ) ! . value ,
385
- )
386
- )
387
- this . variableFormGroup . get ( variableEntityFormControlName ) ! . disable ( ) ;
388
- else this . variableFormGroup . get ( variableEntityFormControlName ) ! . enable ( ) ;
389
- } else {
390
423
// This entity is no variable
391
- if ( ! this . disabled )
392
- this . entityFormGroup . get ( nameFormControlName ) ! . enable ( ) ;
393
424
this . entityFormGroup . get ( nameFormControlName ) ! . setValue ( entity . name ) ;
394
425
if ( entity . mqttname )
395
426
this . entityFormGroup
@@ -403,15 +434,13 @@ export class EntityComponent
403
434
this . entityFormGroup . get ( nameFormControlName ) ! . value ,
404
435
) ,
405
436
) ;
406
- this . variableFormGroup . get ( variableEntityFormControlName ) ! . disable ( ) ;
407
- delete entity . variableConfiguration ;
408
- }
437
+ this . entityFormGroup . get ( "icon" ) ! . setValue ( entity . icon ) ;
438
+ this . entityFormGroup . get ( "forceUpdate" ) ! . setValue ( entity . forceUpdate ) ;
439
+ this . entityFormGroup . get ( "value_template" ) ! . setValue ( entity . value_template ) ;
440
+ this . entityFormGroup . get ( "entityCategory" ) ! . setValue ( entity . entityCategory ) ;
441
+ this . entityFormGroup . get ( "readonly" ) ! . setValue ( entity . readonly ) ;
442
+ }
409
443
410
- this . entityFormGroup . get ( "icon" ) ! . setValue ( entity . icon ) ;
411
- this . entityFormGroup . get ( "forceUpdate" ) ! . setValue ( entity . forceUpdate ) ;
412
- this . entityFormGroup . get ( "value_template" ) ! . setValue ( entity . value_template ) ;
413
- this . entityFormGroup . get ( "entityCategory" ) ! . setValue ( entity . entityCategory ) ;
414
- this . entityFormGroup . get ( "readonly" ) ! . setValue ( entity . readonly ) ;
415
444
this . entityFormGroup
416
445
. get ( "registerType" ) !
417
446
. setValue ( this . getFunctionCode ( entity . registerType ) ) ;
@@ -592,8 +621,7 @@ export class EntityComponent
592
621
// set entity.name, entity.mqttname and entity.variableConfiguration
593
622
if ( ! this . entity ) return ;
594
623
this . specificationMethods . setEntitiesTouched ( ) ;
595
- let vt = this . variableFormGroup . get ( variableTypeFormControlName ) ;
596
- if ( vt != null && vt . value != null && Number . parseInt ( vt . value ) != 0 ) {
624
+ if ( this . isVariableType ( ) ) {
597
625
this . entity . variableConfiguration = {
598
626
targetParameter : this . variableFormGroup . get (
599
627
variableTypeFormControlName ,
@@ -605,17 +633,8 @@ export class EntityComponent
605
633
: this . variableFormGroup . get ( variableEntityFormControlName ) ! . value
606
634
. id ,
607
635
} ;
608
- if ( ! isDeviceVariable ( vt . value ) )
609
- this . variableFormGroup . get ( variableEntityFormControlName ) ! . disable ( ) ;
610
- else this . variableFormGroup . get ( variableEntityFormControlName ) ! . enable ( ) ;
611
636
this . entity . name = undefined ;
612
- this . entityFormGroup . get ( nameFormControlName ) ! . setValue ( null ) ;
613
- this . entityFormGroup . get ( nameFormControlName ) ! . disable ( ) ;
614
- this . entityFormGroup . get ( mqttNameFormControlName ) ! . setValue ( null ) ;
615
- this . entityFormGroup . get ( mqttNameFormControlName ) ! . disable ( ) ;
616
637
} else {
617
- this . variableFormGroup . get ( variableEntityFormControlName ) ! . disable ( ) ;
618
- this . entityFormGroup . get ( nameFormControlName ) ! . enable ( ) ;
619
638
delete this . entity . variableConfiguration ;
620
639
if (
621
640
this . entityFormGroup . get ( nameFormControlName ) ! . value != null &&
@@ -636,6 +655,7 @@ export class EntityComponent
636
655
. get ( mqttNameFormControlName ) !
637
656
. setValue ( this . entity . mqttname != null ? this . entity . mqttname : null ) ;
638
657
}
658
+ this . enableEntityFieldsVariableType ( ) ;
639
659
this . specificationMethods . copy2Translation ( this . entity ) ;
640
660
}
641
661
onModbusAddressChange ( ) {
@@ -937,8 +957,7 @@ export class EntityComponent
937
957
control : AbstractControl ,
938
958
) : ValidationErrors | null => {
939
959
if ( this . variableFormGroup ) {
940
- let vtc = this . variableFormGroup ! . get ( variableTypeFormControlName ) ;
941
- return ( this . entity && vtc ! . value != null ) ||
960
+ return ( this . isVariableType ( ) ) ||
942
961
( control . value != null && control . value . length > 0 )
943
962
? null
944
963
: { required : control . value } ;
@@ -951,14 +970,13 @@ export class EntityComponent
951
970
control : AbstractControl ,
952
971
) : ValidationErrors | null => {
953
972
if ( this . variableFormGroup && this . entity && this . specificationMethods ) {
954
- let vtc = this . variableFormGroup ! . get ( variableTypeFormControlName ) ;
955
973
let found = this . specificationMethods
956
974
. getMqttNames ( this . entity . id )
957
975
. find ( ( mqttname ) => mqttname && mqttname == control . value ) ;
958
976
if ( found ) {
959
977
return { unique : control . value } ;
960
978
}
961
- return ( this . entity && vtc ! . value != null ) ||
979
+ return ( this . entity && this . isVariableType ( ) ) ||
962
980
( control . value != null && control . value . length > 0 )
963
981
? null
964
982
: { required : control . value } ;
@@ -983,10 +1001,11 @@ export class EntityComponent
983
1001
) : ValidationErrors | null => {
984
1002
if ( ! this . variableFormGroup ) return null ;
985
1003
let vt = this . variableFormGroup . get ( variableTypeFormControlName ) ! ;
1004
+
986
1005
// variables for devices don't need entity
987
1006
if ( isDeviceVariable ( vt . value ) ) return null ;
988
1007
989
- if ( control == null || control . value == null || control . value . id == null )
1008
+ if ( control == null || control . value == null || control . value == 0 )
990
1009
return { invalid : control . value } ;
991
1010
992
1011
return this . specificationMethods . hasDuplicateVariableConfigurations (
@@ -1062,7 +1081,28 @@ export class EntityComponent
1062
1081
compareEntities ( f1 : Ientity , f2 : Ientity ) {
1063
1082
return f1 && f2 && f1 . id == f2 . id ;
1064
1083
}
1065
-
1084
+ getEntityLabel ( ) :string {
1085
+ if ( ! this . entity . name && this . isVariableType ( ) )
1086
+ {
1087
+ let type = this . variableTypes . find ( e => e . id == this . entity . variableConfiguration ! . targetParameter )
1088
+ if ( ! isDeviceVariable ( this . entity . variableConfiguration ! . targetParameter ) )
1089
+ {
1090
+ let e = this . specificationMethods . getNonVariableNumberEntities ( ) . find ( e => e . id == this . entity . variableConfiguration ! . entityId )
1091
+ return ( e ? "=>" + e . name :"" )
1092
+ }
1093
+ }
1094
+ return ""
1095
+ }
1096
+ getVariableTypeOrEntityNameLabel ( ) :string {
1097
+ if ( this . entity . name )
1098
+ return this . entity . name
1099
+ else
1100
+ if ( this . isVariableType ( ) ) {
1101
+ let type = this . variableTypes . find ( e => e . id == this . entity . variableConfiguration ! . targetParameter )
1102
+ return type ? type . name : ""
1103
+ }
1104
+ return ""
1105
+ }
1066
1106
getFunctionCode ( functionCode : ModbusRegisterType | undefined ) : IRegisterType {
1067
1107
let rc : IRegisterType | undefined = undefined ;
1068
1108
if ( functionCode )
0 commit comments