Aliyun IoT .NET Core Client | CZGL.AliIoTClient: 6. Device Event Reporting

2019年12月15日 42点热度 0人点赞 1条评论
内容目录

Document Directory:


 

According to the general definition of Alibaba Cloud IoT, event reporting involves three types: information, alarm, and fault. Events are message notifications uploaded by devices and should be processed promptly.

1) Define Events

Open the Alibaba Cloud IoT Console, go to the product, click on Custom Function, and add an event.
Add an Event
Add Event Parameters


2) Methods to Upload Events

In 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: The name of the event, i.e., the identifier. content: Alink JSON content isToLower: Whether to convert to lowercase encoding: Custom encoding for uploading Alink JSON model: The model of the event

The first method requires manually writing the JSON and then uploading it through the method. The second method allows custom character encoding based on the first method. The third and fourth methods take in a model, which is handled by CZGL.AliIoTClient before uploading.


3) Write Event Model

Only one event can be uploaded at a time, and one event corresponds to one model or Alink JSON. In CZGL.AliIoTClient, each time you upload an event, you need to set the event's name.

Based on the events defined in the Alibaba Cloud IoT Console above, write the model. Preview the Alink JSON to be generated:

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

The corresponding 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 one class; if there are multiple output parameters in the event, define them in the Value class accordingly.

```markdown
.

```csharp
{
...
...
public class Value
{
public float temperature { get; set; }
/*
* Define multiple output parameters
*/
}
...
...
}
```

Reporting Event:
```csharp
Cpuerror cpuerror = new Cpuerror();
cpuerror.@params.value.temperature = 100.1F;
client.Thing_Event_Post(cpuerror, "cpuerror", false);
```

4) Fault Tolerance The Alink JSON for uploading events can be `fault tolerant`, which brings convenience when we write code.

For example, modify the above upload event code as follows:
```csharp
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 { } }
}
```
```csharp
Cpuerror cpuerror = new Cpuerror();
cpuerror.@params.value.temperature = 100.2F;
client.Thing_Event_Post(cpuerror, cpuerror.name, false);
```

For message IDs and other necessary fields, it is "better to have more than less". Other unrelated fields can be added without affecting the upload and usage; for example, the above example added a name property.

![Upload Event](https://images.gitee.com/uploads/images/2019/0608/201641_1c947651_1935277.png)

---

## 5) Additional Notes
```

痴者工良

高级程序员劝退师

文章评论