2017-10-10 1 views
0


温度データ(pahoクライアントから取得)を 'single'という配列に保存します。最後に、ライブデータをゲージに表示したい。
私はこれを使ってデータを保存し、いくつかのコンポーネントにデータを渡すサービスを作成しました。
しかし、私はERRだけを取得します: ""は、タイプ '{value:string; } '。
私は本当にこれについていくつかの新しい考えに感謝します!ありがとう!配列データをpahoクライアントの角4に保存します。

マイサービス:

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)); 
    }; 
    } 

答えて

2

私は問題を解決しました。
あなただけonMessageArrived機能で、このラインを使用して配列(シングル)にデータをプッシュする必要があります。

this.single.push({name: 'test', value: message.payloadString}); 
関連する問題