-
Notifications
You must be signed in to change notification settings - Fork 14
/
switchHelper.js
61 lines (56 loc) · 3.22 KB
/
switchHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const MQTT = 'mqtt';
const path = require('path');
const settings = require(path.join(__dirname,'settings'));
//LOGGING SETUP AND WRAPPING
//Disable the NEEO library console warning.
const { metaMessage, LOG_TYPE } = require("./metaMessage");
console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
function metaLog(message) {
let initMessage = { component:'switchHelper', type:LOG_TYPE.INFO, content:'', deviceId: null };
let myMessage = {...initMessage, ...message}
return metaMessage (myMessage);
}
class switchHelper {
constructor(deviceId, name, variableListened, evaldo, controller) {
this.name = name;
this.deviceId = deviceId;
this.variableListened = variableListened;
this.evaldo = evaldo;
this.controller = controller;
this.value = false;
var self = this;
this.get = function () {
return self.value;
};
this.update = function (deviceId, theValue) {
return new Promise(function (resolve, reject) {
if (self.value != theValue) {
self.value = theValue;
self.controller.sendComponentUpdate({ uniqueDeviceId: deviceId, component: self.name, value: theValue })
.then((result) => {metaLog({type:LOG_TYPE.VERBOSE, content:"Updates performed, new value : " + theValue + " component " + self.controller.name + "/"+ self.name+"/"+deviceId, deviceId:deviceId})})
.catch((err) => {metaLog({type:LOG_TYPE.ERROR, content:"Error while trying to put the value : " + theValue+ " in this component : " + deviceId + " / " + self.name + " => " + err, deviceId:deviceId}); reject(err); });
}
resolve();
});
};
this.set = function (deviceId, theValue) {
return new Promise(function (resolve, reject) {
if (self.value != theValue) {
self.value = theValue;
self.controller.commandProcessor("{\"topic\":\"" + settings.mqtt_topic + self.controller.name + "/" + deviceId + "/switch/" + self.name + "\",\"message\":\"" + theValue + "\", \"options\":\"{\\\"retain\\\":true}\"}", MQTT, deviceId)
self.controller.sendComponentUpdate({ uniqueDeviceId: deviceId, component: self.name, value: theValue })
.then((result) => {metaLog({type:LOG_TYPE.VERBOSE, content:"Set performed, new value : " + theValue + " component " + self.controller.name + "/"+ self.name+"/"+deviceId, deviceId:deviceId})})
.catch((err) => {metaLog({type:LOG_TYPE.ERROR, content:"Error while trying to put the value : " + theValue+ " in this component : " + deviceId + " / " + self.name + " => " + err, deviceId:deviceId}); reject(err); });
}
controller.vault.writeVariable(variableListened, theValue, deviceId);
controller.evalDo(evaldo, theValue, deviceId)
resolve();
});
};
this.initialise = function (deviceId) {
metaLog({type:LOG_TYPE.VERBOSE, content:"Initialise switchHelper", deviceId:deviceId})
controller.vault.addObserver(self.variableListened, self.update, deviceId, self.name);
}
}
}
exports.switchHelper = switchHelper;