Series Tutorial Directory
http://pan.whuanle.cn/index.php?dir=uploads/阿里云IOT/AliIOTXFclient-dll类库&response
Download the three libraries and include them in the header for usage
Sample download address
http://pan.whuanle.cn/index.php?dir=uploads/阿里云IOT/demo示例
This chapter's example: AliIOTXF.Three
Defining Product Model - Attributes
Open the Alibaba Cloud IoT console https://iot.console.aliyun.com/product
Click on the product, open --functional definition, and then create a new custom function.

Define Two Attributes
Note: Use lowercase throughout. The transparency data and JSON upload are case-sensitive. It’s advisable to keep everything in lowercase to avoid naming conflicts. Especially when storing data using models, some fields may conflict with keywords—consider capitalizing parts of the field names, and convert everything to lowercase when uploading as JSON.
Read-write permissions: read-only means that attribute data can only be sent from the client to the server. Read-write means that the server can set the value of this attribute to the client.


View Attributes
The added attributes can be seen by opening the device - running status.

Writing Client Device Models
Official Alibaba Cloud documentation about attributes, events, services https://help.aliyun.com/document_detail/89301.html?spm=5176.11065259.1996646101.searchclickresult.5d587dafQbHe20
This section mainly discusses JSON uploads; pass-through can be more complicated, though the XF SDK supports it. Methods for pass-through will be supplemented later.
Device attribute upload in JSON format
{
"id": "123",
"version": "1.0",
"params": {
"Attribute1": {
"value": "on",
"time": 1524448722000
},
"Attribute2": {
"value": 23.6,
"time": 1524448722000
}
},
"method": "thing.event.property.post"
}
Inside params, the attributes are placed, and each attribute has two parameters: value and time.
Create a new file below named TestModel.cs
File content:
public class TestModel
{
public string id { get; set; }
public string version { get; set; }
</span><span style="color: #0000ff;">public</span> Params paramS { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> TestModel()
{
paramS </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Params();
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Params
{
</span><span style="color: #0000ff;">public</span> Cpuwd cpuwd { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
</span><span style="color: #0000ff;">public</span> Cpuxh cpuxh { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Params()
{
cpuwd </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Cpuwd();
cpuxh </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Cpuxh();
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Cpuwd
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">float</span> value { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">long</span> time { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Cpuxh
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">float</span> value { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">long</span> time { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
}
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> methoD { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
}</span></pre>
The above defines the model, which can be converted to JSON for data upload. The model can be customized according to actual conditions.
In the last chapter, we used the complete code, and we will continue using the project code:

class Program
{
static void Main(string[] args)
{
// Create connection object
XFMQTT client = new XFMQTT("a1BiPoNawLI", "Raspberry");
// Initialize client configuration
client.Init("2NOaBeqXcIzLQEhlJFEfKbWeug0o3m0c", "cn-shanghai");
// Topic to subscribe
string[] topic = { client.CombineHeadTopic("PubData") };
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Use custom delegate events</span>
事件类 事件 = <span style="color: #0000ff;">new</span><span style="color: #000000;"> 事件类();
client.PubEventHandler </span>+=<span style="color: #000000;"> 事件.收到消息;
client.PubedEventHandler </span>+=<span style="color: #000000;"> 事件.重复收到消息;
client.SubedEventHandler </span>+=<span style="color: #000000;"> 事件.发布消息时;
client.UnSubedEventHandler </span>+=<span style="color: #000000;"> 事件.发送失败;
client.ConnectionClosedEventHandler </span>+=<span style="color: #000000;"> 事件.断开连接;
</span><span style="color: #008000;">//</span><span style="color: #008000;">client.add
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Connect to server</span>
client.ConnectMqtt(topic);
</span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">true</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">string</span> str =<span style="color: #000000;"> Console.ReadLine();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Push content to specific Topic</span>
client.Subscribe(client.CombineHeadTopic(<span style="color: #800000;">"</span><span style="color: #800000;">SubData</span><span style="color: #800000;">"</span><span style="color: #000000;">), str);
}
Console.ReadKey();
}
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> 事件类
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> 收到消息(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgPublishEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Topic Name:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.Topic);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Receive Time:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongDateString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message Content:\n</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Encoding.ASCII.GetString(e.Message));
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> 重复收到消息(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgPublishedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Receive Time: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message ID: </span><span style="color: #800000;">"</span> + e.MessageId + <span style="color: #800000;">"</span><span style="color: #800000;"> Is Published: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.IsPublished);
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> 发布消息时(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgSubscribedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message Sent to Server</span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Send Time: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message ID: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.MessageId);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">QOS: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Encoding.UTF8.GetString(e.GrantedQoSLevels));
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> 发送失败(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgUnsubscribedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message Sending Failed</span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Time: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Failed Message Message ID: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.MessageId);
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> 断开连接(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, EventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Disconnected</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
}
</span>
</pre>
. The connection has been disconnected: " + DateTime.Now.ToLongTimeString());
}
}
View Code
// Use default event method
client.UseDefaultEventHandler();
Thread.Sleep(1000);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Simulated data
</span><span style="color: #008000;">//</span><span style="color: #008000;"> CPU temperature</span>
<span style="color: #0000ff;">float</span> cpuwd = (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span> Random()).Next(<span style="color: #800080;">0</span>, <span style="color: #800080;">120</span>) + (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Random()).NextDouble();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> CPU usage</span>
<span style="color: #0000ff;">float</span> cpuxh = (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span> Random()).Next(<span style="color: #800080;">0</span>, <span style="color: #800080;">99</span>) + (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Random()).NextDouble();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Model object</span>
TestModel model = <span style="color: #0000ff;">new</span><span style="color: #000000;"> TestModel
{
id </span>= <span style="color: #800000;">"</span><span style="color: #800000;">123456</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Custom</span>
version = <span style="color: #800000;">"</span><span style="color: #800000;">1.0</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Fixed!</span>
methoD = <span style="color: #800000;">"</span><span style="color: #800000;">thing.event.property.post</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Fixed!</span>
};
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Data and timestamp</span>
model.paramS.cpuwd.value =<span style="color: #000000;"> cpuwd;
model.paramS.cpuwd.time </span>= 1524448722000<span style="color: #000000;">;
model.paramS.cpuxh.value </span>=<span style="color: #000000;"> cpuxh;
model.paramS.cpuxh.time </span>= 1524448722000<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Push content to a specific Topic</span>
client.Thing_Property_Post<TestModel>(model, <span style="color: #0000ff;">true</span>);</pre>
Then run the program and check Alibaba Cloud IOT -- Devices -- Operating Status -- Data Changes
If you pay attention, you will find that the time is not real-time.
/// <summary>
/// Converts C# DateTime format to Unix timestamp format
/// </summary>
/// <param name="time">Time</param>
/// <returns>long</returns>
public static long ConvertDateTimeToInt()
{
System.DateTime time = DateTime.Now;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000; // Divide by 10000 to adjust to 13 digits
return t;
}
while (true)
{
Thread.Sleep(1000);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Simulated data
</span><span style="color: #008000;">//</span><span style="color: #008000;"> CPU temperature</span>
<span style="color: #0000ff;">float</span> cpuwd = (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span> Random()).Next(<span style="color: #800080;">0</span>, <span style="color: #800080;">120</span>) + (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Random()).NextDouble();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> CPU usage</span>
<span style="color: #0000ff;">float</span> cpuxh = (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span> Random()).Next(<span style="color: #800080;">0</span>, <span style="color: #800080;">99</span>) + (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Random()).NextDouble();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Model object</span>
TestModel model = <span style="color: #0000ff;">new</span><span style="color: #000000;"> TestModel
{
id </span>= <span style="color: #800000;">"</span><span style="color: #800000;">123456</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Custom</span>
version = <span style="color: #800000;">"</span><span style="color: #800000;">1.0</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Fixed!</span>
methoD = <span style="color: #800000;">"</span><span style="color: #800000;">thing.event.property.post</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Fixed!</span>
};
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Data and timestamp</span>
model.paramS.cpuwd.value =<span style="color: #000000;"> cpuwd;
model.paramS.cpuwd.time </span>=<span style="color: #000000;"> ConvertDateTimeToInt();
model.paramS.cpuxh.value </span>=<span style="color: #000000;"> cpuxh;
model.paramS.cpuxh.time </span>=<span style="color: #000000;"> ConvertDateTimeToInt();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Push content to a specific Topic</span>
client.Thing_Property_Post<TestModel>(model, <span style="color: #0000ff;">true</span><span style="color: #000000;">);
}</span></pre>
Due to the wide range of random numbers used, the fluctuations are too large and do not conform to the norm, so the data report does not look good. This needs to be adjusted according to the actual situation.
The complete code is as follows.
class Program
{
static void Main(string[] args)
{
// Create connection object
XFMQTT client = new XFMQTT("a1BiPoNawLI", "Raspberry");
// Initialize client configuration
client.Init("2NOaBeqXcIzLQEhlJFEfKbWeug0o3m0c", "cn-shanghai");
// Topics to subscribe to
string[] topic = { client.CombineHeadTopic("PubData")};
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Use custom delegate events</span>
EventClass events = <span style="color: #0000ff;">new</span><span style="color: #000000;"> EventClass();
client.PubEventHandler </span>+=<span style="color: #000000;"> events.OnMessageReceived;
client.PubedEventHandler </span>+=<span style="color: #000000;"> events.OnMessageRepeated;
client.SubedEventHandler </span>+=<span style="color: #000000;"> events.OnMessagePublished;
client.UnSubedEventHandler </span>+=<span style="color: #000000;"> events.OnSendFailed;
client.ConnectionClosedEventHandler </span>+=<span style="color: #000000;"> events.OnConnectionClosed;
</span><span style="color: #008000;">//</span><span style="color: #008000;">client.add
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Connect to the server</span>
client.ConnectMqtt(topic);
</span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">true</span><span style="color: #000000;">)
{
Thread.Sleep(</span><span style="color: #800080;">1000</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Simulate data
</span><span style="color: #008000;">//</span><span style="color: #008000;"> CPU temperature</span>
<span style="color: #0000ff;">float</span> cpuTemp = (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span> Random()).Next(<span style="color: #800080;">40</span>, <span style="color: #800080;">60</span>) + (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Random()).NextDouble();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> CPU consumption</span>
<span style="color: #0000ff;">float</span> cpuUsage = (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span> Random()).Next(<span style="color: #800080;">30</span>, <span style="color: #800080;">40</span>) + (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Random()).NextDouble();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Model object</span>
TestModel model = <span style="color: #0000ff;">new</span><span style="color: #000000;"> TestModel
{
id </span>= <span style="color: #800000;">"</span><span style="color: #800000;">123456</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Custom</span>
version = <span style="color: #800000;">"</span><span style="color: #800000;">1.0</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Fixed!</span>
method = <span style="color: #800000;">"</span><span style="color: #800000;">thing.event.property.post</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Fixed!</span>
};
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Data and timestamp</span>
model.paramS.cpuwd.value =<span style="color: #000000;"> cpuTemp;
model.paramS.cpuwd.time </span>=<span style="color: #000000;"> ConvertDateTimeToInt();
model.paramS.cpuxh.value </span>=<span style="color: #000000;"> cpuUsage;
model.paramS.cpuxh.time </span>=<span style="color: #000000;"> ConvertDateTimeToInt();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Push content to specific Topic</span>
client.Thing_Property_Post<TestModel>(model, <span style="color: #0000ff;">true</span><span style="color: #000000;">);
}
Console.ReadKey();
}
</span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
<span style="color: #808080;">///</span><span style="color: #008000;"> Convert C# DateTime format to Unix timestamp format
</span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><param name="time"></span><span style="color: #008000;">Time</span><span style="color: #808080;"></param></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><returns></span><span style="color: #008000;">long</span><span style="color: #808080;"></returns></span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">long</span><span style="color: #000000;"> ConvertDateTimeToInt()
{
System.DateTime time </span>=<span style="color: #000000;"> DateTime.Now;
System.DateTime startTime </span>= TimeZone.CurrentTimeZone.ToLocalTime(<span style="color: #0000ff;">new</span> System.DateTime(<span style="color: #800080;">1970</span>, <span style="color: #800080;">1</span>, <span style="color: #800080;">1</span>, <span style="color: #800080;">0</span>, <span style="color: #800080;">0</span>, <span style="color: #800080;">0</span>, <span style="color: #800080;">0</span><span style="color: #000000;">));
</span><span style="color: #0000ff;">long</span> t = (time.Ticks - startTime.Ticks) / <span style="color: #800080;">10000</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> Divide by 10000 to adjust to 13 digits</span>
<span style="color: #0000ff;">return</span><span style="color: #000000;"> t;
}
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> EventClass
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnMessageReceived(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgPublishEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Topic Name:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.Topic);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Receive Time:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongDateString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message Content:\n</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Encoding.ASCII.GetString(e.Message));
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnMessageRepeated(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgPublishedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Receive Time: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message ID: </span><span style="color: #800000;">"</span> + e.MessageId + <span style="color: #800000;">"</span><span style="color: #800000;"> Is Published: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.IsPublished);
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> OnMessagePublished(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgSubscribedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Sent a message to the server</span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">
<br />
发送时间: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message ID: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.MessageId);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">QOS is: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Encoding.UTF8.GetString(e.GrantedQoSLevels));
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> MessageSendFailed(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgUnsubscribedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message send failed </span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Time: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Failed message MessageId: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.MessageId);
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Disconnected(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, EventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Oh...The connection has been disconnected: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
}
}</span></pre>
</div>
<p> </p>
<p> </p>
<p> </p>
<p>For some junior brothers watching this~ Here are some explanations of part of the code.</p>
<div class="cnblogs_code">
<pre>Random random = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Random();
</span><span style="color: #0000ff;">int</span> wd = random.Next(<span style="color: #800080;">40</span>,<span style="color: #800080;">60</span><span style="color: #000000;">);
Simplified as
(</span><span style="color: #0000ff;">new</span> Random()).Next(<span style="color: #800080;">40</span>, <span style="color: #800080;">60</span>)</pre>
</div>
<p>.Next(,) generates an int value within the range, and .NextDouble() generates a number in the range 0<=n<1, of type double.</p>
<p>time is of long int type, and you need to fill in a 13-digit timestamp. If you don't understand, please click <a title="https://www.jb51.net/article/149152.htm" href="https://www.jb51.net/article/149152.htm" target="_blank" rel="noopener noreferrer">https://www.jb51.net/article/149152.htm</a></p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Thing_Property_Post<AlinkModel>(AlinkModel model, <span style="color: #0000ff;">bool</span> isToLower = <span style="color: #0000ff;">true</span>);</pre>
</div>
<p>The Thing_Property_Post() method can upload properties to the server. The isToLower parameter indicates whether to convert to lowercase before uploading.</p>
<p>Note: Alibaba Cloud IOT JSON is case-sensitive, so it is recommended to define attributes on the console using lowercase identifiers, and to convert all JSON to lowercase during client uploads to avoid errors.</p>
<h3>Three overloaded methods</h3>
<p>There are three overloaded methods for uploading properties</p>
<div class="cnblogs_code">
<pre> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Thing_Property_Post<AlinkModel>(AlinkModel model, <span style="color: #0000ff;">bool</span> isToLower = <span style="color: #0000ff;">true</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Thing_Property_Post(<span style="color: #0000ff;">byte</span><span style="color: #000000;">[] json);
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Thing_Property_Post(<span style="color: #0000ff;">string</span> json, <span style="color: #0000ff;">bool</span> isToLower = <span style="color: #0000ff;">true</span>);</pre>
</div>
<p>By using the first method, you can directly upload a custom attribute model, skipping unnecessary operations.</p>
<p>You can convert the JSON string to byte[] and use the second method to upload it to the server. Be mindful of byte[] hexadecimal issues!</p>
<p>If you only have JSON and do not need a custom model, use the third method to upload.</p>
<p> </p>
<h2>Receiving responses</h2>
<p>According to the protocol, for attributes, events, and services, when uploaded or issued, the other party must respond.</p>
<p>The console does not show a response after running the example above.</p>
<p>This is because we did not set to receive a response.</p>
<p>Let's modify the subscription Topic</p>
<div class="cnblogs_code">
<pre> <span style="color: #008000;">//</span><span style="color: #008000;"> Topic to subscribe to</span>
<span style="color: #0000ff;">string</span>[] topic = { client.CombineHeadTopic(<span style="color: #800000;">"</span><span style="color: #800000;">PubData</span><span style="color: #800000;">"</span>),client.thingModel.upTopic.post_reply };</pre>
</div>
<p>client.thingModel.upTopic.post_reply is a Topic address that receives the response message from the server after publishing attributes.</p>
<p>thingModel defines the device attributes service events, sending, receiving, and responding Topics. More about this will be discussed in the next chapter.</p>
<br />
```markdown
</p>
<p>Server Response Content Format</p>
<div class="cnblogs_code">
<pre><span style="color: #000000;">{
</span><span style="color: #800000;">"</span><span style="color: #800000;">id</span><span style="color: #800000;">"</span>: <span style="color: #800000;">"</span><span style="color: #800000;">123</span><span style="color: #800000;">"</span><span style="color: #000000;">,
</span><span style="color: #800000;">"</span><span style="color: #800000;">code</span><span style="color: #800000;">"</span>: <span style="color: #800080;">200</span><span style="color: #000000;">,
</span><span style="color: #800000;">"</span><span style="color: #800000;">data</span><span style="color: #800000;">"</span><span style="color: #000000;">: {}
}</span></pre>
</div>
<p>Modified Complete Code</p>
<div class="cnblogs_code" onclick="cnblogs_code_show('1e265a83-745b-4490-8e7f-f621f40637a9')"><img id="code_img_closed_1e265a83-745b-4490-8e7f-f621f40637a9" class="code_img_closed" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_1e265a83-745b-4490-8e7f-f621f40637a9" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('1e265a83-745b-4490-8e7f-f621f40637a9',event)" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt=""><div id="cnblogs_code_open_1e265a83-745b-4490-8e7f-f621f40637a9" class="cnblogs_code_hide">
<pre> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
{
</span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Create connection object</span>
XFMQTT client = <span style="color: #0000ff;">new</span> XFMQTT(<span style="color: #800000;">"</span><span style="color: #800000;">a1BiPoNawLI</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">Raspberry</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Initialize client configuration</span>
client.Init(<span style="color: #800000;">"</span><span style="color: #800000;">2NOaBeqXcIzLQEhlJFEfKbWeug0o3m0c</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">cn-shanghai</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Topics to subscribe</span>
<span style="color: #0000ff;">string</span>[] topic = { client.CombineHeadTopic(<span style="color: #800000;">"</span><span style="color: #800000;">PubData</span><span style="color: #800000;">"</span><span style="color: #000000;">),client.thingModel.upTopic.post_reply };
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Use custom delegate events</span>
事件类 事件 = <span style="color: #0000ff;">new</span><span style="color: #000000;"> 事件类();
client.PubEventHandler </span>+=<span style="color: #000000;"> 事件.收到消息;
client.PubedEventHandler </span>+=<span style="color: #000000;"> 事件.重复收到消息;
client.SubedEventHandler </span>+=<span style="color: #000000;"> 事件.发布消息时;
client.UnSubedEventHandler </span>+=<span style="color: #000000;"> 事件.发送失败;
client.ConnectionClosedEventHandler </span>+=<span style="color: #000000;"> 事件.断开连接;
</span><span style="color: #008000;">//</span><span style="color: #008000;">client.add
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Connect to server</span>
<span style="color: #000000;"> client.ConnectMqtt(topic);
</span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">true</span><span style="color: #000000;">)
{
Thread.Sleep(</span><span style="color: #800080;">1000</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Simulated data
</span><span style="color: #008000;">//</span><span style="color: #008000;"> CPU temperature</span>
<span style="color: #0000ff;">float</span> cpuwd = (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span> Random()).Next(<span style="color: #800080;">40</span>, <span style="color: #800080;">60</span>) + (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Random()).NextDouble();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> CPU consumption</span>
<span style="color: #0000ff;">float</span> cpuxh = (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span> Random()).Next(<span style="color: #800080;">30</span>, <span style="color: #800080;">40</span>) + (<span style="color: #0000ff;">float</span>)(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Random()).NextDouble();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Model object</span>
TestModel model = <span style="color: #0000ff;">new</span><span style="color: #000000;"> TestModel
{
id </span>= <span style="color: #800000;">"</span><span style="color: #800000;">123456</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Custom</span>
version = <span style="color: #800000;">"</span><span style="color: #800000;">1.0</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Fixed!</span>
methoD = <span style="color: #800000;">"</span><span style="color: #800000;">thing.event.property.post</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> Fixed!</span>
<span style="color: #000000;"> };
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Data and timestamp</span>
model.paramS.cpuwd.value =<span style="color: #000000;"> cpuwd;
model.paramS.cpuwd.time </span>=<span style="color: #000000;"> ConvertDateTimeToInt();
model.paramS.cpuxh.value </span>=<span style="color: #000000;"> cpuxh;
model.paramS.cpuxh.time </span>=<span style="color: #000000;"> ConvertDateTimeToInt();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Push content to specific Topic</span>
client.Thing_Property_Post<TestModel>(model, <span style="color: #0000ff;">true</span><span style="color: #000000;">);
}
Console.ReadKey();
}
</span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
<span style="color: #808080;">///</span><span style="color: #008000;"> Convert c# DateTime format to Unix timestamp format
</span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><param name="time"></span><span style="color: #008000;">Time</span><span style="color: #808080;"></param></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><returns></span><span style="color: #008000;">long</span><span style="color: #808080;"></returns></span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">long</span><span style="color: #000000;"> ConvertDateTimeToInt()
{
System.DateTime time </span>=<span style="color: #000000;"> DateTime.Now;
System.DateTime startTime </span>= TimeZone.CurrentTimeZone.ToLocalTime(<span style="color: #0000ff;">new</span> System.DateTime(<span style="color: #800080;">1970</span>, <span style="color: #800080;">1</span>, <span style="color: #800080;">1</span>, <span style="color: #800080;">0</span>, <span style="color: #800080;">0</span>, <span style="color: #800080;">0</span>, <span style="color: #800080;">0</span><span style="color: #000000;">));
</span><span style="color: #0000ff;">long</span> t = (time.Ticks - startTime.Ticks) / <span style="color: #800080;">10000</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> Divide by 10000 to adjust to 13 digits</span>
<span style="color: #0000ff;">return</span><span style="color: #000000;"> t;
}
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> 事件类
{
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> 收到消息(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgPublishEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Topic name:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.Topic);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Receive time:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongDateString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message content:\n</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Encoding.ASCII.GetString(e.Message));
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> 重复收到消息(<span style=
object sender, MqttMsgPublishedEventArgs e)
{
Console.WriteLine("Reception time: " + DateTime.Now.ToLongTimeString());
Console.WriteLine("Message ID: " + e.MessageId + " Is Published: " + e.IsPublished);
}
public void OnMessagePublished(object sender, MqttMsgSubscribedEventArgs e)
{
Console.WriteLine("Message sent to the server");
Console.WriteLine("Sending time: " + DateTime.Now.ToLongTimeString());
Console.WriteLine("Message ID: " + e.MessageId);
Console.WriteLine("QOS is: " + Encoding.UTF8.GetString(e.GrantedQoSLevels));
}
public void OnSendFailed(object sender, MqttMsgUnsubscribedEventArgs e)
{
Console.WriteLine("Message sending failed");
Console.WriteLine("Time: " + DateTime.Now.ToLongTimeString());
Console.WriteLine("Failed message MessageId: " + e.MessageId);
}
public void OnDisconnected(object sender, EventArgs e)
{
Console.WriteLine("Disconnected: " + DateTime.Now.ToLongTimeString());
}
}
运行程序可以发现控制台会接收到服务器响应提示
<img src="https://img2018.cnblogs.com/blog/1315495/201904/1315495-20190429230824521-1509226075.png" alt="">