-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest-toggle.ts
81 lines (66 loc) · 1.71 KB
/
test-toggle.ts
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
import { KNXClient, KNXClientEvents } from '../src'
if (process.argv.length < 3) {
console.log('usage: %s <ga> <optional: status_ga> to toggle a light on & off',
process.argv[1]);
process.exit(1);
}
const groupAddress = process.argv[2]
let client: KNXClient
async function initClient() {
const interfaces = await KNXClient.discover(1000)
if(interfaces.length === 0) {
console.log('No interfaces found')
return
}
console.log('Discovered interfaces:', interfaces)
const [ip, port] = interfaces[0].split(':')
console.log('Connecting to', ip, port)
client = new KNXClient({
ipAddr: ip,
ipPort: port,
loglevel: 'trace',
suppress_ack_ldatareq: false,
hostProtocol: 'TunnelUDP',
sniffingMode: true,
})
client.on(KNXClientEvents.connected, info => {
// The client is connected
console.log('Connected. On Duty', info)
onConnect()
})
client.Connect()
}
function onConnect() {
console.log('----------')
console.log('Connected!')
console.log('----------')
// Now send off a couple of requests:
console.log('\n\n\n')
console.log('PRESS ANY KEY TO TOGGLE %s AND "q" TO QUIT.', process.argv[2])
console.log('\n\n\n')
let dpVal = false
process.stdin.setRawMode(true)
process.stdin.resume()
process.stdin.on('data', async (data) => {
console.log(JSON.stringify(data))
if (data[0] === 113) {
if (client && client?.isConnected()) {
await client.Disconnect()
client = null
console.log('\n\n\n')
console.log('PRESS ANY KEY TO RECONNECT AND "q" TO QUIT')
return
} else {
process.exit(0)
}
}
if(!client) {
initClient()
return
}
dpVal = !dpVal
console.log('Sending ' + dpVal, 'to', groupAddress)
client.write(groupAddress, dpVal, '1.001')
})
}
initClient()