-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (131 loc) · 3.7 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
146
147
148
149
150
151
152
153
const fs = require("fs")
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const Datastore = require("nedb")
const random = require("random")
// Johnny-Five framework to use Raspberry Pi GPIO in NodeJS
const { RaspiIO } = require("raspi-io")
const five = require("johnny-five")
// Setup Johnny-Five for RPi GPIO access
const board = new five.Board({
io: new RaspiIO(),
})
const SerialPort = require('serialport'),
serialPort = new SerialPort('/dev/ttyUSB0', {
baudRate: 9600,
charset: 0
}),
Printer = require('thermalprinter');
serialPort.on('open',() => {
printer = new Printer(serialPort)
printer.on('ready', () => {
console.log('we are all ready...')
})
})
// Setup NeDB datastore
let db = new Datastore({ filename: 'data/stories.db', autoload: true})
let nbStories = 23
db.count({}, (err, count) => nbStories = count) //count actual number of stories
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
let isPrinting = false
board.on("ready", async () => {
const btn = new five.Button({
pin: "P1-15",
isPullup: true
})
const btnLed = new five.Led("P1-11")
btnLed.blink()
await sleep(3000)
btnLed.stop().off()
btn.on("press", async () => {
if (isPrinting) return
console.log("pressed...")
btnLed.on()
// Pick a random story
let rdNb = Math.ceil(Math.random() * nbStories)
db.findOne({_id: rdNb}, async (err, doc) => {
if (err) return
// Print the story
isPrinting = true
await printStory(doc)
doc.countPrint++
db.update({_id: doc._id}, doc)
btnLed.off()
isPrinting = false;
})
})
board.repl.inject({
counters: () => viewCounters()
})
board.on("exit", () => {
btnLed.off()
console.log("LED off, leaving now...")
})
})
// const pickAStory = async () => {
// let rdNb = Math.ceil(Math.random() * nbStories)
// // let rdNb = random.int((min = 1), (max = nbStories))
// await db.findOne({_id: rdNb}, (err, doc) => {return doc})
// }
const qrcode = async (doc) => {
// let cmd = `qrencode -s 6 -l H -o - "${doc.story.URL}" | lp -o fit-to-page`
let id = (doc._id < 10) ? `0${doc._id}` : doc._id
let cmd = `lp data/qrcodes/story-${id}.png`
const { stdout, stderr } = await exec(cmd);
}
const printStory = async (doc) => {
printer.reset()
let story = doc.story
// fix special characters
story.title = story.title.replaceAll('’','\047')
story.text = story.text.replaceAll('’','\047')
story.text = story.text.replaceAll('“', '\042')
story.text = story.text.replaceAll('”', '\042')
// await printer
// .printLine('')
// .bold(true)
// .center()
// .printLine(story.title)
// .bold(false)
// .printLine(`by ${story.author}`)
// .print()
// await qrcode(doc)
await printer
.printLine('')
.bold(true)
.center()
.printLine(story.title)
.bold(false)
.printLine(`by ${story.author}`)
.left()
.printLine('')
.printLine(`...${story.text}...`)
.printLine('')
.center()
.printLine("<<Scan to read the full story>>")
.printLine('')
.left()
// .printImage(qrcode)
.print(async () => {
await qrcode(doc)
// process.exit();
})
await sleep(20000)
}
// temporary migration
// let { stories } = require('./data/stories.json')
// stories.forEach((story, idx) => {
// db.insert({...{_id: idx+1},...{countPrint: 0},...{story}})
// })
const viewCounters = (isReset = false) => {
db.find({}, (err, docs) => {
docs.forEach(doc => {
console.log(doc._id, doc.countPrint)
if (isReset) {
console.log("resetting counters...")
doc.countPrint = 0;
db.update({_id: doc._id}, doc)
}
})
})
}