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년 전328회 조회
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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인