Currently, I am working on a project that involves displaying real-time data from a server. During the testing phase using MQTT client WebSocket tools like HiveMQ, I encountered an issue. While I can see the value of the data in the Chrome console, I'm struggling to visualize this value graphically using NGX-GAUGE.
When attempting to retrieve the value from an MQTT broker, the ngx-gauge component fails to update the gauge's green line in real time as expected.
import { Component, OnInit } from '@angular/core';
import { Paho } from 'ng2-mqtt/mqttws31';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
valore:String;
gaugeType = "semi";
gaugeValue=this.valore;
gaugeLabel = "Valore";
gaugeAppendText = "km/hr";s
animate=true;
duration=1500;
private client;
mqttbroker = 'broker.mqttdashboard.com';
ngOnInit() {
this.client = new Paho.MQTT.Client(this.mqttbroker, Number(8000), 'client1');
this.client.onMessageArrived=this.onMessageArrived.bind(this);
this.client.onConnectionLost=this.onConnectionLost.bind(this);
this.client.connect({onSuccess: this.onConnect.bind(this)});
}
onConnect() {
console.log('onConnect');
this.client.subscribe('testtopic/40/xxx');
}
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log('onConnectionLost:' + responseObject.errorMessage);
}
}
onMessageArrived(message) {
console.log('onMessageArrived: ' + message.destinationName + ': ' + message.payloadString);
if (message.destinationName.indexOf('xxx') !== -1) {
this.valore = (message.payloadString);
}
}
}
The goal is to display the value and have the ngx-gauge's line respond in real time according to that value.