Ali cloud Things .NET Core Client | CZGL.AliIoTClient: 6 Equipment event reporting

Documents directory:


 

According to Ali cloud of things common definitions, event reporting information, alarms, three types of fault, the event message notification device is uploaded, it should be timely manner.

1) define event

Open Ali cloud Things console, enter the product, click the custom function, add an event.
Add an event

Add an event parameters


Method 2) upload events

CZGL.AliIoTClient, there are four methods to upload events

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: name of the event, that identifier.
content: Alink json content isToLower: whether to lowercase encoding: Upload custom Alink json encoding model: model event

The first method need to manually write good json, then upload by method. The second method allows to customize character encoding based on the first method. Third, fourth incoming model is handled by CZGL.AliIoTClient well before uploading.


3) Writing an event model

You can only upload one event, an event corresponds to a model or Alink json.
In CZGL.AliIoTClient, you upload every time an event, you need to set the name of this event.

According to the above-defined networking Console Ali Clouds event, program models.
To generate a preview of Alink json:

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

Corresponding to the model is as follows:

        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 { } } } 

An event corresponds to a class, if the event there are multiple output parameters, define good in Value in.

{
...
    ...
                public class Value
                {
                    public float temperature { get; set; } /* *定义多个输出参数 */ } ... ... } 

Event reporting:

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

4) fault tolerance can upload events Alink json  容错 , which brings us convenience when writing code. ,

For example, the code above to upload events change it:

   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); 

For message ID is essential and the like, "can be more essential", regardless of other fields can be increased up, and does not affect the use of uploading, for example, the above example adds a name attribute.

Upload an event


5) Supplement

Guess you like

Origin www.cnblogs.com/whuanle/p/10994707.html