-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkers.js
162 lines (146 loc) · 3.29 KB
/
workers.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
const path = require('path')
const chalk = require('chalk')
const draftlog = require('draftlog').into(console)
const SerialPort = require('serialport')
const Bot = require('./Bot')
const Printer = require('./Printer')
const PrinterWorker = require('./PrinterWorker')
const sleep = ms => new Promise(res => setTimeout(res, ms))
const PRINTER_5 = {
debug: false,
name: 'PRINTER 5',
port: {serialNumber: 'CZPX2617X003XK24982'},
params: {
temperatureExtruder: 205,
temperatureBed: 50,
babyHeight: '0.55',
printOffset: 0
}
}
const PRINTER_4 = {
debug: false,
name: 'PRINTER 4',
port: {serialNumber: 'CZPX2617X003XK25033'},
params: {
temperatureExtruder: 205,
temperatureBed: 50,
babyHeight: '0.32',
printOffset: 0
}
}
const PRINTER_3 = { //is at home
debug: false,
name: 'PRINTER 3',
port: {serialNumber: 'CZPX2617X003XK250xx'},
params: {
temperatureExtruder: 205,
temperatureBed: 50,
babyHeight: '0.55',
printOffset: 0
}
}
const PRINTER_2 = {
debug: false,
name: 'PRINTER 2',
port: {serialNumber: 'CZPX2617X003XK25035'},
params: {
temperatureExtruder: 205,
temperatureBed: 50,
babyHeight: '0.55',
printOffset: 0
}
}
const PRINTER_1 = {
debug: false,
name: 'PRINTER 1',
port: {serialNumber: 'CZPX2617X003XK24826'},
params: {
temperatureExtruder: 205,
temperatureBed: 50,
babyHeight: '0.55',
printOffset: 0
}
}
const PRINTER_JOHN = {
debug: false,
name: 'PRINTER 6',
port: {serialNumber: 'CZPX1517X003XK17121'},
params: {
temperatureExtruder: 200,
temperatureBed: 60,
babyHeight: '0.35',
printOffset: 0
}
}
const PRINTERS = [
PRINTER_1,
// PRINTER_2,
// PRINTER_3,
// PRINTER_4,
// PRINTER_5,
]
async function main() {
let workersPromises = []
for (let printer of PRINTERS) {
workersPromises.push(launchWorker(printer))
}
await Promise.all(workersPromises)
}
async function launchWorker(opts) {
// Wait for connection
let printer = null
while(true) {
while (true) {
try {
await sleep(2000);
printer = new Printer(opts)
await printer.connect()
await printer.ready()
break
} catch (e) {
console.log(e)
throw e
}
}
while (true) {
try {
console.log("Here")
await printer.waitForButtonPress()
let printerWorker = new PrinterWorker(printer, opts)
await printerWorker.start()
await printerWorker.run()
} catch (e) {
if (e == 'Connection opening') {
console.log(chalk.bgRed.white(` ${opts.name} Disconnected. Re-start a porra toda `))
printerWorker.log('Disconnected')
Bot.run(opts.name, true)
while(1) {
await sleep(100000)
}
throw e
}
Bot.run(opts.name)
console.log(chalk.bgRed.white(` ${opts.name} Failed part. Waiting for human `))
}
}
}
}
async function getPorts() {
let ports = await SerialPort.list()
console.log(ports)
}
;(async () => {
try {
await main()
} catch (e) {
console.log()
console.log(chalk.red('failed'))
console.error(e)
process.exit(1)
}
})()
process.on('unhandledRejection', async (e) => {
console.error('Unhandled Rejection')
console.error(e.stack)
process.exit(1)
})