Skip to content

Commit

Permalink
Add timeout as a config input
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinThorReid committed Jan 7, 2020
1 parent e64ce82 commit 2b12ef7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 54 deletions.
17 changes: 10 additions & 7 deletions lutron-config.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<script type="text/javascript">
RED.nodes.registerType('lutron-config', {
category: 'config',
Expand All @@ -13,13 +12,16 @@
deviceMap: {
"ALL": 0,
},
timeout: {
value: 45000
}
},
label: function () {
return this.name;
},
oneditprepare: function () {

var key='';
var key = '';
var tableContent = '<table class="table"> <tr><th>Name</th><th>Device ID</th><th></th>';
tableContent += '<th></th><span id="lutron-table-add" class="lutron-table-add fa fa-plus"></span></tr>';
var deviceMap = this.deviceMap || this._def.defaults.deviceMap;
Expand Down Expand Up @@ -73,7 +75,7 @@

// Use the headers from earlier to name our hash keys
headers.forEach(function (header, i) {
h[header] = $td.eq(i).text();
h[header] = $td.eq(i).text();
});

data[h['name']] = h['device id'];
Expand All @@ -93,6 +95,10 @@
<label for="node-config-input-ipaddress"><i class="fa fa-globe"></i> IP Address</label>
<input type="text" id="node-config-input-ipaddress" placeholder="192.168.xx.xx"></input>
</div>
<div class="form-row">
<label for="node-config-input-timeout"><i class="fa fa-globe"></i> Timeout (ms)</label>
<input type="text" id="node-config-input-timeout" placeholder="30000"></input>
</div>
<!-- Table Class -->
<h2 style="color:brown; margin-left:490px;margin-top:0px;">Device mapping table</h2>

Expand Down Expand Up @@ -132,7 +138,4 @@ <h2 style="color:brown; margin-left:490px;margin-top:0px;">Device mapping table<
}
}
</style>
</script>



</script>
79 changes: 37 additions & 42 deletions lutron-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var Telnet = require('telnet-client');
var events = require("events");

module.exports = function(RED) {
module.exports = function (RED) {

function LutronConfigNode(config) {
RED.nodes.createNode(this, config);
Expand All @@ -18,77 +18,72 @@ module.exports = function(RED) {
host: this.lutronLoc,
port: this.port,
shellPrompt: 'GNET>',
debug:true,
debug: true,
username: 'lutron',
password: 'integration',
timeout: 5000
timeout: parseInt(config.timeout) || 45000
};
this.sendLutronCommand = function(devId, val) {
console.log('sendLutronCommand');
console.log(devId, ' =' , val);
var str = '#OUTPUT,'+ devId + ',1,' + val;
this.sendLutronCommand = function (devId, val) {
var str = '#OUTPUT,' + devId + ',1,' + val;
this.telnet.getSocket().write(str + '\n');
};
this.sendLutronStatus = function(devId) {
console.log('sendLutronCommand');
console.log(devId, ' =' , val);
var str = '?OUTPUT,'+ devId + ',1';
this.sendLutronStatus = function (devId) {
var str = '?OUTPUT,' + devId + ',1';
this.telnet.getSocket().write(str + '\n');
};
this.telnet.on('data', (function (self, pkt) {
self.lutronRecv(pkt);
}).bind(null, node));
this.telnet.on('connect', function() {
this.telnet.on('connect', function () {
this.connected = true;
console.log('telnet connect');
});
this.telnet.on('close', function() {
this.telnet.on('close', function () {
this.connected = false;
console.log('telnet close');
});
this.telnet.on('error', function() {
this.telnet.on('error', function () {
console.log('telent error');
});
this.telnet.on('failedlogin', function() {
this.telnet.on('failedlogin', function () {
console.log('telent failed login');
});
this.lutronSend = function(msg, fn) {
this.lutronSend = function (msg, fn) {
this.telent.getSocket().write(msg + '\n', fn);
}
this.lutrongUpdate = function(deviceId, fn) {
this.lutronSend('?OUTPUT,'+ deviceId + ',1', fn);
this.lutrongUpdate = function (deviceId, fn) {
this.lutronSend('?OUTPUT,' + deviceId + ',1', fn);
}
this.lutronSend = function(deviceId, val, fn) {
this.lutronSend('#OUTPUT,'+ deviceId + ',1,' + val, fn);
this.lutronSend = function (deviceId, val, fn) {
this.lutronSend('#OUTPUT,' + deviceId + ',1,' + val, fn);
}
this.lutronRecv = function (data) {
var st = data.toString().trim();
console.log('data Received:', st)
var cmd = st[0]
var cs = st.substring(1).split(',')
var type = cs[0]
if (cs.length >3) {
var deviceId = parseInt(cs[1])
var action = parseInt(cs[2])
var param = parseFloat(cs[3])
// console.log('[',cmd,',', type, ',',deviceId,
// ',', action,',', param,']')
this.lutronEvent.emit('data', {
cmd: cmd,
type: type,
deviceId: deviceId,
action: action,
param: param
});
if (cmd == '~') {
// event notification
if (type == 'OUTPUT' && action == 1) {
this.devices[deviceId] = param
//this.scheduleLoxoneUpdate(deviceId, action, param)
} else if (type == 'DEVICE') {
//this.scheduleLoxoneUpdate(deviceId, action, param)
if (cs.length > 3) {
var deviceId = parseInt(cs[1])
var action = parseInt(cs[2])
var param = parseFloat(cs[3])
// console.log('[',cmd,',', type, ',',deviceId,
// ',', action,',', param,']')
this.lutronEvent.emit('data', {
cmd: cmd,
type: type,
deviceId: deviceId,
action: action,
param: param
});
if (cmd == '~') {
// event notification
if (type == 'OUTPUT' && action == 1) {
this.devices[deviceId] = param
//this.scheduleLoxoneUpdate(deviceId, action, param)
} else if (type == 'DEVICE') {
//this.scheduleLoxoneUpdate(deviceId, action, param)
}
}
}
}
}
this.telnet.connect(params);
Expand Down
6 changes: 2 additions & 4 deletions lutron-control.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
module.exports = function(RED) {
module.exports = function (RED) {

function LutronControlNode(control) {
RED.nodes.createNode(this, control);
var configNode = RED.nodes.getNode(control.confignode);
this.devName = control.name;
this.devId = configNode.deviceMap[this.devName];
this.on('input', function(msg) {
console.log('foo');
console.log(msg);
this.on('input', function (msg) {
// data is msg.payload
if (!isNaN(msg.payload)) {
configNode.sendLutronCommand(this.devId, msg.payload);
Expand Down
1 change: 0 additions & 1 deletion lutron-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = function (RED) {
// node
// then it will call the this.send(msg), msg = {payload: "hi"}
configNode.lutronEvent.on('data', (function (node, d) {

if (node.devId && node.devId !== 0) {
if (d.cmd === '~' && (d.type === 'DEVICE' || d.type === 'OUTPUT') &&
parseInt(d.deviceId) == node.devId) {
Expand Down

0 comments on commit 2b12ef7

Please sign in to comment.