@digitransit/

Vehicle_Positions_MQTT_raw_JSON

Nodejs

Print raw JSON data received from the HFP API

fork
loading
Files
  • index.js
  • package-lock.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.30279481074618","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgbXF0dCA9IHJlcXVpcmUoJ21xdHQnKTsKCmNvbnN0IHRvcGljID0gJy9oZnAvdjEvam91cm5leS8jJzsKCmNvbnN0IGNsaWVudCAgPSBtcXR0LmNvbm5lY3QoJ21xdHRzOi8vbXF0dC5oc2wuZmk6ODg4MycpOwoKY2xpZW50Lm9uKCdjb25uZWN0JywgKCkgPT4gewogIGNsaWVudC5zdWJzY3JpYmUodG9waWMpOwogIGNvbnNvbGUubG9nKCdDb25uZWN0ZWQnKTsKfSk7CiAKbGV0IGNvdW50ID0gMDsKCmNsaWVudC5vbignbWVzc2FnZScsICh0b3BpYywgbWVzc2FnZSkgPT4gewogICAgaWYgKGNvdW50KysgPCAzKSB7CiAgICAgIGNvbnNvbGUubG9nKCItLS0iKTsKICAgICAgY29uc29sZS5sb2codG9waWMpOwogICAgICBjb25zb2xlLmxvZyhKU09OLnN0cmluZ2lmeShKU09OLnBhcnNlKG1lc3NhZ2UpLCBudWxsLCA0KSk7CiAgICB9IGVsc2UgewogICAgICAvL0Nsb3NlIGNvbm5lY3Rpb24gYWZ0ZXIgcmVjZWl2aW5nIDMgbWVzc2FnZXMKICAgICAgY2xpZW50LmVuZCh0cnVlKTsKICAgIH0KfSk7"},"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
const mqtt = require('mqtt');

const topic = '/hfp/v1/journey/#';

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

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

client.on('message', (topic, message) => {
    if (count++ < 3) {
      console.log("---");
      console.log(topic);
      console.log(JSON.stringify(JSON.parse(message), null, 4));
    } else {
      //Close connection after receiving 3 messages
      client.end(true);
    }
});
node v10.16.0