Skip to content
Snippets Groups Projects
Commit 358cd15d authored by Janne Alatalo's avatar Janne Alatalo :neutral_face:
Browse files

Update the app to use the new mqtt websocket api

parent 54af653e
No related branches found
No related tags found
No related merge requests found
...@@ -27,10 +27,10 @@ class App extends Component { ...@@ -27,10 +27,10 @@ class App extends Component {
let width = window.innerWidth; let width = window.innerWidth;
let height = window.innerHeight; let height = window.innerHeight;
this.state = { lam_points: Map(), mapSize: { width, height } }; this.state = { lam_points: Map(), mapSize: { width, height } };
get_lam_points("http://tie.digitraffic.fi", this.update_state.bind(this)) get_lam_points("https://tie.digitraffic.fi", this.update_state.bind(this))
.then(() => { .then(() => {
connect_lam_ws("ws://tie.digitraffic.fi", this.update_state.bind(this)); return connect_lam_ws("mqtts://tie.digitraffic.fi:61619", this.update_state.bind(this));
}) });
this.handleResize = this.handleResize.bind(this); this.handleResize = this.handleResize.bind(this);
} }
......
import { Map } from 'immutable'; import { Map } from 'immutable';
import mqtt from 'mqtt';
import LamStation from './lam-station.js'; import LamStation from './lam-station.js';
...@@ -86,20 +87,38 @@ export function get_lam_station_data(api_url, set_state) { ...@@ -86,20 +87,38 @@ export function get_lam_station_data(api_url, set_state) {
export function connect_lam_ws(api_url, update_state) { export function connect_lam_ws(api_url, update_state) {
let data_buff = []; let data_buff = [];
let url = `${api_url}/api/v1/plain-websockets/tmsdata`; let url = `${api_url}/mqtt`;
let socket = new WebSocket(url); // The username and password is documented on digitraffic api documentation site here:
socket.addEventListener('open', () => { // https://www.digitraffic.fi/tieliikenne/#websocket-rajapinnat
console.info(`WebSocket open to ${url}`); let client = mqtt.connect(url, {
username: 'digitraffic',
password: 'digitrafficPassword',
}); });
socket.addEventListener('message', (msg) => { client.on('connect', () => {
let data = JSON.parse(msg.data).sensorValue; console.info(`MQTT connection open to ${url}`);
const topic = "tms/#";
client.subscribe(topic, (err) => {
if (err) {
console.error(`Subscription error: ${err}`);
} else {
console.log(`Subscribed to ${topic}`);
}
});
});
client.on('error', (err) => {
console.error(`error: ${err}`);
});
client.on('message', (topic, msg) => {
let data = JSON.parse(msg.toString());
if (!data) { if (!data) {
return return
} }
let id = data.roadStationId; let id = data.roadStationId;
let value = data.sensorValue; let value = data.sensorValue;
let name = data.name; let name = data.name;
data_buff.push({id, value, name}) if (id && value && name) {
data_buff.push({ id, value, name });
}
}); });
function buffer_flusher() { function buffer_flusher() {
...@@ -111,6 +130,7 @@ export function connect_lam_ws(api_url, update_state) { ...@@ -111,6 +130,7 @@ export function connect_lam_ws(api_url, update_state) {
let { id, value, name } = obj; let { id, value, name } = obj;
let point = lam_points.get(id); let point = lam_points.get(id);
if (!point) { if (!point) {
console.error(`point not found ${id}`);
continue; continue;
} }
if (name === "KESKINOPEUS_5MIN_LIUKUVA_SUUNTA1") { if (name === "KESKINOPEUS_5MIN_LIUKUVA_SUUNTA1") {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment