0

this code subscribes me to a broker and I output the messages coming for that particular family of topics. Now when I start for the first time, I have a list of messages as many as there are devices ... a kind of status check on all devices. In the code as written for each line produces an action on a remote screen via the pyautogui libraries. Is it possible to tell him that for the first n lines he sends me not to do anything and then start doing the actions I told him?

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()

this is the output

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
hardillb
  • 54,545
  • 11
  • 67
  • 105
chpiero
  • 41
  • 5

1 Answers1

0

No, you can not tell the MQTT client library to ignore messages on topics you have subscribed to.

As mentioned in the answer to previous question you asked about this, these are most likely messages published with the retained flag set. Any client subscribing to a topic with a retained message will always receive this message before any new messages are published.

In this case it sounds like the retained messages hold the current state of the system, so doesn't make sense to ignore them, you should use them to ensure the GUI you are creating is in sync with the system.

If you have changed the publisher to no longer set the retained flag and you are now trying to remove these messages from the broker as they no longer reflect the current state of the system then you can publish a message with a NULL payload and the retained but set. e.g. with the mosquitto_pub command line tool:

mosquittto_pub -t house/click/xxxxxxx/cover -n

You can read more about retained MQTT messages on the HiveMQ blog here

hardillb
  • 54,545
  • 11
  • 67
  • 105