Skip to content

Commit c1e2479

Browse files
committed
Fixesd issues in variable Configuration added UOM to entity header. Closes #80
1 parent a666bfc commit c1e2479

File tree

7 files changed

+110
-55
lines changed

7 files changed

+110
-55
lines changed

cypress/Entity.cy.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Subject } from "rxjs"
99
let specificationMethods:ISpecificationMethods = {
1010
getCurrentMessage:()=>{return {type:0, category: 0}},
1111
getMqttLanguageName:()=>{return "english"},
12+
getUom:(entity_id:number):string => {return "cm"},
1213
getNonVariableNumberEntities:()=>{ return []},
1314
getMqttNames: ()=> {return []},
1415
getSaveObservable:()=> {return new Subject<void>()},

src/app/services/specificationInterface.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface ISpecificationMethods {
2525
): boolean;
2626
canEditEntity(): boolean;
2727
getMqttLanguageName(): string;
28+
getUom(entity_id:number): string;
2829
addEntity(addedEntity: ImodbusEntityWithName): void;
2930
deleteEntity(entityId: number): void;
3031
copy2Translation(entity: Ientity): void;

src/app/specification/entity-value-control/entity-value-control.component.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<mat-error *ngIf="numberFormControl.hasError('max')">
1010
The value is greater than the maximum
1111
</mat-error>
12-
</mat-form-field>
12+
</mat-form-field>{{getUom()}}
1313
</ng-container>
1414
<ng-container *ngIf="getConverterName().indexOf('text') >=0 && !entity!.readonly ">
1515
<mat-form-field class="width150pt">
@@ -34,6 +34,6 @@
3434
<mat-form-field class="width100pt">
3535
<mat-label>MQTT Value</mat-label>
3636
<input matInput [formControl]="textFormControl"/>
37-
</mat-form-field>
37+
</mat-form-field>{{getUom()}}
3838

3939
</ng-container>

src/app/specification/entity-value-control/entity-value-control.component.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class EntityValueControlComponent
5454
implements OnInit, OnDestroy, OnChanges
5555
{
5656
@Input({ required: true }) entity: ImodbusEntityWithName | undefined;
57+
@Input({ required: false }) uom: string ="";
5758
@Input({ required: true }) specificationMethods: ISpecificationMethods;
5859
@Input()
5960
mqttValueObservable: Observable<ImodbusData | undefined>;
@@ -197,12 +198,16 @@ export class EntityValueControlComponent
197198
}
198199
});
199200
}
200-
201+
getUom():string{
202+
return this.entity? this.specificationMethods.getUom( this.entity.id ):""
203+
}
204+
201205
getOptions(): IselectOption[] {
202206
if (this.entity) {
203207
let options = (this.entity.converterParameters as Iselect).options;
204208
if (options) return options;
205209
}
206210
return [];
207211
}
212+
208213
}

src/app/specification/entity/entity.component.html

+12-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</ng-container>
3131
</div>
3232
</div>
33-
<span [ngClass]="{'errorText': isCurrentMessage()}">{{entity.name}}</span>
33+
<span [ngClass]="{'errorText': isCurrentMessage()}">{{getVariableTypeOrEntityNameLabel()}}<br *ngIf="entity.name == null"/>{{getEntityLabel()}}</span>
3434
<mat-icon [color]="entity.identified== 1?'primary':entity.identified== -1?'':'warn'" class="icon-display">
3535
{{(entity.identified == undefined || entity.identified == -1?'thumbs_up_down':(entity.identified ==
3636
1?'thumb_up':'thumb_down')) }}</mat-icon>
@@ -358,24 +358,29 @@ <h3 class="smallfield width150pt"> Identification{{entity.readonly?"":"/step"}}<
358358
<mat-label>Set Value </mat-label>
359359
<mat-select matInput type="number" formControlName="variableType" [compareWith]="compareNumber"
360360
(selectionChange)="onEntityNameValueChange();entityFormGroup.updateValueAndValidity();">
361+
<mat-option
362+
*ngFor="let c of variableTypes"
363+
[value]="c.id">
364+
{{ c.name }}
365+
</mat-option>
361366
<mat-option [value]="0"></mat-option>
362367
<mat-option [value]="5">Serial Number</mat-option>
363368
<mat-option [value]="6">Software Version</mat-option>
364369
<mat-option [value]="1">Identification for Discovery</mat-option>
365-
<mat-option [value]="2">Multiplier</mat-option>
366-
<mat-option [value]="3">Offset</mat-option>
367-
<mat-option [value]="4">Unit of Measurement</mat-option>
370+
<mat-option [value]="3">Multiplier</mat-option>
371+
<mat-option [value]="4">Offset</mat-option>
372+
<mat-option [value]="2">Unit of Measurement</mat-option>
368373
</mat-select>
369374
</mat-form-field>
370375
<mat-form-field class="smallfield">
371376
<mat-label>Referenced Entity</mat-label>
372-
<mat-select matInput formControlName="variableEntity" [compareWith]="compareEntities"
377+
<mat-select matInput formControlName="variableEntity" [compareWith]="compareNumber"
373378
[errorStateMatcher]="errorStateMatcher" [errorStateMatcher]="errorStateMatcher"
374379
(selectionChange)="onEntityNameValueChange()">
375380
<mat-option
376381
*ngFor="let c of specificationMethods?specificationMethods.getNonVariableNumberEntities():[]"
377-
[value]="c">
378-
{{ c.name }}
382+
[value]="c.id">
383+
{{ c.name? c.name: c.id }}
379384
</mat-option>
380385
</mat-select>
381386
<mat-error *ngIf="entityFormGroup!.get('variableEntity')?.hasError('unique') != null">

src/app/specification/entity/entity.component.ts

+84-44
Original file line numberDiff line numberDiff line change
@@ -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+
144147
export 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)

src/app/specification/specification/specification.component.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import {
5252
} from "@modbus2mqtt/specification.shared";
5353
import { ActivatedRoute, Router, RouterLink } from "@angular/router";
5454
import { SessionStorage } from "../../services/SessionStorage";
55-
import { Imessage } from "@modbus2mqtt/specification.shared";
55+
import { Imessage, getUom } from "@modbus2mqtt/specification.shared";
5656
import { GalleryConfig } from "ng-gallery";
5757
import {
5858
ISpecificationMethods,
@@ -218,6 +218,9 @@ export class SpecificationComponent
218218
getMqttLanguageName: () => {
219219
return I18nService.getLanguageName(this.config.mqttdiscoverylanguage);
220220
},
221+
getUom: (entity_id:number):string => {
222+
return getUom( this.currentSpecification as ImodbusSpecification, entity_id )
223+
},
221224
postModbusEntity: (
222225
changedEntity: ImodbusEntityWithName,
223226
): Observable<ImodbusData> => {

0 commit comments

Comments
 (0)