您现在的位置是:首页 > 文章详情

阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:6. 设备事件上报

日期:2019-06-08点击:275

根据阿里云物联网普通的定义,事件上报有 信息、告警、故障三种类型,事件是设备上传的消息通知,应当及时处理。

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)补充说明

原文链接:https://yq.aliyun.com/articles/704924
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章