-
Notifications
You must be signed in to change notification settings - Fork 1
/
template-node.js
107 lines (93 loc) · 3.52 KB
/
template-node.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
module.exports = function(RED) {
function TemplateNode(config) {
RED.nodes.createNode(this,config);
var node = this;
var sceneMap = {
2: 'up',
1: 'down',
3: 'config'
};
var clickMap = {
7680: 1,
7860: 2,
7920: 3,
7980: 4,
8040: 5,
7800: 1,
7740: 1
};
initConfiguration();
setStatus("grey");
node.on('input', function(msg, send, done) {
try{
send = send || function() { node.send.apply(node,arguments) } //For backwards compatibility with node-red 0.x
if(msg.payload){
if(msg.payload.event){
if(msg.payload.event.scene_id && msg.payload.event.scene_data){
var scene_id = msg.payload.event.scene_id;
var scene_data = msg.payload.event.scene_data;
var button = getButton(scene_id);
var clickCount = getClickCount(scene_data);
if(clickCount && button){
var action = getAction(scene_data);
msg.payload.event.button = button;
msg.payload.event.click_count = clickCount;
msg.payload.event.action = action;
setStatus("green", button, clickCount);
}
}
}
}
send([msg])
if (node.done) {
node.done();
}
}catch(err){
if(done){
node.done(err);
}
else
node.error(err, msg)
}
});
function getButton(scene_id){
return sceneMap[scene_id];
}
function getClickCount(scene_data){
return clickMap[scene_data];
}
function getAction(scene_data){
if(scene_data == 7740){
return "release";
}else if(scene_data == 7800){
return "hold";
}
return "tap";
}
function initConfiguration() {
node.config1 = config.config1 != undefined ? config.config1 : "default_config_1" ;
}
function setStatus(fill, button, clickCount){
if(typeof button == 'undefined' || typeof clickCount == 'undefined'){
node.status({fill:fill,shape:"dot",text: "No scene event yet."});
return;
}
node.status({fill:fill,shape:"dot",text: getStatusString(button, clickCount)});
}
function getStatusString(button, clickCount){
var timesText = clickCount == 1 ? " time":" times";
var status = "" + button + " clicked " + clickCount + timesText + " at " + getPrettyDate(new Date());
return status;
}
function getPrettyDate(date) {
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
hour12: false,
hour: 'numeric',
minute: 'numeric'
});
}
}
RED.nodes.registerType("inovelli-scene-decoder",TemplateNode);
}