-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (81 loc) · 3.06 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*** Mathematica2 Z-Way HA module *******************************************
Version: 0.0.4
(c) 2016 / 2021
-----------------------------------------------------------------------------
Author: Pieter E. Zanstra, mod. by M.Pruefer
Description:
This module allows the user to create a multilevel sensor, which value is derived
from one or two user selectable sensors, and up to two user specified fixed values
The user can supply any valid mathematical expression to combine these inputs.
******************************************************************************/
// ----------------------------------------------------------------------------
// --- Class definition, inheritance and setup
// ----------------------------------------------------------------------------
function Mathematica2(id, controller) {
// Call superconstructor first (AutomationModule)
Mathematica2.super_.call(this, id, controller);
}
inherits(Mathematica2, AutomationModule);
_module = Mathematica2;
// ----------------------------------------------------------------------------
// --- Module instance initialized
// ----------------------------------------------------------------------------
Mathematica2.prototype.init = function (config) {
Mathematica2.super_.prototype.init.call(this, config);
var self = this, icon = "";
this.vDev = self.controller.devices.create({
deviceId : "Mathematica2_" + this.id,
defaults : {
deviceType : "sensorMultilevel",
metrics : {
probeTitle : self.config.Title,
icon: icon // here to allow changing icon to custom one
}
},
overlay : {
metrics : {
scaleTitle : self.config.scaleTitle,
title : self.config.Title
}
},
moduleId : this.id
});
// Following code is a replacement for the 'timed' call to fetch equation. It should fix
// the problem of not initialised variables at start up.
this.controller.devices.on(self.config.sensor1, 'change:metrics:level', function() {
self.fetchEquation(self);
});
this.controller.devices.on(self.config.sensor2, 'change:metrics:level', function() {
self.fetchEquation(self);
});
};
Mathematica2.prototype.stop = function () {
Mathematica2.super_.prototype.stop.call(this);
if (this.timer) {
clearInterval(this.timer);
}
if (this.vDev) {
this.controller.devices.remove(this.vDev.id);
this.vDev = null;
}
};
// ----------------------------------------------------------------------------
// --- Module methods
// ----------------------------------------------------------------------------
Mathematica2.prototype.fetchEquation = function (instance) {
var self = instance,
result = 0;
var calculation = self.config.formula;
var metric1 = "metrics:" + self.config.metric1;
var metric2 = "metrics:" + self.config.metric2;
var a = controller.devices.get(self.config.sensor1).get(metric1);
try {
var b = controller.devices.get(self.config.sensor2).get(metric2);
}
catch(err){
var b = 0;
}
result = eval(calculation);
self.vDev.set("metrics:level", result);
self.vDev.set("metrics:icon", "/ZAutomation/api/v1/load/modulemedia/Mathematica2/icon.png");
};