AWSIoTMQTTClient - how can I tell if the client is offline?

0

Hello all,
I am new to aws IoT and AWSIoTPythonSDK :)

I have a python script (which is based on basicPubSub.py) that reads small files from directory and send their content to topic using :
self.myAWSIoTMQTTClient.publish(topic, message_to_send, 1)

I have long disconnection periodד, and i want to stop sending message to the topic while the device is offline (I dont want the messages to be send to the offline queue).
Can I tell the client status using the SDK?

p.s
I thought about copy the logic from MqttCore.publish() method, but it based on:
self._client_status.get_status()
and I dont think I can access this data...

p.s.s
I can NOT use greengrass

Thanks,
Aya

已提問 5 年前檢視次數 324 次
1 個回答
0

I found the solution:

    self.myAWSIoTMQTTClient.onOffline = self.myOnOfflineCallback  
    self.myAWSIoTMQTTClient.onOnline = self.myOnOnlineCallback  

def myOnOfflineCallback(self):  
    self.sent_to_topic = False  
    print("OFFLINE")  

def myOnOnlineCallback(self):  
    self.sent_to_topic = True  
    print("ONLINE")  

def publish(self, topic, message_to_send):  
    if self.sent_to_topic:  
        self.myAWSIoTMQTTClient.publish(topic, message_to_send, 1)  
        print(f"Published topic {topic}")
已回答 5 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南