阿里云物联网 .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)补充说明

转载于:https://www.cnblogs.com/whuanle/p/10994707.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值