Aws-iot-device-sdk-python-v2: AWS IOT MQTT مع جلسة ثابتة = صحيح ولكن الاشتراك فشل

تم إنشاؤها على ٣١ مايو ٢٠٢٠  ·  4تعليقات  ·  مصدر: aws/aws-iot-device-sdk-python-v2

أعزائي،

أثناء تجربة AWS IOT MQTT مع جلسة ثابتة ، وجدت أن الوسيط أعاد "session_present = True" ردًا على طلب الاتصال الذي يحتوي على "clean_session = False".
وهو أمر متوقع لأنني أعدت الاتصال في غضون 60 دقيقة كما هو محدد بواسطة AWS Documentation (عمليا كان أقل من 5 دقائق)

القضية :
وفقًا للنظرية ، إذا كانت "session_present = True" ، فإن متاجر الوسيط تقوم بتأكيد معلومات الجلسة السابقة وبالتالي لا داعي للاشتراك في الموضوعات مرة أخرى.
ولكن إذا تخطيت خطوة الاشتراك في الموضوع فلن تصلني أية رسائل.
هل هو السلوك الواضح؟ هل أحتاج إلى إلغاء الاشتراك بالرغم من أن "session_present = True"؟

أنا أستخدم aws-iot-device-sdk-python-v2

def conenct_mqtt(mqtt_connection):
    connect_future = mqtt_connection.connect()

    excep_count = 0
    while True:
        print(f"[{datetime.now()}]Connecting......")
        try:
            # Future.result() waits until a result is available
            result = connect_future.result(timeout=30)
            print(f"Connected! session persistent = {result['session_present']}")
            return result['session_present']

        except Exception as exc:
            print(f"[{datetime.now()}]Error while connecting {exc}")
            time.sleep(10)
            excep_count=excep_count+1
            if excep_count > 5 :
                print(f"[{datetime.now()}]Exiting Script.....")
                quit()
bug guidance needs-triage

التعليق الأكثر فائدة

مرحبًاGauravPatni

أرجوك حاول:

   if not is_session_persistent:
        XXX
   else:
        mqtt_connection.on_message(on_message_received) #register your callback
        print("-----------Session is Persistent---------")

اتصالك متصل ، ولكنك لا تزال بحاجة إلى تسجيل رد الاتصال الخاص بك على كائن _Connection_ لتلقي الرسالة ، عبر

ال 4 كومينتر

أهلا،
شكرا لك على التواصل. لقد اختبرت ذلك وعملت الجلسة المستمرة بالنسبة لي.
في نموذج pubsub.py ، إذا فُقد الاتصال وعاد ، يستمر mqtt_conneciton في تلقي الرسالة. لكنك تحتاج إلى تسجيل رد الاتصال على كائن _Connection_ لتلقي الرسالة.
على سبيل المثال ، إذا قمت بإنشاء كائن _Connection_ آخر ، باستخدام "session_present = True" كنتيجة اتصال ، فستحتاج إلى استدعاء on_message لتعيين رد الاتصال. ثم يمكنك استلام الرسالة الواردة عبر رد الاتصال الذي قمت بتعيينه.
شكرا لك!

تضمين التغريدة
شكرا لمساعدتك. لكن لسوء الحظ ، ما زلت غير قادر على حل المشكلة.
هل يمكنك إلقاء نظرة على الكود التالي لمعرفة ما هي المشكلة؟

خطواتي:
تشغيل البرنامج النصي للاشتراك
تشغيل نص النشر
// الرسائل المستلمة في برنامج الاشتراك
الآن إنهاء كلا النصين
انتظر دقيقة أو أكثر
تشغيل البرنامج النصي للاشتراك
// إرجاع نتيجة الاتصال المستقبلية .... النتيجة ['session_present'] = صحيح. ومن ثم تم تخطي حلقة الاشتراك
تشغيل نص النشر
// المشكلة: لم يتم استلام رسائل في البرنامج النصي للاشتراك

