Skip to content

Commit

Permalink
Merge pull request #22 from psi-4ward/feat/marker-support
Browse files Browse the repository at this point in the history
feat(plc): Add support for PLC-Marker/Memory area
  • Loading branch information
dixi83 authored Jan 14, 2025
2 parents a0e42ee + 2f9f17e commit fc6adb6
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions device.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,62 @@ module.exports = class device {
// register the attribute to the plc library
new_attribute.subscribePlcUpdates();

// split the plc address to get the type
let offset = new_attribute.plc_address.split(',');
let params = offset[1].match(/(\d+|\D+)/g);
let type = params[0];

// check if the type is correct
// and if it isn't, then print some infos
if (required_type !== "" && type !== required_type) {
sf.debug("Wrong datatype '" + type + "' at attribute '" + name + "'");

let numbers = "";
for (let i = 1; i < params.length; i++) {
numbers += params[i];
let type;
if(new_attribute.plc_address.startsWith('DB')) {
// split the plc address to get the type
let offset = new_attribute.plc_address.split(',');
let params = offset[1].match(/(\d+|\D+)/g);
type = params[0];

// check if the type is correct
// and if it isn't, then print some infos
if(required_type !== "" && type !== required_type) {
sf.debug("Wrong datatype '" + type + "' at attribute '" + name + "'");

let numbers = "";
for(let i = 1; i < params.length; i++) {
numbers += params[i];
}

sf.debug("Did you mean " + offset[0] + "," +
required_type + numbers +
" instead of " + new_attribute.plc_address + " ?");

return;
}
} else if(new_attribute.plc_address.startsWith('M')) {
const parts = new_attribute.plc_address.match(/^(M[A-Z]?)(.*)/);
if(!parts) {
sf.debug("Invalid memory address '" + new_attribute.plc_address + "' at attribute '" + name + "'");
return;
}
const memoryType = parts[1];
// const memoryOffset = parts[2];
switch(memoryType) {
case 'MW': // 16-bit int
case 'MI': // 16-bit int
case 'MB': // 8-bit int
case 'MD': // 16-bit int
type = 'INT';
break;
case 'M': // 1-bit bool
type = 'X';
break;
case 'MR':
type = 'REAL';
break;
default:
sf.debug("Invalid memory type '" + memoryType + "' at attribute '" + name + "'");
return;
}
if(required_type !== "" && type !== required_type) {
sf.debug("Wrong datatype '" + type + "' at attribute '" + name + "', expected '" + required_type + "'");
return;
}
}

sf.debug("Did you mean " + offset[0] + "," +
required_type + numbers +
" instead of " + new_attribute.plc_address + " ?");
new_attribute.type = type;

return;
} else {
new_attribute.type = type;
}

sf.debug("- New attribute '" + new_attribute.full_mqtt_topic + "' was created");

Expand Down

0 comments on commit fc6adb6

Please sign in to comment.