阿里雲物聯網 .NET Core 客戶端 | CZGL.AliIoTClient:6. 設備事件上報


文檔目錄:


 

根據阿里雲物聯網普通的定義,事件上報有 信息、告警、故障三種類型,事件是設備上傳的消息通知,應當及時處理。

1)定義事件

打開阿里雲物聯網控制台,進入產品,點擊 自定義功能 ,添加一個事件。
添加一個事件

添加事件參數


2)上傳事件的方法

CZGL.AliIoTClient 中,有四個上傳事件的方法

public int Thing_Event_Post(string eventName, string content, [bool isToLower = True]) public int Thing_Event_Post(string eventName, string content, [bool isToLower = True], [System.Text.Encoding encoding = null]) public int Thing_Event_Post<TModel>(TModel model, string eventName, [bool isToLower = True]) public int Thing_Event_Post<TModel>(TModel model, string eventName, [bool isToLower = True], [System.Text.Encoding encoding = null]) 

eventName: 事件的名稱,即標識符。
content: Alink json 內容 isToLower:是否轉為小寫 encoding: 自定義上傳 Alink json 的編碼 model: 事件的模型

第一種方法需要手動編寫好 json,然后通過方法上傳。 第二種方法在第一種方法的基礎上允許自定義字符編碼。 第三種、第四種是傳入模型,由 CZGL.AliIoTClient 處理好再上傳。


3)編寫事件模型

每次只能上傳一個事件,一個事件對應一個 模型 或 Alink json。
在 CZGL.AliIoTClient 中,你每次上傳一個事件時,都需要設置此事件的名稱。

根據上面在阿里雲物聯網控制台定義的事件,編寫模型。
預覽要生成的 Alink json :

{
  "id": "123", "version": "1.0", "params": { "value": { "temperature":100.1 }, "time": 1524448722000 }, "method": "thing.event.cpuerror.post" } 

對應模型如下:

        public class Cpuerror { public Cpuerror() { @params = new Params(); } public string id { get { return DateTime.Now.Ticks.ToString(); } set { } } public string version { get { return "1.0"; } set { } } public Params @params { get; set; } public class Params { public Params() { value = new Value(); } public Value value { get; set; } public long time { get { return AliIoTClientJson.GetUnixTime(); } set { } } public class Value { public float temperature { get; set; } } } public string @method { get { return "thing.event.cpuerror.post"; } set { } } } 

一個事件對應一個類,如果事件里有多個輸出參數,則在 Value 里定義好。

{
...
    ...
                public class Value { public float temperature { get; set; } /* *定義多個輸出參數 */ } ... ... } 

上報事件:

            Cpuerror cpuerror = new Cpuerror(); cpuerror.@params.value.temperature = 100.1F; client.Thing_Event_Post<Cpuerror>(cpuerror, "cpuerror", false); 

4)容錯 上傳事件的 Alink json 可以 容錯 ,這給我們編寫代碼時帶來了方便。、

例如將上面上傳事件的代碼改一下:

   public class Cpuerror { public string name = "cpuerror"; public Cpuerror() { @params = new Params(); } public string id { get { return DateTime.Now.Ticks.ToString(); } set { } } public string version { get { return "1.0"; } set { } } public Params @params { get; set; } public class Params { public Params() { value = new Value(); } public Value value { get; set; } public long time { get { return AliIoTClientJson.GetUnixTime(); } set { } } public class Value { public float temperature { get; set; } } } public string @method { get { return $"thing.event.{name}.post"; } set { } } } 
            Cpuerror cpuerror = new Cpuerror(); cpuerror.@params.value.temperature = 100.2F; client.Thing_Event_Post<Cpuerror>(cpuerror, cpuerror.name, false); 

對於 消息ID 等是必不可少的,“可多不可少”,其它無關字段可以增加上去,不會影響到上傳和使用,例如上面的例子增加了一個 name 屬性。

上傳的事件


5)補充說明


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM