Skip to content

Commit

Permalink
using own forked node-lox-ws-api, better connection information, fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
codmpm committed Dec 13, 2019
1 parent f77e56d commit 0730a50
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 59 deletions.
125 changes: 69 additions & 56 deletions loxone/loxone.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module.exports = function (RED) {
return res.json("");
}

var configNode = RED.nodes.getNode(req.query.id);
var result = {
let configNode = RED.nodes.getNode(req.query.id);
let result = {
state: 'error',
msg: 'miniserver not connected',
structure: {}
Expand All @@ -40,16 +40,16 @@ module.exports = function (RED) {

RED.httpAdmin.get('/loxone-miniserver/struct-changed', function (req, res) {

var result = {
let result = {
state: 'error',
msg: 'miniserver not connected',
structure: {}
};

var username = req.query.username;
var password = req.query.password;
let username = req.query.username;
let password = req.query.password;

var configNode = RED.nodes.getNode(req.query.id);
let configNode = RED.nodes.getNode(req.query.id);

if (configNode) {
if (!username) {
Expand Down Expand Up @@ -77,7 +77,7 @@ module.exports = function (RED) {
return;
}

var data = '';
let data = '';
http_res.on('data', function (chunk) {
data += chunk;
});
Expand Down Expand Up @@ -112,7 +112,7 @@ module.exports = function (RED) {

RED.nodes.createNode(this, config);

var node = this;
let node = this;
node.connected = false;
node.authenticated = false;
node.connection = null;
Expand All @@ -126,6 +126,7 @@ module.exports = function (RED) {
node._webserviceNodeQueue = []; //only webservice nodes which are waiting for a return will be here
node._otherEvents = [];

// TODO
//do nothing if miniserver connection is not active
/*
if (config.active !== true) {
Expand All @@ -135,14 +136,14 @@ module.exports = function (RED) {
return;
}*/

var text_logger_limit = 100;
let text_logger_limit = 100;

node.encMethod = 'Token-Enc';
if (encMethods.hasOwnProperty(config.enctype)) {
node.encMethod = encMethods[config.enctype];
}

var client = new node_lox_ws_api(
let client = new node_lox_ws_api(
config.host + ':' + config.port,
node.credentials.username,
node.credentials.password,
Expand All @@ -154,30 +155,36 @@ module.exports = function (RED) {

client.on('connect', function () {
node.log('Miniserver connected (' + config.host + ':' + config.port + ') using ' + node.encMethod);
node.connected = true;
});

client.on('authorized', function () {
//node.log('authorized');
node.authenticated = true;
node.connection = client;

node.connected = true;
node.setConnectionStatusMsg("green", "connected", "dot");
sendOnlineNodeMsg(true, config.id);

});

client.on('connect_failed', function () {
node.error('Miniserver connect failed');
client.on('get_structure_file', function (data) {
node.log("got structure file " + data.lastModified);
node.structureData = prepareStructure(data);
});

client.on('connect_failed', function (error, reason) {
node.error('Miniserver connect failed: ' + reason, JSON.stringify(error));
node.setConnectionStatusMsg("red", "connection failed", "ring");
});

client.on('connection_error', function (error) {
node.error('Miniserver connection error: ' + error);
client.on('connection_error', function (error, reason) {
node.error('Miniserver connection error: ' + reason, JSON.stringify(error));
node.setConnectionStatusMsg("red", "connection error", "ring");
});

client.on('close', function () {
node.log("connection closed");
client.on('close', function (info, reason) {
node.log("connection closed: " + reason, info);
node.connected = false;
node.authenticated = false;
node.connection = null;
Expand Down Expand Up @@ -212,21 +219,21 @@ module.exports = function (RED) {
break;
case 'control':

for (var i in node._webserviceNodeQueue) {
for (let i in node._webserviceNodeQueue) {

var wsNode = node._webserviceNodeQueue[i];
let wsNode = node._webserviceNodeQueue[i];

if (wsNode.uri === 'j' + message.control) {

var msg = {
let msg = {
'payload': message.value,
'topic': message.control,
'code': parseInt(message.code)
};

//and parse all values to msg.data
if (message.hasOwnProperty('data') && message.data.hasOwnProperty('LL')) {
var additionalData = JSON.parse(JSON.stringify(message.data.LL));
let additionalData = JSON.parse(JSON.stringify(message.data.LL));

delete additionalData.Code;
delete additionalData.control;
Expand All @@ -238,8 +245,15 @@ module.exports = function (RED) {
}

//add miniserver info
msg.msInfo = node.structureData.msInfo;
msg.lastModified = node.structureData.lastModified;
if(node.structureData) {
msg.msInfo = node.structureData.msInfo;
msg.lastModified = node.structureData.lastModified;
} else {
msg.msInfo = null;
msg.lastModified = null;

node.log("structure data not ready yet, webservice request processed but without msInfo and lastModified");
}

//send the data out of the requesting node
wsNode.send(msg);
Expand Down Expand Up @@ -268,7 +282,7 @@ module.exports = function (RED) {
nodeData.hasOwnProperty('miniserver') &&
nodeData.miniserver === node.id) {

var keepaliveNode = RED.nodes.getNode(nodeData.id);
let keepaliveNode = RED.nodes.getNode(nodeData.id);
if (keepaliveNode) {
keepaliveNode.send({
topic: 'keepalive',
Expand All @@ -295,11 +309,6 @@ module.exports = function (RED) {
//node.log('received text messages:' + messages.length);
});

client.on('get_structure_file', function (data) {
node.log("got structure file " + data.lastModified);
node.structureData = prepareStructure(data);
});

client.on('update_event_value', _updateEvent);
client.on('update_event_text', _updateEvent);
client.on('update_event_daytimer', _updateEvent);
Expand All @@ -310,8 +319,6 @@ module.exports = function (RED) {
//on (full-)deploys close event shutdown the client
if (node.connected) {
client.once('close', function () {

//console.log('We are here!');
done();
});
client.abort();
Expand Down Expand Up @@ -408,7 +415,7 @@ module.exports = function (RED) {

LoxoneMiniserver.prototype.setConnectionStatusMsg = function (color, text, shape) {
shape = shape || 'dot';
var newState = function (item) {
let newState = function (item) {
item.status({
fill: color,
shape: shape,
Expand All @@ -426,12 +433,12 @@ module.exports = function (RED) {
LoxoneMiniserver.prototype.findControlByState = function (uuid) {

//search in all controls for given state uuid to find the corresponding control
for (var wantedControlUuid in this.structureData.controls) {
for (let wantedControlUuid in this.structureData.controls) {
if (
this.structureData.controls.hasOwnProperty(wantedControlUuid) &&
this.structureData.controls[wantedControlUuid].hasOwnProperty('states')
) {
for (var curState in this.structureData.controls[wantedControlUuid].states) {
for (let curState in this.structureData.controls[wantedControlUuid].states) {

if (
this.structureData.controls[wantedControlUuid].states.hasOwnProperty(curState) &&
Expand All @@ -450,7 +457,7 @@ module.exports = function (RED) {
LoxoneMiniserver.prototype.buildMsgObject = function (event, uuid, controlStructure) {

//get state name
var stateName;
let stateName;
for (stateName in controlStructure.states) {
if (
controlStructure.states.hasOwnProperty(stateName) &&
Expand All @@ -466,23 +473,22 @@ module.exports = function (RED) {
}

//evaluate payload
var payload;
let payload;

try {
payload = JSON.parse(event);
}
catch (err) {
} catch (err) {
payload = event;
}

//check if control has a room
var room = null;
let room = null;
if (controlStructure.room && this.structureData.rooms[controlStructure.room].name) {
room = this.structureData.rooms[controlStructure.room].name;
}

//check if control has a category
var category = null;
let category = null;
if (controlStructure.cat && this.structureData.cats[controlStructure.cat].name) {
category = this.structureData.cats[controlStructure.cat].name;
}
Expand All @@ -507,8 +513,8 @@ module.exports = function (RED) {

LoxoneMiniserver.prototype.handleEvent = function (uuid, event) {

var i, curNode, curRoom, curCategory;
var controlStructure = this.findControlByState(uuid);
let i, curNode, curRoom, curCategory;
let controlStructure = this.findControlByState(uuid);

//do we have a control for this uuid? could also be weather or global
if (controlStructure) {
Expand Down Expand Up @@ -554,23 +560,23 @@ module.exports = function (RED) {
};

function prepareStructure(data) {
var structure = {
let structure = {
rooms: data.rooms || {},
cats: data.cats || {},
controls: {},
msInfo: data.msInfo || {},
lastModified: data.lastModified
};

for (var uuid in data.controls) {
for (let uuid in data.controls) {
if (!data.controls.hasOwnProperty(uuid)) {
continue;
}

structure.controls[uuid] = data.controls[uuid];

if (data.controls[uuid].hasOwnProperty('subControls')) {
for (var sub_uuid in data.controls[uuid].subControls) {
for (let sub_uuid in data.controls[uuid].subControls) {
if (!data.controls[uuid].subControls.hasOwnProperty(sub_uuid)) {
continue;
}
Expand All @@ -584,7 +590,7 @@ module.exports = function (RED) {
}

return structure;
};
}

function sendOnlineNodeMsg(online, configId) {

Expand All @@ -596,7 +602,7 @@ module.exports = function (RED) {
theNode.hasOwnProperty('miniserver') &&
theNode.miniserver === configId) {

var node = RED.nodes.getNode(theNode.id);
let node = RED.nodes.getNode(theNode.id);

if (node) {
/*
Expand All @@ -621,7 +627,7 @@ module.exports = function (RED) {
function LoxoneControlInNode(config) {

RED.nodes.createNode(this, config);
var node = this;
let node = this;

node.state = config.state;
node.control = config.control;
Expand Down Expand Up @@ -649,7 +655,7 @@ module.exports = function (RED) {
function LoxoneControlOutNode(config) {

RED.nodes.createNode(this, config);
var node = this;
let node = this;

node.control = config.control;
node.miniserver = RED.nodes.getNode(config.miniserver);
Expand All @@ -661,6 +667,8 @@ module.exports = function (RED) {
this.on('input', function (msg) {
if (node.miniserver.connected && node.miniserver.connection) {
node.miniserver.connection.send_control_command(node.control, msg.payload);
} else {
node.log(node.id + ' was triggered but miniserver is not connected.');
}
});

Expand All @@ -679,11 +687,10 @@ module.exports = function (RED) {

function LoxoneWebServiceNode(config) {
RED.nodes.createNode(this, config);
var node = this;
let node = this;

node.miniserver = RED.nodes.getNode(config.miniserver);


if (node.miniserver) {

node.miniserver.registerWebserviceNode(node);
Expand All @@ -697,8 +704,7 @@ module.exports = function (RED) {

this.on('input', function (msg) {

node.status({});
var wantedURI = msg.uri || config.uri;
let wantedURI = msg.uri || config.uri;

if (!wantedURI.length) {
node.status({
Expand Down Expand Up @@ -726,13 +732,20 @@ module.exports = function (RED) {
if (node.miniserver.connected && node.miniserver.connection) {
node.miniserver.addWebserviceNodeToQueue(node);
node.miniserver.connection.send_command(node.uri);
} else {
node.status({
fill: 'red',
shape: 'dot',
text: 'not connected'
});
node.log(node.id + " tried to call " + wantedURI + " but miniserver is not connected.");
return null;
}

});

}


}

RED.nodes.registerType('loxone-webservice', LoxoneWebServiceNode);
Expand All @@ -756,7 +769,7 @@ module.exports = function (RED) {
function LoxoneStreamInNode(config) {

RED.nodes.createNode(this, config);
var node = this;
let node = this;

node.category = config.category;
node.room = config.room;
Expand Down Expand Up @@ -785,7 +798,7 @@ module.exports = function (RED) {
function LoxoneStreamAllNode(config) {

RED.nodes.createNode(this, config);
var node = this;
let node = this;

node.category = config.category;
node.room = config.room;
Expand Down
Loading

0 comments on commit 0730a50

Please sign in to comment.