diff --git a/CHANGELOG.md b/CHANGELOG.md index 35ef4dd..9c64b2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## [1.1.0-beta.8] 2022-09-25 +## [1.1.0-beta.9] 2022-09-26 ### Changed **Action required** Breaking changes! In order to support configuration by GUI some configuration options needs to be renamed. @@ -86,6 +86,8 @@ In order to support configuration by GUI some configuration options needs to be - `PLC_AirPurifier` - `PLC_FilterMaintenance` +### Changed +- Use seperate buffer for each set operation ## [1.0.37] 2022-04-23 ### Fixed diff --git a/index.js b/index.js index 2b5400f..8793cc0 100644 --- a/index.js +++ b/index.js @@ -278,7 +278,6 @@ function GenericPLCAccessory(platform, config, accessoryNumber) { this.platform = platform; this.log = platform.log; this.name = config.name; - this.buf = Buffer.alloc(4); var uuid = UUIDGen.generate(config.name + config.accessory); this.config = config; this.accessory = new PlatformAccessory(this.name, uuid); @@ -1691,7 +1690,7 @@ GenericPLCAccessory.prototype = { );}.bind(this)); } - if ('get_FilterLifeLevel' in config) { + if ('get_FilterLifeLevel' in this.config) { this.service.getCharacteristic(Characteristic.FilterLifeLevel) .on('get', function(callback) {this.getByte(callback, config.db, @@ -3186,12 +3185,13 @@ GenericPLCAccessory.prototype = { var logprefix = "[" + this.name + "] " + characteristic + ": %s (setOnOffBit DB" + db + "DBX"+ offset + "." + bit + ")"; var S7Client = this.platform.S7Client; var log = this.log; + var buf = Buffer.alloc(1); //ensure PLC connection if (this.platform.S7ClientConnect()) { - this.buf[0] = 1; - S7Client.WriteArea(S7Client.S7AreaDB, db, ((offset*8) + bit), 1, S7Client.S7WLBit, this.buf, function(err) { + buf.writeUInt8(1); + S7Client.WriteArea(S7Client.S7AreaDB, db, ((offset*8) + bit), 1, S7Client.S7WLBit, buf, function(err) { if(err) { log.error(logprefix, "WriteArea failed #" + err.toString(16) + " - " + S7Client.ErrorText(err)); if(err & 0xFFFFF) {S7Client.Disconnect();} @@ -3218,6 +3218,7 @@ GenericPLCAccessory.prototype = { var logprefix = "[" + this.name + "] " + characteristic + ": %s (setBit DB" + db + "DBX"+ offset + "." + bit + ")"; var S7Client = this.platform.S7Client; var log = this.log; + var buf = Buffer.alloc(1); var valuePLC = value; if (typeof(valueMod) != 'undefined' && valueMod) { @@ -3226,8 +3227,8 @@ GenericPLCAccessory.prototype = { //ensure PLC connection if (this.platform.S7ClientConnect()) { - this.buf[0] = valuePLC ? 1 : 0; - S7Client.WriteArea(S7Client.S7AreaDB, db, ((offset*8) + bit), 1, S7Client.S7WLBit, this.buf, function(err) { + buf.writeInt8(valuePLC ? 1 : 0); + S7Client.WriteArea(S7Client.S7AreaDB, db, ((offset*8) + bit), 1, S7Client.S7WLBit, buf, function(err) { if(err) { log.error(logprefix, "WriteArea failed #" + err.toString(16) + " - " + S7Client.ErrorText(err)); if(err & 0xFFFFF) {S7Client.Disconnect();} @@ -3301,7 +3302,7 @@ GenericPLCAccessory.prototype = { var logprefix = "[" + this.name + "] " + characteristic + ": %s (setReal DB" + db + "DBD"+ offset + ")"; var S7Client = this.platform.S7Client; var log = this.log; - var buf = this.buf; + var buf = Buffer.alloc(4); var valuePLC = value; if (typeof(valueMod) != 'undefined' && valueMod) { @@ -3310,7 +3311,7 @@ GenericPLCAccessory.prototype = { //ensure PLC connection if (this.platform.S7ClientConnect()) { - buf.writeFloatBE(valuePLC, 0); + buf.writeFloatBE(valuePLC); S7Client.WriteArea(S7Client.S7AreaDB, db, offset, 1, S7Client.S7WLReal, buf, function(err) { if(err) { log.error(logprefix, "WriteArea failed #" + err.toString(16) + " - " + S7Client.ErrorText(err)); @@ -3347,19 +3348,16 @@ GenericPLCAccessory.prototype = { var value = 0; var pushMirror = function(value) { this.platform.mirrorGet(logprefix, "&db="+db+"&offset="+ offset + "&value="+ value);}.bind(this); - log.debug(logprefix , "olitest step 1"); //ensure PLC connection if (this.platform.S7ClientConnect()) { - log.debug(logprefix , "olitest step 2"); S7Client.ReadArea(S7Client.S7AreaDB, db, offset, 1, S7Client.S7WLReal, function(err, res) { - log.debug(logprefix , "olitest step 3"); if(err) { log.error(logprefix, "ReadArea failed #" + err.toString(16) + " - " + S7Client.ErrorText(err)); if(err & 0xFFFFF) {S7Client.Disconnect();} callback(new Error('PLC error')); } else { - var valuePLC = res.readFloatBE(0); + var valuePLC = res.readFloatBE(); if (typeof(valueMod) != 'undefined' && valueMod) { value = valueMod(valuePLC); @@ -3388,7 +3386,7 @@ GenericPLCAccessory.prototype = { var logprefix = "[" + this.name + "] " + characteristic + ": %s (setByte DB" + db + "DBB"+ offset + ")"; var S7Client = this.platform.S7Client; var log = this.log; - var buf = this.buf; + var buf = Buffer.alloc(1); var valuePLC = value; if (typeof(valueMod) != 'undefined' && valueMod) { @@ -3397,7 +3395,7 @@ GenericPLCAccessory.prototype = { //ensure PLC connection if (this.platform.S7ClientConnect()) { - buf[0] = valuePLC; + buf.writeUInt8(valuePLC); S7Client.WriteArea(S7Client.S7AreaDB, db, offset, 1, S7Client.S7WLByte, buf, function(err) { if(err) { log.error(logprefix, "WriteArea failed #" + err.toString(16) + " - " + S7Client.ErrorText(err)); @@ -3443,7 +3441,7 @@ GenericPLCAccessory.prototype = { callback(new Error('PLC error')); } else { - var valuePLC = res[0]; + var valuePLC = res.readUInt8(); if (typeof(valueMod) != 'undefined' && valueMod) { value = valueMod(valuePLC); @@ -3471,7 +3469,7 @@ GenericPLCAccessory.prototype = { var logprefix = "[" + this.name + "] " + characteristic + ": %s (setInt DB" + db + "DBW"+ offset + ")"; var S7Client = this.platform.S7Client; var log = this.log; - var buf = this.buf; + var buf = Buffer.alloc(2); var valuePLC = value; if (typeof(valueMod) != 'undefined' && valueMod) { @@ -3480,7 +3478,7 @@ GenericPLCAccessory.prototype = { //ensure PLC connection if (this.platform.S7ClientConnect()) { - buf.writeInt16BE(valuePLC, 0); + buf.writeInt16BE(valuePLC); S7Client.WriteArea(S7Client.S7AreaDB, db, offset, 1, S7Client.S7WLWord, buf, function(err) { if(err) { log.error(logprefix, "WriteArea failed #" + err.toString(16) + " - " + S7Client.ErrorText(err)); @@ -3527,7 +3525,7 @@ GenericPLCAccessory.prototype = { callback(new Error('PLC error')); } else { - valuePLC = res.readInt16BE(0); + valuePLC = res.readInt16BE(); if (typeof(valueMod) != 'undefined' && valueMod) { value = valueMod(valuePLC); @@ -3557,7 +3555,7 @@ GenericPLCAccessory.prototype = { var logprefix = "[" + this.name + "] " + characteristic + ": %s (setDInt DB" + db + "DBD"+ offset + ")"; var S7Client = this.platform.S7Client; var log = this.log; - var buf = this.buf; + var buf = Buffer.alloc(4); var valuePLC = value; if (typeof(valueMod) != 'undefined' && valueMod) { @@ -3566,7 +3564,7 @@ GenericPLCAccessory.prototype = { //ensure PLC connection if (this.platform.S7ClientConnect()) { - buf.writeInt32BE(valuePLC, 0); + buf.writeInt32BE(valuePLC); S7Client.WriteArea(S7Client.S7AreaDB, db, offset, 1, S7Client.S7WLDWord, buf, function(err) { if(err) { log.error(logprefix, "WriteArea failed #" + err.toString(16) + " - " + S7Client.ErrorText(err)); @@ -3613,7 +3611,7 @@ GenericPLCAccessory.prototype = { callback(new Error('PLC error')); } else { - valuePLC = res.readInt32BE(0); + valuePLC = res.readInt32BE(); if (typeof(valueMod) != 'undefined' && valueMod) { value = valueMod(valuePLC); diff --git a/package.json b/package.json index d946e31..0abbda9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-plc", - "version": "1.1.0-beta.8", + "version": "1.1.0-beta.9", "description": "Homebridge plugin for Siemens Step7 and compatible PLCs. (https://github.com/homebridge)", "license": "MIT", "keywords": [