node-red-contrib-omron-fins
Advanced tools
Comparing version
@@ -25,3 +25,2 @@ /* | ||
const clients = {}; | ||
@@ -50,19 +49,27 @@ function convertPayloadToDataArray(payload) { | ||
function describe(host, port, options) { | ||
options = options || {}; | ||
return `{host:'${host || ''}', port:'${port || ''}', protocol:'${options.protocol || 'udp'}', MODE:'${options.MODE}', ICF:'${options.ICF}', DNA:'${options.DNA}', DA1:'${options.DA1}', DA2:'${options.DA2}', SNA:'${options.SNA}', SA1:'${options.SA1}', SA2:'${options.SA2}'}`; | ||
} | ||
const clients = {}; | ||
const fins = require('./omron-fins'); | ||
module.exports = { | ||
get(node, port, host, opts) { | ||
const fins = require('./omron-fins'); | ||
const id = `FinsClient:{host:'${host || ''}', port:'${port || ''}', protocol:'${opts.protocol || 'udp'}', MODE:'${opts.MODE}', ICF:'${opts.ICF}', DNA:'${opts.DNA}', DA1:'${opts.DA1}', DA2:'${opts.DA2}', SNA:'${opts.SNA}', SA1:'${opts.SA1}', SA2:'${opts.SA2}'}`; | ||
get(node, connectionConfig) { | ||
const id = connectionConfig.id; | ||
let options = connectionConfig.options || {}; | ||
let port = parseInt(connectionConfig.port || options.port); | ||
let host = connectionConfig.host || options.host; | ||
let connect = connectionConfig.autoConnect == null ? true : connectionConfig.autoConnect; | ||
if (!clients[id]) { | ||
clients[id] = (function () { | ||
const options = opts || {}; | ||
const h = host || options.host; | ||
const p = port || options.port; | ||
node.log(`[FINS] adding new connection to pool ~ ${id}`); | ||
let client = fins.FinsClient(parseInt(p), h, options); | ||
node.log(`Create new FinsClient. id:${id}, config: ${describe(host, port, options)}`); | ||
let connecting = false; | ||
let client = fins.FinsClient(port, host, options, connect); | ||
options.autoConnect = options.autoConnect == undefined ? true : options.autoConnect; | ||
options.autoConnect = options.autoConnect == null ? true : options.autoConnect; | ||
options.preventAutoReconnect = false; | ||
@@ -72,3 +79,2 @@ | ||
_instances: 0, | ||
write(address, data, opts, tag) { | ||
@@ -164,13 +170,26 @@ if (!client.connected && options.preventAutoReconnect) { | ||
}, | ||
connect() { | ||
connect(host, port, opts) { | ||
options.preventAutoReconnect = false; | ||
if (client && !client.connected && !connecting) { | ||
try { | ||
node.log(`Connecting id:${id}, config: ${describe(this.connectionInfo.host, this.connectionInfo.port, this.connectionInfo.options)}`); | ||
// eslint-disable-next-line no-empty | ||
} catch (error) { } | ||
connecting = true; | ||
client.reconnect(); | ||
try { | ||
if(arguments.length == 0) { | ||
client.reconnect(); | ||
} else { | ||
client.connect(host, port, opts); | ||
} | ||
// eslint-disable-next-line no-empty | ||
} catch (error) { | ||
node.error(error) | ||
} | ||
} | ||
}, | ||
closeConnection() { | ||
disconnect() { | ||
options.preventAutoReconnect = true; | ||
if (client) { | ||
client.close(); | ||
client.disconnect(); | ||
} | ||
@@ -187,12 +206,29 @@ connecting = false; | ||
disconnect() { | ||
this._instances -= 1; | ||
if (this._instances <= 0) { | ||
node.log(`[FINS] closing connection ~ ${id}`); | ||
client.close(); | ||
client = null; | ||
node.log(`[FINS] deleting connection from pool ~ ${id}`); | ||
delete clients[id]; | ||
close() { | ||
if (client && client.connected) { | ||
node.log(`closing connection ~ ${id}`); | ||
client.disconnect(); | ||
} | ||
connecting = false; | ||
}, | ||
get connected() { | ||
return client && client.connected; | ||
}, | ||
get connectionInfo() { | ||
if(client) { | ||
const info = { | ||
port: client.port, | ||
host: client.host, | ||
options: {...client.options}, | ||
} | ||
delete info.options.preventAutoReconnect;//not of interest | ||
if(client.protocol == "tcp") { | ||
info.options.tcp_server_node_no = client.server_node_no;//DA1 | ||
info.options.tcp_client_node_no = client.client_node_no;//SA1 | ||
} | ||
return info; | ||
} | ||
return {} | ||
} | ||
}; | ||
@@ -202,3 +238,3 @@ | ||
if (client) { | ||
node.log(`[FINS] connected ~ ${id}`); | ||
node.log(`connected ~ ${id}`); | ||
connecting = false; | ||
@@ -209,9 +245,9 @@ } | ||
client.on('close', (err) => { | ||
node.log(`[FINS] connection closed ~ ${id}`); | ||
node.log(`connection closed ~ ${id}`); | ||
connecting = false; | ||
if (options.autoConnect && !options.preventAutoReconnect) { | ||
setTimeout(() => { | ||
if (options.autoConnect && !options.preventAutoReconnect) { | ||
node.log(`[FINS] autoConnect call from error handler ~ ${id}`); | ||
finsClientConnection.reconnectTimer = setTimeout(() => { | ||
if (finsClientConnection.reconnectTimer && options.autoConnect && !options.preventAutoReconnect) { | ||
node.log(`autoConnect call from close handler ~ ${id}`); | ||
finsClientConnection.connect(); | ||
@@ -226,5 +262,20 @@ } | ||
} | ||
clients[id]._instances += 1; | ||
return clients[id]; | ||
}, | ||
close(connectionConfig) { | ||
const c = this.get(null, connectionConfig); | ||
if(c && c.connected) { | ||
c.close(); | ||
} | ||
c.close(); | ||
if(c && c.reconnectTimer) { | ||
clearTimeout(c.reconnectTimer); | ||
c.reconnectTimer = null; | ||
} | ||
if(c) { | ||
const id = connectionConfig.id; | ||
delete clients[id]; | ||
} | ||
} | ||
}; |
@@ -25,16 +25,4 @@ /* | ||
var constants = require('omron-fins').FinsConstants; | ||
var {FinsConstants: constants, FinsDataUtils: {isInt}} = require('omron-fins'); | ||
function isInt(x,def){ | ||
var v; | ||
try{ | ||
v = parseInt(x); | ||
if(isNaN(v)) | ||
return def; | ||
} catch(e){ | ||
return def; | ||
} | ||
return v; | ||
} | ||
/*! | ||
@@ -47,4 +35,4 @@ * Get value of environment variable. | ||
function getSetting(_RED, name) { | ||
var result = _RED.util.getObjectProperty(_RED.settings, name); | ||
return result || process.env[name]; | ||
var result = _RED.util.getObjectProperty(_RED.settings, name); | ||
return result || process.env[name]; | ||
} | ||
@@ -63,47 +51,57 @@ | ||
function resolveSetting(value, RED) { | ||
try { | ||
if(!value) return value; | ||
if(typeof value != "string") return value; | ||
var result; | ||
if (/^\${[^}]+}$/.test(value)) { | ||
// ${ENV_VAR} | ||
var name = value.substring(2,value.length-1); | ||
result = getSetting(RED, name); | ||
} else { | ||
// FOO${ENV_VAR}BAR | ||
result = value.replace(/\${([^}]+)}/g, function(match, name) { | ||
return getSetting(RED, name); | ||
}); | ||
try { | ||
if (!value) return value; | ||
if (typeof value != "string") return value; | ||
var result; | ||
if (/^\${[^}]+}$/.test(value)) { | ||
// ${ENV_VAR} | ||
var name = value.substring(2, value.length - 1); | ||
result = getSetting(RED, name); | ||
} else { | ||
// FOO${ENV_VAR}BAR | ||
result = value.replace(/\${([^}]+)}/g, function (match, name) { | ||
return getSetting(RED, name); | ||
}); | ||
} | ||
return (result == null) ? value : result; | ||
} catch (error) { | ||
return value; | ||
} | ||
return (result == null)?value:result; | ||
} catch (error) { | ||
return value; | ||
} | ||
} | ||
module.exports = function (RED) { | ||
function omronConnection(config) { | ||
RED.nodes.createNode(this, config); | ||
this.name = config.name; | ||
this.host = resolveSetting(config.host, RED); | ||
this.port = resolveSetting(config.port, RED); | ||
this.options = {}; | ||
if(config.protocolType == "env") { | ||
this.options.protocol = resolveSetting(config.protocol, RED); | ||
} else { | ||
this.options.protocol = config.protocolType || "udp"; | ||
const connection_pool = require('../connection_pool.js'); | ||
function omronConnection(config) { | ||
RED.nodes.createNode(this, config); | ||
this.name = config.name; | ||
this.host = resolveSetting(config.host, RED); | ||
this.port = resolveSetting(config.port, RED); | ||
this.options = {}; | ||
if (config.protocolType == "env") { | ||
this.options.protocol = resolveSetting(config.protocol, RED); | ||
} else { | ||
this.options.protocol = config.protocolType || "udp"; | ||
} | ||
this.options.MODE = config.MODE ? config.MODE : "CSCJ"; | ||
this.options.ICF = isInt(resolveSetting(config.ICF, RED), constants.DefaultFinsHeader.ICF); | ||
this.options.DNA = isInt(resolveSetting(config.DNA, RED), constants.DefaultFinsHeader.DNA); | ||
this.options.DA1 = isInt(resolveSetting(config.DA1, RED), constants.DefaultFinsHeader.DA1); | ||
this.options.DA2 = isInt(resolveSetting(config.DA2, RED), constants.DefaultFinsHeader.DA2); | ||
this.options.SNA = isInt(resolveSetting(config.SNA, RED), constants.DefaultFinsHeader.SNA); | ||
this.options.SA1 = isInt(resolveSetting(config.SA1, RED), constants.DefaultFinsHeader.SA1); | ||
this.options.SA2 = isInt(resolveSetting(config.SA2, RED), constants.DefaultFinsHeader.SA2); | ||
this.autoConnect = config.autoConnect == null ? true : config.autoConnect; | ||
// eslint-disable-next-line no-unused-vars | ||
this.on('close', function (done) { | ||
try { | ||
connection_pool.close(this, this); | ||
done && done(); | ||
// eslint-disable-next-line no-empty | ||
} catch (error) { } | ||
}); | ||
} | ||
this.options.MODE = config.MODE ? config.MODE : "CSCJ"; | ||
this.options.ICF = isInt(resolveSetting(config.ICF, RED), constants.DefaultFinsHeader.ICF); | ||
this.options.DNA = isInt(resolveSetting(config.DNA, RED), constants.DefaultFinsHeader.DNA); | ||
this.options.DA1 = isInt(resolveSetting(config.DA1, RED), constants.DefaultFinsHeader.DA1); | ||
this.options.DA2 = isInt(resolveSetting(config.DA2, RED), constants.DefaultFinsHeader.DA2); | ||
this.options.SNA = isInt(resolveSetting(config.SNA, RED), constants.DefaultFinsHeader.SNA); | ||
this.options.SA1 = isInt(resolveSetting(config.SA1, RED), constants.DefaultFinsHeader.SA1); | ||
this.options.SA2 = isInt(resolveSetting(config.SA2, RED), constants.DefaultFinsHeader.SA2); | ||
} | ||
RED.nodes.registerType("FINS Connection", omronConnection); | ||
RED.nodes.registerType("FINS Connection", omronConnection); | ||
}; | ||
@@ -28,3 +28,3 @@ /* eslint-disable no-inner-declarations */ | ||
const connection_pool = require('../connection_pool.js'); | ||
const commandTypes = ['status', 'cpu-unit-data-read', 'stop', 'run', 'clock-read', 'clock-write']; | ||
const commandTypes = ['connect', 'disconnect', 'status', 'cpu-unit-data-read', 'stop', 'run', 'clock-read', 'clock-write']; | ||
function omronControl(config) { | ||
@@ -37,3 +37,5 @@ RED.nodes.createNode(this, config); | ||
node.clock = config.clock || 'clock'; | ||
node.clockType = config.clockTypeType || 'msg'; | ||
node.clockType = config.clockType || 'msg'; | ||
node.connectOptions = config.connectOptions || 'connectOptions'; | ||
node.connectOptionsType = config.connectOptionsType || 'msg'; | ||
node.command = config.command || 'status'; | ||
@@ -46,5 +48,4 @@ node.commandType = config.commandType || 'status'; | ||
if (this.connectionConfig) { | ||
const options = Object.assign({}, node.connectionConfig.options); | ||
node.client = connection_pool.get(this, this.connectionConfig.port, this.connectionConfig.host, options); | ||
node.status({ fill: 'yellow', shape: 'ring', text: 'initialising' }); | ||
node.client = connection_pool.get(this, node.connectionConfig); | ||
@@ -111,2 +112,5 @@ this.client.on('error', function (error, seq) { | ||
switch (sequence.request.command.name) { | ||
case 'connect': | ||
case 'disconnect': | ||
break; | ||
case 'status': | ||
@@ -133,2 +137,3 @@ case 'cpu-unit-data-read': | ||
}; | ||
origInputMsg.fins.connectionInfo = node.client.connectionInfo; | ||
origInputMsg.fins.response = sequence.response; | ||
@@ -158,11 +163,3 @@ origInputMsg.fins.stats = sequence.stats; | ||
if (msg.disconnect === true || msg.topic === 'disconnect') { | ||
node.client.closeConnection(); | ||
return; | ||
} else if (msg.connect === true || msg.topic === 'connect') { | ||
node.client.connect(); | ||
return; | ||
} | ||
let command = 'status'; | ||
let command = ''; | ||
if (commandTypes.indexOf(node.commandType + '') >= 0) { | ||
@@ -181,4 +178,20 @@ command = node.commandType; | ||
let clockWriteData; | ||
let connectOptions; | ||
const params = []; | ||
switch (command) { | ||
case 'connect': | ||
connectOptions = RED.util.evaluateNodeProperty(node.connectOptions, node.connectOptionsType, node, msg); | ||
if(connectOptions) { | ||
if( typeof connectOptions != "object") { | ||
nodeStatusError("Connect Options must be an object", msg, "error"); | ||
return; | ||
} else { | ||
params.push(connectOptions.host); | ||
params.push(connectOptions.port); | ||
params.push(connectOptions); | ||
} | ||
} | ||
clientFn = node.client.connect; | ||
break; | ||
case 'disconnect': | ||
case 'status': | ||
@@ -206,3 +219,3 @@ case 'stop': | ||
const opts = msg.finsOptions || {}; | ||
const opts = {}; | ||
let sid; | ||
@@ -233,3 +246,7 @@ try { | ||
}); | ||
node.status({ fill: 'green', shape: 'ring', text: 'ready' }); | ||
if(node.client && node.client.connected) { | ||
node.status({ fill: 'green', shape: 'dot', text: 'connected' }); | ||
} else { | ||
node.status({ fill: 'grey', shape: 'ring', text: 'initialised' }); | ||
} | ||
@@ -241,8 +258,4 @@ } else { | ||
RED.nodes.registerType('FINS Control', omronControl); | ||
omronControl.prototype.close = function () { | ||
if (this.client) { | ||
this.client.disconnect(); | ||
} | ||
}; | ||
}; | ||
@@ -46,6 +46,4 @@ /* | ||
if (err) { | ||
console.error(err); | ||
node.error(err, msg); | ||
} else { | ||
console.error(statusText); | ||
node.error(statusText, msg); | ||
@@ -60,7 +58,5 @@ } | ||
node.status({ fill: 'yellow', shape: 'ring', text: 'initialising' }); | ||
const options = Object.assign({}, node.connectionConfig.options); | ||
this.client = connection_pool.get(this, this.connectionConfig.port, this.connectionConfig.host, options); | ||
node.client = connection_pool.get(this, node.connectionConfig); | ||
this.client.on('error', function (error, seq) { | ||
console.log('Error: ', error); | ||
node.status({ fill: 'red', shape: 'ring', text: 'error' }); | ||
@@ -157,3 +153,3 @@ node.error(error, (seq && seq.tag ? seq.tag : seq)); | ||
if (msg.disconnect === true || msg.topic === 'disconnect') { | ||
node.client.closeConnection(); | ||
node.client.disconnect(); | ||
return; | ||
@@ -213,3 +209,7 @@ } else if (msg.connect === true || msg.topic === 'connect') { | ||
}); | ||
node.status({ fill: 'green', shape: 'ring', text: 'ready' }); | ||
if(node.client && node.client.connected) { | ||
node.status({ fill: 'green', shape: 'dot', text: 'connected' }); | ||
} else { | ||
node.status({ fill: 'grey', shape: 'ring', text: 'initialised' }); | ||
} | ||
@@ -222,8 +222,4 @@ } else { | ||
RED.nodes.registerType('FINS Fill', omronFill); | ||
omronFill.prototype.close = function () { | ||
if (this.client) { | ||
this.client.disconnect(); | ||
} | ||
}; | ||
}; | ||
@@ -48,4 +48,3 @@ /* eslint-disable no-inner-declarations */ | ||
if (this.connectionConfig) { | ||
const options = Object.assign({}, node.connectionConfig.options); | ||
node.client = connection_pool.get(this, this.connectionConfig.port, this.connectionConfig.host, options); | ||
node.client = connection_pool.get(this, node.connectionConfig); | ||
node.status({ fill: 'yellow', shape: 'ring', text: 'initialising' }); | ||
@@ -201,3 +200,3 @@ | ||
if (msg.disconnect === true || msg.topic === 'disconnect') { | ||
node.client.closeConnection(); | ||
node.client.disconnect(); | ||
return; | ||
@@ -250,3 +249,7 @@ } else if (msg.connect === true || msg.topic === 'connect') { | ||
}); | ||
node.status({ fill: 'green', shape: 'ring', text: 'ready' }); | ||
if(node.client && node.client.connected) { | ||
node.status({ fill: 'green', shape: 'dot', text: 'connected' }); | ||
} else { | ||
node.status({ fill: 'grey', shape: 'ring', text: 'initialised' }); | ||
} | ||
@@ -258,8 +261,4 @@ } else { | ||
RED.nodes.registerType('FINS Read', omronRead); | ||
omronRead.prototype.close = function () { | ||
if (this.client) { | ||
this.client.disconnect(); | ||
} | ||
}; | ||
}; | ||
@@ -46,4 +46,3 @@ /* eslint-disable no-inner-declarations */ | ||
if (this.connectionConfig) { | ||
const options = Object.assign({}, node.connectionConfig.options); | ||
node.client = connection_pool.get(this, this.connectionConfig.port, this.connectionConfig.host, options); | ||
node.client = connection_pool.get(this, node.connectionConfig); | ||
node.status({ fill: 'yellow', shape: 'ring', text: 'initialising' }); | ||
@@ -199,3 +198,3 @@ | ||
if (msg.disconnect === true || msg.topic === 'disconnect') { | ||
node.client.closeConnection(); | ||
node.client.disconnect(); | ||
return; | ||
@@ -238,3 +237,7 @@ } else if (msg.connect === true || msg.topic === 'connect') { | ||
}); | ||
node.status({ fill: 'green', shape: 'ring', text: 'ready' }); | ||
if(node.client && node.client.connected) { | ||
node.status({ fill: 'green', shape: 'dot', text: 'connected' }); | ||
} else { | ||
node.status({ fill: 'grey', shape: 'ring', text: 'initialised' }); | ||
} | ||
@@ -246,7 +249,2 @@ } else { | ||
RED.nodes.registerType('FINS Read Multiple', omronReadMultiple); | ||
omronReadMultiple.prototype.close = function () { | ||
if (this.client) { | ||
this.client.disconnect(); | ||
} | ||
}; | ||
}; |
@@ -47,6 +47,4 @@ /* eslint-disable no-inner-declarations */ | ||
if (err) { | ||
console.error(err); | ||
node.error(err, msg); | ||
} else { | ||
console.error(statusText); | ||
node.error(statusText, msg); | ||
@@ -57,14 +55,8 @@ } | ||
// function nodeStatusParameterError(err, msg, propName) { | ||
// nodeStatusError(err, msg, "Unable to evaluate property '" + propName + "' value"); | ||
// } | ||
if (this.connectionConfig) { | ||
node.status({ fill: 'yellow', shape: 'ring', text: 'initialising' }); | ||
const options = Object.assign({}, node.connectionConfig.options); | ||
this.client = connection_pool.get(this, this.connectionConfig.port, this.connectionConfig.host, options); | ||
node.client = connection_pool.get(this, node.connectionConfig); | ||
this.client.on('error', function (error, seq) { | ||
console.log('Error: ', error); | ||
node.status({ fill: 'red', shape: 'ring', text: 'error' }); | ||
@@ -162,3 +154,3 @@ node.error(error, (seq && seq.tag ? seq.tag : seq)); | ||
if (msg.disconnect === true || msg.topic === 'disconnect') { | ||
node.client.closeConnection(); | ||
node.client.disconnect(); | ||
return; | ||
@@ -216,3 +208,7 @@ } else if (msg.connect === true || msg.topic === 'connect') { | ||
}); | ||
node.status({ fill: 'green', shape: 'ring', text: 'ready' }); | ||
if(node.client && node.client.connected) { | ||
node.status({ fill: 'green', shape: 'dot', text: 'connected' }); | ||
} else { | ||
node.status({ fill: 'grey', shape: 'ring', text: 'initialised' }); | ||
} | ||
@@ -225,8 +221,3 @@ } else { | ||
RED.nodes.registerType('FINS Transfer', omronTransfer); | ||
omronTransfer.prototype.close = function () { | ||
if (this.client) { | ||
this.client.disconnect(); | ||
} | ||
}; | ||
}; | ||
@@ -45,6 +45,4 @@ /* eslint-disable no-inner-declarations */ | ||
if (err) { | ||
console.error(err); | ||
node.error(err, msg); | ||
} else { | ||
console.error(statusText); | ||
node.error(statusText, msg); | ||
@@ -62,7 +60,5 @@ } | ||
node.status({ fill: 'yellow', shape: 'ring', text: 'initialising' }); | ||
const options = Object.assign({}, node.connectionConfig.options); | ||
this.client = connection_pool.get(this, this.connectionConfig.port, this.connectionConfig.host, options); | ||
node.client = connection_pool.get(this, node.connectionConfig); | ||
this.client.on('error', function (error, seq) { | ||
console.log('Error: ', error); | ||
node.status({ fill: 'red', shape: 'ring', text: 'error' }); | ||
@@ -159,3 +155,3 @@ node.error(error, (seq && seq.tag ? seq.tag : seq)); | ||
if (msg.disconnect === true || msg.topic === 'disconnect') { | ||
node.client.closeConnection(); | ||
node.client.disconnect(); | ||
return; | ||
@@ -212,3 +208,7 @@ } else if (msg.connect === true || msg.topic === 'connect') { | ||
}); | ||
node.status({ fill: 'green', shape: 'ring', text: 'ready' }); | ||
if(node.client && node.client.connected) { | ||
node.status({ fill: 'green', shape: 'dot', text: 'connected' }); | ||
} else { | ||
node.status({ fill: 'grey', shape: 'ring', text: 'initialised' }); | ||
} | ||
@@ -221,8 +221,3 @@ } else { | ||
RED.nodes.registerType('FINS Write', omronWrite); | ||
omronWrite.prototype.close = function () { | ||
if (this.client) { | ||
this.client.disconnect(); | ||
} | ||
}; | ||
}; | ||
{ | ||
"name": "node-red-contrib-omron-fins", | ||
"version": "0.5.0-beta.2", | ||
"version": "0.5.0-beta.3", | ||
"author": { | ||
@@ -28,3 +28,3 @@ "name": "Steve-Mcl", | ||
"dependencies": { | ||
"omron-fins": "0.5.0-beta.2" | ||
"omron-fins": "0.5.0-beta.3" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
317288
9.11%33
6.45%1644
2.3%+ Added
- Removed
Updated