-
Notifications
You must be signed in to change notification settings - Fork 5
/
myStromBulb.js
70 lines (55 loc) · 1.56 KB
/
myStromBulb.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
module.exports = function(RED) {
function myStromBulb(config) {
RED.nodes.createNode(this, config);
var context = this.context();
var node = this;
this.device = RED.nodes.getNode(config.device);
var helpers = require('../utils/helpers')
var requests = require('../utils/requests')
var deviceHelper = require('../utils/deviceListHelper')
deviceHelper.startDeviceListener(node)
this.DEVICE_TYPE = "bulb"
//EXECUTE REQUEST
this.on("input", function(msg) {
var taskJSON = msg["payload"]
this.status({
fill: "blue",
shape: "ring",
text: "Using json"
});
if (!requests.isValid(taskJSON, this.DEVICE_TYPE)) {
taskJSON = {
"ip": this.device.host,
"mac": this.device.mac,
"request": config.request,
"data": {
"color": config.color,
"ramp": config.ramp
}
}
this.status({
fill: "yellow",
shape: "ring",
text: "Using property"
});
if (!requests.isValid(taskJSON, this.DEVICE_TYPE)) {
node.error("Conversion from property to json failed")
}
}
requests.doAsync(back, this.DEVICE_TYPE, taskJSON, node)
});
return;
//CALLBACK
function back(str) {
if (str["success"] == "false") {
node.error("An error occured while sending")
}
node.send({
payload: str
});
}
//CLOSE
this.on('close', function() {});
}
RED.nodes.registerType("myStrom Bulb", myStromBulb);
};