Skip to content

Commit 0ca9274

Browse files
committed
Added Swap Bytes for Text
1 parent 79625a7 commit 0ca9274

File tree

5 files changed

+133
-51
lines changed

5 files changed

+133
-51
lines changed

cypress/fixtures/converters.json

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
[
2-
{
3-
"name": "number",
4-
"registerTypes": [3, 4]
5-
},
6-
{
7-
"name": "select",
8-
"registerTypes": [3, 4, 1]
9-
},
10-
{
11-
"name": "text",
12-
"registerTypes": [3, 4]
13-
},
14-
{
15-
"name": "binary",
16-
"registerTypes": [1, 3]
17-
},
18-
{
19-
"name": "value",
20-
"registerTypes": [4, 3]
21-
}
2+
"number",
3+
"select",
4+
"text",
5+
"binary",
6+
"value"
227
]

cypress/specification-entity.cy.ts

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {
44
beforeEachHelper as beforeEachEntityHelper,
55
mountEntityComponent,
66
setOnEntityNameOrVariableFieldsChangeFunc,
7+
setOnPostModbusEntityFunc
78
} from "./support/entityHelper";
9+
import { ImodbusData, Inumber, Itext } from "@modbus2mqtt/specification.shared";
10+
import { Subject } from "rxjs";
811

