forked from hassoncs/CAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensorEventTriggerDecider.js
68 lines (55 loc) · 1.84 KB
/
sensorEventTriggerDecider.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
/*
sensorTriggerDecider
*/
var _ = require('underscore'),
util = require('util'),
sensorWrapper = require('./sensorWrapper'),
logger = require('./logger'),
hueWrapper = require('./hueWrapper'),
Facade = require('./facade'),
Events = require('./events');
(function(context) {
var SENSOR_TRIGGERS = [
//new Trigger("C2", "turnOnBottomStairLight"),
new Trigger("roomEntranceMotion", Events.TURN_ON_ENTRANCE_LIGHT)
];
function Trigger() {}
Trigger.prototype.fire = function() { /* noop */ };
Trigger.prototype.shouldFire = function() { return false; };
SensorTrigger.prototype = new Trigger();
function SensorTrigger(sensor, eventId) {
Trigger.call(this);
this.sensor = sensor;
this.eventId = eventId;
}
SensorTrigger.prototype.shouldFire = function(query) {
var sensor = query.sensor || "";
var state = query.state;
return (this.sensor == sensor) && (!state && state == "active");
};
SensorTrigger.prototype.fire = function() {
logger.i("Sensor Triggered Event: " + this.eventId);
Facade.handleEvent(this.eventId);
};
function handleRawInput(query) {
logger.i(util.inspect(query));
sensorMatches(query);
}
function sensorMatches(query) {
var triggersToFire = _.filter(SENSOR_TRIGGERS, function(trigger) {
return trigger.shouldFire(query);
});
logger.i("sensorMatches: " + util.inspect(triggersToFire));
_.each(triggersToFire, function(trigger) {
trigger.fire();
});
}
function triggerAction(actionId) {
hueWrapper.turnOnAllLights();
}
function init() {
logger.i("Initializing the triggers!");
}
context.init = init;
context.handleRawInput = handleRawInput;
})(exports);