UNPKG

mydomoathome

Version:

Imperihome ISS API gateway to Domoticz

1,424 lines (1,303 loc) 109 kB
//############################################################################## // This file is part of MyDomoAtHome - https://github.com/empierre/MyDomoAtHome // Copyright (C) 2014-2023 Emmanuel PIERRE (domoticz@e-nef.com) // // MyDomoAtHome is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // MyDomoAtHome is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with MyDomoAtHome. If not, see <http://www.gnu.org/licenses/>. //############################################################################## // dependencies var http = require("http"); var https = require('https'); var express = require("express"); var _ = require("underscore"); var path = require('path'); var request = require("request"); var nconf = require('nconf'); var fs = require('fs'); var os = require("os"); var moment = require('moment'); var morgan = require('morgan'); var methodOverride = require('method-override'); var bodyParser = require('body-parser'); var basicAuth = require('basic-auth'); var errorHandler = require('errorhandler'); var requester = require('sync-request'); var winston = require('winston'); var pjson = require('./package.json'); var isBase64 = require('is-base64'); var app = express(); global.logger = winston; //working variaboles var last_version_dt; var last_version = getLastVersion(); var ver = pjson.version; var device_tab = {}; var room_tab = []; var domo_room_tab = []; var device = {MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected:null}; var app_name = "MyDomoAtHome"; var port = process.env.PORT || '3002'; var passcode=process.env.SEC||''; var tempmode = 'C'; app.set('port', port); app.set('view engine', 'ejs'); //var home = process.env.MDAH_HOME || path.resolve(__dirname + "/.."); var home = process.cwd(); //app.use(morgan('combined')); app.use(methodOverride()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true})); if (process.env.MDAH_HOME) { app.use(express.static(path.join(process.env.MDAH_HOME + '/public'))); app.set('views', path.resolve(process.env.MDAH_HOME + '/views')); logger.add(winston.transports.File({filename: process.env.MDAH_HOME+'/var/usage.log'})); } else { app.use(express.static(path.join(__dirname + '/public'))); app.set('views', path.resolve(__dirname + '/views')); logger.add(new winston.transports.File({filename: '/var/log/mydomoathome/usage.log'})); } function onError(error) { if (error) { logger.warn("No global conf in /etc/mydomoathome:" + err.message); } } function loadLocalConf() { // load conf file if (fileExists('./config.json')) { try { nconf.use('file', {file: './config.json'}, onError); } catch (err) { // This will not catch the throw! logger.error("Global conf parsing issue !"); logger.error(err); return; } } } function loadGlobalConf() { if (fileExists('/etc/mydomoathome/config.json')) { try { nconf.use('file', {file: '/etc/mydomoathome/config.json'}, onError); } catch (err) { // This will not catch the throw! logger.error("Global conf parsing issue !"); logger.error(err); return; } } if (fileExists('/var/packages/MyDomoAtHome/etc/config.json')) { try { nconf.use('file', {file: '/etc/mydomoathome/config.json'}, onError); } catch (err) { // This will not catch the throw! logger.error("Global conf parsing issue !"); logger.error(err); return; } } } function loadConf() { try { nconf.load(function (err) { if (err) { logger.warn("Conf load error:" + err.message); return; } }); } catch (err) { // This will not catch the throw! logger.error("Global conf load issue !"); logger.error(err); return; } } function getConf(){ if (!(nconf.get('port') || (nconf.get('app_name')))) { logger.warn('basic configuration not found in /etc/mydomoathome/config.json, defaulting') } else { app.set('port', nconf.get('port') || process.env.PORT ); app_name = nconf.get('app_name') || "MyDomoAtHome"; passcode = nconf.get('passcode') || passcode; if (nconf.get('tempmode') == 'F') { tempmode = 'F'; } else { tempmode = 'C'; } } if (!(nconf.get('domoticz:host') || (nconf.get('domoticz:port')))) { logger.warn('domoticz access configuration not found in /etc/mydomoathome/config.json, defaulting') } } function fileExists(filePath) { try { return fs.statSync(filePath).isFile(); } catch (err) { return false; } } function versionCompare(v1, v2, options) { var lexicographical = options && options.lexicographical, zeroExtend = options && options.zeroExtend, v1parts = v1.split('.'), v2parts = v2.split('.'); function isValidPart(x) { return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x); } if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) { return NaN; } if (zeroExtend) { while (v1parts.length < v2parts.length) v1parts.push("0"); while (v2parts.length < v1parts.length) v2parts.push("0"); } if (!lexicographical) { v1parts = v1parts.map(Number); v2parts = v2parts.map(Number); } for (var i = 0; i < v1parts.length; ++i) { if (v2parts.length == i) { return 1; } if (v1parts[i] == v2parts[i]) { continue; } else if (v1parts[i] > v2parts[i]) { return 1; } else { return -1; } } if (v1parts.length != v2parts.length) { return -1; } return 0; } function getURL(req) { var protocole = nconf.get('domoticz:ssl') === true ? 'https' : 'http'; var host = nconf.get('domoticz:host')||'127.0.0.1'; var port = nconf.get('domoticz:port')||process.env.DOMO_PORT||'8080'; var path = nconf.get('domoticz:path')||'/'; var oldpath = nconf.get('domo_path'); var cmd = "json.htm"; // In case of secondary AUTH Basic authentication var secure = false; if (nconf.get('domoticz:auth') && nconf.get('domoticz:auth:username') && nconf.get('domoticz:auth:password')) { var secure = nconf.get('domoticz:auth:username') + ":" + nconf.get('domoticz:auth:password') + "@"; } if (req && req.headers.authorization) { //streamlined primary to secondary auth const base64Credentials = req.headers.authorization.split(' ')[1]; const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii'); const [username, password] = credentials.split(':'); var secure=username+":"+password+"@"; } //logger.info("cred "+secure); if (secure) { var url = protocole + '://' + secure + host + ':' + port + path + cmd; } else { var url = protocole + '://' + host + ':' + port + path + cmd; } if (process.env.DOMO) { return process.env.DOMO+"/json.htm"; } else if (oldpath) { return oldpath+"/json.htm"; } else { return url; } }; function getLastVersion() { var now = moment(); if (typeof last_version_dt !== 'undefined' && last_version_dt !== null && (last_version_dt.isBefore(moment().add(2, 'h')))) { return (last_version); } else { var options = { url: "https://api.github.com/repos/empierre/MyDomoAtHome/releases/latest", headers: { 'User-Agent': 'request' } }; request(options, function (error, response, body) { if (!error && response.statusCode == 200) { var data = JSON.parse(body); last_version_dt = now; last_version = data.tag_name; logger.info("Refreshing version cache: "+data.tag_name); return (data.tag_name); } else { return ("unknown"); } }); } } function getSettingsSecPassword() { var url = getURL(req) + "?type=command&param=getsettings"; var res = requester('GET', url); if (res.statusCode!=200) {return({})}; logger.info(url); var js = JSON.parse(res.body.toString('utf-8')); return (js.result[0].SecPassword); } function devSt(data) { var rbl; var dimmable; if (data.HaveDimmer === 'true') { dimmable = 1 } else { dimmable = 0 } var deviceId = data.deviceId; switch (data.Status) { case "On": rbl = 1; var mydev = device_tab[deviceId]; if (mydev) { if (dimmable) { mydev.Action = 0; mydev.MaxDimLevel = data.MaxDimLevel; } else { mydev.Action = 1; } device_tab[deviceId] = mydev; } else { mydev = {MaxDimLevel: null, Action: 1, graph: null, Selector: null}; } device_tab[deviceId] = mydev; break; case "Off": rbl = 0; var mydev = device_tab[deviceId]; if (mydev) { if (dimmable) { mydev.Action = 0; mydev.MaxDimLevel = data.MaxDimLevel; } else { mydev.Action = 1; } device_tab[deviceId] = mydev; } else { mydev = {MaxDimLevel: null, Action: 1, graph: null, Selector: null}; } device_tab[deviceId] = mydev; break; case "Open": rbl = 1; var mydev = device_tab[deviceId]; if (mydev) { mydev.Action = 2; device_tab[deviceId] = mydev; } else { mydev = {MaxDimLevel: null, Action: 2, graph: null, Selector: null}; } device_tab[deviceId] = mydev; break; case "Closed": rbl = 0; var mydev = device_tab[deviceId]; if (mydev) { mydev.Action = 2; device_tab[deviceId] = mydev; } else { mydev = {MaxDimLevel: null, Action: 2, graph: null, Selector: null}; } device_tab[deviceId] = mydev; break; case "Panic": rbl = 1; var mydev = device_tab[deviceId]; if (mydev) { mydev.Action = 3; device_tab[deviceId] = mydev; } else { mydev = {MaxDimLevel: null, Action: 3, graph: null, Selector: null}; } device_tab[deviceId] = mydev; break; case "Normal": rbl = 0; var mydev = device_tab[deviceId]; if (mydev) { mydev.Action = 3; device_tab[deviceId] = mydev; } else { mydev = {MaxDimLevel: null, Action: 3, graph: null, Selector: null}; } device_tab[deviceId] = mydev; break; default: rbl = data.Status; break; } return rbl; } function DevSwitch(data) { var status = 0; switch (data.Status) { case 'On': case 'Open': case 'Panic': case 'All On': status = 1; break; case 'Normal': case 'Off': case 'Closed': case 'All Off': status = 0; break; default: status = 0; break; } //Protected device var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null}; if (! mydev && (data.Protected === 'true')) { mydev.Protected = 1; } device_tab[data.idx] = mydev; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSwitch", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSwitch", "room": "Switches"}; room_tab.Switches=1; } params = []; params.push({"key": "Status", "value": status.toString()}); if(data.Energy) { params.push({"key": "Energy", "value": data.Energy}); } //TODO key Energy myfeed.params = params; return (myfeed); } function DevMultiSwitch(data) { var ptrn4 = /[\s]+|/; var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf(); var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMultiSwitch", "room": "Switches"}; //Protected device var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null}; if (! mydev && (data.Protected === 'true')) { mydev.Protected = 1; } device_tab[data.idx] = mydev; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMultiSwitch", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMultiSwitch", "room": "Switches"}; room_tab.Switches=1; } var params = []; params.push({"key": "LastRun", "value": dt}); var status; //console.log("L:"+data.Level); var dataLevelNames; if (isBase64(data.LevelNames)) { dataLevelNames = Buffer.from(data.LevelNames, 'base64').toString("ascii");; // Ta-da } else { dataLevelNames=data.LevelNames; } var res = dataLevelNames.split('|').join(','); if(data.LevelOffHidden) { res = res.replace ("Off,",""); } var ret = dataLevelNames.split('|'); var mydev = {MaxDimLevel: null, Action: null, graph: null, Selector: ret}; //console.log(mydev); device_tab[data.idx] = mydev; var lvl = Math.round((data.Level) / 10); //console.log(lvl); params.push({"key": "Value", "value": ret[lvl].toString()}); params.push({"key": "Choices", "value": res}); myfeed.params = params; return (myfeed); }; function DevPush(data) { var status = 0; switch (data.SwitchType) { case 'Push On Button': status = 1; break; case 'Push Off Button': status = 0; break; default: status = 0; break; } //Protected device var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null}; if (! mydev && (data.Protected === 'true')) { mydev.Protected = 1; } device_tab[data.idx] = mydev; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSwitch", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSwitch", "room": "Switches"}; room_tab.Switches=1; } params = []; //params.push({"key": "Status", "value": status.toString()}); params.push({"key": "pulseable", "value": "1"}); myfeed.params = params; return (myfeed); } function DevRGBLight(data) { var status = 0; status = devSt(data); switch (data.SwitchType) { case 'Push On Button': status = 1; break; case 'Push Off Button': status = 0; break; case 'On/Off': if (data.Status == 'On') {status=1}; if (data.Status == 'Off') {status=0}; default: status = 0; break; } //Protected device var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null}; if (! mydev && (data.Protected === 'true')) { mydev.Protected = 1; } device_tab[data.idx] = mydev; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevRGBLight", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevRGBLight", "room": "Switches"}; room_tab.Switches=1; } if (data.Status.match(/Set Level/) || (data.HaveDimmer === true) || (data.HaveDimmer === 'true')) { var mydev = {MaxDimLevel: null, Action: null, graph: null}; if (device_tab[data.idx]) { mydev = device_tab[data.idx]; } mydev.MaxDimLevel = data.MaxDimLevel; mydev.Action = 0; device_tab[data.idx] = mydev; params = []; if (data.Status == 'Off') { params.push({"key": "Status", "value": "0"}); } else { params.push({"key": "Status", "value": "1"});//hyp: set level only when on } params.push({"key": "dimmable", "value": "1"}); if ((data.Level==0)&&(data.LevelInt>0)) { params.push({"key": "Level", "value": data.LevelInt.toString()}); } else { params.push({"key": "Level", "value": data.Level.toString()}); } if(data.Energy) { params.push({"key": "Energy", "value": data.Energy}); } //TODO whitechannel //TODO color //TODO Energy value unit myfeed.params = params; } else { params = []; if (data.HaveDimmer === true) { params.push({"key": "Status", "value": status}); } params.push({"key": "dimmable", "value": "1"}); myfeed.params = params; } return (myfeed); }; function DevDimmer(data) { var status = 0; var myfeed = ''; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { myfeed = {"id": data.idx, "name": data.Name, "type": "DevDimmer", "room": domo_room_tab[data.PlanIDs[0]]}; } else { myfeed = {"id": data.idx, "name": data.Name, "type": "DevDimmer", "room": "Switches"}; room_tab.Switches=1; } //Protected device var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null}; if (! mydev && (data.Protected === 'true')) { mydev.Protected = 1; } device_tab[data.idx] = mydev; status = devSt(data); if (data.Status.match(/Set Level/)) { status = 1; } var mydev = {MaxDimLevel: null, Action: null, graph: null}; if (device_tab[data.idx]) { mydev = device_tab[data.idx]; } mydev.MaxDimLevel = data.MaxDimLevel; mydev.Action = 0; //console.log(mydev); device_tab[data.idx] = mydev; params = []; params.push({"key": "Status", "value": status.toString()}); if(status == 0) { params.push({"key": "Level", "value": "0"}); } else { params.push({"key": "Level", "value": data.Level.toString()}); } if(data.Energy) { params.push({"key": "Energy", "value": data.Energy}); } myfeed.params = params; return (myfeed); }; function DevShutterInverted(data) { var status = 0; status = devSt(data); //Protected device var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null}; if (! mydev && (data.Protected === 'true')) { mydev.Protected = 1; } device_tab[data.idx] = mydev; var lvl = 0; var stoppable = 0; var mydev = {MaxDimLevel: null, Action: null, graph: null}; if (device_tab[data.idx]) { mydev = device_tab[data.idx]; } mydev.Action = 5; mydev.MaxDimLevel = data.MaxDimLevel; device_tab[data.idx] = mydev; //console.log(data.Status+" "+data.Level); if (data.Status === 'Open') { lvl = 100; status = 1; } else if (data.Status.match(/Set Level/) || (data.HaveDimmer === 'true') || (data.HaveDimmer === true)) { lvl = data.Level; //console.log(data.status+" "+lvl); if (lvl > 0) { status = 1 } else { status = 0 } ; } else { lvl = data.Level || 0; status = 0; } ; //console.log(data.idx+" "+status+" "+lvl); if ((data.SwitchType === 'Venetian Blinds EU') || (data.SwitchType === 'Venetian Blinds US') || (data.SwitchType === 'RollerTrol, Hasta new')) { stoppable = 1; } var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": "Switches"}; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": "Switches"}; room_tab.Switches=1; } //console.log(data.idx+" "+status+" "+lvl); params = []; params.push({"key": "Level", "value": lvl.toString()}); params.push({"key": "stopable", "value": stoppable.toString()}); params.push({"key": "pulsable", "value": "1"}); myfeed.params = params; //console.log(params); return (myfeed); }; function DevShutter(data) { var status = 0; status = devSt(data); //Protected device var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null}; if (! mydev && (data.Protected === 'true')) { mydev.Protected = 1; } device_tab[data.idx] = mydev; var lvl = 0; var stoppable = 0; var mydev = {MaxDimLevel: null, Action: null, graph: null}; if (device_tab[data.idx]) { mydev = device_tab[data.idx]; } mydev.Action = 6; mydev.MaxDimLevel = data.MaxDimLevel; device_tab[data.idx] = mydev; //console.log(data.Status+" "+data.Level); //console.log(data.idx+" "+data.Status+" "+status+" "+lvl); if (data.Status == 'Open') { //console.log("Open"+data.idx+" "+data.Status+" "+status+" "+lvl); lvl = 100; status = 1; } else if (data.Status == 'Closed') { lvl = 0; status = 0; //console.log("aClosed "+data.idx+" "+data.Status+" "+status+" "+lvl); } else if (data.Status.match(/Set Level/) || (data.HaveDimmer === 'true')|| (data.HaveDimmer === true) ) { lvl = data.Level; //console.log(data.status+" "+lvl); if (lvl > 0) { status = 1 } else { status = 0 } //console.log("mixed: "+data.idx+" "+data.Status+" "+status+" "+lvl); } //console.log(data.idx+" "+status+" "+lvl); if ((data.SwitchType === 'Venetian Blinds EU') || (data.SwitchType === 'Venetian Blinds US') || (data.SwitchType === 'RollerTrol, Hasta new')|| (data.SwitchType === 'Blinds')) { stoppable = 1; } var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": "Switches"}; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevShutter", "room": "Switches"}; room_tab.Switches=1; } params = []; //params.push({"key": "Status", "value": status}); if (data.Status === 'Closed') { params.push({"key": "Level", "value": 0}); } else if (data.Status === 'Open') { params.push({"key": "Level", "value": 100}); } else { params.push({"key": "Level", "value": lvl.toString()}); } //console.log(data.idx+" "+data.Status+" "+status+" "+lvl); params.push({"key": "stopable", "value": stoppable.toString()}); params.push({"key": "pulsable", "value": "0"}); myfeed.params = params; return (myfeed); } function DevMotion(data) { var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf(); var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMotion", "room": "Switches"}; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMotion", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevMotion", "room": "Switches"}; room_tab.Switches=1; } params = []; var value = devSt(data); //console.log(data.Status+" "+value); params.push({"key": "Armable", "value": "0"}); params.push({"key": "ackable", "value": "0"}); params.push({"key": "Armed", "value": "1"}); params.push({"key": "Tripped", "value": value.toString()}); params.push({"key": "lasttrip", "value": dt.toString()}); myfeed.params = params; return (myfeed); } function DevDoor(data) {//TODO var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf(); var myfeed = {"id": data.idx, "name": data.Name, "type": "DevDoor", "room": "Switches"}; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevDoor", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevDoor", "room": "Switches"}; room_tab.Switches=1; } params = []; var value = devSt(data); params.push({"key": "armable", "value": "0"}); params.push({"key": "Ackable", "value": "0"}); params.push({"key": "Armed", "value": "1"}); params.push({"key": "Tripped", "value": value.toString()}); params.push({"key": "lasttrip", "value": dt.toString()}); myfeed.params = params; return (myfeed); } function DevLock(data) { var status = 0; switch (data.Status) { case 'Locked': status = 1; break; case 'Unlocked': status = 0; break; default: status = 0; break; } //Protected device var mydev = device_tab[data.idx]||{MaxDimLevel: null, Action: null, graph: null, Selector: null, Protected: null}; if (! mydev && (data.Protected === 'true')) { mydev.Protected = 1; } device_tab[data.idx] = mydev; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevLock", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevLock", "room": "Switches"}; room_tab.Switches=1; } params = []; params.push({"key": "Status", "value": status.toString()}); myfeed.params = params; return (myfeed);} function DevSmoke(data) { var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf(); var ackable = 0; if (data.Type == 'Security') { ackable = 1; } var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSmoke", "room": "Switches"}; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSmoke", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevSmoke", "room": "Switches"}; room_tab.Switches=1; } params = []; var value = devSt(data); params.push({"key": "Armable", "value": "0"}); params.push({"key": "ackable", "value": ackable.toString()}); params.push({"key": "Armed", "value": "1"}); params.push({"key": "Tripped", "value": value.toString()}); params.push({"key": "lasttrip", "value": dt.toString()}); myfeed.params = params; return (myfeed); } function DevFlood(data) { var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf(); var ackable = 0; var myfeed = {"id": data.idx, "name": data.Name, "type": "DevFlood", "room": "Switches"}; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevFlood", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevFlood", "room": "Switches"}; room_tab.Switches=1; } params = []; var value = devSt(data); params.push({"key": "armable", "value": "0"}); params.push({"key": "Ackable", "value": ackable.toString()}); params.push({"key": "Armed", "value": "1"}); params.push({"key": "Tripped", "value": value.toString()}); params.push({"key": "lasttrip", "value": dt.toString()}); myfeed.params = params; return (myfeed); } function DevCO2(data) { var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf(); var ackable = 0; var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2", "room": "Switches"}; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2", "room": "Switches"}; room_tab.Switches=1; } params = []; var value = devSt(data); params.push({"key": "armable", "value": "0"}); params.push({"key": "Ackable", "value": ackable.toString()}); params.push({"key": "Armed", "value": "1"}); params.push({"key": "Tripped", "value": value.toString()}); params.push({"key": "lasttrip", "value": dt.toString()}); myfeed.params = params; return (myfeed); } function DevCO2Alert(data) { var dt = moment(data.LastUpdate, 'YYYY-MM-DD HH:mm:ss').valueOf(); var ackable = 0; var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2Alert", "room": "Switches"}; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2Alert", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevCO2Alert", "room": "Switches"}; room_tab.Switches=1; } params = []; var value = devSt(data); params.push({"key": "armable", "value": "0"}); params.push({"key": "ackable", "value": ackable.toString()}); params.push({"key": "Armed", "value": "1"}); params.push({"key": "Tripped", "value": value.toString()}); params.push({"key": "lasttrip", "value": dt.toString()}); myfeed.params = params; return (myfeed); } function DevGenericSensor(data) { if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": "Utility"}; room_tab.Utility=1; } if (data.Status) { params = []; params.push({"key": "Value", "value": data.Status.toString()}); myfeed.params = params; } else if (data.Data) { params = []; //console.log(data;) params.push({"key": "Value", "value": data.Data.toString()}); myfeed.params = params; } return (myfeed); } function DevGenericSensorT(data) { var ptrn = /(-?[0-9]+(?:\.[0-9]+)?) ?(.+)/; var res = data.Data.match(ptrn).slice(1); var value = res[0]; var suffix = res[1]; var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": "Utility"}; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": "Utility"}; room_tab.Utility=1; } params = []; params.push({"key": "Value", "value": value.toString(), "unit": suffix.toString(), "graphable": "true"}); myfeed.params = params; return (myfeed); } function DevElectricity(data) { room_tab.Utility = 1; var ptrn1 = /(\d+) Watt/; var ptrn2 = /([0-9]+(?:\.[0-9]+)?) Watt/; var ptrn3 = /[\s,]+/; var ptrn4 = /([0-9]+(?:\.[0-9]+)?) kWh/; var myfeed; var params = []; var combo = []; if (data.UsageDeliv) { //Energy pannel //develectricity Usage/CounterToday var myfeed1 = {"id": data.idx + "_L1", "name": data.Name, "type": "DevElectricity", "room": "Utility"}; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed1 = {"id": data.idx + "_L1", "name": data.Name, "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed1 = {"id": data.idx + "_L1", "name": data.Name, "type": "DevElectricity", "room": "Utility"}; room_tab.Utility=1; } var params = [] var res = ptrn2.exec(data.Usage); var usage = 0; if (res != null) { usage = Math.ceil(Number(res[1])); } var res = ptrn4.exec(data.CounterToday); var usageToday = 0; if (res != null) { usageToday = Math.ceil(Number(res[1])); } params.push({"key": "Watts", "value": usage, "unit": "W"}); params.push({"key": "ConsoTotal", "value": Math.ceil(usageToday), "unit": "kWh", "graphable": "true"}); myfeed1.params = params; combo.push(myfeed1); //develectricity UsageDeliv/CounterDelivToday var params = []; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed2 = {"id": data.idx + "_L2", "name": data.Name + "Deliv", "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed2 = {"id": data.idx + "_L2", "name": data.Name + "Deliv", "type": "DevElectricity", "room": "Utility"}; room_tab.Utility=1; } var res = ptrn2.exec(data.UsageDeliv); var usagedeliv = 0; if (res != null) { usagedeliv = Math.ceil(Number(res[1])); } var res = ptrn4.exec(data.CounterDelivToday); var usageDelivToday = 0; if (res != null) { usageDelivToday = Math.ceil(Number(res[1])); } params.push({"key": "Watts", "value": usagedeliv, "unit": "W"}); params.push({"key": "ConsoTotal", "value": Math.ceil(usageDelivToday), "unit": "kWh", "graphable": "true"}); myfeed2.params = params; combo.push(myfeed2); //devgeneric for Counter/CounterDeliv var params = []; var myfeed3 = { "id": data.idx + "_L3", "name": data.Name + " CounterToday", "type": "DevGenericSensor", "room": "Utility" }; CounterToday = Math.ceil(data.Counter); params.push({"key": "Value", "value": CounterToday, "unit": "kWh", "graphable": "true"}); myfeed3.params = params; combo.push(myfeed3); var params = []; var myfeed4 = { "id": data.idx + "_L4", "name": data.Name + " CounterDelivToday", "type": "DevGenericSensor", "room": "Utility" }; var res = ptrn4.exec(data.CounterDelivToday); var CounterDelivToday = 0; CounterDelivToday = Math.ceil(data.CounterDeliv); params.push({"key": "Value", "value": CounterDelivToday, "unit": "kWh", "graphable": "false"}); myfeed4.params = params; combo.push(myfeed4); return (combo); } else { if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { myfeed = {"id": data.idx, "name": data.Name, "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]}; } else { myfeed = {"id": data.idx, "name": data.Name, "type": "DevElectricity", "room": "Utility"}; room_tab.Utility=1; } if (data.Usage) { var res = ptrn2.exec(data.Usage); var usage = 0; if (res != null) { usage = res[1] } if (!usage) { usage = 0; } params.push({"key": "Watts", "value": usage, "unit": "W"}); if (data.Data) { var res = ptrn4.exec(data.Data); var total = 0; if (res != null) { total = Math.ceil(Number(res[1])); } params.push({"key": "ConsoTotal", "value": total.toString(), "unit": "kWh", "graphable": "true"}); } } else if (data.CounterToday) { var res = ptrn4.exec(data.CounterToday); var usage = 0; if (res != null) { usage = res[1] } if (!usage) { usage = 0; } params.push({"key": "Watts", "value": usage, "unit": "kWh"}); if (data.Counter) { var res = ptrn4.exec(data.Counter); var total = 0; if (res != null) { total = Math.ceil(Number(res[1])); } params.push({"key": "ConsoTotal", "value": total.toString(), "unit": "kWh", "graphable": "true"}); } } else { var res = ptrn2.exec(data.Data); var total = 0; if (res != null) { total = Math.ceil(Number(res[1])); } params.push({"key": "Watts", "value": total.toString(), "unit": "W", "graphable": "true"}); } myfeed.params = params; return (myfeed); } } function DevElectricityMultiple(data) { var ptrn1 = /(\d+) Watt/; var ptrn2 = /([0-9]+(?:\.[0-9]+)?)/; var ptrn3 = /[\s,]+/; var combo = []; var res = data.Data.split(ptrn3); var usage = 0; if (res != null) { //L1 usage = res[0]; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed1 = {"id": data.idx+ "_L1", "name": data.Name, "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed1 = {"id": data.idx+ "_L1", "name": data.Name, "type": "DevElectricity", "room": "Utility"}; room_tab.Utility=1; } var params1 = []; params1.push({"key": "Watts", "value": usage.toString(), "unit": "W","graphable": "true"}); myfeed1.params = params1; combo.push(myfeed1); //L2 usage = res[2]; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed2 = {"id": data.idx+ "_L2", "name": data.Name, "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed2 = {"id": data.idx+ "_L2", "name": data.Name, "type": "DevElectricity", "room": "Utility"}; room_tab.Utility=1; } var params2 = []; params2.push({"key": "Watts", "value": usage.toString(),"unit": "W","graphable": "true"}); myfeed2.params = params2; combo.push(myfeed2); //L3 usage = res[4]; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed3 = {"id": data.idx+ "_L3", "name": data.Name, "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed3 = {"id": data.idx+ "_L3", "name": data.Name, "type": "DevElectricity", "room": "Utility"}; room_tab.Utility=1; } var params3 = []; params3.push({"key": "Watts", "value": usage.toString(),"unit": "W", "graphable": "true"}); myfeed3.params = params3; combo.push(myfeed3); } return (combo); } function DevCounterIncremental(data) { var ptrn1 = /(\d+) Watt/; var ptrn2 = /([0-9]+(?:\.[0-9]+)?) /; var ptrn3 = /[\s,]+/; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevElectricity", "room": "Utility"}; room_tab.Utility=1; } var params = []; var unit; switch (data.SwitchTypeVal) { case '0': //energy unit = 'kWh'; break; case '1': //gas unit = 'm3'; break; case '2': //water unit = 'm3'; break; case '3': //counter free unit = data.ValueUnits.toString(); break; case '4': //energy generated unit = 'kWh'; break; default: break; } if (data.Counter) { var res = ptrn2.exec(data.CounterToday); var usage = 0; if (res != null) { usage = Math.ceil(Number(res[1])); } if (!usage) { usage = 0; } res = Math.ceil(usage); //console.log("T1 "+data.CounterToday); params.push({"key": "Watts", "value": usage, "unit": unit}); if (data.Counter) { var res = ptrn2.exec(data.Counter); //console.log(res[1]); var total = 0; if (res != null) { total = Math.ceil(Number(res[1])); } //console.log("T2"+total); params.push({"key": "ConsoTotal", "value": total.toString(), "unit": unit, "graphable": "true"}); } } else { var res = ptrn2.exec(data.Data); var total = 0; if (res != null) { total = Math.ceil(Number(res[1])); } //console.log(total); params.push({"key": "Watts", "value": total.toString(), "unit": unit, "graphable": "true"}); } myfeed.params = params; return (myfeed); } function DevGas(data) { var ptrn1 = /([0-9]+(?:\.[0-9]+)?) m3/; var ptrn2 = /([0-9]+(?:\.[0-9]+)?)/; var ptrn3 = /[\s,]+/; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevElectricity", "room": "Utility"}; room_tab.Utility=1; } var params = []; if (data.CounterToday) { var res = ptrn1.exec(data.CounterToday); var usage = 0; if (res != null) { usage = res[1] } if (!usage) { usage = 0; } params.push({"key": "Watts", "value": usage.toString(), "unit": "m3", "graphable": "true"}); } if (data.Data) { var res = ptrn2.exec(data.Counter); var total = 0; if (res != null) { total = Math.ceil(res[1]); } params.push({"key": "ConsoTotal", "value": total.toString(), "unit": "m3", "graphable": "true"}); } myfeed.params = params; return (myfeed); } function DevWater(data) { var ptrn1 = /(\d+) Liter/; var ptrn2 = /([0-9]+(?:\.[0-9]+)?) Liter/; var ptrn2b = /([0-9]+(?:\.[0-9]+)?) m3/; var ptrn3 = /[\s,]+/; var combo = []; var usage_l = 0; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevElectricity", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevElectricity", "room": "Utility"}; room_tab.Utility=1; } var params = []; if (data.CounterToday) { var res = ptrn2.exec(data.CounterToday); var usage = 0; if (res != null) { usage = Number(res[1]); usage_l = Number(res[1]); } if (!usage) { usage = 0; } params.push({"key": "Watts", "value": usage.toString(), "unit": "l", "graphable": "false"}); } if (data.Counter) { var res = ptrn2b.exec(data.Counter); var total = 0; if (res != null) { total = Number(res[1]); } params.push({"key": "ConsoTotal", "value": total.toString(), "unit": "m3", "graphable": "true"}); } //console.log(myfeed); myfeed.params = params; //combo.push(myfeed); /*var myfeed = {"id": data.idx+"_1", "name": data.Name, "type": "DevGenericSensor", "room": "Utility"}; params=[]; params.push({"key": "Value", "value": usage_l, "unit": "L", "graphable": "true"}); myfeed.params=params; combo.push(myfeed);*/ return (myfeed); } function DevFlow(data) { var ptrn1 = /(\d+) Liter/; var ptrn2 = /([0-9]+(?:\.[0-9]+)?) Liter/; var ptrn2b = /([0-9]+(?:\.[0-9]+)?) m3/; var ptrn3 = /[\s,]+/; var ptrn4 = /([0-9]+(?:\.[0-9]+)?) l\/min/; var combo = []; var usage_l = 0; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevGenericSensor", "room": "Utility"}; room_tab.Utility=1; } var params = []; var res = ptrn4.exec(data.Data); //console.log(res[1]); var total = 0; if (res != null) { total = Number(res[1]); } params.push({"key": "Value", "value": total.toString(), "unit": "l/s", "graphable": true}); myfeed.params = params; return (myfeed); } function DevTH(data) { var status = 0; switch (data.Type) { case 'Temp': if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevTemperature", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevTemperature", "room": "Temp"}; room_tab.Temp=1; } params = []; params.push({"key": "Value", "value": data.Temp, "unit": "°"+tempmode,"graphable": "true"}); myfeed.params = params; return (myfeed); case 'Humidity': if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevHygrometry", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevHygrometry", "room": "Temp"}; room_tab.Temp=1; } params = []; params.push({"key": "Value", "value": data.Humidity, "unit": "%", "graphable": "true"}); myfeed.params = params; return (myfeed); case 'Temp + Humidity': if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevTempHygro", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevTempHygro", "room": "Temp"}; room_tab.Temp=1; } var params = []; params.push({"key": "Hygro", "value": data.Humidity, "unit": "%", "graphable": "true"}); params.push({"key": "Temp", "value": data.Temp, "unit": "°"+tempmode, "graphable": "true"}); myfeed.params = params; return (myfeed); case 'Temp + Humidity + Baro': var combo = []; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevTempHygro", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevTempHygro", "room": "Temp"}; room_tab.Temp=1; } var params = []; params.push({"key": "Hygro", "value": data.Humidity, "unit": "%", "graphable": "true"}); params.push({"key": "Temp", "value": data.Temp, "unit": "°"+tempmode, "graphable": "true"}); myfeed.params = params; combo.push(myfeed); if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx+ "_1", "name": data.Name, "type": "DevPressure", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx+ "_1", "name": data.Name, "type": "DevPressure", "room": "Weather"}; room_tab.Weather=1; } params = []; params.push({"key": "Value", "value": data.Barometer, "unit": "mbar", "graphable": "true"}); myfeed.params = params; combo.push(myfeed); return (combo); return (combo); default: logger.info("General: should not happen"); break; } } function DevPressure(data) { room_tab.Weather = 1; if (typeof data.PlanIDs !== 'undefined' && data.PlanIDs[0] !== null && data.PlanIDs[0] > 0) { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevPressure", "room": domo_room_tab[data.PlanIDs[0]]}; } else { var myfeed = {"id": data.idx, "name": data.Name, "type": "DevPressure", "room": "Weather"}; room_tab.Weather=1; } params = []; if (data.SubType === "Pressure") { if (data.Pressure < 800) { var mb = data.Pressure * 1000; params.push({"key": "Value", "value": mb, "unit": "mbar", "graphable": "true"}); } else {