@@ -107,6 +107,8 @@ const newEntity: ImodbusEntityWithName = {
107107 modbusAddress : 0 ,
108108 id : - 1 ,
109109} ;
110+
111+
110112@Component ( {
111113 selector : "app-entity" ,
112114 templateUrl : "./entity.component.html" ,
@@ -141,6 +143,7 @@ const newEntity: ImodbusEntityWithName = {
141143 AsyncPipe ,
142144 ] ,
143145} )
146+
144147export class EntityComponent
145148 extends SessionStorage
146149 implements AfterViewInit , OnChanges , OnDestroy
@@ -159,7 +162,15 @@ export class EntityComponent
159162 }
160163 return true ;
161164 }
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+ ] ;
163174 @Input ( { required : true } )
164175 specificationMethods : ISpecificationMethods ;
165176
@@ -334,7 +345,42 @@ export class EntityComponent
334345 if ( dc && dc . uom ) return true ;
335346 return false ;
336347 }
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 ;
337354
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+ }
338384 private copyEntityToForm ( entity : ImodbusEntityWithName ) {
339385 if ( ! this . entityFormGroup ) return ;
340386 let converterFormControl = this . entityFormGroup . get ( "converter" ) ! ;
@@ -354,9 +400,7 @@ export class EntityComponent
354400 this . selectPropertiesFormGroup . enable ( ) ;
355401 this . entityFormGroup . get ( mqttNameFormControlName ) ?. setErrors ( null ) ;
356402 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 ( )
360404 }
361405 this . variableFormGroup
362406 . get ( variableTypeFormControlName ) !
@@ -373,23 +417,10 @@ export class EntityComponent
373417 : null ,
374418 ) ;
375419 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
378422 ) {
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 {
390423 // This entity is no variable
391- if ( ! this . disabled )
392- this . entityFormGroup . get ( nameFormControlName ) ! . enable ( ) ;
393424 this . entityFormGroup . get ( nameFormControlName ) ! . setValue ( entity . name ) ;
394425 if ( entity . mqttname )
395426 this . entityFormGroup
@@ -403,15 +434,13 @@ export class EntityComponent
403434 this . entityFormGroup . get ( nameFormControlName ) ! . value ,
404435 ) ,
405436 ) ;
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+ }
409443
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 ) ;
415444 this . entityFormGroup
416445 . get ( "registerType" ) !
417446 . setValue ( this . getFunctionCode ( entity . registerType ) ) ;
@@ -592,8 +621,7 @@ export class EntityComponent
592621 // set entity.name, entity.mqttname and entity.variableConfiguration
593622 if ( ! this . entity ) return ;
594623 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 ( ) ) {
597625 this . entity . variableConfiguration = {
598626 targetParameter : this . variableFormGroup . get (
599627 variableTypeFormControlName ,
@@ -605,17 +633,8 @@ export class EntityComponent
605633 : this . variableFormGroup . get ( variableEntityFormControlName ) ! . value
606634 . id ,
607635 } ;
608- if ( ! isDeviceVariable ( vt . value ) )
609- this . variableFormGroup . get ( variableEntityFormControlName ) ! . disable ( ) ;
610- else this . variableFormGroup . get ( variableEntityFormControlName ) ! . enable ( ) ;
611636 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 ( ) ;
616637 } else {
617- this . variableFormGroup . get ( variableEntityFormControlName ) ! . disable ( ) ;
618- this . entityFormGroup . get ( nameFormControlName ) ! . enable ( ) ;
619638 delete this . entity . variableConfiguration ;
620639 if (
621640 this . entityFormGroup . get ( nameFormControlName ) ! . value != null &&
@@ -636,6 +655,7 @@ export class EntityComponent
636655 . get ( mqttNameFormControlName ) !
637656 . setValue ( this . entity . mqttname != null ? this . entity . mqttname : null ) ;
638657 }
658+ this . enableEntityFieldsVariableType ( ) ;
639659 this . specificationMethods . copy2Translation ( this . entity ) ;
640660 }
641661 onModbusAddressChange ( ) {
@@ -937,8 +957,7 @@ export class EntityComponent
937957 control : AbstractControl ,
938958 ) : ValidationErrors | null => {
939959 if ( this . variableFormGroup ) {
940- let vtc = this . variableFormGroup ! . get ( variableTypeFormControlName ) ;
941- return ( this . entity && vtc ! . value != null ) ||
960+ return ( this . isVariableType ( ) ) ||
942961 ( control . value != null && control . value . length > 0 )
943962 ? null
944963 : { required : control . value } ;
@@ -951,14 +970,13 @@ export class EntityComponent
951970 control : AbstractControl ,
952971 ) : ValidationErrors | null => {
953972 if ( this . variableFormGroup && this . entity && this . specificationMethods ) {
954- let vtc = this . variableFormGroup ! . get ( variableTypeFormControlName ) ;
955973 let found = this . specificationMethods
956974 . getMqttNames ( this . entity . id )
957975 . find ( ( mqttname ) => mqttname && mqttname == control . value ) ;
958976 if ( found ) {
959977 return { unique : control . value } ;
960978 }
961- return ( this . entity && vtc ! . value != null ) ||
979+ return ( this . entity && this . isVariableType ( ) ) ||
962980 ( control . value != null && control . value . length > 0 )
963981 ? null
964982 : { required : control . value } ;
@@ -983,10 +1001,11 @@ export class EntityComponent
9831001 ) : ValidationErrors | null => {
9841002 if ( ! this . variableFormGroup ) return null ;
9851003 let vt = this . variableFormGroup . get ( variableTypeFormControlName ) ! ;
1004+
9861005 // variables for devices don't need entity
9871006 if ( isDeviceVariable ( vt . value ) ) return null ;
9881007
989- if ( control == null || control . value == null || control . value . id == null )
1008+ if ( control == null || control . value == null || control . value == 0 )
9901009 return { invalid : control . value } ;
9911010
9921011 return this . specificationMethods . hasDuplicateVariableConfigurations (
@@ -1062,7 +1081,28 @@ export class EntityComponent
10621081 compareEntities ( f1 : Ientity , f2 : Ientity ) {
10631082 return f1 && f2 && f1 . id == f2 . id ;
10641083 }
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+ }
10661106 getFunctionCode ( functionCode : ModbusRegisterType | undefined ) : IRegisterType {
10671107 let rc : IRegisterType | undefined = undefined ;
10681108 if ( functionCode )
0 commit comments