Ali cloud Things .NET Core Client | CZGL.AliIoTClient:. 1 connection Ali cloud Things

Documents directory:


 1) Ali cloud Things

Ali cloud Things supports multiple communication protocols CZGL.AliIoTClient used MQTT communication protocol, communication library  M2MQTT .
Ali cloud of things, there are two data transmission data transmission, respectively,  透传 and  Alink jsontwo ways only attribute to read / write, event reporting, there are differences in service calls on these four Topic, other intercellular communication, ordinary Topic, responses, etc., no difference. Recommended Alink json data upload issued.

Transfer form Nature Explanation
Alink json json According to Ali cloud need to configure things json document
Penetrate Binary When using the properties, events, and service functions, binary data, there are specific requirements of transmission bits

CZGL.AliIoTClient support Alink json and pass-through, SDK has two client categories,

class Explanation
AliIoTClientJson In the form of a transmission Alink json client
AliIoTClientBinary: In binary form for the transmission of client

Both are largely the same, only in the form of data transmission services in the event properties are different. Thus AliIoTClientJson mainly be described later.


2) connected to Ali cloud IOT

2.1) Creating Client

When you create a client class, need to pass  DeviceOptions an object, we need to advance in networking Ali Clouds console, copy the information and other key equipment, to fill  DeviceOptions in.

Example:

            AliIoTClientJson client = new AliIoTClientJson(new DeviceOptions
            {
                ProductKey = "a1xrkGSkb5R",
                DeviceName = "mire",
                DeviceSecret = "CqGMkOBDiKJfrOWp1evLZC2O6fsMtEXw",
                RegionId = "cn-shanghai"
            });

2.2) Set to subscribe Topic

Before connecting the client should be set to subscribe to ordinary Topic and configure whether to receive property of the command, service call and response, etc. Topic.

Ordinary topic

Set to subscribe to ordinary Topic:

string[] topics = new string[] { ... , ... , ...  };

Topic asked to fill in the full length of the URI, can see into the console. For example, "/ a1xrkGSkb5R / dockertest / user / update / error"

If you do not want too much trouble, you can use

string[] topics = new string[] { client.CombineHeadTopic("get") };

Just enter the Topic of the  /user/ contents of the back can, AliIoTClientJson.CombineHeadTopic () will generate a complete address for your Topic. Such as the need to subscribe "/ a1xrkGSkb5R / dockertest / user / update / error"

string[] topics = new string[] { client.CombineHeadTopic("update/error") };

Topic addition to the ordinary, but also various device upload of data, the server receives the response, the server device attribute set, the server calls the service equipment, etc., which will be described in the following chapter.


3) Set the default event

When you want to receive the message, the program needs to do something? Corresponding write method, the event is bound to delegate, when conditions are met, these methods will be triggered.
In this chapter, the default delegate method, use the following chapters CZGL.AliIoTClient preset will explain in detail how to customize methods.

Use the default event:

            client.UseDefaultEventHandler();

4) the client connection

Configuration has already done the work before the connection is now connected to Ali cloud things.

CZGL.AliIoTClient, there are about three connection methods:

method Explanation
AliIoTClientJson.ConnectIoT(string[], byte[], ushort) Things connected to Ali cloud server
AliIoTClientJson.ConnectIoTAsync(string[], byte[], ushort) The method of using the asynchronous connection to the server things aliyun
AliIoTClientJson.ConnectIoTClose() Closed, the release client

5) Example

Ali cloud Things console to create a new product, then create a new device in this product, do not need to add other functions. Under the new key information recording apparatus and the like.
In Visual Studio, create a new .NET Core console application, find CZGL.AliIoTClient in Nuget and add. Console code is as follows:

            // 创建客户端
            client = new AliIoTClientJson(new DeviceOptions
            {
                ProductKey = "a1xrkGSkb5R",
                DeviceName = "mire", DeviceSecret = "CqGMkOBDiKJfrOWp1evLZC2O6fsMtEXw", RegionId = "cn-shanghai" }); // 设置要订阅的Topic、运行接收内容的Topic string[] topics = new string[] { client.CombineHeadTopic("get") }; // 使用默认事件 client.UseDefaultEventHandler(); // 连接服务器 client.ConnectIoT(topics,null,60); Console.ReadKey(); 

Open Ali cloud Things console, refresh the device list, you can see the device online.


Retransmission data) at 6

A new product, there are several default Topic, we do not have to make other modifications, for the present, you can use the default Topic for demonstration.

Already written to run on top of the console program, do not close.

Open Ali cloud Things console, open the corresponding device, found in the Topic list of devices inside  .../user/get this Topic, for example: /a1xrkGSkb5R/dockertest/user/get

Click  发布消息 , and then enter the content to be transmitted, and finally see if the console receives the delivered message.
Due to the use CZGL.AliIoTClient, the default event method, so in addition to the message content, the output will be some of the attributes of this information Topic message.


7) Upload data

Topic list of devices, there is  .../user/update , for example  /a1xrkGSkb5R/dockertest/user/update . Topic This allows the client to upload the data, the following will explain how a client to upload data to the cloud Ali Things servers.

Upload ordinary Topic ways:

I. upload byte

public int CommonToServer(string topicName, byte[] content)

Abstract: In
this way uploaded to byte [] in the form of data, pay attention to byte [] of the band

Parameters: topicName: full name Topic, use the  CombineHeadTopic() method to get content: news content

Return: Message ID

II. Ordinary string

public int CommonToServer(string topicName, string content) 

Abstract: Topic common way to push to the server, directly upload string

Return: Message ID

III. OTHER upload method

But also several other methods, instructions put together.

public int CommonToServer(string topicName, string content, [System.Text.Encoding encoding = null]) 

Description: uploading data to a specific encoding format Topic, the specified string. Ali cloud Things default UTF8.
CZGL.AliIoTClient also default UTF8 as encoded data to be uploaded string custom coding. Generally do not need to change, otherwise the Chinese will be garbled strings.

public int CommonToServerBase64(string topicName, string content) 

Description: After passing the string, will carry out a Base64 encoding before uploading.

public int CommonToServerBase64(string topicName, string content, [System.Text.Encoding encoding = null]) 

Description: After the incoming string that specifies the encoding of the string, then Base64 encoded after upload.


8) Creating Topic

You can open a console Ali cloud of Things products, create one or more Topic in the product, set this Topic has a subscription / publishing rights. Then try to modify the program if normal upload, issued data.

 

Guess you like

Origin www.cnblogs.com/whuanle/p/10994663.html