Package: wt-mqtt
WaveletThings MQTT client Python SDKWaveletThings is an open-source IoT platform for data collection, processing, visualization, and device management.SDK supports:未加密和加密（TLS v1.2）连接QoS 0 和 1自动重连所有设备的MQTT接口所有网关的MQTT接口当前的SDK基于paho-mqtt库安装使用pip安装：pip3installwt_mqtt快速开始初始化客户端并发布遥测数据fromwt_mqtt.wt_device_mqttimportWTDeviceMqttClient,WTPublishInfotelemetry={"temperature":41.9,"enabled":False,"currentFirmwareVersion":"v1.2.2"}client=WTDeviceMqttClient(host="things.xiaobodata.com",port=1883,token="A1_TEST_TOKEN")# Connect to WaveletThingsclient.connect()# Sending telemetry without checking the delivery statusclient.send_telemetry(telemetry)# Sending telemetry and checking the delivery status (QoS = 1 by default)result=client.send_telemetry(telemetry)# get is a blocking call that awaits delivery statussuccess=result.get()==WTPublishInfo.WT_ERR_SUCCESS# Disconnect from WaveletThingsclient.disconnect()Connection using TLS要通过SSL上的MQTT连接到WaveletThings，首先，您应该生成一个证书，并使用如下代码：fromwt_mqtt.wt_device_mqttimportWTDeviceMqttClientclient=WTDeviceMqttClient(host="things.xiaobodata.com",port=1883)client.connect(tls=True,ca_certs="mqttserver.pub.pem",cert_file="mqttclient.nopass.pem")client.disconnect()使用设备APIWTDeviceMQTClient提供对Things平台的设备MQTT API的访问。
它允许发布遥测和属性更新、订阅属性更改、发送和接收RPC命令等。订阅共享属性更新如果需要接收共享属性更新，可以使用以下代码：fromtimeimportsleepfromwt_mqtt.wt_device_mqttimportWTDeviceMqttClientdefcallback(client,content,*args):print(content)client=WTDeviceMqttClient("things.xiaobodata.com","A1_TEST_TOKEN")client.connect()client.subscribe_to_attribute("uploadFrequency",callback)client.subscribe_to_all_attributes(callback)whileTrue:sleep(1)发布遥测数据为了向Things发送数据，可以使用如下代码：fromtimeimporttimefromwt_mqtt.wt_device_mqttimportWTDeviceMqttClient,WTPublishInfotelemetry_with_ts={"ts":int(round(time()*1000)),"values":{"temperature":42.1,"humidity":70}}client=WTDeviceMqttClient("things.xiaobodata.com","A1_TEST_TOKEN")# we set maximum amount of messages sent to send them at the same time. it may stress memory but increases performanceclient.max_inflight_messages_set(100)client.connect()results=[]result=Trueforiinrange(0,100):results.append(client.send_telemetry(telemetry_with_ts))fortmp_resultinresults:result&=tmp_result.get()==WTPublishInfo.WT_ERR_SUCCESSprint("Result",str(result))client.disconnect()请求属性为了从Things请求属性值，可以使用以下示例，这个示例获取了"configuration", "targetFirmwareVersion"的值：fromtimeimportsleepfromwt_mqtt.wt_device_mqttimportWTDeviceMqttClientdefon_attributes_change(client,result,*args):print(result)client=WTDeviceMqttClient("things.xiaobodata.com","A1_TEST_TOKEN")client.connect()client.request_attributes(["configuration","targetFirmwareVersion"],callback=on_attributes_change)whileTrue:sleep(1)响应服务端的RPC回调如果您想发送某个RPC请求的响应，可以使用下面代码中的逻辑。
下面的示例连接到Things本地实例并等待RPC请求。
当接收到RPC请求时，客户机将向Things发送响应，其中包含来自具有客户机的机器的数据。importjsonfromtimeimportsleepfrompsutilimportcpu_percent,virtual_memoryfromwt_mqtt.wt_device_mqttimportWTDeviceMqttClient# dependently of request method we send different data backdefon_server_side_rpc_request(client,request_id,request_body):print(request_id,request_body)ifrequest_body["method"]=="getCPULoad":client.send_rpc_reply(request_id,json.dumps({"CPU percent":cpu_percent()}))elifrequest_body["method"]=="getMemoryUsage":client.send_rpc_reply(request_id,json.dumps({"Memory":virtual_memory().percent}))client=WTDeviceMqttClient("things.xiaobodata.com","A1_TEST_TOKEN")client.set_server_side_rpc_request_handler(on_server_side_rpc_request)client.connect()whileTrue:sleep(1)使用网关APIWTGatewayMqttClient继承了WTDeviceMqttClient，因此可以作为常规设备访问其所有API。
此外，网关能够代表连接到它的多个设备。发送遥测和属性可以使用如下代码：fromtimeimporttimefromwt_mqtt.wt_gateway_mqttimportWTGatewayMqttClientgateway=WTGatewayMqttClient("things.xiaobodata.com","GATEWAY_TEST_TOKEN")gateway.connect()gateway.gw_connect_device("Test Device A1")gateway.gw_send_telemetry("Test Device A1",{"ts":int(round(time()*1000)),"values":{"temperature":42.2}})gateway.gw_send_attributes("Test Device A1",{"firmwareVersion":"2.3.1"})gateway.gw_disconnect_device("Test Device A1")gateway.disconnect()从server获取共享属性获取Test Device A1的共享属性，可以使用如下代码：fromtimeimportsleepfromwt_mqtt.wt_gateway_mqttimportWTGatewayMqttClientdefcallback(client,result,*args):print(result)gateway=WTGatewayMqttClient("things.xiaobodata.com","GATEWAY_TEST_TOKEN")gateway.connect()gateway.gw_request_shared_attributes("Test Device A1",["temperature"],callback)whileTrue:sleep(1)响应服务端的RPC回调可以使用如下代码：importjsonfromtimeimportsleepfrompsutilimportcpu_percent,virtual_memoryfromwt_mqtt.wt_gateway_mqttimportWTGatewayMqttClientdefrpc_request_response(client,request_id,request_body):# request body contains id, method and other parametersprint(request_body)method=request_body["data"]["method"]device=request_body["device"]req_id=request_body["data"]["id"]# dependently of request method we send different data backifmethod=='getCPULoad':client.send_rpc_reply(request_id,json.dumps({"CPU percent":cpu_percent()}))elifmethod=='getMemoryLoad':client.send_rpc_reply(request_id,json.dumps({"Memory":virtual_memory().percent}))else:print('Unknown method: '+method)gateway=WTGatewayMqttClient("things.xiaobodata.com","GATEWAY_TEST_TOKEN")gateway.connect()# now rpc_request_response will process rpc requests from serversgateway.gw_set_server_side_rpc_request_handler(rpc_request_response)# without device connection it is impossible to get any messagesgateway.gw_connect_device("Test Device A1")whileTrue:sleep(1)