912
describe("Entity Component tests", () => {
1013
beforeEach(beforeEachEntityHelper); // mounts entity and opens all expansion panels
@@ -41,40 +44,74 @@ describe("Entity Component tests", () => {
4144
});
4245
it("No Variable Type => no variableConfiguration", () => {
4346
cy.get('mat-select[formControlName="variableType"]')
47+
.click()
48+
.get("mat-option")
49+
.first()
50+
.click()
51+
cy.get('input[formControlName="name"]').type("test")
52+
cy.get('input[formControlName="icon"]').click().then(()=>{setOnEntityNameOrVariableFieldsChangeFunc((entity) => {
53+
expect(entity.variableConfiguration).to.be.undefined;
54+
expect((entity as ImodbusEntityWithName).name).to.be.equal('test');
55+
})})
56+
cy.get('mat-select[formControlName="variableEntity"]').invoke('val')
57+
.then(val=>{
58+
const myVal = val;
59+
expect(myVal).to.equal('');
60+
})
61+
62+
//cy.get('input[formControlName="name"]').should(
63+
// "not.be.null");
64+
65+
});
66+
it("Set Byte Order for Number", () => {
67+
cy.get('mat-select[formControlName="converter"]')
4468
.click()
4569
.get("mat-option")
4670
.first()
47-
.click();
48-
cy.get('input[formControlName="name"]').type("test");
49-
cy.get('input[formControlName="icon"]')
71+
.click().then(() => {
72+
// Validation will be called after value change of any name or variable field
73+
// onVariableEntityValueChange or onEntityNameValueChange
74+
setOnPostModbusEntityFunc((entity) => {
75+
expect((entity!.converterParameters! as Inumber).swapBytes).to.be.true;
76+
return new Subject<ImodbusData>();
77+
});
78+
});;
79+
cy.openAllExpansionPanels()
80+
cy.get('mat-slide-toggle[formControlName="swapBytes"]')
81+
.click()
82+
83+
});
84+
it("Set Byte Order for Text", () => {
85+
cy.get('mat-select[formControlName="converter"]')
5086
.click()
51-
.then(() => {
52-
setOnEntityNameOrVariableFieldsChangeFunc((entity) => {
53-
expect(entity.variableConfiguration).to.be.undefined;
54-
expect((entity as ImodbusEntityWithName).name).to.be.equal("test");
87+
.get("mat-option")
88+
.eq(2)
89+
.click().then(() => {
90+
// Validation will be called after value change of any name or variable field
91+
// onVariableEntityValueChange or onEntityNameValueChange
92+
setOnPostModbusEntityFunc((entity) => {
93+
expect((entity!.converterParameters! as Itext).swapBytes, "swapBytes is not defined").not.to.be.undefined;
94+
expect((entity!.converterParameters! as Itext).swapBytes).to.be.true;
95+
return new Subject<ImodbusData>();
5596
});
56-
});
57-
cy.get('mat-select[formControlName="variableEntity"]')
58-
.invoke("val")
59-
.then((val) => {
60-
const myVal = val;
61-
expect(myVal).to.equal("");
62-
});
63-
64-
//cy.get('input[formControlName="name"]').should(
65-
// "not.be.null");
97+
98+
});;
99+
cy.openAllExpansionPanels()
100+
cy.get('[formControlName= "textSwapBytes"]')
101+
.click()
102+
66103
});
67104
});
68-
describe("Test for Modbus Address", () => {
69-
beforeEach(()=>{mountEntityComponent(true)}); // mounts entity and opens all expansion panels
70-
afterEach(afterEachEntityHelper);
71-
it("Modbus address in hex", () => {
72-
const inputField='input[formControlName="modbusAddress"]'
73-
const matField='mat-form-field input[formControlName="modbusAddress"]'
74-
cy.get(inputField).should('have.value', '0x4');
75-
cy.get(inputField).clear().type('1234').blur().should('have.value', '0x4d2');
76-
cy.get(inputField).clear().type('0X12s32').blur().should('have.value', '0x1232');
77-
cy.get(inputField).clear().type('0xx7')
78-
cy.get(inputField).parent().get( "mat-error").should('contain', 'dec or hex')
79-
})
80-
});
105+
// describe("Test for Modbus Address", () => {
106+
// beforeEach(()=>{mountEntityComponent(true)}); // mounts entity and opens all expansion panels
107+
// afterEach(afterEachEntityHelper);
108+
// it("Modbus address in hex", () => {
109+
// const inputField='input[formControlName="modbusAddress"]'
110+
// const matField='mat-form-field input[formControlName="modbusAddress"]'
111+
// cy.get(inputField).should('have.value', '0x4');
112+
// cy.get(inputField).clear().type('1234').blur().should('have.value', '0x4d2');
113+
// cy.get(inputField).clear().type('0X12s32').blur().should('have.value', '0x1232');
114+
// cy.get(inputField).clear().type('0xx7')
115+
// cy.get(inputField).parent().get( "mat-error").should('contain', 'dec or hex')
116+
// })
117+
// });

cypress/support/entityHelper.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from "@modbus2mqtt/specification.shared";
1313
import { ISpecificationMethods } from "angular/src/app/services/specificationInterface";
1414
import { EntityComponent } from "angular/src/app/specification/entity/entity.component";
15-
import { Subject } from "rxjs";
15+
import { Observable, Subject } from "rxjs";
1616
/**
1717
* specification methods
1818
*/
@@ -68,6 +68,14 @@ export function setOnEntityNameOrVariableFieldsChangeFunc(
6868
if (valFunc) specificationMethods.copy2Translation = valFunc;
6969
else specificationMethods.copy2Translation = () => {};
7070
}
71+
export function setOnPostModbusEntityFunc(
72+
valFunc?: (entity: ImodbusEntity|undefined) => Observable<ImodbusData>
73+
) {
74+
if (valFunc) specificationMethods.postModbusEntity = valFunc;
75+
else specificationMethods.postModbusEntity = () => {return new Subject<ImodbusData>()};
76+
}
77+
78+
7179
let selectEntity: ImodbusEntity = {
7280
id: 1,
7381
modbusValue: [4, 1, 1, 1],
@@ -122,4 +130,5 @@ export function beforeEachHelper() {
122130
export function afterEachEntityHelper() {
123131
// reset specificationMethods
124132
setOnEntityNameOrVariableFieldsChangeFunc();
133+
setOnPostModbusEntityFunc();
125134
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@ <h3 class="smallfield width150pt"> Identification{{entity.readonly?"":"/step"}}<
264264
formControlName="step" (change)="onConverterValueChange()">
265265
</mat-form-field>
266266
</div>
267+
<h3 class="smallfield width150pt"> Byte/Word Order</h3>
268+
<div class="flexrows">
269+
<mat-slide-toggle class="width50 toggleField" formControlName="swapWords"
270+
matTooltip="Swap Words in 32 Bit Values"
271+
(change)="onConverterValueChange()">Swap Word</mat-slide-toggle>
272+
<mat-slide-toggle class="width50 toggleField" formControlName="swapBytes"
273+
matTooltip="Swap Bytes"
274+
(change)="onConverterValueChange()">Swap Bytes</mat-slide-toggle>
275+
</div>
267276
</div>
268277
</form>
269278
</mat-expansion-panel>
@@ -290,6 +299,12 @@ <h3 class="smallfield width150pt"> Identification{{entity.readonly?"":"/step"}}<
290299
matTooltip="Optional: The entity is identified if the mqttvalue matches the regular expression"
291300
formControlName="identExpr" (change)="onConverterValueChange()">
292301
</mat-form-field>
302+
<div >
303+
<mat-slide-toggle class="width50 toggleField" formControlName="textSwapBytes"
304+
matTooltip="Swap Bytes"
305+
(change)="onConverterValueChange()">Swap Bytes</mat-slide-toggle>
306+
</div>
307+
293308
</form>
294309
</mat-expansion-panel>
295310
<mat-expansion-panel

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,13 @@ export class EntityComponent
265265
null as string | null,
266266
Validators.compose([Validators.min(0.001), Validators.max(10000)]),
267267
],
268+
swapBytes: [false],
269+
swapWords: [false]
268270
})),
269271
(this.stringPropertiesFormGroup = this.fb.group({
270272
identExpr: [null as string | null],
271273
stringlength: [null as number | null, Validators.required],
274+
textSwapBytes: [false]
272275
})),
273276
(this.selectPropertiesFormGroup = this.fb.group({
274277
optionModbus: [
@@ -504,6 +507,20 @@ export class EntityComponent
504507
? np.numberFormat
505508
: EnumNumberFormat.default,
506509
);
510+
this.numberPropertiesFormGroup
511+
.get("swapBytes")!
512+
.setValue(
513+
np.swapBytes !== undefined
514+
? np.swapBytes
515+
: false
516+
);
517+
this.numberPropertiesFormGroup
518+
.get("swapWords")!
519+
.setValue(
520+
np.swapWords !== undefined
521+
? np.swapWords
522+
: false
523+
);
507524
this.numberPropertiesFormGroup
508525
.get("min")!
509526
.setValue(
@@ -535,6 +552,13 @@ export class EntityComponent
535552
this.stringPropertiesFormGroup
536553
.get("identExpr")!
537554
.setValue(nt.identification ? nt.identification : null);
555+
this.stringPropertiesFormGroup
556+
.get("textSwapBytes")!
557+
.setValue(
558+
nt.swapBytes !== undefined
559+
? nt.swapBytes
560+
: false
561+
);
538562
break;
539563
case "Iselect":
540564
this.allFormGroups.setControl(
@@ -802,6 +826,14 @@ export class EntityComponent
802826
max: max,
803827
};
804828
}
829+
if( this.numberPropertiesFormGroup.get("numberFormat")!.value in [EnumNumberFormat.default, EnumNumberFormat.signedInt16])
830+
this.numberPropertiesFormGroup.get("swapWords")!.disable();
831+
else
832+
this.numberPropertiesFormGroup.get("swapWords")!.enable();
833+
if(this.numberPropertiesFormGroup.get("swapWords")!.value != undefined)
834+
enumber.swapWords = this.numberPropertiesFormGroup.get("swapWords")!.value
835+
if(this.numberPropertiesFormGroup.get("swapBytes")!.value != undefined)
836+
enumber.swapBytes = this.numberPropertiesFormGroup.get("swapBytes")!.value
805837
if (this.numberPropertiesFormGroup.get("deviceClass")!.value != null)
806838
enumber.device_class =
807839
this.numberPropertiesFormGroup.get("deviceClass")!.value.name;
@@ -837,6 +869,10 @@ export class EntityComponent
837869
if (this.stringPropertiesFormGroup.get("identExpr")! !== null) {
838870
(this.entity.converterParameters as any).identification =
839871
this.stringPropertiesFormGroup.get("identExpr")!.value;
872+
873+
if (this.stringPropertiesFormGroup.get("textSwapBytes")! !== null)
874+
(this.entity.converterParameters as Itext).swapBytes =
875+
this.stringPropertiesFormGroup.get("textSwapBytes")!.value;
840876
}
841877
break;
842878
case "Iselect":

0 commit comments

Comments
 (0)