-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraspi-slave.js
186 lines (156 loc) · 5.25 KB
/
raspi-slave.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const net = require('net');
const clientsOnTelnet = [];
const Hardware = require("./hardware.js"),
hardware = new Hardware();
function slavesExecute(action,args) {
//broadcast()
slaves.forEach(slave=>{slave.send(JSON.stringify([action,args]))});
(hardware[action])(args);
}
net.createServer(function (socket) {
socket.name = socket.remoteAddress + ":" + socket.remotePort
clientsOnTelnet.push(socket);
socket.write("Welcome " + socket.name + "\r\n");
broadcast(socket.name + " joined.", socket);
socket.on('data', function (data) {
const keyToFun = {
"u": "update",
"r": "restart",
"k": "kill",
"o": "led1_switch",
"p": "led2_switch",
"w": "forward",
"s": "backwards",
" ": "stop",
"a": "left",
"d": "right"
}
const action = keyToFun[data.toString().trim()[0]];
if (action==undefined){
broadcast(socket.name + "> " + data + " <- unknown command");
} else {
broadcast(socket.name + "> " + action, socket);
slavesExecute(action,null);
}
});
socket.on('end', function () {
clientsOnTelnet.splice(clientsOnTelnet.indexOf(socket), 1);
broadcast(socket.name + " left.");
});
}).listen(5000);
console.log("Telnet server running at port 5000, v2\n");
const express = require('express')
const app = express()
const expressWs = require('express-ws')(app);
//const aWss = expressWs.getWss('/socket');
app.use(express.static(__dirname + '/public'));
const clientsOnWS = [];
app.ws('/socket', function(ws, req) {
broadcast("client (web browser) connected on web socket.");
clientsOnWS.push(ws);
ws.on('message', function(msg) {
broadcast("message received from client (web browser): "+msg);
const cmd = JSON.parse(msg);
const method = cmd[0];
const arg = cmd[1];
slavesExecute(method,arg);
});
ws.on('close', function() {
//ws.send(msg);
clientsOnWS.splice(clientsOnWS.indexOf(ws), 1);
broadcast("client connection closed.");
});
});
const slaves = [];
app.ws('/slaveSocket', function(ws, req) {
slaves.push(ws);
broadcast("Slave has connected.");
ws.on('message', function(msg) {
broadcast("Slave WS> Message from slave: "+msg);
});
ws.on('close', function() {
slaves.splice(slaves.indexOf(ws), 1);
//ws.send(msg);
broadcast("Slave disconnected: "+ws);
});
});
const port = process.env.PORT || 2000;
app.listen(port, function () {
console.log('Web app listening on port '+port+'. http://127.0.0.1:'+port+'/ !')
})
var uplinkConnection = null;
const config = require("./config.js");
if (config.remoteMasterHostAndPort != null) {
const remoteHostAndPort = config.remoteMasterHostAndPort;
const WebSocketClient = require('websocket').client;
const client = new WebSocketClient();
client.on('connectFailed', function(error) {
broadcast('WSC Connect Error: ' + error.toString());
broadcastObject({statusUpdate:{uplink:"connectFailed: "+error.toString()}});
scheduleWSCReconnect();
});
client.on('connect', function(connection) {
broadcast('WSC WebSocket Client Connected');
broadcastObject({statusUpdate:{uplink:"connected"}});
connection.on('error', function(error) {
broadcast("WSC Connection Error: " + error.toString());
broadcastObject({statusUpdate:{uplink:"error: "+error.toString()}});
scheduleWSCReconnect();
});
connection.on('close', function() {
broadcast('WSC Connection Closed');
broadcastObject({statusUpdate:{uplink:"closed"}});
uplinkConnection = null;
scheduleWSCReconnect();
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
broadcast("WSC Received: '" + message.utf8Data + "'");
const cmd = JSON.parse(message.utf8Data);
const method = cmd[0];
const arg = cmd[1];
hardware[method](arg);
} else {
broadcast("WSC Unknown message type: "+message.type);
broadcast("WSC Full message dump: "+JSON.stringify(message));
}
});
});
function wscReconnect() {
const socketUrl = 'ws://'+remoteHostAndPort+'/slaveSocket';
broadcast("WSC Connecting to remote master web socket: "+socketUrl+" ...");
client.connect(socketUrl);
}
var wscReconnectTimeout;
function scheduleWSCReconnect(){
broadcast("WSC Reconnect attempt in 5 secs...");
clearTimeout(wscReconnectTimeout)
wscReconnectTimeout=setTimeout(wscReconnect,5000);
}
wscReconnect();
}
function broadcast(message, sender) {
process.stdout.write(message+"\n");
clientsOnTelnet.forEach(function (client) {
// Don't want to send it to sender
if (client !== sender) {
client.write(message+"\r\n");
}
});
clientsOnWS.forEach(function (client) {
if (client !== sender) {
client.send(message);
}
});
if (uplinkConnection !== sender && uplinkConnection!=null) {
uplinkConnection.sendUTF(message);
}
}
function broadcastObject(ob, sender) {
broadcast("X"+JSON.stringify(ob), sender);
}
//const channelClients
//function broadcastToSubscribed(channelName,frame) {
//}
hardware.on('message',broadcast);
hardware.on('statusUpdated',status=>{broadcastObject({statusUpdate:{slave:status}});})