-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi-clock.js
186 lines (186 loc) · 6.23 KB
/
pi-clock.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
"use strict";
var SevenSegment = require('ht16k33-sevensegment-display');
var GPIO = require('onoff').Gpio;
var fs = require("fs");
var moment = require('moment');
var LightState;
(function (LightState) {
LightState[LightState["wake"] = 0] = "wake";
LightState[LightState["sleep"] = 1] = "sleep";
LightState[LightState["off"] = 2] = "off";
})(LightState = exports.LightState || (exports.LightState = {}));
var days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
function hoursToMillis(hours) {
return hours * 3600000;
}
function mod(x, y) {
return ((x % y) + y) % y;
}
function getTime(d) {
return d.getHours() + d.getMinutes() / 60 + d.getSeconds() / 3600;
}
function dayOfWeek() {
var now = new Date();
return now.getDay();
}
function DSTmodifier() {
var today = moment(new Date()), tomorrow = moment(new Date(Date.now() + 24 * 3600 * 1000));
if (today.isDST() == tomorrow.isDST()) {
return 0;
}
else if (today.isDST() && !tomorrow.isDST()) {
return 1;
}
else if (!today.isDST() && tomorrow.isDST()) {
return -1;
}
}
var PiClock = (function () {
function PiClock() {
this.wakeTime = 7;
this.firstLightOnTime = 6;
this.lastLightOffTime = 8;
this.sleepLED = new GPIO(17, 'out');
this.wakeLED = new GPIO(22, 'out');
this.display = new SevenSegment(0x70, 1);
this.display.display.setBrightness(1);
}
PiClock.prototype.test = function () {
setInterval(function () {
var state = this.wakeLED.readSync();
this.wakeLED.writeSync(Number(!state));
this.sleepLED.writeSync(Number(!state));
}, 1000);
};
PiClock.prototype.readConfig = function () {
this.config = JSON.parse(fs.readFileSync('./config/config.json', 'utf8'));
this.wakeTime = getTime(new Date(this.config.wakeTime[days[dayOfWeek()]]));
this.firstLightOnTime = this.wakeTime - this.config.before / 60;
this.lastLightOffTime = this.wakeTime + this.config.after / 60;
};
PiClock.prototype.setState = function (lightState) {
this.lightState = lightState;
var _a = this.unpackState(), sleepState = _a[0], wakeState = _a[1];
this.sleepLED.writeSync(sleepState);
this.wakeLED.writeSync(wakeState);
console.log("set state=" + LightState[this.lightState] + " at " + new Date());
};
PiClock.prototype.initialize = function () {
var time = getTime(new Date());
this.readConfig();
var state = LightState.off;
if (time >= this.firstLightOnTime && time < this.wakeTime) {
state = LightState.sleep;
}
else if (time >= this.wakeTime && time < this.lastLightOffTime) {
state = LightState.wake;
}
return state;
};
PiClock.prototype.nextState = function (state) {
switch (state) {
case LightState.wake: return LightState.off;
case LightState.sleep: return LightState.wake;
case LightState.off: return LightState.sleep;
}
};
PiClock.prototype.nextTime = function (stateNext) {
var time = getTime(new Date());
switch (stateNext) {
case LightState.wake:
return mod(this.wakeTime - time, 24);
case LightState.sleep:
return mod(this.firstLightOnTime - time, 24) + DSTmodifier();
case LightState.off:
return mod(this.lastLightOffTime - time, 24);
}
};
PiClock.prototype.updateAndSchedule = function (state) {
var _this = this;
this.setState(state);
var newState = this.nextState(state);
var wait = hoursToMillis(this.nextTime(newState));
if (wait < 0) {
throw Error("wait is negative: " + wait);
}
console.log("wait " + wait / 1000 / 3600 + " hours");
return setTimeout(function () {
try {
_this.eventTimeout = _this.updateAndSchedule(newState);
}
catch (e) {
throw e;
}
}, wait);
};
PiClock.prototype.displaySchedule = function () {
var _this = this;
var now = new Date(), hour = now.getHours(), minute = now.getMinutes();
hour = hour % 12;
if (hour == 0) {
hour = 12;
}
if (hour >= 10) {
this.display.writeDigit(0, Math.floor(hour / 10));
}
else {
this.display.writeDigit(0, null);
}
this.display.writeDigit(1, hour % 10);
this.display.writeDigit(3, Math.floor(minute / 10));
this.display.writeDigit(4, minute % 10);
this.display.setColon(true);
var coeff = 1000 * 60;
var nextMinute = Math.ceil(now.getTime() / coeff) * coeff - now.getTime();
this.displayTimeout = setTimeout(function () {
_this.displayTimeout = _this.displaySchedule();
}, nextMinute);
};
PiClock.prototype.start = function () {
console.log('pi-clock started');
var state = this.initialize();
try {
clearTimeout(this.eventTimeout);
clearTimeout(this.displayTimeout);
}
finally { }
this.displayTimeout = this.displaySchedule();
this.eventTimeout = this.updateAndSchedule(state);
};
PiClock.prototype.unpackState = function () {
switch (this.lightState) {
case LightState.wake:
return [0, 1];
case LightState.sleep:
return [1, 0];
case LightState.off:
return [0, 0];
}
};
PiClock.prototype.dispose = function () {
this.sleepLED.unexport();
this.wakeLED.unexport();
clearTimeout(this.eventTimeout);
clearTimeout(this.displayTimeout);
this.display.clear();
console.log('pi-clock stopped');
};
return PiClock;
}());
exports.PiClock = PiClock;
if (!module.parent) {
var clock_1 = new PiClock();
if (process.argv[2] == undefined) {
clock_1.start();
}
else if (process.argv[2] == "test") {
clock_1.test();
}
else {
console.log("Invalid arg");
}
process.on('SIGINT', function () {
clock_1.dispose();
process.exit();
});
}