-
Notifications
You must be signed in to change notification settings - Fork 4
/
domoticz.js
executable file
·197 lines (178 loc) · 6.08 KB
/
domoticz.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/nodejs
// This Node creates a simple function to publish switch changes to Domoticz.
var mqtt = require('mqtt'),
events = require('events'),
util = require('util');
STATUS = 'domoticz-mqtt-app/connected',
TRACE = true,
HOST = '127.0.0.1',
REQUEST = true,
IDX = [ ]; // Device IDX you want to watch.
// Get Options
var domoticz = function(options) {
events.EventEmitter.call(this); // inherit from EventEmitter
TRACE = options.log;
IDX = options.idx;
STATUS = options.status;
HOST = options.host;
REQUEST = options.request;
this.domoMQTT = this.connect(HOST);
}
util.inherits(domoticz, events.EventEmitter);
// Connnect
domoticz.prototype.connect = function(host) {
var self = this;
var domoMQTT = mqtt.connect('mqtt://' + host);
// Incoming MQTT Message
domoMQTT.on('message', function (topic, message) {
var jsonData = JSON.parse(message)
if (TRACE) { console.log('MQTT IN: ' + message.toString()) };
if (IDX.contains(jsonData.idx)) {
self.mqttData(jsonData);
}
});
// OnConnect
domoMQTT.on('connect', function () {
domoMQTT.publish(STATUS, 'true');
domoMQTT.subscribe('domoticz/out');
if (TRACE) { console.log("Domoticz MQTT: connected") };
var i = 0;
if (REQUEST) {
while (IDX[i]) { // Request Fresh device status on connect.
self.request(IDX[i]);
i++;
}
}
self.emit('connect');
});
// OnError
domoMQTT.on('error', function (error) {
if (TRACE) { console.log("ERROR: " + error.toString()) };
self.emit('error', self.error);
});
// OnExit
process.on( "SIGINT", function() {
domoMQTT.publish(STATUS, 'false');
domoMQTT.end();
});
return domoMQTT;
}
// Callback on matching Device IDX
domoticz.prototype.mqttData = function(data) {
var self = this;
self.data = data;
self.emit('data', self.data);
return self;
}
// Publish Switch Commands (0=Off/255=On/-1=Toggle)
domoticz.prototype.switch = function(id,lvl) {
var self = this;
if ((isNaN(id)) || (isNaN(lvl))) {
self.emit('error','invalid switch input')
return false
};
var cmd = "Set Level";
if (lvl === 255) { cmd = "On" }
else if (lvl === 0) { cmd = "Off" }
else if (lvl < 0) { cmd = "Toggle" }
else if (lvl > 99) { lvl = 100 }
var state = { 'command': 'switchlight', 'idx': id, 'switchcmd': cmd };
if (cmd === 'Set Level') { state['level'] = lvl };
if(TRACE) { console.log('domoticz/in: ' + JSON.stringify(state).toString()) }
self.domoMQTT.publish('domoticz/in', JSON.stringify(state));
return true;
}
// Publish Contact Switch Commands
domoticz.prototype.switchContact = function(id,lvl) {
var self = this;
if ((isNaN(id)) || (isNaN(lvl))) {
self.emit('error','invalid contact switch input')
return false
};
if (lvl) { lvl=1 }
else { lvl=0 };
var state = { 'command': 'udevice', 'idx': id, 'nvalue': lvl };
if(TRACE) { console.log('domoticz/in: ' + JSON.stringify(state).toString()) }
self.domoMQTT.publish('domoticz/in', JSON.stringify(state));
return true;
}
// Publish uDevice Commands
domoticz.prototype.device = function(id,nvalue,svalue) {
var self = this;
if ((isNaN(id)) || (isNaN(nvalue)) || (typeof svalue === 'undefined')) {
self.emit('error','invalid device input')
return false
}
var state = { 'command': 'udevice', 'idx': id, 'nvalue': nvalue, 'svalue': svalue.toString() };
if(TRACE) { console.log('domoticz/in: ' + JSON.stringify(state)) }
self.domoMQTT.publish('domoticz/in', JSON.stringify(state))
return true
}
// Publish Scene Command
domoticz.prototype.scene = function(id,cmd) {
var self = this;
if (isNaN(id)) {
self.emit('error','invalid scene input')
return false
}
if (!cmd) { cmd = 'On' }
var state = { 'command': 'switchscene', 'idx': id, 'switchcmd': cmd };
if(TRACE) { console.log('domoticz/in: ' + JSON.stringify(state)) }
self.domoMQTT.publish('domoticz/in', JSON.stringify(state))
return true
}
// Publish User Variable
domoticz.prototype.uservar = function(id,val) {
var self = this;
if ((isNaN(id)) || (typeof val === 'undefined')) {
self.emit('error','invalid uservar input')
return false
}
var state = { 'command': 'setuservariable', 'idx': id, 'value': val.toString() };
if(TRACE) { console.log('domoticz/in: ' + JSON.stringify(state)) }
self.domoMQTT.publish('domoticz/in', JSON.stringify(state))
return true
}
// Publish Domoticz Notification
domoticz.prototype.notify = function(subject,body,priority,sound) {
var self = this;
if ((typeof subject === 'undefined') || (typeof body === 'undefined')) {
self.emit('error','invalid notification input')
return false
}
if(isNaN(priority)) { priority = 0 }
if(!sound) { sound = "default" }
var state = { 'command': 'sendnotification', 'subject': subject.toString(), 'body': body.toString(), 'priority': priority, 'sound': sound.toString() };
if(TRACE) { console.log('domoticz/in: ' + JSON.stringify(state)) }
self.domoMQTT.publish('domoticz/in', JSON.stringify(state))
return true
}
// Publish Device Request
domoticz.prototype.request = function(id) {
var self = this;
if (isNaN(id)) {
self.emit('error','invalid request input')
return false
}
var state = { 'command': 'getdeviceinfo', 'idx': id };
if(TRACE) { console.log('domoticz/in: ' + JSON.stringify(state)) }
self.domoMQTT.publish('domoticz/in', JSON.stringify(state))
return true
}
// Publish Domoticz Log Entry
domoticz.prototype.log = function(msg) {
var self = this;
if (typeof msg === 'undefined') {
self.emit('error','invalid log input')
return false
}
var state = { 'command': 'addlogmessage', 'message': msg.toString() };
if(TRACE) { console.log('domoticz/in: ' + JSON.stringify(state)) }
self.domoMQTT.publish('domoticz/in', JSON.stringify(state))
return true
}
// Checks if Array contains
Array.prototype.contains = function(element){
return this.indexOf(element) > -1
};
exports.domoticz = domoticz;