Newer
Older
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
/*
browser -> filesys
*/
const WebSocketServer = require('ws').Server
const fs = require('fs')
const STATUS_UNKNOWN = 'unknown...'
const STATUS_OPENING = 'opening...'
const STATUS_OPEN = 'open'
const STATUS_CLOSED = 'closed'
const STATUS_ERROR = 'error'
// port to issue on,
let port = '2042'
let deviceName = 'lp0'
const WSS = new WebSocketServer({port: port}, () => {
if(process.send){ // check if we have a parent to send to
process.send({
startup: true,
port: port
})
}
})
let WS = null
WSS.on('connection', (ws) => {
console.log('vfpts websocket connects')
// handles,
WS = ws
// send current status
sendSerialStatus()
// handlers,
ws.onmessage = (msg) => {
if(typeof msg.data === 'string'){
// probably a control object, do
let data = JSON.parse(msg.data)
if(data.type === 'echo'){
sendToBrowser(msg.data)
} else {
console.log("how to handle:", data)
}
} else {
if(Buffer.isBuffer(msg.data)){
if(serport){
serport.write(encode(msg.data, true), 'utf8')
}
}
}
}
ws.onclose = (evt) => {
// shutdown,
console.log('ws closes, pipe exiting')
process.exit()
}
})
// send wrapper
let sendToBrowser = (msg) => {
if (WS) {
WS.send(msg)
return true
} else {
return false
}
}
// device business,
/*
So, right now (tired!) I'm trying to figure out how Neil writes commands to the Modelas... So far:
- neil has 'command' and 'file' types: these look like just writing strings to the device
- the path walks the segments, writes to strings, makes a big blob, sends that to the device server, OK
- 'jog height' is some path-work built into the machine code... should be separate: path should *just* be move segments ...
- serverside device server writes to the device at `dev/name` ... in a medium-odd loop, might take a minute to get this together.
*/
// default: no real way to 'find' without some secondary usb library,
// or: make a call to system lsusb ?
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
fs.open(`/dev/usb/${file}`, 'w', (err, fd) => {
if(err){
console.log(err)
} else {
// integers: 100 count in one mm, thx roland
let hx = 1000
let hz = 1000
let hy = 1000
let cm = `PA;PA;!PZ0,${hz};PU$${hx},${hy};!MC0;\u0004`
console.log('attempt for', cm, 'to')
console.log(fd)
fs.write(fd, cm, (err, bytesWritten, buffer) => {
if(err){
console.log(err)
} else {
console.log('numBytesWritten', bytesWritten)
console.log('buffer', buffer)
}
})
}
})
/*
fs.readdir('/dev/usb', (err, files) => {
if(err){
console.log(err)
} else {
console.log(files)
for(file of files){
if(file === deviceName){
console.log('ok')
// let's write to it...
// first we have to open it...
//
}
}
}
})
*/
}
let sendSerialStatus = () => {
if(serport){
if(serport.opening){
sendToBrowser(JSON.stringify({
type: 'serial status',
status: STATUS_OPENING
}))
} else if (serport.readable){
sendToBrowser(JSON.stringify({
type: 'serial status',
status: STATUS_OPEN
}))
} else {
sendToBrowser(JSON.stringify({
type: 'serial status',
status: STATUS_CLOSED
}))
}
} else {
sendToBrowser(JSON.stringify({
type: 'serial status',
status: STATUS_CLOSED
}))
}
}
findDevice()