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

2019年12月15日 1785点热度 0人点赞 1条评论
内容纲要

文档目录:


 

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

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

痴者工良

高级程序员劝退师

文章评论