-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (147 loc) · 4.43 KB
/
Copy pathindex.js
File metadata and controls
157 lines (147 loc) · 4.43 KB
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
require('clarify');
require('trace');
const Gpio = require('pigpio').Gpio;
const readFileSync = require('fs').readFileSync;
const writeFileSync = require('fs').writeFileSync;
const join = require('path').join;
const { spawn } = require('child_process');
const pify = require('util').promisify;
const rimraf = pify(require('rimraf'));
const promiseRetry = require('promise-retry');
const exitHook = require('exit-hook');
const errorled = new Gpio(20, { mode: Gpio.OUTPUT });
const statusled = new Gpio(21, { mode: Gpio.OUTPUT });
// init leds
statusled.pwmWrite(0);
errorled.pwmWrite(0);
exitHook(() => {
statusled.pwmWrite(0);
errorled.pwmWrite(255);
});
const usbPath = '/media/pi/PISTICK';
const logError = (error) => {
try {
writeFileSync(join(usbPath, 'errors.txt'), error, { flag: 'a' });
console.error(error);
} catch (e) {
console.error(`Failed to write out error to drive: ${e}`);
writeFileSync(join('/home/pi', 'errors.txt'), error, { flag: 'a' });
}
};
process.on('uncaughtException', (error) => {
setLightState(errorled, 'slowFlash');
setLightState(statusled, 'off');
logError(error);
});
const currentInterval = {};
const setLightState = (led, state) => {
const flash = (speed) => {
let dutyCycle = 0;
if (currentInterval[led]) clearInterval(currentInterval[led]);
currentInterval[led] = setInterval(() => {
led.pwmWrite(dutyCycle);
if (dutyCycle === 0) {
dutyCycle = 255;
} else {
dutyCycle = 0;
}
}, speed);
};
switch (state) {
case 'slowFlash':
flash(500);
break;
case 'fastFlash':
flash(200);
break;
case 'on':
if (currentInterval[led]) clearInterval(currentInterval[led]);
led.pwmWrite(255);
break;
case 'off':
if (currentInterval[led]) clearInterval(currentInterval[led]);
led.pwmWrite(0);
break;
default:
}
};
let feedPrinter;
setLightState(statusled, 'slowFlash');
let prom = Promise.resolve();
const settings = JSON.parse(readFileSync(join(usbPath, 'settings.json')));
if (settings.wifi) {
console.log('Connecting to wifi');
prom = promiseRetry((retry) => {
return new Promise((resolve, reject) => {
const nmcli = spawn('nmcli', ['-w', '90', 'device', 'wifi', 'connect', settings.wifi.ssid, 'password', settings.wifi.password]);
nmcli.stdout.on('data', (data) => {
console.log(`${data}`);
});
let errMsg = '';
nmcli.stderr.on('data', (data) => {
errMsg += data;
});
nmcli.on('close', (code) => {
if (code > 0) return reject(new Error(`wifi error ${code}: ${errMsg}`));
resolve();
});
}).catch((e) => retry(e));
});
}
prom.then((stuff) => {
console.log('Updating');
setLightState(statusled, 'fastFlash');
// root uid update only works on plain install
// delete hack to make update work
return rimraf(join('node_modules', 'feed-printer'))
.then(() => rimraf(join('package-lock.json')))
.then(() => {
return new Promise(function (resolve, reject) {
const npmu = spawn('npm', ['--unsafe-perm', 'install']);
npmu.stdout.on('data', (data) => {
console.log(`${data}`);
});
let errMsg = '';
npmu.stderr.on('data', (data) => {
errMsg += data;
});
npmu.on('close', (code) => {
if (code > 0) return reject(new Error(`Self update failed with code ${code}: ${errMsg}`));
resolve();
});
});
});
}).then(() => {
feedPrinter = require('feed-printer');
}).then(() => {
setLightState(statusled, 'on');
console.log('Starting');
// start feed-printer
let retries = 1;
let execTime = Date.now();
const keepItFed = () => {
feedPrinter.saveEmitter.on('saving', setLightState.bind(null, errorled, 'on'));
feedPrinter.saveEmitter.on('saved', setLightState.bind(null, errorled, 'off'));
if (retries && retries < 500) {
feedPrinter.start(join(usbPath, 'articlesdb.json'), settings.printer)
.catch((error) => {
if (execTime - Date.now() < 500) {
retries = null;
} else {
execTime = Date.now();
retries += 1;
}
logError(error);
keepItFed();
});
} else {
setLightState(errorled, 'slowFlash');
setLightState(statusled, 'off');
}
};
keepItFed();
}).catch((error) => {
setLightState(errorled, 'slowFlash');
setLightState(statusled, 'off');
logError(error);
});