nep-cli 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.js +834 -834
- package/package.json +16 -16
- package/package-lock.json +0 -188
package/bin/index.js
CHANGED
|
@@ -1,834 +1,834 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
//console.log("You are using nep-cli :)")
|
|
4
|
-
var args = process.argv.splice(process.execArgv.length + 2);
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var zmq = require("zeromq");
|
|
8
|
-
var zmqc = require("zeromq/v5-compat");
|
|
9
|
-
var nep = require('nep-js');
|
|
10
|
-
|
|
11
|
-
// Retrieve the first argument
|
|
12
|
-
var value = args[0];
|
|
13
|
-
var value1 = "";
|
|
14
|
-
var value2 = "";
|
|
15
|
-
var value3 = "";
|
|
16
|
-
|
|
17
|
-
try {
|
|
18
|
-
value1 = args[1];
|
|
19
|
-
value2 = args[2];
|
|
20
|
-
value3 = args[3];
|
|
21
|
-
} catch (error) {
|
|
22
|
-
value1 = "";
|
|
23
|
-
value2 = "";
|
|
24
|
-
value3 = "";
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
var PORT_DISCOVER = 7777
|
|
28
|
-
var PORT_MASTER = 7000
|
|
29
|
-
var PORT_MASTER_INFO = 7010
|
|
30
|
-
|
|
31
|
-
var devices = {};
|
|
32
|
-
var devices_ethernet = {};
|
|
33
|
-
var watch = {};
|
|
34
|
-
var watch_ethernet = {};
|
|
35
|
-
var timerDiscoverWifi = "";
|
|
36
|
-
var timerDiscoverEthernet = "";
|
|
37
|
-
var nep_configuration = { current_port: 10000, IP: "127.0.0.1", brokers: {} }
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
function dicovery_send() {
|
|
41
|
-
|
|
42
|
-
var dgram = require("dgram");
|
|
43
|
-
var devices = [];
|
|
44
|
-
|
|
45
|
-
var ips = nep.getIP();
|
|
46
|
-
var IP_EXTERNAL = ips.wifi;
|
|
47
|
-
var IP_LAN = ips.ethernet;
|
|
48
|
-
|
|
49
|
-
const search = new Buffer([
|
|
50
|
-
'M-SEARCH * HTTP/1.1',
|
|
51
|
-
'HOST: 239.255.255.250:1900',
|
|
52
|
-
'MAN: "ssdp:discover"',
|
|
53
|
-
'MX: 3',
|
|
54
|
-
'ST: upnp:rootdevice'
|
|
55
|
-
].join('\r\n'));
|
|
56
|
-
|
|
57
|
-
const socket = dgram.createSocket('udp4');
|
|
58
|
-
socket.on('listening', () => {
|
|
59
|
-
socket.addMembership('239.255.255.250');
|
|
60
|
-
socket.send(search, 0, search.length, 1900, "239.255.255.250");
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
function discover_lan() {
|
|
65
|
-
socket.send(search, 0, search.length, 1900, "239.255.255.250");
|
|
66
|
-
}
|
|
67
|
-
timerDiscoverEthernet = setInterval(discover_lan, 1000);
|
|
68
|
-
|
|
69
|
-
socket.bind(1900);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
function dicovery_server() {
|
|
76
|
-
|
|
77
|
-
var dgram = require("dgram");
|
|
78
|
-
var node = new nep.Node("nep-cli-ipdiscover");
|
|
79
|
-
var pub = node.new_pub("ipdiscover", "json");
|
|
80
|
-
|
|
81
|
-
var devices = [];
|
|
82
|
-
|
|
83
|
-
var ips = nep.getIP();
|
|
84
|
-
var IP_EXTERNAL = ips.wifi;
|
|
85
|
-
var IP_LAN = ips.ethernet;
|
|
86
|
-
|
|
87
|
-
const socket = dgram.createSocket('udp4');
|
|
88
|
-
socket.on('listening', () => {
|
|
89
|
-
socket.addMembership('239.255.255.250');
|
|
90
|
-
});
|
|
91
|
-
socket.on('message', (message, rinfo) => {
|
|
92
|
-
if (!(devices.includes(rinfo.address))) {
|
|
93
|
-
if (!(rinfo.address === IP_EXTERNAL)) {
|
|
94
|
-
devices.push(rinfo.address)
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
console.log(devices)
|
|
98
|
-
try {
|
|
99
|
-
pub.publish({ "devices": devices })
|
|
100
|
-
} catch (error) {
|
|
101
|
-
console.log("NEP master not avaliable")
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
socket.bind(1900);
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
function discoverable() {
|
|
116
|
-
var ips = nep.getIP();
|
|
117
|
-
var IP_EXTERNAL = ips.wifi;
|
|
118
|
-
var IP_LAN = ips.ethernet;
|
|
119
|
-
var node = new nep.Node("nep-cli-discoverable");
|
|
120
|
-
|
|
121
|
-
if (IP_EXTERNAL == "") {
|
|
122
|
-
console.log("No Wi-fi connection")
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
var pub_list = nep.setHostsPublishers(IP_EXTERNAL, node);
|
|
126
|
-
function discover_wifi() {
|
|
127
|
-
nep.publishDevices(topic_register, pub_list, IP_EXTERNAL)
|
|
128
|
-
}
|
|
129
|
-
timerDiscoverWifi = setInterval(discover_wifi, 1000);
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (IP_LAN == "") {
|
|
134
|
-
console.log("No Ethernet connection")
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
var pub_list_lan = nep.setHostsPublishers(IP_LAN, node);
|
|
138
|
-
function discover_lan() {
|
|
139
|
-
nep.publishDevices(topic_register, pub_list_lan, IP_EXTERNAL)
|
|
140
|
-
}
|
|
141
|
-
timerDiscoverEthernet = setInterval(discover_lan, 1000);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
function getWifi() {
|
|
148
|
-
|
|
149
|
-
var ips = nep.getIP();
|
|
150
|
-
|
|
151
|
-
if (ips.wifi == "") {
|
|
152
|
-
|
|
153
|
-
console.log("No Wi-fi")
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
var node = new nep.Node("nep-cli-wifi");
|
|
158
|
-
var conf = node.direct(ips.wifi, PORT_DISCOVER, "many2one");
|
|
159
|
-
|
|
160
|
-
var callback_remote_sub = function (msg) {
|
|
161
|
-
devices[msg.computer] = { "ip": msg.ip, "avaliable": true }
|
|
162
|
-
watch[msg.computer] = msg.timestamp
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
var watchdevices = function (msg) {
|
|
167
|
-
for (device in watch) {
|
|
168
|
-
var difference = Math.abs(Date.now() - watch[device])
|
|
169
|
-
if (difference > 5 * 1000) {
|
|
170
|
-
devices[device]["avaliable"] = false
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
var timerCleanWifi = setInterval(watchdevices, 1000);
|
|
177
|
-
sub_remote = node.new_sub("master_ex", "json", callback_remote_sub, conf);
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function getEthernet() {
|
|
184
|
-
|
|
185
|
-
var ips = nep.getIP();
|
|
186
|
-
|
|
187
|
-
if (ips.ethernet == "") {
|
|
188
|
-
|
|
189
|
-
console.log("No Ethernet")
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
|
|
194
|
-
var node = new nep.Node("nep-cli-ethernet");
|
|
195
|
-
var conf = node.direct(ips.ethernet, PORT_DISCOVER, "many2one");
|
|
196
|
-
|
|
197
|
-
var callback_remote_sub_et = function (msg) {
|
|
198
|
-
devices_ethernet[msg.computer] = { "ip": msg.ip, "avaliable": true }
|
|
199
|
-
watch_ethernet[msg.computer] = msg.timestamp
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
var watchdevices_et = function (msg) {
|
|
204
|
-
for (device in watch) {
|
|
205
|
-
var difference = Math.abs(Date.now() - watch_ethernet[device])
|
|
206
|
-
if (difference > 5 * 1000) {
|
|
207
|
-
devices_ethernet[device]["avaliable"] = false
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
var timerCleanET = setInterval(watchdevices_et, 1000);
|
|
214
|
-
sub_remote_et = node.new_sub("master_ex", "json", callback_remote_sub_et, conf);
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
if (value == "master") {
|
|
224
|
-
|
|
225
|
-
var node = new nep.Node("nep-cli");
|
|
226
|
-
|
|
227
|
-
console.log("Starting NEP master in terminal")
|
|
228
|
-
|
|
229
|
-
// ----------- Main variables ---------------
|
|
230
|
-
// Topics saved here
|
|
231
|
-
var topic_register = {};
|
|
232
|
-
// Node saved here
|
|
233
|
-
var nodes_register = {};
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
class MasterLocal {
|
|
240
|
-
constructor(IP = '127.0.0.1', port_ = 7000) {
|
|
241
|
-
this.port = String(port_)
|
|
242
|
-
var address = "tcp://" + IP + ":" + this.port
|
|
243
|
-
console.log("New Master in: " + address)
|
|
244
|
-
try {
|
|
245
|
-
async function run() {
|
|
246
|
-
const reply = new zmq.Reply
|
|
247
|
-
await reply.bind(address)
|
|
248
|
-
for await (const [node_request] of reply) {
|
|
249
|
-
await reply.send(processMsg(node_request, nodes_register, topic_register))
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
run()
|
|
253
|
-
} catch (error) {
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
var onRegisteredTopic = function (node_request, topic_register, topic) {
|
|
259
|
-
|
|
260
|
-
var msg = { "state": "failure" }
|
|
261
|
-
|
|
262
|
-
try {
|
|
263
|
-
var port = topic_register[topic]["port"]
|
|
264
|
-
var socket_ = topic_register[topic]["socket"]
|
|
265
|
-
|
|
266
|
-
if ("mode" in topic_register[topic]) {
|
|
267
|
-
var mode = topic_register[topic]["mode"]
|
|
268
|
-
msg = { 'topic': topic, 'port': port, 'mode': mode, 'ip': nep_configuration["IP"], 'socket': socket_, "state": "success" }
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
271
|
-
msg = { 'topic': topic, 'port': port, 'ip': nep_configuration["IP"], 'socket': socket_, "state": "success" }
|
|
272
|
-
}
|
|
273
|
-
updatePID(node_request, topic_register, topic)
|
|
274
|
-
|
|
275
|
-
} catch (error) {
|
|
276
|
-
console.log("Error in topic request")
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
if ("language" in node_request) {
|
|
280
|
-
|
|
281
|
-
if (node_request["language"] == "C++") {
|
|
282
|
-
msg.port = msg.port.toString();
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return msg
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
var onResetTopic = function (node_request) {
|
|
292
|
-
|
|
293
|
-
var topic = node_request["topic"]
|
|
294
|
-
nep_configuration["current_port"] = Math.max(nep_configuration["current_port"], node_request["port"] + 2);
|
|
295
|
-
|
|
296
|
-
if (node_request["socket"] === "publisher" || node_request["socket"] === "subscriber") {
|
|
297
|
-
// Create new broker for many2many communication
|
|
298
|
-
if (node_request["mode"] === "many2many") {
|
|
299
|
-
restartBroker(node_request["topic"], node_request["port"]);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
if ("msg_type" in node_request) {
|
|
303
|
-
topic_register[topic] = { "port": node_request["port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"], "mode": node_request["mode"], "msg_type": node_request["msg_type"] }
|
|
304
|
-
}
|
|
305
|
-
else {
|
|
306
|
-
topic_register[topic] = { "port": node_request["port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"], "mode": node_request["mode"], "msg_type": "json" }
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
topic_register[topic]["nodes"] = [];
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
var onNewTopic = function (node_request, topic_register) {
|
|
315
|
-
|
|
316
|
-
// Asign new ports
|
|
317
|
-
nep_configuration["current_port"] = nep_configuration["current_port"] + 2
|
|
318
|
-
// Topic must be an string
|
|
319
|
-
topic = String(node_request['topic'])
|
|
320
|
-
var msg = {}
|
|
321
|
-
// If publisher or subscriber
|
|
322
|
-
if (node_request["socket"] === "publisher" || node_request["socket"] === "subscriber") {
|
|
323
|
-
// Create new broker for many2many communication
|
|
324
|
-
if (node_request["mode"] === "many2many") {
|
|
325
|
-
createBroker(topic);
|
|
326
|
-
msg = m2mResponse(node_request, topic_register, topic);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
else if (node_request["socket"] === "surveyor" || node_request["socket"] === "respondent") {
|
|
330
|
-
msg = surveyResponse(node_request, topic_register, topic)
|
|
331
|
-
}
|
|
332
|
-
else if (node_request["socket"] === "client" || node_request["socket"] === "server") {
|
|
333
|
-
msg = csResponse(node_request, topic_register, topic)
|
|
334
|
-
}
|
|
335
|
-
// Key for related nodes
|
|
336
|
-
topic_register[topic]["nodes"] = {}
|
|
337
|
-
// Set PID of node
|
|
338
|
-
updatePID(node_request, topic_register, topic);
|
|
339
|
-
|
|
340
|
-
if ("language" in node_request) {
|
|
341
|
-
|
|
342
|
-
if (node_request["language"] == "C++") {
|
|
343
|
-
msg.port = msg.port.toString();
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
return msg
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
var createBroker = function (topic) {
|
|
354
|
-
try {
|
|
355
|
-
var broker = new Broker(nep_configuration["IP"], nep_configuration["current_port"] + 1, nep_configuration["current_port"])
|
|
356
|
-
//var brokersubex = new BrokerBridgeSUBEX(conf["IP"],IP_EXTERNAL, conf["current_port"] , conf["current_port"])
|
|
357
|
-
|
|
358
|
-
// Add broker defined to list of brokers
|
|
359
|
-
nep_configuration["brokers"][topic] = broker
|
|
360
|
-
} catch (error) {
|
|
361
|
-
// console.log("NEP ERROR: ports " + String(nep_configuration["current_port"]) + " and " + String(nep_configuration["current_port"] + 1) + " not avaliable")
|
|
362
|
-
nep_configuration["current_port"] = nep_configuration["current_port"] + 2
|
|
363
|
-
var broker = new nep.Broker(nep_configuration["IP"], nep_configuration["current_port"] + 1, nep_configuration["current_port"])
|
|
364
|
-
// Add broker defined to list of brokers
|
|
365
|
-
nep_configuration["brokers"][topic] = broker
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
var restartBroker = function (topic, port) {
|
|
370
|
-
try {
|
|
371
|
-
var broker = new nep.Broker(nep_configuration["IP"], port + 1, port)
|
|
372
|
-
//var brokersubex = new BrokerBridgeSUBEX(conf["IP"],IP_EXTERNAL, conf["current_port"] , conf["current_port"])
|
|
373
|
-
// Add broker defined to list of brokers
|
|
374
|
-
nep_configuration["brokers"][topic] = broker
|
|
375
|
-
} catch (error) {
|
|
376
|
-
console.log("NEP ERROR: ports " + String(port) + " and " + String(port + 1) + " not avaliable")
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
var m2mResponse = function (node_request, topic_register, topic) {
|
|
381
|
-
if ("msg_type" in node_request) {
|
|
382
|
-
topic_register[topic] = { "port": nep_configuration["current_port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"], "mode": node_request["mode"], "msg_type": node_request["msg_type"] }
|
|
383
|
-
}
|
|
384
|
-
else {
|
|
385
|
-
topic_register[topic] = { "port": nep_configuration["current_port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"], "mode": node_request["mode"], "msg_type": "json" }
|
|
386
|
-
}
|
|
387
|
-
return { 'topic': topic, 'port': nep_configuration["current_port"], 'mode': node_request["mode"], 'ip': nep_configuration["IP"], 'socket': node_request["socket"], "state": "success" }
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
var surveyResponse = function (node_request, topic_register, topic) {
|
|
391
|
-
topic_register[topic] = { "port": nep_configuration["current_port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"] }
|
|
392
|
-
return { 'topic': topic, 'port': nep_configuration["current_port"], 'ip': nep_configuration["IP"], 'socket': node_request["socket"], "state": "success" }
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
var csResponse = function (node_request, topic_register, topic) {
|
|
396
|
-
topic_register[topic] = { "port": nep_configuration["current_port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"], "msg_type": "json" }
|
|
397
|
-
return { 'topic': topic, 'port': nep_configuration["current_port"], 'ip': nep_configuration["IP"], 'socket': node_request["socket"], "state": "success" }
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
var updatePID = function (node_request, topic_register, topic) {
|
|
402
|
-
if ("pid" in node_request) {
|
|
403
|
-
topic_register[topic]["nodes"][node_request['node']] = { 'socket': node_request["socket"], 'pid': node_request["pid"], 'ip': nep_configuration["IP"] }
|
|
404
|
-
}
|
|
405
|
-
else {
|
|
406
|
-
topic_register[topic]["nodes"][node_request['node']] = { 'socket': node_request["socket"], 'pid': "n.a", 'ip': nep_configuration["IP"] }
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
var restartTopics = function (node_request) {
|
|
412
|
-
console.log(node_request)
|
|
413
|
-
// Get topic name
|
|
414
|
-
var topic = String(node_request['topic'])
|
|
415
|
-
|
|
416
|
-
console.log(" --- Reset topic : *" + topic + "* ---")
|
|
417
|
-
// Create new broker
|
|
418
|
-
onResetTopic(node_request);
|
|
419
|
-
onUpdateTopicList(topic_register)
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
var processMsg = function (json_msg, nodes_register, topic_register) {
|
|
423
|
-
|
|
424
|
-
let node_request = JSON.parse(json_msg)
|
|
425
|
-
console.log("NEP : Node request - node: " + node_request["node"] + ", socket: " + node_request["socket"] + ", topic: " + node_request["topic"])
|
|
426
|
-
|
|
427
|
-
// Check node status
|
|
428
|
-
if ('node' in node_request) {
|
|
429
|
-
|
|
430
|
-
// Kill previous node with the same name
|
|
431
|
-
if (node_request['node'] in nodes_register) {
|
|
432
|
-
|
|
433
|
-
console.log("--- Node *" + node_request['node'] + "* already defined ---")
|
|
434
|
-
var current_pid = nodes_register[node_request['node']]
|
|
435
|
-
if (node_request['pid'] === current_pid) {
|
|
436
|
-
|
|
437
|
-
}
|
|
438
|
-
else {
|
|
439
|
-
// TODO -- reset topics in node for graph
|
|
440
|
-
console.log("Kill node: " + node_request['node'])
|
|
441
|
-
nep.killNode(current_pid)
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
else {
|
|
445
|
-
console.log("--- New Node *" + node_request['node'] + "* ---")
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
// Update node info
|
|
450
|
-
nodes_register[node_request['node']] = node_request['pid']
|
|
451
|
-
// Get topic name
|
|
452
|
-
var topic = String(node_request['topic'])
|
|
453
|
-
// Check topic status
|
|
454
|
-
if (topic in topic_register) {
|
|
455
|
-
console.log("--- Topic *" + topic + "* already registered --- ")
|
|
456
|
-
|
|
457
|
-
// Send information of topic already defined
|
|
458
|
-
var response = onRegisteredTopic(node_request, topic_register, topic)
|
|
459
|
-
return JSON.stringify(response)
|
|
460
|
-
}
|
|
461
|
-
else {
|
|
462
|
-
console.log(" --- New topic : *" + topic + "* ---")
|
|
463
|
-
// Create new broker
|
|
464
|
-
var response = onNewTopic(node_request, topic_register);
|
|
465
|
-
return JSON.stringify(response)
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
if (value1 === "wifi") {
|
|
470
|
-
|
|
471
|
-
var ips = nep.getIP();
|
|
472
|
-
|
|
473
|
-
if (ips.wifi === "") {
|
|
474
|
-
console.log("NEP Error: No Wifi network detected")
|
|
475
|
-
}
|
|
476
|
-
else {
|
|
477
|
-
console.log("NEP master in Wifi - " + ips.wifi);
|
|
478
|
-
nep_configuration["IP"] = ips.wifi;
|
|
479
|
-
var master = new MasterLocal(IP = ips.wifi);
|
|
480
|
-
var info = new nep.MasterInfoServer(IP = ips.wifi, topics = topic_register);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
else if (value1 === "eth") {
|
|
484
|
-
|
|
485
|
-
var ips = nep.getIP();
|
|
486
|
-
|
|
487
|
-
if (ips.ethernet === "") {
|
|
488
|
-
console.log("NEP Error: No ethernet network detected")
|
|
489
|
-
}
|
|
490
|
-
else {
|
|
491
|
-
console.log("NEP master in Ethernet - " + ips.ethernet)
|
|
492
|
-
nep_configuration["IP"] = ips.ethernet;
|
|
493
|
-
var master = new MasterLocal(IP = ips.ethernet);
|
|
494
|
-
var info = new nep.MasterInfoServer(IP = ips.ethernet, topics = topic_register);
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
}
|
|
498
|
-
else if (value1 === "set") {
|
|
499
|
-
|
|
500
|
-
console.log("NEP master in - " + value2)
|
|
501
|
-
nep_configuration["IP"] = value2;
|
|
502
|
-
var master = new MasterLocal(IP = value2);
|
|
503
|
-
var info = new nep.MasterInfoServer(IP = value2, topics = topic_register);
|
|
504
|
-
|
|
505
|
-
}
|
|
506
|
-
//else if (value1 == "discover") {
|
|
507
|
-
//var master = new MasterLocal();
|
|
508
|
-
//console.log("NEP: Discover network ON")
|
|
509
|
-
//getWifi();
|
|
510
|
-
//getEthernet();
|
|
511
|
-
//discoverable();
|
|
512
|
-
|
|
513
|
-
//pub_params = node.new_pub("nep_network", "json");
|
|
514
|
-
|
|
515
|
-
//var pubParams = function (msg) {
|
|
516
|
-
// var params = { "wifi": devices, "ethernet": devices_ethernet }
|
|
517
|
-
// pub_params.publish(params)
|
|
518
|
-
//}
|
|
519
|
-
|
|
520
|
-
//var paramTimer = setInterval(pubParams, 1000);
|
|
521
|
-
//}
|
|
522
|
-
|
|
523
|
-
else {
|
|
524
|
-
var master = new MasterLocal();
|
|
525
|
-
var info = new nep.MasterInfoServer(IP = '127.0.0.1', topics=topic_register);
|
|
526
|
-
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
}
|
|
530
|
-
if (value == "ip") {
|
|
531
|
-
var ips = nep.getIP();
|
|
532
|
-
console.log("Wifi - " + ips.wifi);
|
|
533
|
-
console.log("Ethernet - " + ips.ethernet)
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
if (value === "ipsend") {
|
|
537
|
-
|
|
538
|
-
try {
|
|
539
|
-
dicovery_send()
|
|
540
|
-
} catch (error) {
|
|
541
|
-
console.log("NEP: Error socket in use")
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
if (value == "ipdiscover") {
|
|
546
|
-
dicovery_server()
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
if (value === "topics") {
|
|
551
|
-
|
|
552
|
-
var master_ip = "127.0.0.1";
|
|
553
|
-
var ips = nep.getIP();
|
|
554
|
-
|
|
555
|
-
if (value1 === undefined) {
|
|
556
|
-
master_ip = "127.0.0.1";
|
|
557
|
-
}
|
|
558
|
-
else if (value1 === "wifi") {
|
|
559
|
-
master_ip = ips.wifi;
|
|
560
|
-
}
|
|
561
|
-
else if (value1 === "eth") {
|
|
562
|
-
master_ip = ips.ethernet;
|
|
563
|
-
}
|
|
564
|
-
else {
|
|
565
|
-
master_ip = value1;
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
async function run() {
|
|
569
|
-
var requester = new zmq.Request;
|
|
570
|
-
|
|
571
|
-
requester.connect("tcp://" + master_ip + ":" + PORT_MASTER_INFO);
|
|
572
|
-
|
|
573
|
-
let msg = { "input": "topics" }
|
|
574
|
-
var message = JSON.stringify(msg);
|
|
575
|
-
await requester.send(message)
|
|
576
|
-
const [result] = await requester.receive()
|
|
577
|
-
var results = JSON.parse(result.toString())
|
|
578
|
-
//console.log(results);
|
|
579
|
-
if (results["state"] === "failure") {
|
|
580
|
-
console.log("No topics yet")
|
|
581
|
-
}
|
|
582
|
-
else {
|
|
583
|
-
console.log("")
|
|
584
|
-
|
|
585
|
-
results["input"].forEach(element => {
|
|
586
|
-
console.log(element)
|
|
587
|
-
});
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
run()
|
|
594
|
-
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
if (value === "help") {
|
|
598
|
-
|
|
599
|
-
var help = `
|
|
600
|
-
master - master in local host
|
|
601
|
-
master wifi - master in current wifi network
|
|
602
|
-
master eth - master in ethernet network
|
|
603
|
-
master set <ip> - master in <ip>
|
|
604
|
-
|
|
605
|
-
topics - print list of topics registered in localhost
|
|
606
|
-
topics wifi - print list of topics registered in wifi
|
|
607
|
-
topics eth - print list of topics registered in ethernet
|
|
608
|
-
topics <ip> - print list of topics registered in <ip>
|
|
609
|
-
|
|
610
|
-
publish <topic> <msg> - publish <msg> in <topic> in localhost
|
|
611
|
-
publish <topic> wifi <msg> - publish <msg> in <topic> in wifi network
|
|
612
|
-
publish <topic> eth <msg> - publish <msg> in <topic> in ethernet network
|
|
613
|
-
publish <topic> <ip> <msg> - publish <msg> in <topic> in <ip>
|
|
614
|
-
|
|
615
|
-
listen <topic> - subscribe to <topic> and print results in json
|
|
616
|
-
listen <topic> wifi - subscribe to <topic> in wifi network
|
|
617
|
-
listen <topic> eth - subscribe to <topic> in ethernet network
|
|
618
|
-
listen <topic> <ip> - subscribe to <topic> in <ip>
|
|
619
|
-
|
|
620
|
-
listen-t <topic> - subscribe to <topic> and print as a table
|
|
621
|
-
listen-t <topic> wifi - subscribe to <topic> in wifi network
|
|
622
|
-
listen-t <topic> eth - subscribe to <topic> in ethernet network
|
|
623
|
-
listen-t <topic> <ip> - subscribe to <topic> in <ip>
|
|
624
|
-
|
|
625
|
-
ip - see current ip for wifi and ethernet
|
|
626
|
-
|
|
627
|
-
`;
|
|
628
|
-
console.log(help)
|
|
629
|
-
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
if (value === "publish" || value === "pub") {
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
var openpub = function (master_ip = "127.0.0.1") {
|
|
636
|
-
|
|
637
|
-
var pubFunction = function () {
|
|
638
|
-
|
|
639
|
-
var msg = value2.replace(/'/g, '"')
|
|
640
|
-
console.log(JSON.parse(msg))
|
|
641
|
-
pub.publish(JSON.parse(msg))
|
|
642
|
-
console.log("message published")
|
|
643
|
-
|
|
644
|
-
process.exit(1)
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
var node = new nep.Node("nep-cli-pub")
|
|
649
|
-
var pub = node.new_pub(value1, "json")
|
|
650
|
-
setTimeout(pubFunction, 1000)
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
var master_ip = "127.0.0.1";
|
|
654
|
-
var ips = nep.getIP();
|
|
655
|
-
|
|
656
|
-
if (value3 === undefined) {
|
|
657
|
-
master_ip = "127.0.0.1";
|
|
658
|
-
}
|
|
659
|
-
else if (value3 === "wifi") {
|
|
660
|
-
master_ip = ips.wifi;
|
|
661
|
-
}
|
|
662
|
-
else if (value3 === "eth") {
|
|
663
|
-
master_ip = ips.ethernet;
|
|
664
|
-
}
|
|
665
|
-
else {
|
|
666
|
-
master_ip = value3;
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
async function run() {
|
|
670
|
-
var requester = new zmq.Request;
|
|
671
|
-
|
|
672
|
-
requester.connect("tcp://" + master_ip + ":" + PORT_MASTER_INFO);
|
|
673
|
-
|
|
674
|
-
let msg = { "input": "topics" }
|
|
675
|
-
var message = JSON.stringify(msg);
|
|
676
|
-
await requester.send(message)
|
|
677
|
-
const [result] = await requester.receive()
|
|
678
|
-
var results = JSON.parse(result.toString())
|
|
679
|
-
//console.log(results);
|
|
680
|
-
if (results["state"] === "failure") {
|
|
681
|
-
console.log("Topic is not registered");
|
|
682
|
-
}
|
|
683
|
-
else {
|
|
684
|
-
console.log("")
|
|
685
|
-
|
|
686
|
-
if (results["input"].includes(value1)) {
|
|
687
|
-
openpub(master_ip);
|
|
688
|
-
}
|
|
689
|
-
else {
|
|
690
|
-
console.log("Topic is not registered")
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
run()
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
if (value === "listen" || value === "listen-t") {
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
var opensub = function (master_ip = "127.0.0.1") {
|
|
706
|
-
var callback = function (msg) {
|
|
707
|
-
var date = new Date();
|
|
708
|
-
var dateString = date.toISOString();
|
|
709
|
-
console.log(dateString);
|
|
710
|
-
|
|
711
|
-
if (msg.length > 10000) {
|
|
712
|
-
console.log("the message is so long to be displayed")
|
|
713
|
-
}
|
|
714
|
-
else {
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
if (value == "listen") {
|
|
718
|
-
console.log(JSON.stringify(msg, null, 4));
|
|
719
|
-
}
|
|
720
|
-
else {
|
|
721
|
-
|
|
722
|
-
console.log(msg)
|
|
723
|
-
console.table(msg);
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
var node = new nep.Node("nep-cli-sub");
|
|
733
|
-
var conf = node.hybrid(master_ip)
|
|
734
|
-
sub = node.new_sub(value1, "json", callback, conf)
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
var master_ip = "127.0.0.1";
|
|
738
|
-
var ips = nep.getIP();
|
|
739
|
-
|
|
740
|
-
if (value2 === undefined) {
|
|
741
|
-
master_ip = "127.0.0.1";
|
|
742
|
-
}
|
|
743
|
-
else if (value2 === "wifi") {
|
|
744
|
-
master_ip = ips.wifi;
|
|
745
|
-
}
|
|
746
|
-
else if (value2 === "eth") {
|
|
747
|
-
master_ip = ips.ethernet;
|
|
748
|
-
}
|
|
749
|
-
else {
|
|
750
|
-
master_ip = value2;
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
async function run() {
|
|
754
|
-
var requester = new zmq.Request;
|
|
755
|
-
|
|
756
|
-
requester.connect("tcp://" + master_ip + ":" + PORT_MASTER_INFO);
|
|
757
|
-
|
|
758
|
-
let msg = { "input": "topics" }
|
|
759
|
-
var message = JSON.stringify(msg);
|
|
760
|
-
await requester.send(message)
|
|
761
|
-
const [result] = await requester.receive()
|
|
762
|
-
var results = JSON.parse(result.toString())
|
|
763
|
-
//console.log(results);
|
|
764
|
-
if (results["state"] === "failure") {
|
|
765
|
-
console.log("Topic is not registered");
|
|
766
|
-
}
|
|
767
|
-
else {
|
|
768
|
-
console.log("")
|
|
769
|
-
|
|
770
|
-
if (results["input"].includes(value1)) {
|
|
771
|
-
opensub(master_ip);
|
|
772
|
-
}
|
|
773
|
-
else {
|
|
774
|
-
console.log("Topic is not registered")
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
run()
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
}
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
if (value === "init") {
|
|
790
|
-
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
if (value === "bridge") {
|
|
795
|
-
|
|
796
|
-
var pathNEP = 'C:/nep'
|
|
797
|
-
|
|
798
|
-
if (opsys == "darwin") {
|
|
799
|
-
opsys = "MacOS";
|
|
800
|
-
pathNEP = require("os").userInfo.homedir
|
|
801
|
-
} else if (opsys === "win32" || opsys === "win64") {
|
|
802
|
-
opsys = "Windows";
|
|
803
|
-
pathNEP = 'C:/nep'
|
|
804
|
-
} else if (opsys == "linux") {
|
|
805
|
-
opsys = "Linux";
|
|
806
|
-
pathNEP = process.env.HOME + '/Documents/nep'
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
const { spawn } = require('child_process');
|
|
810
|
-
var opsys = process.platform;
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
var args = pathNEP + "/bridge.py"
|
|
814
|
-
|
|
815
|
-
robot_process = spawn('python', [args]);
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
robot_process.stdout.on('data', (data) => {
|
|
819
|
-
console.log("--------ROBOT -----")
|
|
820
|
-
console.log(`stdout: ${data}`);
|
|
821
|
-
});
|
|
822
|
-
|
|
823
|
-
robot_process.stderr.on('data', (data) => {
|
|
824
|
-
console.log("--------ROBOT -----")
|
|
825
|
-
console.error(`stderr: ${data}`);
|
|
826
|
-
});
|
|
827
|
-
|
|
828
|
-
robot_process.on('close', (code) => {
|
|
829
|
-
console.log(`child process exited with code ${code}`);
|
|
830
|
-
});
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
//console.log("You are using nep-cli :)")
|
|
4
|
+
var args = process.argv.splice(process.execArgv.length + 2);
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var zmq = require("zeromq");
|
|
8
|
+
var zmqc = require("zeromq/v5-compat");
|
|
9
|
+
var nep = require('nep-js');
|
|
10
|
+
|
|
11
|
+
// Retrieve the first argument
|
|
12
|
+
var value = args[0];
|
|
13
|
+
var value1 = "";
|
|
14
|
+
var value2 = "";
|
|
15
|
+
var value3 = "";
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
value1 = args[1];
|
|
19
|
+
value2 = args[2];
|
|
20
|
+
value3 = args[3];
|
|
21
|
+
} catch (error) {
|
|
22
|
+
value1 = "";
|
|
23
|
+
value2 = "";
|
|
24
|
+
value3 = "";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var PORT_DISCOVER = 7777
|
|
28
|
+
var PORT_MASTER = 7000
|
|
29
|
+
var PORT_MASTER_INFO = 7010
|
|
30
|
+
|
|
31
|
+
var devices = {};
|
|
32
|
+
var devices_ethernet = {};
|
|
33
|
+
var watch = {};
|
|
34
|
+
var watch_ethernet = {};
|
|
35
|
+
var timerDiscoverWifi = "";
|
|
36
|
+
var timerDiscoverEthernet = "";
|
|
37
|
+
var nep_configuration = { current_port: 10000, IP: "127.0.0.1", brokers: {} }
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
function dicovery_send() {
|
|
41
|
+
|
|
42
|
+
var dgram = require("dgram");
|
|
43
|
+
var devices = [];
|
|
44
|
+
|
|
45
|
+
var ips = nep.getIP();
|
|
46
|
+
var IP_EXTERNAL = ips.wifi;
|
|
47
|
+
var IP_LAN = ips.ethernet;
|
|
48
|
+
|
|
49
|
+
const search = new Buffer([
|
|
50
|
+
'M-SEARCH * HTTP/1.1',
|
|
51
|
+
'HOST: 239.255.255.250:1900',
|
|
52
|
+
'MAN: "ssdp:discover"',
|
|
53
|
+
'MX: 3',
|
|
54
|
+
'ST: upnp:rootdevice'
|
|
55
|
+
].join('\r\n'));
|
|
56
|
+
|
|
57
|
+
const socket = dgram.createSocket('udp4');
|
|
58
|
+
socket.on('listening', () => {
|
|
59
|
+
socket.addMembership('239.255.255.250');
|
|
60
|
+
socket.send(search, 0, search.length, 1900, "239.255.255.250");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
function discover_lan() {
|
|
65
|
+
socket.send(search, 0, search.length, 1900, "239.255.255.250");
|
|
66
|
+
}
|
|
67
|
+
timerDiscoverEthernet = setInterval(discover_lan, 1000);
|
|
68
|
+
|
|
69
|
+
socket.bind(1900);
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
function dicovery_server() {
|
|
76
|
+
|
|
77
|
+
var dgram = require("dgram");
|
|
78
|
+
var node = new nep.Node("nep-cli-ipdiscover");
|
|
79
|
+
var pub = node.new_pub("ipdiscover", "json");
|
|
80
|
+
|
|
81
|
+
var devices = [];
|
|
82
|
+
|
|
83
|
+
var ips = nep.getIP();
|
|
84
|
+
var IP_EXTERNAL = ips.wifi;
|
|
85
|
+
var IP_LAN = ips.ethernet;
|
|
86
|
+
|
|
87
|
+
const socket = dgram.createSocket('udp4');
|
|
88
|
+
socket.on('listening', () => {
|
|
89
|
+
socket.addMembership('239.255.255.250');
|
|
90
|
+
});
|
|
91
|
+
socket.on('message', (message, rinfo) => {
|
|
92
|
+
if (!(devices.includes(rinfo.address))) {
|
|
93
|
+
if (!(rinfo.address === IP_EXTERNAL)) {
|
|
94
|
+
devices.push(rinfo.address)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
console.log(devices)
|
|
98
|
+
try {
|
|
99
|
+
pub.publish({ "devices": devices })
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.log("NEP master not avaliable")
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
socket.bind(1900);
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
function discoverable() {
|
|
116
|
+
var ips = nep.getIP();
|
|
117
|
+
var IP_EXTERNAL = ips.wifi;
|
|
118
|
+
var IP_LAN = ips.ethernet;
|
|
119
|
+
var node = new nep.Node("nep-cli-discoverable");
|
|
120
|
+
|
|
121
|
+
if (IP_EXTERNAL == "") {
|
|
122
|
+
console.log("No Wi-fi connection")
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
var pub_list = nep.setHostsPublishers(IP_EXTERNAL, node);
|
|
126
|
+
function discover_wifi() {
|
|
127
|
+
nep.publishDevices(topic_register, pub_list, IP_EXTERNAL)
|
|
128
|
+
}
|
|
129
|
+
timerDiscoverWifi = setInterval(discover_wifi, 1000);
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (IP_LAN == "") {
|
|
134
|
+
console.log("No Ethernet connection")
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
var pub_list_lan = nep.setHostsPublishers(IP_LAN, node);
|
|
138
|
+
function discover_lan() {
|
|
139
|
+
nep.publishDevices(topic_register, pub_list_lan, IP_EXTERNAL)
|
|
140
|
+
}
|
|
141
|
+
timerDiscoverEthernet = setInterval(discover_lan, 1000);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
function getWifi() {
|
|
148
|
+
|
|
149
|
+
var ips = nep.getIP();
|
|
150
|
+
|
|
151
|
+
if (ips.wifi == "") {
|
|
152
|
+
|
|
153
|
+
console.log("No Wi-fi")
|
|
154
|
+
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
var node = new nep.Node("nep-cli-wifi");
|
|
158
|
+
var conf = node.direct(ips.wifi, PORT_DISCOVER, "many2one");
|
|
159
|
+
|
|
160
|
+
var callback_remote_sub = function (msg) {
|
|
161
|
+
devices[msg.computer] = { "ip": msg.ip, "avaliable": true }
|
|
162
|
+
watch[msg.computer] = msg.timestamp
|
|
163
|
+
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
var watchdevices = function (msg) {
|
|
167
|
+
for (device in watch) {
|
|
168
|
+
var difference = Math.abs(Date.now() - watch[device])
|
|
169
|
+
if (difference > 5 * 1000) {
|
|
170
|
+
devices[device]["avaliable"] = false
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
}
|
|
176
|
+
var timerCleanWifi = setInterval(watchdevices, 1000);
|
|
177
|
+
sub_remote = node.new_sub("master_ex", "json", callback_remote_sub, conf);
|
|
178
|
+
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function getEthernet() {
|
|
184
|
+
|
|
185
|
+
var ips = nep.getIP();
|
|
186
|
+
|
|
187
|
+
if (ips.ethernet == "") {
|
|
188
|
+
|
|
189
|
+
console.log("No Ethernet")
|
|
190
|
+
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
|
|
194
|
+
var node = new nep.Node("nep-cli-ethernet");
|
|
195
|
+
var conf = node.direct(ips.ethernet, PORT_DISCOVER, "many2one");
|
|
196
|
+
|
|
197
|
+
var callback_remote_sub_et = function (msg) {
|
|
198
|
+
devices_ethernet[msg.computer] = { "ip": msg.ip, "avaliable": true }
|
|
199
|
+
watch_ethernet[msg.computer] = msg.timestamp
|
|
200
|
+
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
var watchdevices_et = function (msg) {
|
|
204
|
+
for (device in watch) {
|
|
205
|
+
var difference = Math.abs(Date.now() - watch_ethernet[device])
|
|
206
|
+
if (difference > 5 * 1000) {
|
|
207
|
+
devices_ethernet[device]["avaliable"] = false
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
}
|
|
213
|
+
var timerCleanET = setInterval(watchdevices_et, 1000);
|
|
214
|
+
sub_remote_et = node.new_sub("master_ex", "json", callback_remote_sub_et, conf);
|
|
215
|
+
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
if (value == "master") {
|
|
224
|
+
|
|
225
|
+
var node = new nep.Node("nep-cli");
|
|
226
|
+
|
|
227
|
+
console.log("Starting NEP master in terminal")
|
|
228
|
+
|
|
229
|
+
// ----------- Main variables ---------------
|
|
230
|
+
// Topics saved here
|
|
231
|
+
var topic_register = {};
|
|
232
|
+
// Node saved here
|
|
233
|
+
var nodes_register = {};
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
class MasterLocal {
|
|
240
|
+
constructor(IP = '127.0.0.1', port_ = 7000) {
|
|
241
|
+
this.port = String(port_)
|
|
242
|
+
var address = "tcp://" + IP + ":" + this.port
|
|
243
|
+
console.log("New Master in: " + address)
|
|
244
|
+
try {
|
|
245
|
+
async function run() {
|
|
246
|
+
const reply = new zmq.Reply
|
|
247
|
+
await reply.bind(address)
|
|
248
|
+
for await (const [node_request] of reply) {
|
|
249
|
+
await reply.send(processMsg(node_request, nodes_register, topic_register))
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
run()
|
|
253
|
+
} catch (error) {
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
var onRegisteredTopic = function (node_request, topic_register, topic) {
|
|
259
|
+
|
|
260
|
+
var msg = { "state": "failure" }
|
|
261
|
+
|
|
262
|
+
try {
|
|
263
|
+
var port = topic_register[topic]["port"]
|
|
264
|
+
var socket_ = topic_register[topic]["socket"]
|
|
265
|
+
|
|
266
|
+
if ("mode" in topic_register[topic]) {
|
|
267
|
+
var mode = topic_register[topic]["mode"]
|
|
268
|
+
msg = { 'topic': topic, 'port': port, 'mode': mode, 'ip': nep_configuration["IP"], 'socket': socket_, "state": "success" }
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
msg = { 'topic': topic, 'port': port, 'ip': nep_configuration["IP"], 'socket': socket_, "state": "success" }
|
|
272
|
+
}
|
|
273
|
+
updatePID(node_request, topic_register, topic)
|
|
274
|
+
|
|
275
|
+
} catch (error) {
|
|
276
|
+
console.log("Error in topic request")
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if ("language" in node_request) {
|
|
280
|
+
|
|
281
|
+
if (node_request["language"] == "C++") {
|
|
282
|
+
msg.port = msg.port.toString();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return msg
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
var onResetTopic = function (node_request) {
|
|
292
|
+
|
|
293
|
+
var topic = node_request["topic"]
|
|
294
|
+
nep_configuration["current_port"] = Math.max(nep_configuration["current_port"], node_request["port"] + 2);
|
|
295
|
+
|
|
296
|
+
if (node_request["socket"] === "publisher" || node_request["socket"] === "subscriber") {
|
|
297
|
+
// Create new broker for many2many communication
|
|
298
|
+
if (node_request["mode"] === "many2many") {
|
|
299
|
+
restartBroker(node_request["topic"], node_request["port"]);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if ("msg_type" in node_request) {
|
|
303
|
+
topic_register[topic] = { "port": node_request["port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"], "mode": node_request["mode"], "msg_type": node_request["msg_type"] }
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
topic_register[topic] = { "port": node_request["port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"], "mode": node_request["mode"], "msg_type": "json" }
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
topic_register[topic]["nodes"] = [];
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
var onNewTopic = function (node_request, topic_register) {
|
|
315
|
+
|
|
316
|
+
// Asign new ports
|
|
317
|
+
nep_configuration["current_port"] = nep_configuration["current_port"] + 2
|
|
318
|
+
// Topic must be an string
|
|
319
|
+
topic = String(node_request['topic'])
|
|
320
|
+
var msg = {}
|
|
321
|
+
// If publisher or subscriber
|
|
322
|
+
if (node_request["socket"] === "publisher" || node_request["socket"] === "subscriber") {
|
|
323
|
+
// Create new broker for many2many communication
|
|
324
|
+
if (node_request["mode"] === "many2many") {
|
|
325
|
+
createBroker(topic);
|
|
326
|
+
msg = m2mResponse(node_request, topic_register, topic);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
else if (node_request["socket"] === "surveyor" || node_request["socket"] === "respondent") {
|
|
330
|
+
msg = surveyResponse(node_request, topic_register, topic)
|
|
331
|
+
}
|
|
332
|
+
else if (node_request["socket"] === "client" || node_request["socket"] === "server") {
|
|
333
|
+
msg = csResponse(node_request, topic_register, topic)
|
|
334
|
+
}
|
|
335
|
+
// Key for related nodes
|
|
336
|
+
topic_register[topic]["nodes"] = {}
|
|
337
|
+
// Set PID of node
|
|
338
|
+
updatePID(node_request, topic_register, topic);
|
|
339
|
+
|
|
340
|
+
if ("language" in node_request) {
|
|
341
|
+
|
|
342
|
+
if (node_request["language"] == "C++") {
|
|
343
|
+
msg.port = msg.port.toString();
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return msg
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
var createBroker = function (topic) {
|
|
354
|
+
try {
|
|
355
|
+
var broker = new Broker(nep_configuration["IP"], nep_configuration["current_port"] + 1, nep_configuration["current_port"])
|
|
356
|
+
//var brokersubex = new BrokerBridgeSUBEX(conf["IP"],IP_EXTERNAL, conf["current_port"] , conf["current_port"])
|
|
357
|
+
|
|
358
|
+
// Add broker defined to list of brokers
|
|
359
|
+
nep_configuration["brokers"][topic] = broker
|
|
360
|
+
} catch (error) {
|
|
361
|
+
// console.log("NEP ERROR: ports " + String(nep_configuration["current_port"]) + " and " + String(nep_configuration["current_port"] + 1) + " not avaliable")
|
|
362
|
+
nep_configuration["current_port"] = nep_configuration["current_port"] + 2
|
|
363
|
+
var broker = new nep.Broker(nep_configuration["IP"], nep_configuration["current_port"] + 1, nep_configuration["current_port"])
|
|
364
|
+
// Add broker defined to list of brokers
|
|
365
|
+
nep_configuration["brokers"][topic] = broker
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
var restartBroker = function (topic, port) {
|
|
370
|
+
try {
|
|
371
|
+
var broker = new nep.Broker(nep_configuration["IP"], port + 1, port)
|
|
372
|
+
//var brokersubex = new BrokerBridgeSUBEX(conf["IP"],IP_EXTERNAL, conf["current_port"] , conf["current_port"])
|
|
373
|
+
// Add broker defined to list of brokers
|
|
374
|
+
nep_configuration["brokers"][topic] = broker
|
|
375
|
+
} catch (error) {
|
|
376
|
+
console.log("NEP ERROR: ports " + String(port) + " and " + String(port + 1) + " not avaliable")
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
var m2mResponse = function (node_request, topic_register, topic) {
|
|
381
|
+
if ("msg_type" in node_request) {
|
|
382
|
+
topic_register[topic] = { "port": nep_configuration["current_port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"], "mode": node_request["mode"], "msg_type": node_request["msg_type"] }
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
topic_register[topic] = { "port": nep_configuration["current_port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"], "mode": node_request["mode"], "msg_type": "json" }
|
|
386
|
+
}
|
|
387
|
+
return { 'topic': topic, 'port': nep_configuration["current_port"], 'mode': node_request["mode"], 'ip': nep_configuration["IP"], 'socket': node_request["socket"], "state": "success" }
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
var surveyResponse = function (node_request, topic_register, topic) {
|
|
391
|
+
topic_register[topic] = { "port": nep_configuration["current_port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"] }
|
|
392
|
+
return { 'topic': topic, 'port': nep_configuration["current_port"], 'ip': nep_configuration["IP"], 'socket': node_request["socket"], "state": "success" }
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
var csResponse = function (node_request, topic_register, topic) {
|
|
396
|
+
topic_register[topic] = { "port": nep_configuration["current_port"], "socket": node_request["socket"], 'ip': nep_configuration["IP"], "msg_type": "json" }
|
|
397
|
+
return { 'topic': topic, 'port': nep_configuration["current_port"], 'ip': nep_configuration["IP"], 'socket': node_request["socket"], "state": "success" }
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
var updatePID = function (node_request, topic_register, topic) {
|
|
402
|
+
if ("pid" in node_request) {
|
|
403
|
+
topic_register[topic]["nodes"][node_request['node']] = { 'socket': node_request["socket"], 'pid': node_request["pid"], 'ip': nep_configuration["IP"] }
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
topic_register[topic]["nodes"][node_request['node']] = { 'socket': node_request["socket"], 'pid': "n.a", 'ip': nep_configuration["IP"] }
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
var restartTopics = function (node_request) {
|
|
412
|
+
console.log(node_request)
|
|
413
|
+
// Get topic name
|
|
414
|
+
var topic = String(node_request['topic'])
|
|
415
|
+
|
|
416
|
+
console.log(" --- Reset topic : *" + topic + "* ---")
|
|
417
|
+
// Create new broker
|
|
418
|
+
onResetTopic(node_request);
|
|
419
|
+
onUpdateTopicList(topic_register)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
var processMsg = function (json_msg, nodes_register, topic_register) {
|
|
423
|
+
|
|
424
|
+
let node_request = JSON.parse(json_msg)
|
|
425
|
+
console.log("NEP : Node request - node: " + node_request["node"] + ", socket: " + node_request["socket"] + ", topic: " + node_request["topic"])
|
|
426
|
+
|
|
427
|
+
// Check node status
|
|
428
|
+
if ('node' in node_request) {
|
|
429
|
+
|
|
430
|
+
// Kill previous node with the same name
|
|
431
|
+
if (node_request['node'] in nodes_register) {
|
|
432
|
+
|
|
433
|
+
console.log("--- Node *" + node_request['node'] + "* already defined ---")
|
|
434
|
+
var current_pid = nodes_register[node_request['node']]
|
|
435
|
+
if (node_request['pid'] === current_pid) {
|
|
436
|
+
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
// TODO -- reset topics in node for graph
|
|
440
|
+
console.log("Kill node: " + node_request['node'])
|
|
441
|
+
nep.killNode(current_pid)
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
else {
|
|
445
|
+
console.log("--- New Node *" + node_request['node'] + "* ---")
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Update node info
|
|
450
|
+
nodes_register[node_request['node']] = node_request['pid']
|
|
451
|
+
// Get topic name
|
|
452
|
+
var topic = String(node_request['topic'])
|
|
453
|
+
// Check topic status
|
|
454
|
+
if (topic in topic_register) {
|
|
455
|
+
console.log("--- Topic *" + topic + "* already registered --- ")
|
|
456
|
+
|
|
457
|
+
// Send information of topic already defined
|
|
458
|
+
var response = onRegisteredTopic(node_request, topic_register, topic)
|
|
459
|
+
return JSON.stringify(response)
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
console.log(" --- New topic : *" + topic + "* ---")
|
|
463
|
+
// Create new broker
|
|
464
|
+
var response = onNewTopic(node_request, topic_register);
|
|
465
|
+
return JSON.stringify(response)
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (value1 === "wifi") {
|
|
470
|
+
|
|
471
|
+
var ips = nep.getIP();
|
|
472
|
+
|
|
473
|
+
if (ips.wifi === "") {
|
|
474
|
+
console.log("NEP Error: No Wifi network detected")
|
|
475
|
+
}
|
|
476
|
+
else {
|
|
477
|
+
console.log("NEP master in Wifi - " + ips.wifi);
|
|
478
|
+
nep_configuration["IP"] = ips.wifi;
|
|
479
|
+
var master = new MasterLocal(IP = ips.wifi);
|
|
480
|
+
var info = new nep.MasterInfoServer(IP = ips.wifi, topics = topic_register);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
else if (value1 === "eth") {
|
|
484
|
+
|
|
485
|
+
var ips = nep.getIP();
|
|
486
|
+
|
|
487
|
+
if (ips.ethernet === "") {
|
|
488
|
+
console.log("NEP Error: No ethernet network detected")
|
|
489
|
+
}
|
|
490
|
+
else {
|
|
491
|
+
console.log("NEP master in Ethernet - " + ips.ethernet)
|
|
492
|
+
nep_configuration["IP"] = ips.ethernet;
|
|
493
|
+
var master = new MasterLocal(IP = ips.ethernet);
|
|
494
|
+
var info = new nep.MasterInfoServer(IP = ips.ethernet, topics = topic_register);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
}
|
|
498
|
+
else if (value1 === "set") {
|
|
499
|
+
|
|
500
|
+
console.log("NEP master in - " + value2)
|
|
501
|
+
nep_configuration["IP"] = value2;
|
|
502
|
+
var master = new MasterLocal(IP = value2);
|
|
503
|
+
var info = new nep.MasterInfoServer(IP = value2, topics = topic_register);
|
|
504
|
+
|
|
505
|
+
}
|
|
506
|
+
//else if (value1 == "discover") {
|
|
507
|
+
//var master = new MasterLocal();
|
|
508
|
+
//console.log("NEP: Discover network ON")
|
|
509
|
+
//getWifi();
|
|
510
|
+
//getEthernet();
|
|
511
|
+
//discoverable();
|
|
512
|
+
|
|
513
|
+
//pub_params = node.new_pub("nep_network", "json");
|
|
514
|
+
|
|
515
|
+
//var pubParams = function (msg) {
|
|
516
|
+
// var params = { "wifi": devices, "ethernet": devices_ethernet }
|
|
517
|
+
// pub_params.publish(params)
|
|
518
|
+
//}
|
|
519
|
+
|
|
520
|
+
//var paramTimer = setInterval(pubParams, 1000);
|
|
521
|
+
//}
|
|
522
|
+
|
|
523
|
+
else {
|
|
524
|
+
var master = new MasterLocal();
|
|
525
|
+
var info = new nep.MasterInfoServer(IP = '127.0.0.1', topics=topic_register);
|
|
526
|
+
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
}
|
|
530
|
+
if (value == "ip") {
|
|
531
|
+
var ips = nep.getIP();
|
|
532
|
+
console.log("Wifi - " + ips.wifi);
|
|
533
|
+
console.log("Ethernet - " + ips.ethernet)
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (value === "ipsend") {
|
|
537
|
+
|
|
538
|
+
try {
|
|
539
|
+
dicovery_send()
|
|
540
|
+
} catch (error) {
|
|
541
|
+
console.log("NEP: Error socket in use")
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
if (value == "ipdiscover") {
|
|
546
|
+
dicovery_server()
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
if (value === "topics") {
|
|
551
|
+
|
|
552
|
+
var master_ip = "127.0.0.1";
|
|
553
|
+
var ips = nep.getIP();
|
|
554
|
+
|
|
555
|
+
if (value1 === undefined) {
|
|
556
|
+
master_ip = "127.0.0.1";
|
|
557
|
+
}
|
|
558
|
+
else if (value1 === "wifi") {
|
|
559
|
+
master_ip = ips.wifi;
|
|
560
|
+
}
|
|
561
|
+
else if (value1 === "eth") {
|
|
562
|
+
master_ip = ips.ethernet;
|
|
563
|
+
}
|
|
564
|
+
else {
|
|
565
|
+
master_ip = value1;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
async function run() {
|
|
569
|
+
var requester = new zmq.Request;
|
|
570
|
+
|
|
571
|
+
requester.connect("tcp://" + master_ip + ":" + PORT_MASTER_INFO);
|
|
572
|
+
|
|
573
|
+
let msg = { "input": "topics" }
|
|
574
|
+
var message = JSON.stringify(msg);
|
|
575
|
+
await requester.send(message)
|
|
576
|
+
const [result] = await requester.receive()
|
|
577
|
+
var results = JSON.parse(result.toString())
|
|
578
|
+
//console.log(results);
|
|
579
|
+
if (results["state"] === "failure") {
|
|
580
|
+
console.log("No topics yet")
|
|
581
|
+
}
|
|
582
|
+
else {
|
|
583
|
+
console.log("")
|
|
584
|
+
|
|
585
|
+
results["input"].forEach(element => {
|
|
586
|
+
console.log(element)
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
run()
|
|
594
|
+
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
if (value === "help") {
|
|
598
|
+
|
|
599
|
+
var help = `
|
|
600
|
+
master - master in local host
|
|
601
|
+
master wifi - master in current wifi network
|
|
602
|
+
master eth - master in ethernet network
|
|
603
|
+
master set <ip> - master in <ip>
|
|
604
|
+
|
|
605
|
+
topics - print list of topics registered in localhost
|
|
606
|
+
topics wifi - print list of topics registered in wifi
|
|
607
|
+
topics eth - print list of topics registered in ethernet
|
|
608
|
+
topics <ip> - print list of topics registered in <ip>
|
|
609
|
+
|
|
610
|
+
publish <topic> <msg> - publish <msg> in <topic> in localhost
|
|
611
|
+
publish <topic> wifi <msg> - publish <msg> in <topic> in wifi network
|
|
612
|
+
publish <topic> eth <msg> - publish <msg> in <topic> in ethernet network
|
|
613
|
+
publish <topic> <ip> <msg> - publish <msg> in <topic> in <ip>
|
|
614
|
+
|
|
615
|
+
listen <topic> - subscribe to <topic> and print results in json
|
|
616
|
+
listen <topic> wifi - subscribe to <topic> in wifi network
|
|
617
|
+
listen <topic> eth - subscribe to <topic> in ethernet network
|
|
618
|
+
listen <topic> <ip> - subscribe to <topic> in <ip>
|
|
619
|
+
|
|
620
|
+
listen-t <topic> - subscribe to <topic> and print as a table
|
|
621
|
+
listen-t <topic> wifi - subscribe to <topic> in wifi network
|
|
622
|
+
listen-t <topic> eth - subscribe to <topic> in ethernet network
|
|
623
|
+
listen-t <topic> <ip> - subscribe to <topic> in <ip>
|
|
624
|
+
|
|
625
|
+
ip - see current ip for wifi and ethernet
|
|
626
|
+
|
|
627
|
+
`;
|
|
628
|
+
console.log(help)
|
|
629
|
+
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
if (value === "publish" || value === "pub") {
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
var openpub = function (master_ip = "127.0.0.1") {
|
|
636
|
+
|
|
637
|
+
var pubFunction = function () {
|
|
638
|
+
|
|
639
|
+
var msg = value2.replace(/'/g, '"')
|
|
640
|
+
console.log(JSON.parse(msg))
|
|
641
|
+
pub.publish(JSON.parse(msg))
|
|
642
|
+
console.log("message published")
|
|
643
|
+
|
|
644
|
+
process.exit(1)
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
var node = new nep.Node("nep-cli-pub")
|
|
649
|
+
var pub = node.new_pub(value1, "json")
|
|
650
|
+
setTimeout(pubFunction, 1000)
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
var master_ip = "127.0.0.1";
|
|
654
|
+
var ips = nep.getIP();
|
|
655
|
+
|
|
656
|
+
if (value3 === undefined) {
|
|
657
|
+
master_ip = "127.0.0.1";
|
|
658
|
+
}
|
|
659
|
+
else if (value3 === "wifi") {
|
|
660
|
+
master_ip = ips.wifi;
|
|
661
|
+
}
|
|
662
|
+
else if (value3 === "eth") {
|
|
663
|
+
master_ip = ips.ethernet;
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
master_ip = value3;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
async function run() {
|
|
670
|
+
var requester = new zmq.Request;
|
|
671
|
+
|
|
672
|
+
requester.connect("tcp://" + master_ip + ":" + PORT_MASTER_INFO);
|
|
673
|
+
|
|
674
|
+
let msg = { "input": "topics" }
|
|
675
|
+
var message = JSON.stringify(msg);
|
|
676
|
+
await requester.send(message)
|
|
677
|
+
const [result] = await requester.receive()
|
|
678
|
+
var results = JSON.parse(result.toString())
|
|
679
|
+
//console.log(results);
|
|
680
|
+
if (results["state"] === "failure") {
|
|
681
|
+
console.log("Topic is not registered");
|
|
682
|
+
}
|
|
683
|
+
else {
|
|
684
|
+
console.log("")
|
|
685
|
+
|
|
686
|
+
if (results["input"].includes(value1)) {
|
|
687
|
+
openpub(master_ip);
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
console.log("Topic is not registered")
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
run()
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
if (value === "listen" || value === "listen-t") {
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
var opensub = function (master_ip = "127.0.0.1") {
|
|
706
|
+
var callback = function (msg) {
|
|
707
|
+
var date = new Date();
|
|
708
|
+
var dateString = date.toISOString();
|
|
709
|
+
console.log(dateString);
|
|
710
|
+
|
|
711
|
+
if (msg.length > 10000) {
|
|
712
|
+
console.log("the message is so long to be displayed")
|
|
713
|
+
}
|
|
714
|
+
else {
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
if (value == "listen") {
|
|
718
|
+
console.log(JSON.stringify(msg, null, 4));
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
|
|
722
|
+
console.log(msg)
|
|
723
|
+
console.table(msg);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
var node = new nep.Node("nep-cli-sub");
|
|
733
|
+
var conf = node.hybrid(master_ip)
|
|
734
|
+
sub = node.new_sub(value1, "json", callback, conf)
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
var master_ip = "127.0.0.1";
|
|
738
|
+
var ips = nep.getIP();
|
|
739
|
+
|
|
740
|
+
if (value2 === undefined) {
|
|
741
|
+
master_ip = "127.0.0.1";
|
|
742
|
+
}
|
|
743
|
+
else if (value2 === "wifi") {
|
|
744
|
+
master_ip = ips.wifi;
|
|
745
|
+
}
|
|
746
|
+
else if (value2 === "eth") {
|
|
747
|
+
master_ip = ips.ethernet;
|
|
748
|
+
}
|
|
749
|
+
else {
|
|
750
|
+
master_ip = value2;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
async function run() {
|
|
754
|
+
var requester = new zmq.Request;
|
|
755
|
+
|
|
756
|
+
requester.connect("tcp://" + master_ip + ":" + PORT_MASTER_INFO);
|
|
757
|
+
|
|
758
|
+
let msg = { "input": "topics" }
|
|
759
|
+
var message = JSON.stringify(msg);
|
|
760
|
+
await requester.send(message)
|
|
761
|
+
const [result] = await requester.receive()
|
|
762
|
+
var results = JSON.parse(result.toString())
|
|
763
|
+
//console.log(results);
|
|
764
|
+
if (results["state"] === "failure") {
|
|
765
|
+
console.log("Topic is not registered");
|
|
766
|
+
}
|
|
767
|
+
else {
|
|
768
|
+
console.log("")
|
|
769
|
+
|
|
770
|
+
if (results["input"].includes(value1)) {
|
|
771
|
+
opensub(master_ip);
|
|
772
|
+
}
|
|
773
|
+
else {
|
|
774
|
+
console.log("Topic is not registered")
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
run()
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
if (value === "init") {
|
|
790
|
+
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
if (value === "bridge") {
|
|
795
|
+
|
|
796
|
+
var pathNEP = 'C:/nep'
|
|
797
|
+
|
|
798
|
+
if (opsys == "darwin") {
|
|
799
|
+
opsys = "MacOS";
|
|
800
|
+
pathNEP = require("os").userInfo.homedir
|
|
801
|
+
} else if (opsys === "win32" || opsys === "win64") {
|
|
802
|
+
opsys = "Windows";
|
|
803
|
+
pathNEP = 'C:/nep'
|
|
804
|
+
} else if (opsys == "linux") {
|
|
805
|
+
opsys = "Linux";
|
|
806
|
+
pathNEP = process.env.HOME + '/Documents/nep'
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const { spawn } = require('child_process');
|
|
810
|
+
var opsys = process.platform;
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
var args = pathNEP + "/bridge.py"
|
|
814
|
+
|
|
815
|
+
robot_process = spawn('python', [args]);
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
robot_process.stdout.on('data', (data) => {
|
|
819
|
+
console.log("--------ROBOT -----")
|
|
820
|
+
console.log(`stdout: ${data}`);
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
robot_process.stderr.on('data', (data) => {
|
|
824
|
+
console.log("--------ROBOT -----")
|
|
825
|
+
console.error(`stderr: ${data}`);
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
robot_process.on('close', (code) => {
|
|
829
|
+
console.log(`child process exited with code ${code}`);
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
}
|