This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (69 loc) · 2.13 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("Server running on port 3000");
});
const mqtt = require('mqtt');
const client = mqtt.connect("mqtts://mqtt.hsl.fi:8883",{clientId:"mqttjs01"});
const vehicleDeadTimeout = 120;
let vehicles = {};
const topicMatcher = new RegExp('/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/([^\/]*)\/');
client.on('message',function(topic, message, packet){
let msg = JSON.parse(message);
msg = msg.VP;
//console.log(topic, msg);
const matchedTopic = topicMatcher.exec(topic);
const prefix = matchedTopic[1],
verson = matchedTopic[2],
journey_type = matchedTopic[3],
temporal_type = matchedTopic[4],
event_type = matchedTopic[5],
transport_mode = matchedTopic[6];
const uid = msg.oper + "-" + msg.veh;
if (msg.lat && msg.long && msg.veh) {
vehicles[uid] = {
id: uid,
mode: transport_mode,
route: msg.route,
heading: msg.hdg,
lat: msg.lat,
lon: msg.long,
speed: msg.spd,
lastUpdate: getTs()
};
}
});
app.get("/get_vehicles", (req, res, next) => {
res.json(vehicles);
});
setInterval(function() {
const ts = getTs();
console.log("Checking dead vehicles", ts);
for (var i in vehicles) {
const veh = vehicles[i];
if (veh.lastUpdate + vehicleDeadTimeout < ts) {
console.log("Removig vehicle " + i + ", because it stopped sending data");
delete vehicles[i];
}
}
}, 10000);
function getTs() {
return Math.round((new Date()).getTime() / 1000);
}
setInterval(function() {
console.log("Actual vehicles are: " + Object.keys(vehicles).length);
console.debug("Vehicle list are: ", vehicles);
}, 5000);
client.on("connect",function(){
console.log("connected "+ client.connected);
});
client.on("error",function(error) {
console.log("Can't connect" + error);
process.exit(1)
});
const options={
retain:true,
qos:1
};
const topic="/hfp/v2/journey/ongoing/vp/+/+/+/+/+/+/+/+/0/#";
client.subscribe(topic,{qos:1});