Skip to content

Commit

Permalink
Merge pull request #1040 from eclipse-thingweb/resource-addessing-modbus
Browse files Browse the repository at this point in the history
fix (binding-modbus): Reading Modbus URI design
  • Loading branch information
relu91 authored Jul 29, 2023
2 parents 567e979 + 068550b commit 73bc53c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packages/binding-modbus/src/modbus-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,14 @@ export default class ModbusClient implements ProtocolClient {
}

private validateAndFillDefaultForm(form: ModbusForm, contentLength = 0): ModbusForm {
const result: ModbusForm = { ...form };
const mode = contentLength > 0 ? "w" : "r";

// Use form values if provided, otherwise use form values (we are more merciful then the spec for retro-compatibility)
this.overrideFormFromURLPath(form);

// take over latest content of form into a new result set
const result: ModbusForm = { ...form };

if (!form["modbus:function"] && !form["modbus:entity"]) {
throw new Error("Malformed form: modbus:function or modbus:entity must be defined");
}
Expand Down
22 changes: 11 additions & 11 deletions packages/binding-modbus/test/modbus-client-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ describe("Modbus client test", () => {
};

// eslint-disable-next-line dot-notation
client["validateAndFillDefaultForm"](form);
form["modbus:unitID"].should.be.equal(2, "unitID value not set");
form["modbus:address"].should.be.equal(2, "address value not set");
form["modbus:quantity"].should.be.equal(5, "quantity value not set");
const result = client["validateAndFillDefaultForm"](form);
result["modbus:unitID"].should.be.equal(2, "unitID value not set");
result["modbus:address"].should.be.equal(2, "address value not set");
result["modbus:quantity"].should.be.equal(5, "quantity value not set");
});

it("should accept just URL parameters without quantity", () => {
Expand All @@ -141,9 +141,9 @@ describe("Modbus client test", () => {
};

// eslint-disable-next-line dot-notation
client["validateAndFillDefaultForm"](form);
form["modbus:unitID"].should.be.equal(2, "unitID value not set");
form["modbus:address"].should.be.equal(2, "address value not set");
const result = client["validateAndFillDefaultForm"](form);
result["modbus:unitID"].should.be.equal(2, "unitID value not set");
result["modbus:address"].should.be.equal(2, "address value not set");
});

it("should override correctly form values with URL", () => {
Expand All @@ -156,10 +156,10 @@ describe("Modbus client test", () => {
};

// eslint-disable-next-line dot-notation
client["overrideFormFromURLPath"](form);
form["modbus:unitID"].should.be.equal(2, "unitID value not overridden");
form["modbus:address"].should.be.equal(2, "address value not overridden");
form["modbus:quantity"].should.be.equal(5, "quantity value not overridden");
const result = client["validateAndFillDefaultForm"](form);
result["modbus:unitID"].should.be.equal(2, "unitID value not overridden");
result["modbus:address"].should.be.equal(2, "address value not overridden");
result["modbus:quantity"].should.be.equal(5, "quantity value not overridden");
});

describe("misc", () => {
Expand Down

0 comments on commit 73bc53c

Please sign in to comment.