-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (55 loc) · 2.35 KB
/
index.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
var gps = require("gps-tracking");
var count = 0;
var options = {
'debug': false,
'port': 8090,
'device_adapter': "GT06"
}
const rmq_config = require('./configs/rmq.json');
let rmq = require('amqplib');
rmq.connect(rmq_config.broker_uri).then(async (conn) => {
let ch = await conn.createChannel();
await ch.assertExchange(rmq_config.exchange_name, 'topic', {durable: false});
let q = await ch.assertQueue(rmq_config.queue_name, {exclusive: false, durable:true});
await ch.bindQueue(q.queue, rmq_config.exchange_name, rmq_config.route_name);
var server = gps.server(options, function (device, connection) {
device.on("connected", function (data) {
return data;
});
device.on("login_request", function (device_id, msg_parts) {
this.login_authorized(true);
});
device.on("ping", async function (data) {
try {
count = count+1;
if(data.latitude > 0) {
data.latitude = -data.latitude;
}
console.log('NO MESSAGE:'+count+'. #' + this.getUID() + ' ( ' +data.latitude + ',' + data.longitude +' )');
try {
let msg = {id : this.getUID(), latitude: data.latitude, longitude: data.longitude, time: new Date(), speed: data.speed };
msg = JSON.stringify(msg);
await ch.publish(rmq_config.exchange_name, rmq_config.route_name, new Buffer(msg), { deliveryMode: 2 });
await ch.publish('', 'gps-tracker-rawtile', new Buffer(msg), { deliveryMode: 2 })
}catch (err){
console.log('publish msg error');
console.log(err);
}
return data;
} catch(error) {
console.log('something broken');
console.log(error);
}
});
device.on("alarm", function (alarm_code, alarm_data, msg_data) {
console.log("Help! Something happend: "+alarm_code+" ("+alarm_data.msg+")");
});
//Also, you can listen on the native connection object
connection.on('data', function (data) {
//echo raw data package
//console.log(data.toString());
})
});
}).catch(err => {
console.log(err);
});