python Mqtt订阅如何删除第一个会话/消息 [英] python Mqtt subscribe how to delete first session/message

查看:123
本文介绍了python Mqtt订阅如何删除第一个会话/消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用具有 HA 作为代理的 python 创建了一个 Mqtt 订阅......当我建立第一个连接时,我开始一系列我连接的所有 mqtt 按钮的消息,即使我没有激活它们,因为我的订阅是房子"/点击/# ".列表完成后……如果你按下一个按钮,我就会开始收到我需要的消息……有没有办法删除它给我的第一个列表.

I created a Mqtt subscribe with python that has HA as broker … when I make the first connection I start a series of messages of all the mqtt buttons that I connected even if I am not activating them because my subscription is "house/click/ # ". Once the list is done … if you press a button I start getting the messages I need … is there a way to delete this first list that it gives me.

这是我使用的代码:

import paho.mqtt.client as paho
import pandas as pd
import pyautogui
import os
import platform


def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))


def on_message(client, userdata, msg):
    pyautogui.FAILSAFE=False
    os_system=platform.system()


    # Selezione area dello screenshot

    if os_system == 'Darwin':
        region=(10,80, 850, 100) #MAC
    else:
        region=(60,130,900,250) #RASP

    tab_pos=pd.read_csv('table_position.csv')
    print(msg.topic+"  "+str(msg.payload)+" "+str(msg.qos))
    mqtt_name=str(msg.topic).split("click/")[0]+str(msg.topic).split("click/")[1]
    mqtt_payload=msg.payload.decode('utf-8')
    index_loc=tab_pos.index[tab_pos['mqtt_topic'] == mqtt_name].tolist()
    x_loc=tab_pos.iloc[index_loc[0]][0]
    y_loc=tab_pos.iloc[index_loc[0]][1]
    new_xloc=x_loc+region[0]
    new_yloc=y_loc+region[1]
    if tab_pos.iloc[index_loc[0]][5] == "cover":
        if str(mqtt_payload) == "OPEN":
            if str(mqtt_payload) == "STOP":
                pyautogui.click(new_xloc+8,new_yloc+2)
                pyautogui.moveTo(region[0],region[1])
                print(str(new_xloc)+","+str(new_yloc))
            else:
                pyautogui.click(new_xloc+8,new_yloc+2)
                pyautogui.moveTo(region[0],region[1])
                print(str(new_xloc)+","+str(new_yloc))
        if str(mqtt_payload) == "CLOSE":
            if str(mqtt_payload) == "STOP":
                pyautogui.click(new_xloc+8,new_yloc+25)
                pyautogui.moveTo(region[0],region[1])
                print(str(new_xloc)+","+str(new_yloc))
            else:
                pyautogui.click(new_xloc+8,new_yloc+25)
                pyautogui.moveTo(region[0],region[1])
                print(str(new_xloc)+","+str(new_yloc))
    else:
        pyautogui.click(new_xloc+10,new_yloc+10)
        pyautogui.moveTo(region[0],region[1])
        print(str(new_xloc)+","+str(new_yloc))


client = paho.Client(client_id = "atena_mqtt_click")
client.on_subscribe = on_subscribe
client.on_message = on_message
client.username_pw_set("xxxx", "xxxxx")
client.connect("XXX.XXX.XXX.XXX", 1883)
client.subscribe("house/click/#", qos=1)
client.loop_forever()

这是输出

Subscribed: 1 (1,)
house/click/xxxxxx/light b’OFF’ 0
711,104
house/click/xxxxxx/alarm b’OFF’ 0
454,134
house/click/xxxxxx/light b’OFF’ 0
795,104
house/click/xxxxxxx/cover b’STOP’ 0

在所有这些消息汇集在一起​​之后,由于我对按钮的操作,我可以开始接收消息

after these messages that all come together I can begin to have the messages due to my actions on the buttons

有人可以告诉我如何在没有这些第一行的情况下进行干净的会话吗?

someone could tell me how I can do a clean session without having these first lines?

谢谢

推荐答案

检查您的 MQTT 发布者标志.如果您的发布商使用了保留标志",您需要将其设为 false.

Check your MQTT Publisher flag. If your publisher is using the 'retain flag' you need to make it false.

并且要从主题中删除保留的消息,客户端必须将其替换为保留标志设置为 True 的另一条消息,或者客户端必须在该主题上发布保留标志设置为 True 的空白消息.这是删除保留消息的唯一方法.

and to delete a retained message from a topic, a client must either replace it with another message with retain flag set to True OR a client must publish a blank message with retain flag set to True on that topic. This is the only way to delete a retained message.

"保留消息是一条普通的 MQTT 消息,保留标志设置为 true.代理存储最后保留的消息和该主题的相应 QoS.每个订阅与保留消息主题匹配的主题模式的客户端在订阅后立即收到保留消息.代理仅存储每个主题的一个保留消息".来源:https://www.hivemq.com/blog/mqtt-essentials-part-8-retained-messages/

"A retained message is a normal MQTT message with the retained flag set to true. The broker stores the last retained message and the corresponding QoS for that topic. Each client that subscribes to a topic pattern that matches the topic of the retained message receives the retained message immediately after they subscribe. The broker stores only one retained message per topic". Source: https://www.hivemq.com/blog/mqtt-essentials-part-8-retained-messages/

这篇关于python Mqtt订阅如何删除第一个会话/消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