masterblaster
Version:
Scheduling and componsation system for biodome
55 lines (42 loc) • 1.19 kB
JavaScript
var client = {}
, time = require('./time-utils')
, util = require('util')
, AbstractDeviceEvent = require('./abstract-device-event');
function schedule(device, state) {
var e = new RecurringDeviceEvent();
e.opts.device = device;
e.opts.scheduledState = state;
return e;
}
function fromJSON(json) {
var e = new RecurringDeviceEvent();
e.opts = (typeof json === Object) ? json : JSON.parse(json);
return e;
}
function RecurringDeviceEvent() {
AbstractDeviceEvent.call(this);
}
util.inherits(RecurringDeviceEvent, AbstractDeviceEvent);
var def = RecurringDeviceEvent.prototype;
def.execute = function() {
this.resolveOptions();
var self = this;
this.startTimer = time.repeatTimer(
this.parsed.startSchedule,
function() {
client.setDeviceState(self.opts.device, self.opts.scheduledState);
}
);
return this;
};
def.resolveOptions = function() {
if (this.resolved) return;
// prepare the things!
this.parsed = {
'startSchedule' : time.textToSchedule(this.opts.goTime)
}
this.resolved = true;
};
module.exports = schedule;
module.exports.fromJSON = fromJSON;
module.exports.RecurringDeviceEvent = RecurringDeviceEvent;