-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconditions.js
91 lines (78 loc) · 2.92 KB
/
conditions.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
/*
conditions.js
*/
var _ = require('underscore');
var util = require('util');
var logger = require('./logger');
var actionRunner = require('./actionRunner');
var state = require('./state');
var idUtil = require('./casIdUtil');
var genId = idUtil.genId;
var makeValuesHumanReadable = idUtil.makeValuesHumanReadable;
var SimpleState = {};
SimpleState.Active = genId();
SimpleState.Inactive = genId();
SimpleState.Present = genId();
SimpleState.NotPresent = genId();
makeValuesHumanReadable(SimpleState); // For debugging
var DEFAULT_COOLDOWN_DURATION = 60; /* seconds */
function Condition(thingId, stateId) {
this.thingId = thingId;
this.stateId = stateId;
this.lastFiredTime = 0;
}
Condition.prototype.then = function(actionId) {
this.actionId = actionId;
};
Condition.prototype.isSatisfied = function() {
var curTime = process.hrtime()[0]; // seconds
var passedCooldown = (curTime >= this.lastFiredTime + DEFAULT_COOLDOWN_DURATION);
return passedCooldown;
};
Condition.prototype.runAction = function() {
logger.i("Condition is running action " + this.actionId);
actionRunner.runAction(this.actionId);
this.lastFiredTime = process.hrtime()[0]; // seconds
};
function SimpleStateCondition(thingId, stateId) {
Condition.call(this, thingId, stateId);
}
SimpleStateCondition.prototype = new Condition();
SimpleStateCondition.prototype.isSatisfied = function() {
var superConditionIsSatisfied = Condition.prototype.isSatisfied.call(this);
var currentState = state.getState(this.thingId, "simpleState");
var stateMatches = (currentState == this.stateId.toLowerCase());
return superConditionIsSatisfied && stateMatches;
};
function ComputedStateTrueCondition(computedStateId) {
Condition.call(this, computedStateId);
}
ComputedStateTrueCondition.prototype = new Condition();
ComputedStateTrueCondition.prototype.isSatisfied = function() {
var superConditionIsSatisfied = Condition.prototype.isSatisfied.call(this);
var currentState = state.getState(this.thingId, "computedState");
return superConditionIsSatisfied && !!currentState;
};
function ConditionRegistry() {
this.conditions = [];
}
ConditionRegistry.prototype.registerCondition = function(condition) {
this.conditions.push(condition);
};
ConditionRegistry.prototype.runApplicableConditions = function() {
var satisfiedConditions = _.filter(this.conditions, function(condition) {
return condition.isSatisfied();
});
logger.i(util.format("%d of %d condition(s) satisfied.", satisfiedConditions.length, this.conditions.length));
_.each(satisfiedConditions, function(condition) {
condition.runAction();
});
};
var theConditionRegistry = new ConditionRegistry();
module.exports = conditions = {
SimpleState: SimpleState,
Condition: Condition,
SimpleStateCondition: SimpleStateCondition,
ComputedStateTrueCondition: ComputedStateTrueCondition,
conditionRegistry: theConditionRegistry
};