首页 > 解决方案 > 如何在 mqtt.client 循环中处理 unix.timestamp 解码继续消息

问题描述

我在我的 python 代码中连接到一个 mqtt 代理,它向我发送一个 json 对象

{值:7504,时间戳:1562595566}

我想解码时间戳,然后通过 http post 或另一个 mqtt 客户端将值发送到另一个服务器。所以它基本上是流转发。

所以在我开始将它发送到服务器之前,我只想先打印解码后的值。这应该是可能的,因为我每 10 秒收到一次消息。但是,一旦我输入解码 (print(datetime.fromtimestamp(unix_time_from_json_object))),我将无法再打印有效负载。没有异常被调用。所以我把解码放在一个函数中,并认为线程在这里可能会有所帮助。但这也不起作用。我的想法不多了。

# -*- coding: utf-8 -*-
# Author: 
# Company: 
#
# Program: 
#
#
# ----------------------------------------------------------------------------------------------------------------------
# Module
import paho.mqtt.client as mqtt
import json
from threading import Thread
from datetime import datetime
from threading import Thread


# ----------------------------------------------------------------------------------------------------------------------
# Variablen
mqtt_broker = "broker_address"
client_id = "some_client_id"
client_user_id = "client_user_id"
client_pw = "some_password"
server = "http://{ip_adress}/"
topic = "topic/1/topic/#"

# ----------------------------------------------------------------------------------------------------------------------
# Functions

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    # Manual subscription to the topics of the charging stations on the sensor things server
    #for x in range(54, 758):
    #    client.subscribe("v1.0/Datastreams(" + str(x) + ")/Observations")
    client.subscribe(topic)


def time_decode(unix_time):
    utc = datetime.fromtimestamp(unix_time)
    return utc


# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    # print(msg.topic +" "+str(msg.payload))
    payload = json.loads(msg.payload)
    utc = payload['timestamp']
    print(time_decode(utc))
    print(payload)
    #threading.Thread(target=time_decode, args=(utc)).start()
    #t = Thread(target=time_decode, args=(payload['timestamp'])
    #t.start()

    #print(datetime.utcfromtimestamp(payload['timestamp']).strftime('%Y-%m-%d %H:%M:%S'))



# ----------------------------------------------------------------------------------------------------------------------
# Main program

client = mqtt.Client()
client.username_pw_set(client_user_id, client_pw)
client.on_connect = on_connect
client.on_message = on_message

# client.connect("ip_adress", 1883, 60)
client.connect(mqtt_broker, 1883, 60)
client.loop_forever()
#thread2 = Thread(target=client.loop_forever)


#thread2.start()

所以我首先想要的是每次收到消息时打印解码的时间。其次,我想用值和日期时间戳构建一个新的 json 对象,并通过 http post 或 mqtt 将其发送到另一台服务器

标签: pythonmqttunix-timestampdecoding

解决方案


找到了问题的解决方案。代码本身是正确的。

我将代码编辑为

try:
    print(time_decode(utc))
except Exception as e:
    print(e)
    print(e.args)
print(payload)

这导致了一条错误消息(有效参数)。因此事实证明,unix 时间戳是作为整数提供的,包括 ms。因此,我只需要将值除以 1000。


推荐阅读