Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Hotfix S7 Overwrite Config (#45)
Browse files Browse the repository at this point in the history
Fix an issue with 2 devices sharing the same connection.

When the second device is configured it clears the `s7Vars` of the first
device.

This fix adds second config to the to a variable list instead of
clearing it.

(cherry picked from commit 78aa216)
  • Loading branch information
grigals authored Nov 30, 2023
2 parents 4487eed + 09728c1 commit 221e294
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/devices/S7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ export class S7Connection extends DeviceConnection {
* Builds the S7 item group from the defined metric list
* @param {object} vars object containing metric names and PLC addresses
*/
setItemGroup(vars: s7Vars) {
if (this.#itemGroup) {
this.#itemGroup.destroy();
addToItemGroup(vars: s7Vars) {
// If item group doesn't exist, create a fresh setup
if (!this.#itemGroup) {
this.#itemGroup = new S7ItemGroup(this.#s7Conn);
this.#vars = {}
this.#itemGroup.setTranslationCB((metric: string) => this.#vars[metric]); //translates a metric name to its address
}
this.#itemGroup = new S7ItemGroup(this.#s7Conn);
this.#itemGroup.setTranslationCB((metric: string) => this.#vars[metric]); //translates a metric name to its address
this.#vars = vars;
// Merge existing vars with new ones
this.#vars = {...this.#vars, ...vars}
// Add metrics to read for this connection
this.#itemGroup.addItems(Object.keys(this.#vars));
}

Expand All @@ -90,7 +93,7 @@ export class S7Connection extends DeviceConnection {

try {
let newVals = await this.#itemGroup.readAllItems(); // name: value
log(JSON.stringify(newVals));
log("S7 Read: " + JSON.stringify(newVals));
this.emit('data', newVals, false);
} catch (error) {
// When a read fails, the connection is closed and the error is emitted
Expand Down Expand Up @@ -145,6 +148,9 @@ export class S7Connection extends DeviceConnection {
// https://github.com/st-one-io/node-red-contrib-s7#variable-addressing


/**
* S7 Device class
*/
export class S7Device extends (Device) {
s7Vars: {
[index: string]: string // name: address
Expand All @@ -165,7 +171,7 @@ export class S7Device extends (Device) {
});

// Set S7 variables as item group (this allows optimization of PLC transactions)
this.#devConn.setItemGroup(this.s7Vars);
this.#devConn.addToItemGroup(this.s7Vars);

}
}

0 comments on commit 221e294

Please sign in to comment.