-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (122 loc) · 3.35 KB
/
index.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
var fs = require('fs');
var express = require('express');
var bodyParser = require('body-parser');
var Gpio = require('onoff').Gpio;
var r1 = new Gpio(20, 'out');
var r2 = new Gpio(21, 'out');
const app = express();
const port = 3000;
var hourHand = 0;
var minHand = 0;
var polarity = false;
var advancing = false;
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/', (req, res) => {
console.log(req.body.txtHours);
console.log(req.body.txtMinutes);
res.send('OK');
hourHand = parseInt(req.body.txtHours);
minHand = parseInt(req.body.txtMinutes);
autoAdvance(true);
});
app.get('/', (req, res) => {
res.send(`<html>
<head>
<title>ClockSignalGenerator</title>
</head>
<body>
<div>${hourHand}:${minHand}</div>
<form action="/" method="POST">
Hours: <input name="txtHours" type="text" />
Minutes: <input name="txtMinutes" type="text" />
<input type="submit" value="Set" />
</form>
</body>
</html>`);
});
app.listen(port, () => console.log('Clock HTTP service running on port ' + port ));
function getLastKnownClockTime() {
try {
return JSON.parse(fs.readFileSync('time.json', 'utf8'));
} catch(e) {
return { hours: 0, minutes: 0 };
}
}
var clockTime = getLastKnownClockTime();
hourHand = clockTime.hours;
minHand = clockTime.minutes;
console.log('time.json reports time as:');
console.log(clockTime);
function autoAdvance(checkOnly) {
advancing = true;
if (!checkOnly) {
advance(false);
}
var currentHour = new Date().getHours();
if (currentHour > 11) {
currentHour = currentHour - 12;
}
console.log("Clock reads: " + hourHand + ":" + minHand + " RT: " + currentHour + ":" + new Date().getMinutes());
if (minHand !== new Date().getMinutes() || currentHour !== hourHand) {
setTimeout(autoAdvance, 600);
} else {
advancing = false;
}
}
function advance(noReset) {
polarity = !polarity;
var value = 0;
if(polarity) {
value = 1;
}
r1.writeSync(value);
r2.writeSync(value);
minHand++;
if (minHand > 59) {
minHand = 0;
hourHand++;
if (hourHand > 11) {
hourHand = 0;
}
}
console.log("Clock advancing: " + hourHand + ":" + minHand);
if (!noReset) {
setTimeout(() => {
// This is a safety check, to turn the power off to the
// clock after sending the signal
r1.writeSync(1 - value);
}, 100);
}
fs.writeFile(
'time.json',
JSON.stringify({ hours: hourHand, minutes: minHand }),
(err) => { if (err) console.error(err); }
);
}
//var currentMinutes = new Date().getMinutes();
const keypress = async () => {
process.stdin.setRawMode(true);
return new Promise(resolve => process.stdin.once('data', data => {
const byteArray = [...data];
if (byteArray.length > 0 && byteArray[0] === 3) {
console.log('^C');
process.exit(1);
}
process.stdin.setRawMode(false);
resolve();
}));
}
;(async () => {
//advance();
await keypress();
//advance();
//await keypress();
//advance();
})().then(process.exit);
autoAdvance(true);
setInterval(() => {
var nowMinutes = new Date().getMinutes();
if (nowMinutes !== minHand && !advancing) {
advance();
}
}, 100);