def conenct_mqtt(mqtt_connection):
    connect_future = mqtt_connection.connect()

    excep_count = 0
    while True:
        print(f"[{datetime.now()}]Connecting......")
        try:
            # Future.result() waits until a result is available
            result = connect_future.result(timeout=30)
            print(f"[{datetime.now()}]Connected! session persistent = {result['session_present']}")
            return result['session_present']

        except Exception as exc:
            print(f"[{datetime.now()}]Error while connecting {exc}")
            time.sleep(10)
            excep_count=excep_count+1
            if excep_count > 5 :
                print(f"[{datetime.now()}]Exiting Script.....")
                quit()

if __name__ == '__main__':
    # Spin up resources
    event_loop_group = io.EventLoopGroup(1)
    host_resolver = io.DefaultHostResolver(event_loop_group)
    client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)


    mqtt_connection = mqtt_connection_builder.mtls_from_path(
        endpoint=host,
        cert_filepath=certificatePath,
        pri_key_filepath=privateKeyPath,
        client_bootstrap=client_bootstrap,
        ca_filepath=rootCAPath,
        on_connection_interrupted=on_connection_interrupted,
        on_connection_resumed=on_connection_resumed,
        client_id=clientId,
        clean_session=False, # set as persistent sessions
        keep_alive_secs=60, # 1 min as any data communication or PINGREQ . x1.5 to detect disconenct
        reconnect_min_timeout_secs=2,
        reconnect_max_timeout_secs =300,
        ping_timeout_ms = 10000, # wait for 6 secs for ping response
        )

    print(f"[{datetime.now()}]Connecting with client ID '{clientId}'...")
    is_session_persistent = conenct_mqtt(mqtt_connection)

    # subscribe to topics only if previous session was lost 
    if not is_session_persistent:
        topic_index = 0
        excep_count = 0
        while topic_index < 2:
            # Subscribe
            topic = topic_list[topic_index]
            print(f"[{datetime.now()}]Subscribing to topic '{topic}'...")
            subscribe_future, packet_id = mqtt_connection.subscribe(
                topic=topic,
                qos=mqtt.QoS.AT_LEAST_ONCE,
                callback=on_message_received)


            try:
                subscribe_result = subscribe_future.result()
                print(f"[{datetime.now()}]Subscribed with {str(subscribe_result['qos'])}")
                topic_index+=1
                excep_count = 0
            except Exception as exc:
                print(f"Error:Subscribe: {exc}")
                excep_count+=1
                if excep_count > 3 :
                    print(f"[{datetime.now()}]Exiting Script.....")
                    quit()
                time.sleep(10)
    else:
        print("-----------Session is Persistent---------")




    # Wait for all messages to be received.
    # This waits forever if count was set to 0.
    if max_count != 0 and not received_all_event.is_set():
        print("Waiting for all messages to be received...")

    received_all_event.wait()
    print("{} message(s) received.".format(received_count))

مرحبًاGauravPatni

أرجوك حاول:

   if not is_session_persistent:
        XXX
   else:
        mqtt_connection.on_message(on_message_received) #register your callback
        print("-----------Session is Persistent---------")

اتصالك متصل ، ولكنك لا تزال بحاجة إلى تسجيل رد الاتصال الخاص بك على كائن _Connection_ لتلقي الرسالة ، عبر

شكرا

مرحبًاGauravPatni

أرجوك حاول:

   if not is_session_persistent:
        XXX
   else:
        mqtt_connection.on_message(on_message_received) #register your callback
        print("-----------Session is Persistent---------")

اتصالك متصل ، ولكنك لا تزال بحاجة إلى تسجيل رد الاتصال الخاص بك على كائن _Connection_ لتلقي الرسالة ، عبر

شكرا TingDaoK

بعد الإضافة
mqtt_connection.on_message (on_message_received) # تسجيل رد الاتصال الخاص بك
قضية حلها !!

مرة أخرى شكرا على الرد السريع !!

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات