AWSIoT所有设备离线。
创始人
2024-09-25 22:30:24
0

可以通过以下示例代码,使用 AWS Python SDK (Boto3) 来检查设备是否在线。

import boto3
import time

iot = boto3.client('iot')

def get_device_status(device_id):
    response = iot.describe_endpoint(endpointType='iot:Data-ATS')
    endpoint = response['endpointAddress']
    client_id = device_id + str(time.time())
    mqtt = boto3.client('iot-data', endpoint_url=endpoint)
    try:
        response = mqtt.publish(
            topic='$aws/things/{}/shadow/get'.format(device_id),
            qos=1,
            payload='{}')
        response = mqtt.subscribe(topic='$aws/things/{}/shadow/update'.format(device_id), qos=1)
        status_response = ''
        while True:
            status_response = mqtt.receive()
            if status_response.get('status'):
                break
        shadow = json.loads(status_response['payload'].decode('utf-8'))['state']['reported']
        return shadow['state']['reported']['connected']
    except Exception as e:
        print("Exception occurred: ", e)
        return False


def main():
    device_id_list = ['device_1', 'device_2', 'device_3']
    for device_id in device_id_list:
        status = get_device_status(device_id)
        print(f"Device {device_id} is online: {status}")

if __name__ == "__main__":
    main()

这个示例代码将会检查 AWS IoT 中给定设备的在线状态,使用 describe_endpoint() 函数返回 AWS IoT 数据端点的 URL,使用 MQTT 协议来进行数据传输,并对设备的在线状态进行了判断和输出。

相关内容

热门资讯

黑科技辅助挂"广东雀... 黑科技辅助挂"广东雀神智能插件"确实是真的有辅助软件(哔哩哔哩)1、完成广东雀神智能插件辅助器v3....
黑科技攻略"聚财ap... 黑科技攻略"聚财app辅助"本来确实有辅助器(哔哩哔哩)1、用户打开应用后不用登录就可以直接使用,点...
透视免费"雀友会免费... 透视免费"雀友会免费自动挂机脚本"总是存在有辅助app(哔哩哔哩)1、超多福利:超高返利,海量正版游...
据目击者称"欢乐对决... 据目击者称"欢乐对决脚本辅助器ios"好像真的是有辅助工具(哔哩哔哩)暗藏猫腻,小编详细说明欢乐对决...
透视黑科技"微信决胜... 透视黑科技"微信决胜游戏辅助"原来真的是有辅助攻略(哔哩哔哩)1、微信决胜游戏辅助有没有辅助教程、微...
技巧辅助挂"欢乐对决... 技巧辅助挂"欢乐对决手游辅助"总是存在有辅助方法(哔哩哔哩)1、超多福利:超高返利,海量正版游戏,欢...
据了解"新众乐辅助&... 据了解"新众乐辅助"都是是有辅助脚本(哔哩哔哩)1、新众乐辅助有没有辅助教程、新众乐辅助有透视吗等服...
透视挂透视"福建天天... 透视挂透视"福建天天开心辅助器"好像是有辅助方法(哔哩哔哩)1)福建天天开心辅助器免费钻石:进一步探...
透视玄学"边锋老友怎... 透视玄学"边锋老友怎么开挂"都是是真的有辅助app(哔哩哔哩)1、边锋老友怎么开挂模拟器是什么优化,...
黑科技攻略"大唐山西... 黑科技攻略"大唐山西辅助软件"都是确实有辅助神器(哔哩哔哩)1、很好的工具软件,可以解锁游戏的大唐山...