thinq-connect / pythinqconnect

Apache License 2.0
10 stars 1 forks source link

Example for ThinQMQTTClient #5

Open thecabinet opened 6 days ago

thecabinet commented 6 days ago

Can you provide some example code for how to use ThinQMQTTClient? I assume this is intended to allow users to receive push notifications instead of polling with ThinQApi.

I've tried:

def on_message_received(topic: str, payload: bytes, dup: bool,
                              qos: awscrt.mqtt.QoS, retain: bool,
                              **kwargs):
    print(f"on_message_received: {topic}, {payload}, {dup}, {qos}, {retain}, {kwargs}")

async def main(access_token: str, country_code: str, client_id: str):
    async with ClientSession() as session:
        thinq_api = ThinQApi(...)
        mqtt_client = ThinQMQTTClient(thinq_api, client, on_message_received, ...)
        await mqtt_client.async_init()
        await mqtt_client.async_prepare_mqtt()
        await mqtt_client.async_connect_mqtt()
        await asyncio.sleep(99999)

I've defined similar functions for all the callbacks (per the signatures at https://awslabs.github.io/aws-crt-python/api/mqtt.html#awscrt.mqtt.Connection) and I see my on_connection_success handler getting called, but never on_message_received, even when I'm sleeping through periods when my app receives a notification.

thinq-connect commented 5 days ago

@thecabinet, Thank you for your inquiry. Could you please share the client that you used when creating the ThinQMQTTClient object in the code you provided? I can look into it for you. Since it contains personal information, please send it to the email address thinq-connect@lge.com. Thank you!

thinq-connect commented 4 days ago

@thecabinet, I have received your email. Please note that I will respond here. I have checked the API called by you, and the client registration has been done properly. However, in order to receive the push/event messages from the devices via MQTT, you first need to query the device list, followed by querying the profile and status, and then call the subscription APIs for each device.

Please refer to the code flow below. As the SDK is still in its early stages of release, I kindly ask for your understanding that the detailed documentation is currently being prepared.

device_list = await thinq_api.async_get_device_list()
logger.info("device_list : %s", device_list)

for device in device_list:
    logger.info("device : %s", device)
    device_id = device.get("deviceId")

    profile = await thinq_api.async_get_device_profile(device_id)
    logger.info("profile : %s", profile)

    status = await thinq_api.async_get_device_status(device_id)
    logger.info("status : %s", status)

    response = await thinq_api.async_post_push_subscribe(device_id)
    logger.info("push subscribe : %s", response)

    response = await thinq_api.async_post_event_subscribe(device_id)
    logger.info("event subscribe : %s", response)