-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.js
executable file
·232 lines (196 loc) · 7.94 KB
/
common.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var moment = require('moment');
var timez = require('moment-timezone');
var _ = require('underscore');
var suncalc = require('suncalc');
var request = require('request');
var timers = {};
////////////////////////////
// MESSAGE/RULES PROCESSING
////////////////////////////
exports.processMessage = function(packet){
// process rules that are contingent on msg events and update the DB model
// just ignore packets of devices that are not in our DB
if(DB.devices[packet.group]){
var oldLevel = DB.devices[packet.group].level;
updateDeviceData(packet.group,packet.level);
// info packets come from a bootstrap operation, we dont want to process rules for them
if(packet.type!='info'){
processRules(packet.group,DB.devices[packet.group]);
}
// TODO: not sure how to accomplish this without knowing that the status event was driven by a button press (is sourceunit == 1 what I am after?)
// kill any timers that might be running for this group if we got a manual state change
//if (packet.level > 0 && packet.sourceunit > 1) {
// killTimer(packet.group);
//}
// only notify the UI of stuff that is not hidden and it is a new level
if(DB.devices[packet.group].visible && oldLevel!=packet.level){
var sendMsg = {type:"update_status",group:packet.group,level:parseInt(packet.level),name:DB.devices[packet.group].name,location:DB.devices[packet.group].location};
console.log('sending');
console.log(sendMsg);
var realtimepacket = sendMsg;
IO.emit('statusStream', realtimepacket);
}
}
}
exports.doCommands = function(cmdArray,level) {
console.log(cmdArray);
processCommands(cmdArray,level);
}
function updateDeviceData(group, level){
// record the timestamp of this level change and update the current level
console.log('updating data for group '+group);
DB.devices[group].lastchange = moment().unix();
DB.devices[group].level = level;
}
function processCommands(cmdArray,level) {
// how many commands are we looping over?
var i = 0;
// define the delayed loop function
function delayedLoop() {
var waitTillNextCommand = 0;
var command = cmdArray[i];
if(command.type=='lighting') {
processLightingCommand(command,level);
}
else if(command.type == 'delay') {
waitTillNextCommand = command.delay;
}
else if(command.type == 'url') {
processUrlCommand(command);
}
else if(command.type == 'email') {
// TODO: email someone
}
else if(command.type == 'sms') {
// TODO: send sms
}
else if(command.type == 'rule') {
// TODO: enable/disable a rule
}
else if(command.type == 'task') {
// TODO: enable/disable a task
}
else if(command.type == 'scene') {
// TODO: trigger a scene
}
else {
console.log('Did not recognize command:');
console.log(command);
}
// if the end of the array has been reached, stop
if(++i == cmdArray.length) {
return;
}
// recursively call the delayed loop function with a delay
setTimeout(delayedLoop, 1000 * waitTillNextCommand);
}
// kick off these commands one at a time
delayedLoop();
}
function processRules(id,device) {
// first off we need to extract some values so we can process the expression
var dd = timez().tz(CONFIG.location.timezone);
var times = suncalc.getTimes(dd,CONFIG.location.latitude,CONFIG.location.longitude);
// variables we can use in our expressions
var time = timez().tz(CONFIG.location.timezone).format('HH:mm'); // the current time
var sunset = timez(times.sunset).tz(CONFIG.location.timezone).format('HH:mm'); // sun starts to set
var dusk = timez(times.dusk).tz(CONFIG.location.timezone).format('HH:mm'); // sun has fully set and it is starting to get dark
var sunrise = timez(times.sunrise).tz(CONFIG.location.timezone).format('HH:mm'); // sun has started to rise
var dawn = timez(times.dawn).tz(CONFIG.location.timezone).format('HH:mm'); // sun has not risen but it is starting to get light
var group = id; // group address of the group that triggered the rules engine
var level = device.level; // this is the level of the group that triggered the rules engine, the rules say this is 0-100
console.log('---------------START PROCESSING RULES---------------');
console.log('rule vars: ' + time + ' ' + sunset + ' ' + dusk + ' ' + dawn + ' ' + sunrise + ' ' + group + ' ' + level);
_.each(DB.rules,function(rule,ind){
// only process enabled rules
if(rule.enabled){
if(eval(rule.expression)) {
console.log('----------- BINGO --------------')
console.log(rule);
var therules = rule.commands;
processCommands(therules,level);
}
}
if(DB.rules.length==ind+1){
console.log('---------------DONE PROCESSING RULES---------------');
}
})
}
function processUrlCommand(command) {
request(command.url, function (error, response, body) {
if (error) {
console.log('got back an error when calling '+command.url+': '+error.message);
} else if (response.statusCode != 200) {
console.log('got back an error when calling '+command.url+': '+response.statusCode);
}
});
}
function processLightingCommand(command,level) {
// only need to adjust the actual lighting level (which we will cast to 0-100) if it is not at the level the scene says it needs to be
var changeto = parseInt(command.level);
var cmdtext = 'ramp';
if(changeto!=DB.devices[command.group].level){
// they might have a command passed from a rule, and want to use the level from the group that triggered the rule (useful for syncing)
if(command.level == 'level') {
changeto = level;
}
if(changeto==100){
cmdtext = 'on';
}
else if (changeto==0) {
cmdtext = 'off';
}
// if they want to fade up slowly
if(command.delay) {
cmdtext = 'ramp';
}
sendLightingCommand(command.group,cmdtext,changeto,command.delay,DB.devices[command.group].vendor);
}
// TIMERS SUPPORT
if(command.timeout > 0 && command.level > 0){
// if there is already a timer running, lets clear it and start it up again
killTimer(command.group);
console.log('TIMER: will turn off group '+command.group+' in '+command.timeout+' seconds')
timers[command.group] = setTimeout(function(){
sendLightingCommand(command.group,'off',0,0,DB.devices[command.group].vendor);
}, command.timeout*1000);
}
function sendLightingCommand(device,command,level,delay,vendor){
if(vendor=='cbus'){
var cmd = CBUS.cmdString(device,command,level,delay);
console.log('DEBUG!!!!');
console.log(cmd);
CBUS.write(cmd);
}
// TODO: integration with other vendors here?
}
}
function killTimer(group) {
if(timers[group]) {
console.log('Killing TIMER: '+group);
clearTimeout(timers[group]);
}
}
////////////////////////////
// OTHER METHODS
////////////////////////////
// we store devices funky natively for speed reasons, this converts to an array that devs will like better
exports.deviceObjToArray = function(devices){
//console.log(devices);
var groups = _.keys(devices);
var returnval = [];
_.each(groups,function(group){
var obj = {group:group};
obj.level = parseInt(devices[group].level);
obj.name = devices[group].name;
obj.location = devices[group].location;
obj.lastchange = devices[group].lastchange;
obj.type = devices[group].type;
obj.vendor = devices[group].vendor;
// only pop it on the list if it is visible
if(devices[group].visible){
returnval.push(obj);
}
});
return returnval;
}