Skip to content

Commit

Permalink
use seperate buffer for all set operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Feilner committed Sep 26, 2022
1 parent 33b6d69 commit 2573671
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
40 changes: 19 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();}
Expand All @@ -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)
{
Expand All @@ -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();}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit 2573671

Please sign in to comment.