android - 当数据来自 native 中的 mqtt 时如何增加静态变量值?

标签 android react-native

每次在 onMessageArrived() 中接收数据时,我想增加一个特定的静态变量。在 onMessageArrived 方法中意味着当新数据来自 mqtt 时,想要增加一个初始设置为 0 的静态变量。任何人都可以帮助我实现这一点。我是 React Native 的初学者。提前致谢。

import React, { Component } from 'react';
import init from 'react_native_mqtt';
import { AsyncStorage, StyleSheet, Text, View } from 'react-native';
import Pie from 'react-native-pie';

init({
  size: 10000,
  storageBackend: AsyncStorage,
  defaultExpires: 1000 * 3600 * 24,
  enableCache: true,
  sync: {},
});



export default class MqttLog extends Component {
  constructor(props) {
    super(props);

    const client = new Paho.MQTT.Client('iot.eclipse.org', 443, 'uname');
    client.connect({ onSuccess: this.onConnect, useSSL: true });
    client.onConnectionLost = this.onConnectionLost;
    client.onMessageArrived = this.onMessageArrived;


    this.state = {
      text: '10',
      client,

    };


  }




  onConnect = () => {
    const { client } = this.state;
    client.subscribe('WORLD');
    // this.pushText('connected');
    console.log('connect');
  };

  onConnectionLost = responseObject => {
    if (responseObject.errorCode !== 0) {
      var connectionLostMessage = `connection lost: ${responseObject.errorMessage}`;
      console.log(connectionLostMessage);
      // this.pushText(`connection lost: ${responseObject.errorMessage}`);
    }
  };



  onMessageArrived = message => {



    var msg = message.payloadString;
    var messageResult = `${msg}`;

    this.setState({ text: messageResult });

    console.log(this.state.text);
  };

  render() {
    // const { text } = this.state;
    // console.log(this.state);

    return (
      <View style={styles.container}>
        {/* {text.map(entry => <Text>{entry}</Text>)} */}
        {/* <Text>Data</Text>
        <Text>{this.state.text}</Text> */}

              <Pie
              radius={150}
              innerRadius={130}
              series={[this.state.text, 30, 60]}
              colors={['#EAD026', '#FAFAFA', '#EAD026']} 
              backgroundColor = '#FAFAFA'
              />
      </View>
    );
  }
}


const styles = StyleSheet.create({

  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'space-around',
  },
  gauge: {
    position: 'absolute',
    width: 100,
    height: 100,
    alignItems: 'center',
    justifyContent: 'center',
  },
  gaugeText: {
    backgroundColor: 'transparent',
    color: '#000',
    fontSize: 24,
  },
});

最佳答案

您可以将新值附加到先前的状态值 -

let messageResult = `${msg}`;
let oldMessage = this.state.text;
let newMessageIntValue = parseInt(messageResult) + parseInt(oldMessage);
this.setState({text:newMessageIntValue});

注意 - 如果有任何其他问题,请告诉我,我刚刚在 React Native 中集成了 MQTT。

关于android - 当数据来自 native 中的 mqtt 时如何增加静态变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54108161/

相关文章:

java - 如何解决重复的 zip 条目

react-native - _clearer 不是运行 React Native 应用程序时的函数错误

react-native - 如何自动打开表情符号键盘? [ react native ]

javascript - 使用 param 调用函数时出现错误

android - 如何在前台刷新原生 Deeplink 应用程序?

java - 我无法从模型中获取数据

android - 如何将 azure sql 数据库中的表连接到 Easy Tables 的表?

android - 使用 Volley 在谷歌地图 v2 上添加多个标记

android - 在 RemoteViews 中获取 ImageView 的大小

css - 如何在 React Native 中使用 flexbox 使一项居中?