.NET Core 跨平台物联网开发:SDK 属性、方法、委托、类(四)

2019年12月15日 1898点热度 0人点赞 1条评论
内容纲要

系列教程目录

 (一) 连接阿里云IOT

 (二) 设置委托事件

 (三) 上报属性

 (四)  SDK文档 属性、方法、委托、类

 http://pan.whuanle.cn/index.php?dir=uploads/阿里云IOT/AliIOTXFclient-dll类库&response

下载三个库,头部引入 即可使用

using AliIOTXFClient;

示例下载地址

http://pan.whuanle.cn/index.php?dir=uploads/阿里云IOT/demo示例

 

AliIOTXFClient.XFMQTT 类是核心功能

生成设备属性、服务、事件通讯的topic

public readonly ThingModel thingModel

有四个主要属性,用来获取或设置属性、服务、事件的Topic地址。

 

名称 说明
upTopic 用于上传设备属性数据,有以下几个字段

设备上报属性请求Topic--透传:up_raw 

设备上报属性响应Topic--透传:up_raw_reply

设备上报属性请求Topic--Alink JSON: post

设备上报属性响应Topic--Alink JSON:post_reply

setTopic  设置设备属性,有以下几个字段

下行(透传)响应Topic:down_raw

下行(透传)响应Topic:down_raw_reply

下行(Alink JSON)请求Topic:set

下行(Alink JSON)响应Topic:set_reply

eventTopic  设备事件上报,有以下几个字段

上行(透传) 请求Topic:up_raw

上行(透传)响应Topic:up_raw_reply

上行(Alink JSON)请求Topic:post

上行(Alink JSON)响应Topic:post_reply

serviceTopic  设备服务调用

下行(透传)请求Topic:down_raw

下行(透传)响应Topic:down_raw_reply

下行(Alink JSON)请求Topic:identifier

下行(Alink JSON)Topic:identifier_reply

 

与连接前有关

初始化连接设置

public void Init(string DeviceSecret, string RegionId)

 

生成唯一clientId

public string CreateClientId()

 

创建MQTT连接并与服务器通讯,订阅需要的Topic

public void ConnectMqtt(string[] SubTopic, byte[] QOS = null)

 

委托

 订阅回调 - 当收到服务器消息时

public uPLibrary.Networking.M2Mqtt.MqttClient.MqttMsgPublishEventHandler PubEventHandler;

 

 当 QOS=1或2时,收到订阅触发

public uPLibrary.Networking.M2Mqtt.MqttClient.MqttMsgPublishedEventHandler PubedEventHandler;

 

 向服务器发布 Topic 时

public uPLibrary.Networking.M2Mqtt.MqttClient.MqttMsgSubscribedEventHandler SubedEventHandler;

 

向服务器发布 Topic 失败时

public uPLibrary.Networking.M2Mqtt.MqttClient.MqttMsgUnsubscribedEventHandler UnSubedEventHandler;

 

 断开连接时

public uPLibrary.Networking.M2Mqtt.MqttClient.ConnectionClosedEventHandler ConnectionClosedEventHandler;

 

默认的方法

Default_PubEventHandler(object sender, MqttMsgPublishEventArgs e)
public void Default_PubedEventHandler(object sender, MqttMsgPublishedEventArgs e)
public void Default_SubedEventHandler(object sender, MqttMsgSubscribedEventArgs e)
public void Default_UnSubedEventHandler(object sender, MqttMsgUnsubscribedEventArgs e)
public void Default_ConnectionClosedEventHandler(object sender, EventArgs e)

 

发布消息

上传属性或发布 Topic

public int Subscribe(string PubTopic, byte[] content)

上传属性或发布 Topic

public int Subscribe(string PubTopic, string content)

上传属性或发布 Topic,会将源数据进行 Base 64位加密再上传

public int SubscribeToBase(string PubTopic, byte[] betys)

 

属性上传

设备上传属性--透传

public int Thing_Property_Up_Raw(byte[] bytes)

自定义设备上传属性地址、上传属性--透传。不建议使用,建议使用 Up_Raw(byte[] bytes)

public int Thing_Property_Up_Raw(string up_rawTopic, byte[] bytes)

设备上传属性--透传,转为 Base 64位加密后上传

public int Thing_Property_Up_RawToBase64(byte[] bytes)

设备上传属性--透传,Base 64 位加密后上传--不建议使用此方法

public int Thing_Property_Up_Raw_ToBase64(string up_rawTopic, byte[] bytes)

上传设备属性--Alink Json

public int Thing_Property_Post(string json,bool isToLwer=true)

同上

public int Thing_Property_Post(byte[] json)

上传设备属性--Alink Json

public int Thing_Property_Post<AlinkModel>(AlinkModel model,bool isToLower=true)

 

设置设备属性

收到服务器属性设置命令,返回响应

public int Thing_Property_set(string content,bool isToLower=true)

同上

public int Thing_Property_set(byte[] content)

设备属性下发设置

public int Thing_Property_set<SetJson>(SetJson model,bool isToLower=true)

 

设备事件上报

设备事件上报,以字符串内容上传

public int Thing_Event_up_raw(string content)

设备事件上报,把原始报文 Base64 加密后上传

public int Thing_Event_up_raw_Base64(byte[] content)

设备事件上报 Alink JSON

public int Thing_Event_Post(string content,bool isToLower=true)

设备事件上报 Alink JSON

public int Thing_Event_Post<EventJson>(EventJson model,bool isToLower=true)

 

设备服务调用

设备服务调用--透传

public int Thing_Service_Down_Reply(byte[] content)

设备服务调用

public int Thing_Service_Identifier_Reply(string content,bool isToLower=true)

设备服务调用

public int Thing_Service_Identifier_Reply<ServiceJsonModel>(ServiceJsonModel model,bool isToLower=true)

 

需要注意的是,SDK中,无论是普通订阅还是上传属性响应、下发设置命令、事件、服务调用等,凡是“收”到服务器的消息,均是触发

public uPLibrary.Networking.M2Mqtt.MqttClient.MqttMsgPublishEventHandler PubEventHandler;

如果想区别不同的接收到的Topic,需手动修改方法,将其绑定到委托中。

痴者工良

高级程序员劝退师

文章评论