-
Notifications
You must be signed in to change notification settings - Fork 0
/
inky.js
83 lines (62 loc) · 2.18 KB
/
inky.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
const fs = require('fs');
const shortid = require('shortid');
const mqtt = require('mqtt');
const spawn = require('child_process').spawn;
let deviceID;
let screenLocked = false;
try{
deviceID = fs.readFileSync(`${__dirname}/deviceID.txt`).toString('utf8');
} catch(err){
console.log(`'deviceID' not found. Creating new one now`);
deviceID = shortid.generate();
fs.writeFileSync(`${__dirname}/deviceID.txt`, deviceID);
}
console.log('Assigned deviceID:', deviceID);
function outputToEInkDisplay(text){
if(screenLocked){
console.log('Screen already rendering. Message will not be displayed');
return;
}
screenLocked = true;
console.log("Outputting text to display:", text);
const output = spawn('python', [`${__dirname}/output-eink.py`, deviceID, text]);
let timeout;
output.on('close', (exitCode) => {
console.log("output-eink.py exited with code:", exitCode);
screenLocked = false;
clearTimeout(timeout);
});
// Reset screenLocked if not unset after 30 seconds
timeout = setTimeout(function(){
console.log("Screen took too long to render. Resetting lock. Killing child process.");
screenLocked = false;
output.kill();
}, 30000);
}
function handleMessage(topic, message){
console.log('Received msg:', topic, message.toString());
outputToEInkDisplay( message.toString() );
}
const mqttBrokerAddress = "mqtt://iot.eclipse.org";
const deviceMQTTTopic = `/ibm_developer_uk/${deviceID}`;
const teacherMQTTTopic = `/ibm_developer_uk/teacher`;
const mqttClient = mqtt.connect(mqttBrokerAddress);
mqttClient.on('connect', () => {
console.log(`Connected to ${mqttBrokerAddress}`);
mqttClient.subscribe(deviceMQTTTopic, (err) => {
if(err){
console.log(`Could not subscribe to device topic ${deviceMQTTTopic}`);
} else {
console.log(`Subscribed to device topic ${deviceMQTTTopic}`);
}
});
mqttClient.subscribe(teacherMQTTTopic, (err) => {
if(err){
console.log(`Could not subscribe to device topic ${deviceMQTTTopic}`);
} else {
console.log(`Subscribed to device topic ${teacherMQTTTopic}`);
}
});
mqttClient.on('message', handleMessage);
outputToEInkDisplay('Ready!');
});