0


i want to save temperature data ( which i get from a paho client) in an array named 'single'. Finally, i want to show the live data in a gauge.
For this i created a service to store the data and to hand out the data to several components.
But i get only the ERR: "" is not assignable to parameter of type '{ value: string; }'.
I would really appreciate some new thoughts on this! Thanks!

My Service:

import { Injectable } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import {Paho} from '../../../node_modules/ng2-mqtt/mqttws31';


@Injectable()
export class ClassdataproviderService {

  public name: string;
  public value: string;

  single = [
    {
      name: 'eins',
      value: '15'
    },
    {
      name: 'zwei',
      value: '20'
    },
    {
      name: 'drei',
      value: '73'
    }
  ];


// Create a client instance


  client: any;
  packet: any;

  constructor() {
    this.client = new Paho.MQTT.Client('wpsdemo.gia.rwth-aachen.de', 8080, 'Steffen');

    this.onMessage();
    this.onConnectionLost();
    // connect the client
    this.client.connect({onSuccess: this.onConnected.bind(this)});
  }

  // called when the client connects


  onConnected() {
    console.log('Connected');
    this.client.subscribe('node/m1/temperature');
    //this.sendMessage('HelloWorld');
  }

  sendMessage(message: string) {
    const packet = new Paho.MQTT.Message(message);
    packet.destinationName = 'World';
    this.client.send(packet);
  }

  // called when a message arrives


  onMessage() {
    this.client.onMessageArrived = (message: Paho.MQTT.Message) => {
      console.log('Message arrived : ' + message.payloadString);
      this.single.push('test', message.payloadString); **//<-- Here i want to push the data in my array**
    };
  }

  // called when the client loses its connection


  onConnectionLost() {
    this.client.onConnectionLost = (responseObject: Object) => {
      console.log('Connection lost : ' + JSON.stringify(responseObject));
    };
  }
Steffn
  • 275
  • 1
  • 6
  • 21

1 Answers1

2

I solved the problem.
You just have to push the data in the array(single) by using this line in the onMessageArrived function:

this.single.push({name: 'test', value: message.payloadString});
Steffn
  • 275
  • 1
  • 6
  • 21