Aliyun IoT .NET Core Client | CZGL.AliIoTClient: 3. Subscribe to Topic and Respond to Topic

2019年12月15日 38点热度 0人点赞 1条评论
内容目录

Documentation Directory:


CZGL.AliIoTClient classifies Topics into five types

They are: General Topic, Attribute Reporting, Attribute Distribution, Event Reporting, Service Invocation. Except for the General Topic, each type of Topic has message sending and response.

The General Topic guarantees that messages can reach the other end according to the MQTT protocol, strictly adhered to by the SDK.
When the device pushes data such as attributes and events to the server, the server must respond.
When the server pushes (distributes) data to the device, the device must respond.
Of course, these responses can be optional, having no substantial impact.

Each type of Topic has a specific MQTT communication address, which has been automatically generated in CZGL.AliIoTClient; you only need to fill in the communication address for the General Topic.


1) Subscribe to Topic

Before subscribing to a Topic, you need to define the corresponding Topic on Alibaba Cloud IoT and set the subscribe permission.
For the General Topic, use a string[] collection and pass it as a parameter when calling the connection method. You can also add required subscriptions after connecting to the server.
The General Topic can be dynamically added and is a hot subscription.

Usage:

 // Set the Topic to subscribe to and run the Topic to receive content
            string[] topics = new string[] { 
                                client.CombineHeadTopic("get"),
                                "/a1xrkGSkb5R/mire/user/get1" };
        client.ConnectIoT(topics,<span class="hljs-literal">null,<span class="hljs-number">60);

The Topic address is relatively long; you can record the content after .../user/ and use CombineHeadTopic() to automatically generate it.

client.CombineHeadTopic("get") 

2) Response

When you use attributes, events, and service functionalities to transmit data to the server, the server will respond, and you can choose to receive the response or not.
CZGL.AliIoTClient does not receive server response messages by default. In fact, these response messages are typically only needed during debugging.

Response Method Description
OpenEventPostReply() Receive the server's response after reporting an event
OpenPropertyPostReply() Receive the server's response after the device uploads property
OpenServicePostRaw() Allow the server to invoke services This setting has been removed in the new version.
OpenPropertyDownPost() Allow the server to issue commands to set device attributes

Note, these responses must be set before connecting to the client, and after the client is connected, the above methods cannot be used again, otherwise an exception will be thrown.
This has been fixed, and you can open or close the reception feature at any stage of the program.

To check whether these functions are enabled:

public CZGL.AliIoTClient.OpenTopic getOpenTopic()

OpenTopic has 8 properties to get or set whether to turn on the reception of a specific feature.

Property Description Default Value
CommonTopic Whether to receive General Topics Fixed to true, cannot be changed
PropertyUpRawReplyTopic Server's response after device uploads pass-through property data false
PropertyPostReplyTopic Server's response after device uploads Alink JSON property data false
PropertyDownRaw Server's command to set attributes, pass-through false
PropertyDownPost Server's command to set attributes, Alink JSON false
EventUpRawReply Device event reporting, receiving server's response, pass-through false
EventPostReply Device event reporting, receiving server's response, Alink JSON false
ServiceDownRaw Server invoking service, pass-through false
ServicePostRaw Server invoking service, Alink JSON false

As the distinction between pass-through and Alink JSON, if it is pass-through, setting the above Alink JSON items is invalid, and vice versa.


3) After Connecting to the Server


After connecting to the server, you can also add the General Topics you want to subscribe to:

public void TopicAdd(string[] topics, [byte[] QOS = null])

Example:

client.TopicAdd(new string[]{ client.CombineHeadTopic("get") })

To remove an already subscribed Topic:

public void TopicRemove(string[] topics)

After connecting to the server, you can cease receiving server response messages but cannot re-enable them.

Method Description
CloseEventPostReply() No longer receive the server's response after the device reports an event
ClosePropertyPostReply() No longer receive the server's response after the device uploads properties
CloseServicePostRaw() No longer allow server instructions to invoke device servicesThis setting has been removed.
ClosePropertyDownPost() No longer allow server instructions to set device attributes

You can cease receiving responses after the client connects to the server. However, after ceasing, you cannot subscribe again!
This has been fixed, you can cancel and later re-enable reception at any stage of the program.


4) Topic Description

To obtain the list of already subscribed Topics:

public string[] GetSubedList { get; }

Example:

var topicList = client.GetSubedList;

For functions such as setting device attributes and calling services, the communication protocol is MQTT, which is subscription/push-based. Therefore, regardless of the function, the essence of data transmission is still based on Topics. Thus, device attribute reporting and setting can be configured with Topic attributes.

In CZGL.AliIoTClient, the client can subscribe to the needed Topics. Once connected to the server, the server can send data to all Topics. However, only the Topics specified for subscription by the client will be received; otherwise, regardless of how you click send in the console, the client won't receive anything.

One point to note is that for General Topics, the configuration during the client connection determines whether the server can push messages to the client, and it's dynamic.

On the other hand, attributes, events, and services functionalities save the last configuration. When you enable:

client.OpenPropertyPostReply();

And then delete it in the program:

client.OpenPropertyPostReply();

Upon running it again, you will still receive the response unless you set:

client.ClosePropertyPostReply();

When the device uploads data to the server, the server will respond. Of course, when the server sets device attributes or calls device services, the client can also respond. This will be explained in later chapters.

痴者工良

高级程序员劝退师

文章评论