forked from anthonywebb/node-cbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgate.js
executable file
·191 lines (161 loc) · 5.24 KB
/
cgate.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
var net = require('net');
var carrier = require('carrier');
var control = {};
var events = {};
var statuses = {};
exports.init = function(){
// TELNET SESSION TO CONTROL
control = net.createConnection(CONFIG.cgate.contolport,CONFIG.cgate.host);
control.on('error', function(error){
console.log('cgate control socket error: ' + error);
});
control.on('end', function(){
console.log('cgate control socket terminated');
});
control.on('close', function(){
console.log('cgate control socket closed');
});
control.on('timeout', function(){
console.log('cgate control socket timed out');
});
carrier.carry(control, function(line) {
pushRealtime('controlStream',line);
});
// TELNET CHANNEL TO STATUS UPDATES
events = net.createConnection(CONFIG.cgate.eventport,CONFIG.cgate.host);
events.on('error', function(error){
console.log('cgate events socket error: ' + error);
});
events.on('end', function(){
console.log('cgate events socket terminated');
});
events.on('close', function(){
console.log('cgate events socket closed');
});
events.on('timeout', function(){
console.log('cgate events socket timed out');
});
carrier.carry(events, function(line) {
pushRealtime('eventStream',line);
});
// TELNET CHANNEL TO STATUS UPDATES
statuses = net.createConnection(CONFIG.cgate.statusport,CONFIG.cgate.host);
statuses.on('error', function(error){
console.log('cgate statuses socket error: ' + error);
});
statuses.on('end', function(){
console.log('cgate statuses socket terminated');
});
statuses.on('close', function(){
console.log('cgate statuses socket closed');
});
statuses.on('timeout', function(){
console.log('cgate statuses socket timed out');
});
carrier.carry(statuses, function(line) {
pushRealtime('statusStream',line);
});
// every time that a message arrives, we need to send it out the realtime websocket
function pushRealtime(type, message) {
console.log(type+' : '+message);
// every message, before being sent out needs to be parsed to create a nice object that can be consumed
var parsedMessage = parseMessage(message,type);
IO.emit(type, parsedMessage);
}
// periodically list the levels of all devices to make sure they are in sync
setTimeout(syncLevels, 5000);
// repeat every 20 mins
setInterval(syncLevels, 1200000)
return module.exports;
}
exports.write = function(msg){
if(msg){
control.write(msg);
}
}
exports.cmdString = function(device,command,level,delay) {
var message = '';
if(command=='on') {
message = 'ON //'+CONFIG.cgate.cbusname+'/'+CONFIG.cgate.network+'/'+CONFIG.cgate.application+'/'+device+'\n';
}
else if (command=='off') {
message = 'OFF //'+CONFIG.cgate.cbusname+'/'+CONFIG.cgate.network+'/'+CONFIG.cgate.application+'/'+device+'\n';
}
else if (command=='ramp') {
if (level <= 100) {
if (delay) {
message = 'RAMP //'+CONFIG.cgate.cbusname+'/'+CONFIG.cgate.network+'/'+CONFIG.cgate.application+'/'+device+' '+level+'% '+delay+'\n';
} else {
message = 'RAMP //'+CONFIG.cgate.cbusname+'/'+CONFIG.cgate.network+'/'+CONFIG.cgate.application+'/'+device+' '+level+'%\n';
}
}
}
return message;
}
function humanLevelValue(level) {
// convert levels from 0-255 to 0-100
var temp = Math.round((level/255)*100)
if(temp > 100){
temp = 100;
}
else if(temp < 0){
temp = 0;
}
return temp;
}
function syncLevels(){
console.log('cgate syncing levels');
var msg = message = 'GET //'+CONFIG.cgate.cbusname+'/'+CONFIG.cgate.network+'/'+CONFIG.cgate.application+'/* level\n';
control.write(msg);
}
////////////////////////
// MESSAGE PROCESSING
////////////////////////
function parseMessage(data,type) {
console.log(data);
var packet = {raw:data};
packet.type = 'unknown';
packet.source = 'cbus';
var array = data.match(/\b[\S]+\b/g);
// is this a lighting packet?
if (array[0]=='lighting') {
packet.type = 'lighting';
packet.action = array[1];
// last element of arr2 is the group
temp = array[2].match(/\d+/g);
packet.group = temp[3];
var parseunit = array[3];
var parseoid = array[4];
if (packet.action == 'ramp') {
packet.level = humanLevelValue(array[3]);
packet.time = array[4];
parseunit = array[5];
parseoid = array[6];
} else if (packet.action == 'on') {
packet.level = 100;
} else if (packet.action == 'off') {
packet.level = 0;
}
temp = parseunit.split('=');
packet.sourceunit = temp[1];
temp = parseoid.split('=');
packet.oid = temp[1];
}
// are we getting group level report?
if (array[0].substring(0, 3) == '300') {
var temp = array[array.length-1].split('=');
if(temp[0] == 'level') {
packet.type = 'info';
packet.level = humanLevelValue(temp[1]);
var ind = (array.length == 3 ? 1 : 0);
var temp2 = array[ind].match(/\d+/g);
packet.group = temp2[temp2.length-1];
}
}
console.log(packet);
// are there custom things we want to do when this event occurs? ONLY do this for the status stream
if(type=='statusStream'||packet.type=='info'){
COMMON.processMessage(packet);
}
return packet;
}