-
Notifications
You must be signed in to change notification settings - Fork 0
/
lmu_server.js
142 lines (113 loc) · 4.03 KB
/
lmu_server.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
const dgram = require('dgram');
const net = require('net');
const winston = require('winston')
const openAmqp = require('amqplib').connect('amqp://imonnetplus.com.bo');
const co = require('co');
const lmuDecoder= require('./m2m_decoders/lmu_decoder');
const reverseGeocoding = require('./reversegeocoding.js');
const dataAccessConnect = require('./data_access.js');
const port = 1996;
const xchange= 'main_valve';
const url = 'mongodb://imonnetplus.com.bo:27017/platform2';
winston.add(winston.transports.File, {filename: 'lmu.log', level:'debug'});
function LMU_Server(chan){
let devices = []
let clients = [];
const server = dgram.createSocket("udp4");
/**
*
* @param {string} message An hex string to be send to all the connected clients
*/
function broadcast(message){
clients.forEach(client => {
try {
client.write(message);
} catch (e){
console.log("An error has ocurred when trying to replicate to clients")
winston.log("An error has ocurred when trying to replicate to clients")
winston.log('error', e.toString())
}
})
}
const serverClientCalamp = net.createServer(client => {
console.log(client);
client.name = `${client.remoteAddress}:${client.remotePort}`;
clients.push(client);
winston.log('debug', `Client connected: ${client.name}`);
console.log(`Client connected: ${client.name}`);
client.on('end',() => {
winston.log(`Client disconnected: ${client.name}`);
});
client.on('data', (data) => {
try {
console.log('command recieved');
let {unitId, command} = JSON.parse(data);
sendTelecommand(unitId, command)
} catch (error) {
console.log(error);
}
});
})
serverClientCalamp.listen(8005,() => {
winston.log('debug',`Server Calamp Clients started on port 8000`);
console.log(`Server Calamp Clients started on port 8000`);
})
serverClientCalamp.on('error',err => {
console.log(err);
})
server.on('listening',() => {
let address = server.address();
console.log(`LMU Server(UDP) ready: ${address.address}:${address.port}`);
})
server.on('message',(msg, rinfo) => {
let deviceInfo = `${rinfo.address}:${rinfo.port}`;
winston.log(msg);
let decodedMessage= lmuDecoder.decodeMessage(msg);
let ackMessage = lmuDecoder.generateAckMessage(msg);
co(lmuDecoder.getPotableMessage(decodedMessage))
.then(pMess => {
console.log(`Potable Message is: ${pMess}`);
console.log(`Potable Message is: ${pMess["driver"]["keyId"]}`);
chan.publish(xchange, '', new Buffer(JSON.stringify(pMess)));
server.send(ackMessage,rinfo.port, rinfo.address); //ack
}).catch(err => {
console.log(`An error has ocurred when getting the potable message ${err}`);
process.exit(1);
});
msg = msg.toString('hex');
msg +='@';
msg = msg.toUpperCase();
winston.log('debug', msg);
broadcast(msg);
})
server.on('close', () => {
console.log('server closed');
})
server.on('error', error => {
console.log(error.toString());
})
server.bind(port, '0.0.0.0');
}
var chan = undefined;
openAmqp.then(conn => {
return conn.createChannel();
}).then(ch => {
return ch.assertExchange(xchange, 'fanout', {durable:false})
.then(ok => {
chan = ch
console.log("Connection stablished to Broker imnonetplus.com.bo")
co(dataAccessConnect.connect(url))
.then(()=>{
new LMU_Server(chan);
}).catch(err =>{
winston.log('error',`An error has occurred when initializing the server: ${err}`)
})
chan.on('error', err => {
process.exit(1);
})
})
}).catch(err =>{
console.warn;
winston.log('error',`An error has occurred when initializing the server: ${err}`)
process.exit(1);
})