Document Directory:
In the previous chapter, the Gree air conditioner temperature gree_temperature
was set with read and write permissions, as the output temperature of the air conditioner can be set.
The CPU temperature is collected based on actual conditions, while the air conditioner temperature is set by the remote control. The server can read this temperature data and can also set it.
Read/write permissions indicate that the server has the authority to issue commands to set device attributes.
It should be noted that there are only read/read-write
situations, no write
.
1) Allow the server to set device attributes
Communication involves subscribing/pushing and data uploading and downloading, which is not complex at all; whether it's attributes, events, or services, it essentially remains a Topic.
The CZGL.AliIoTClient
has a detailed division (applauding the author), with many parameters set for greater freedom and convenience.
Chapter 3 has already explained how to turn on and cancel responses and other features, so this will not be elaborated on here.
The following is the initial code, which will be explained based on it (please modify the information in DeviceOptions
):
static AliIoTClientJson client;
static void Main(string[] args)
{
// Create client
client = new AliIoTClientJson(new DeviceOptions
{
ProductKey = "a1A6VVt72pD",
DeviceName = "json",
DeviceSecret = "7QrjTptQYCdepjbQvSoqkuygic2051zM",
RegionId = "cn-shanghai"
});
client.OpenPropertyDownPost();
<span class="hljs-comment">// Set the Topic to subscribe, and the Topic for receiving content
<span class="hljs-keyword">string[] topics = <span class="hljs-keyword">new <span class="hljs-keyword">string[] { client.CombineHeadTopic(<span class="hljs-string">"get") };
<span class="hljs-comment">// Use default events
client.UseDefaultEventHandler();
<span class="hljs-comment">// Connect to server
client.ConnectIoT(topics, <span class="hljs-literal">null, <span class="hljs-number">60);
Console.ReadKey();
}
Add a line of code before Console.ReadKey()
to run the server's command to set attributes:
client.OpenPropertyDownPost();
Run the program.
2) Issue Commands
Open the Alibaba Cloud IoT console, go to Online Debugging
, then select the product and device that has been created earlier.
You can also directly open: https://iot.console.aliyun.com/lk/monitor/debug to configure as follows:
Debug Device: Debug Real Device
Function: Gree Air Conditioner Temperature (gree_temperature)
Method: Set
Then change the value in the input box to 20.0 and click Send Command
{
"gree_temperature": 20
}
Then you can see the console program received the command:
get topic message,Date: 16:52:55
topic: /sys/a1A6VVt72pD/json/thing/service/property/set
get message :
{"method":"thing.service.property.set","id":"666237842","params":{"gree_temperature":20},"version":"1.0.0"}
Since using Alink JSON, the command issued by the server is actually like this:
{"method":"thing.service.property.set","id":"666237842","params":{"gree_temperature":20},"version":"1.0.0"}
So why does it output other information? This is because CZGL.AliIoTClient
has set multiple default event methods,
it will output the received message content (message) and other information, you can customize methods to handle them.
After formatting the string:
{
"method": "thing.service.property.set",
"id": "666237842",
"params": {
"gree_temperature": 20
},
"version": "1.0.0"
}
However, currently only the commands issued by the server can be received, and there are no methods written to handle these commands; you need to write corresponding methods bound to delegate events.
When receiving attribute messages, these methods will be triggered. For how to set them, please refer to the following chapters.
3) Respond
When you receive commands issued by the server, you can respond to that Topic.
// Return message ID
public int Thing_Property_set(CZGL.AliIoTClient.PropertyDownModel model,
[bool isToLower = True])
public int Thing_Property_set(CZGL.AliIoTClient.PropertyDownModel model,
[bool isToLower = True],
[System.Text.Encoding encoding = null])
public int Thing_Property_set<TModel>(TModel model, [bool isToLower = True])
In fact, there is no need to respond... If needed, you can customize methods, add responses in the methods, and bind them to delegates for automatic response.
For how to set this up, please refer to the following chapters.
文章评论