loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8041555420346123","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgbXF0dCA9IHJlcXVpcmUoJ21xdHQnKTsKCi8vPHByZWZpeD48dmVyc2lvbj4vam91cm5leS88dGVtcG9yYWxfdHlwZT4vPHRyYW5zcG9ydF9tb2RlPi88b3BlcmF0b3JfaWQ+Lzx2ZWhpY2xlX251bWJlcj4vPHJvdXRlX2lkPi88ZGlyZWN0aW9uX2lkPi88aGVhZHNpZ24+LzxzdGFydF90aW1lPi88bmV4dF9zdG9wPi88Z2VvaGFzaF9sZXZlbD4vPGdlb2hhc2g+Lwpjb25zdCB0b3BpYyA9ICcvaGZwL3YxL2pvdXJuZXkvb25nb2luZy8rLysvKy8rLysvKy8rLysvKy8jJzsKCmNvbnN0IGNsaWVudCAgPSBtcXR0LmNvbm5lY3QoJ21xdHRzOi8vbXF0dC5oc2wuZmk6ODg4MycpOwoKY2xpZW50Lm9uKCdjb25uZWN0JywgZnVuY3Rpb24gKCkgewogIGNsaWVudC5zdWJzY3JpYmUodG9waWMpOwogIGNvbnNvbGUubG9nKCdDb25uZWN0ZWQnKTsKfSk7CiAKbGV0IGNvdW50ID0gMDsKCmNsaWVudC5vbignbWVzc2FnZScsIGZ1bmN0aW9uICh0b3BpYywgbWVzc2FnZSkgewogICAgY29uc3QgdmVoaWNsZV9wb3NpdGlvbiA9IEpTT04ucGFyc2UobWVzc2FnZSkuVlA7CgogICAgLy9Ta2lwIHZlaGljbGVzIHdpdGggaW52YWxpZCBsb2NhdGlvbgogICAgaWYgKCF2ZWhpY2xlX3Bvc2l0aW9uLmxhdCB8fMKgIXZlaGljbGVfcG9zaXRpb24ubG9uZykgewogICAgICByZXR1cm47CiAgICB9CgogICAgY29uc3Qgcm91dGUgPSB2ZWhpY2xlX3Bvc2l0aW9uLmRlc2k7CiAgICAvL3ZlaGljbGVzIGFyZSBpZGVudGlmaWVkIHdpdGggY29tYmluYXRpb24gb2Ygb3BlcmF0b3IgaWQgYW5kIHZlaGljbGUgaWQKICAgIGNvbnN0IHZlaGljbGUgPSB2ZWhpY2xlX3Bvc2l0aW9uLm9wZXIgKyAiLyIgKyB2ZWhpY2xlX3Bvc2l0aW9uLnZlaDsKICAgIAogICAgY29uc3QgcG9zaXRpb24gPSB2ZWhpY2xlX3Bvc2l0aW9uLmxhdCArICIsIiArIHZlaGljbGVfcG9zaXRpb24ubG9uZzsKICAgIGNvbnN0IHNwZWVkID0gdmVoaWNsZV9wb3NpdGlvbi5zcGQ7CgogICAgY29uc29sZS5sb2coIlJvdXRlICIrcm91dGUrIiAodmVoaWNsZSAiK3ZlaGljbGUrIik6ICIrcG9zaXRpb24rIiAtICIrc3BlZWQrIm0vcyIpOwoKICAgIC8vQ2xvc2UgY29ubmVjdGlvbiBhZnRlciByZWNlaXZpbmcgMTAwIG1lc3NhZ2VzCiAgICBpZiAoKytjb3VudCA+PSAxMDApIHsKICAgICAgY2xpZW50LmVuZCh0cnVlKTsKICAgIH0KfSk7"},"asBuffer":null},"loaded":true}}
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
const mqtt = require('mqtt');

//<prefix><version>/journey/<temporal_type>/<transport_mode>/<operator_id>/<vehicle_number>/<route_id>/<direction_id>/<headsign>/<start_time>/<next_stop>/<geohash_level>/<geohash>/
const topic = '/hfp/v1/journey/ongoing/+/+/+/+/+/+/+/+/+/#';

const client  = mqtt.connect('mqtts://mqtt.hsl.fi:8883');

client.on('connect', function () {
  client.subscribe(topic);
  console.log('Connected');
});
 
let count = 0;

client.on('message', function (topic, message) {
    const vehicle_position = JSON.parse(message).VP;

    //Skip vehicles with invalid location
    if (!vehicle_position.lat || !vehicle_position.long) {
      return;
    }

    const route = vehicle_position.desi;
    //vehicles are identified with combination of operator id and vehicle id
    const vehicle = vehicle_position.oper + "/" + vehicle_position.veh;
    
    const position = vehicle_position.lat + "," + vehicle_position.long;
    const speed = vehicle_position.spd;

    console.log("Route "+route+" (vehicle "+vehicle+"): "+position+" - "+speed+"m/s");

    //Close connection after receiving 100 messages
    if (++count >= 100) {
      client.end(true);
    }
});
node v10.16.0