-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.js
94 lines (62 loc) · 2.08 KB
/
bridge.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
import serialport from 'serialport'
import readline from '@serialport/parser-readline'
import { Client } from 'node-osc'
import { config } from 'dotenv'
let _PORT
let OSC = {}
let ENV = config({path: 'config.env'}).parsed
async function createOSC( ) {
return new Promise( (resolve,reject) => {
let key = ENV.OSC_ADDRESS + ':' + ENV.OSC_PORT
if (!OSC[key]) {
OSC[key] = new Client(ENV.OSC_ADDRESS,ENV.OSC_PORT)
resolve( OSC[key] )
} else {
console.log(`[lockd] ${key} already exists!`)
reject('already exists')
}
})
}
const run = async e => {
ENV.MODE = ENV.MODE || 'OSC'
ENV.OSC_ADDRESS = ENV.OSC_ADDRESS || '0.0.0.0'
ENV.OSC_PORT = ENV.OSC_PORT || 4444
ENV.MODE = ENV.MODE || 'OSC'
console.log(`[lockd] ✅ opened`)
let list = await serialport.list()
let arduino = list.find( t => (t?.manufacturer||'').toLowerCase().indexOf( 'arduino' ) != -1 )
if (!arduino || !ENV.BOARD_PORT) return console.log(`[lockd] ❌ no BOARD_PORT or default Arduino found`)
await createOSC()
_PORT = new serialport( ENV.BOARD_PORT || arduino.path)
_PORT.on('open', onOpen)
_PORT.on('close', onClose)
_PORT.pipe(new readline({ delimiter: '\r\n' })).on('data', onData)
}
async function onOpen(e) {
console.log(`[lockd] ✅ opened`)
}
async function onClose(e) {
console.log(`[lockd] 🛑 closed`)
}
async function onData(data) {
let bits = data.split(':')
if (bits[0] == 'change') {
let num = parseInt(bits[1])
let val = parseInt(bits[2])
let idx = num%4
let six = num%16
let page = (num-(six))/16
let letter = ['A','B','C','D'][(six-idx)/4]
idx += 1
page += 1
let value = 1 - (val/1024)
let address = `/P${page}/${letter}/${idx}`
console.log(`[lockd] sending: ${address} ${value.toFixed(2)}`)
for (const [key,o] of Object.entries(OSC) ) {
o.send( address, value )
}
} else {
console.log(`[lockd] received: ${data}`)
}
}
run()