GoogleCloudPlatform / iot-core-micropython

Apache License 2.0
52 stars 14 forks source link

Token Refesh #18

Open Mendez83 opened 3 years ago

Mendez83 commented 3 years ago

By default config token expire in 12 hours. It's needed to be refreshed or recreated ?

BetterAutomations commented 3 years ago

I noticed the same thing. I'll probably set a timer to renew the token an hour before it expires. Or reset the device and let it re-create.

BetterAutomations commented 3 years ago

I implemented it like this in a while loop for now. Here is pseudocode.

import utime as time
from machine import const

EPOCH_OFFSET = const(946684800)

def get_client():
    # Return the mqtt client, fetch jwt_expire_time

def expired():
    now = time.time() + EPOCH_OFFSET
    # One hour buffer
    return now >= (jwt_expire_time - 60*60)

while True:
    if expired():
        client.disconnect()
        client = get_client()

There's a lot of code I'm not showing here such as get_client() or how I fetch jwt_expire_time but I hope that's enough to give you an idea.