UNPKG

@fabianvolkers/n8n-nodes-modbus-trigger

Version:

n8n node to trigger Modbus Address on change

187 lines 7.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Modbus = void 0; const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("./GenericFunctions"); class Modbus { constructor() { this.description = { displayName: 'MODBUS', name: 'modbus', icon: 'file:modbus.svg', group: ['input'], version: 1, description: 'Read and write to MODBUS devices', eventTriggerDescription: '', defaults: { name: 'MODBUS', }, usableAsTool: true, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'modbusApi', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', options: [ { name: 'Read', value: 'read', }, { name: 'Write', value: 'write', }, ], default: 'read', noDataExpression: true, }, { displayName: 'Memory Address', name: 'memoryAddress', type: 'number', default: 1, description: 'The memory address (register index) to read from or write to', }, { displayName: 'Quantity', displayOptions: { show: { operation: ['read'], }, }, name: 'quantity', type: 'number', default: 1, description: 'The number of registers to read from', }, { displayName: 'Write Target', displayOptions: { show: { operation: ['write'], }, }, name: 'writeTarget', type: 'options', options: [ { name: 'Register', value: 'register', }, { name: 'Coil', value: 'coil', }, ], default: 'register', }, { displayName: 'Value', displayOptions: { show: { writeTarget: ['register'] }, }, name: 'value', type: 'number', typeOptions: { maxValue: 32767, minValue: -32768, }, default: 1, description: 'The value to write to the memory address', }, { displayName: 'Coil Value', displayOptions: { show: { writeTarget: ['coil'], }, }, name: 'coilValue', type: 'options', options: [ { name: 'On', value: 0xFF00, }, { name: 'Off', value: 0x0000, }, ], default: 0xFF00, }, ], }; } async execute() { let responseData = {}; const credentials = await this.getCredentials('modbusApi'); const client = await (0, GenericFunctions_1.createClient)(credentials); const memoryAddress = this.getNodeParameter('memoryAddress', 0); const operation = this.getNodeParameter('operation', 0); if (operation === 'read') { const quantity = this.getNodeParameter('quantity', 0); await new Promise((resolve) => { client.readHoldingRegisters({ address: memoryAddress, quantity }, (err, data) => { var _a; if (err) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'MODBUS Error: ' + err.message); } const returnData = { data: (_a = data === null || data === void 0 ? void 0 : data.response.data) === null || _a === void 0 ? void 0 : _a.map((value) => value.readInt16BE(0)), }; responseData = returnData; resolve(responseData); }); }); } if (operation === 'write') { const writeTarget = this.getNodeParameter('writeTarget', 0); if (writeTarget === "register") { const value = this.getNodeParameter('value', 0); const buffer = Buffer.alloc(2); buffer.writeInt16BE(value); await new Promise((resolve) => { client.writeSingleRegister({ address: memoryAddress, value: buffer }, (err, data) => { if (err) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'MODBUS Error: ' + err.message); } const returnData = { data: data.response, }; responseData = returnData; resolve(responseData); }); }); } else if (writeTarget === "coil") { const value = this.getNodeParameter('coilValue', 0); await new Promise((resolve) => { client.writeSingleCoil({ address: memoryAddress, value: value }, (err, data) => { if (err) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'MODBUS Error: ' + err.message); } const returnData = { data: data.response, }; responseData = returnData; resolve(responseData); }); }); } } return [this.helpers.returnJsonArray(responseData)]; } } exports.Modbus = Modbus; //# sourceMappingURL=Modbus.node.js.map