-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappold.js
More file actions
139 lines (112 loc) · 3.6 KB
/
appold.js
File metadata and controls
139 lines (112 loc) · 3.6 KB
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
const net = require('net');
const EverSocket = require('eversocket').EverSocket;
const WebSocket = require('ws')
const ReconnectingWebSocket = require('reconnecting-websocket');
const url = 'ws://192.168.0.40:8000/ws'
// contruct holder for mod time state
const active = {
mod: 0,
}
// contruct connection options for xtra beacon
const options = {
moxaIpAddress: '192.168.0.180',
moxaPort: 23,
}
// construct websocket stream options
const suboptions = {
"topic": "subscribe",
//"data": ['run.scoreboard', 'run.timer']
// "data": ['run.scoreboard']
"data": ['run.timer']
}
// proxy to monitor the time state and send once on state change to enable disable the beacon
const modProxy = new Proxy(active, {
set: function (target, key, value){
console.log(`${key} set from ${active.mod} to ${value}`);
if ((active.mod==0) && (value==1)){
console.log('turning on')
sendnewData('#0:2\\MOD1')
}
if ((active.mod==1) && (value==0)){
console.log('turning off')
sendnewData('#0:2\\MOD0')
}
target[key] = value;
return true;
},
});
// reconnecting websocket
//rws = new ReconnectingWebSocket(`ws://192.168.0.40:8000/ws`);
rws = new ReconnectingWebSocket(url, undefined, {
WebSocket,
maxRetries: 0,
})
rws.addEventListener('open', () => {
console.log('Connected')
this.isConnected = true
//this.subscribe()
rws.send(JSON.stringify(suboptions))
//rws.send(JSON.stringify({ topic: 'run', data: {test: 'here'}}));
})
rws.addEventListener('close', () => {
console.log('Close')
this.isConnected = false
})
rws.addEventListener('error', (err) => {
console.log('Error', err)
})
rws.addEventListener('message', (message) => {
console.log("incoming");
const { topic, data } = JSON.parse(message.data)
console.log(data.payload.elapsed_under_racing.remaining)
const currentTime = (data.payload.elapsed_under_racing.remaining)
if (currentTime >= "60000" ){
modProxy.mod=0;
// sendnewData('#0:2\\MOD0')
}
if (currentTime <= "59999" ){
modProxy.mod=1;
// sendnewData('#0:2\\MOD1')
}
})
// send the command through to xtra beacon with params
function sendnewData(cmd){
dhSocket.write((cmd) + '\r' + '\n', function(err) {
if (err) {
return console.log('Error on write: ', err.message)
}
console.log('message written' + (cmd))
})
// Open errors will be emitted as an error event
dhSocket.on('error', function(err) {
console.log('Error: ', err.message)
})
}
// Connect to Xtra Beacon
const dhSocket = new EverSocket({
reconnectWait: 1000, // wait 100ms after close event before reconnecting
timeout: 5000, // set the idle timeout to 100ms
reconnectOnTimeout: false // reconnect if the connection is idle
});
dhSocket.setEncoding('utf8');
dhSocket.on('reconnect', function() {
console.log('The Beacon socket reconnected following a close or timeout event');
});
dhSocket.on('error', function(err) {
console.log('The Beacon socket returned an error');
console.log(err);
});
// dhSocket.on('close', function() {
// console.log('Moxa connection "close" received');
// process.exit(1);
// });
dhSocket.on('connect', function() {
console.log('Connected to Beacon');
sendnewData('#0:2\\MOD0'); // send inital OFF state to xtra beacon need esc chr for M
});
//listen for reply after sending
dhSocket.on('data', function (data) {
// var buff = Buffer.from(data).toString(); // get buffer and convert to string
console.log('reply: ', data)
})
dhSocket.connect(options.moxaPort, options.moxaIpAddress);