/
README.md
331 lines (265 loc) · 13.3 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<div style="max-width: 1200px; min-width: 600px; font-size: 18px; margin: auto; padding: 50px;">
<h1>Example<a style="color: #d30971 !important;">Blazor.</a> Build on <a style="color: #d30971 !important;"> InfluxDB.</a></h1>
<img src="wwwroot/assets/readme/homepage.png" alt="drawing">
## Features
- bucket management - create, delete, clone
- device registration in InfluxDB
- remove device from InfluxDB
- write testing data to InfluxDB
- get data by query API from InfluxDB
- displaying device info and measurements
- data visualizations in gauge and simple chart
## Getting Started
### Prerequisites
- dotnet - [Download .NET](https://dotnet.microsoft.com/en-us/download)
- Docker - [Get started with docker](https://www.docker.com/get-started)
- **InfluxDB** on port **8086** (influxdb 2.0 OSS)home
- for start use docker-compose.yaml:
```bash
docker-compose up
```
## Run Application
Launch app by typing the following commands in a terminal window:
```bash
cd ExampleBlazor
dotnet compose start
```
After a few seconds, you should get your application up and running. Take a look at your terminal window to get the
address your application is listening to.
<img src="wwwroot/assets/readme/terminal-url.png" alt="drawing">
### Home page
#### Client Settings
<img align="right" src="wwwroot/assets/readme/client-settings.png" alt="drawing" width="300px" style="margin-left: 15px; margin-bottom: 15px; border-radius: 10px; filter: drop-shadow(1px 5px 5px black);">
On Home page is InfluxDB client settings - credentials are preset to same values as in docker-compose.
After editing credentials, changes must be saved by clicking on button in top right corner, connection to InfluxDB is
checked then. There are three types of button state:
- <img src="wwwroot/assets/readme/client-settings-button-save.png" width="35px" alt="drawing"> - connection to InfluxDB wasn't checked
- <img src="wwwroot/assets/readme/client-settings-button-succes.png" width="35px" alt="drawing"> - check connection to InfluxDB was successful, changes was saved
- <img src="wwwroot/assets/readme/client-settings-button-alert.png" width="35px" alt="drawing"> - check connection to InfluxDB wasn't successful
Connection to InfluxDB is checked by `PingAsync()` method:
```csharp
public static async Task<bool> CheckClient(Client? client)
{
var influxDbClient = client?.GetClient();
return await influxDbClient?.PingAsync()!;
}
```
#### Device Registration
<img src="wwwroot/assets/readme/homepage-device-registration.png" alt="drawing">
Load active devices (`_value` field cannot be empty) to `RadzenDataGrid` from InfluxDB using `GetQueryApi` with following query -
[FetchDeviceList](/Data/InfluxModel#L148):
```csharp
var query = $"from(bucket: \"{bucket}\")" +
" |> range(start: -30d)" +
" |> filter(fn: (r) => r[\"_measurement\"] == \"deviceauth\"" +
" and r[\"_field\"] == \"key\")" +
" |> last()" +
" |> filter(fn: (r) => r[\"_value\"] != \"\")";
```
Each row contains `button` for deleting device. After deleting device **isn't** remove from InfluxDB - in this case **deleting** is meaning **removing of device
authorization** and **IoT Authorization**.
Removed device has in InfluxDB empty fields `key` and `token`, it means, that device authorization was removed via
`GetWriteApi`- [_removeDeviceAuthorization](/Data/InfluxModel#L313):
```csharp
var point = PointData.Measurement("deviceauth")
.Tag("deviceId", deviceId)
.Field("key", "")
.Field("token", "");
using var writeApi = influxDbClient.GetWriteApi();
writeApi.WritePoint(point, bucket, orgId);
```
IoT Authorization is also removed, in this case `GetAuthorizationsApi` is used - [_deleteIoTAuthorization](/Data/InfluxModel#L339):
```csharp
var authorizationApi = influxDbClient.GetAuthorizationsApi();
await authorizationApi.DeleteAuthorizationAsync(key);
```
On right top corner are following elements:
<img src="wwwroot/assets/readme/homepage-device-registration-actions.png" width="350px" alt="drawing">
- `select` with buckets list, default option `iot_center` is preselected, after changing option devices are reloaded
for selected bucket
- `input` and `button` for adding new device
- `button` for refresh devices
For adding new device, to `input` enter device id, click to button for create. Device is automatically
registered in InfluxDB - it's write as point via `WriteService` with its authorization.
Example of creating device point in InfluxDB - [CreateDevice](/Data/InfluxModel#L199):
```csharp
var createdAt = DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz", CultureInfo.InvariantCulture);
var point = PointData.Measurement("deviceauth")
.Tag("deviceId", deviceId)
.Tag("device", deviceType)
.Field("createdAt", createdAt);
using var writeApi = influxDbClient.GetWriteApi();
writeApi.WritePoint(point, bucket, orgId);
await _createDeviceAuthorization(deviceId, bucket, orgId);
```
Creating device IoT authorization via `GetAuthorizationsApi` - [_createIoTAuthorization](/Data/InfluxModel#L248):
```csharp
var bucketId = await GetBucketId(bucket);
var influxDbClient = Client.GetClient();
var authorizationApi = influxDbClient.GetAuthorizationsApi();
var permissions = new List<Permission>
{
new(
Permission.ActionEnum.Read,
new PermissionResource(
"buckets",
bucketId,
orgID: orgId,
org: Client.Org)),
new(
Permission.ActionEnum.Write,
new PermissionResource(
"buckets",
bucketId,
orgID: orgId,
org: Client.Org))
};
var request = new AuthorizationPostRequest(
orgId,
description: "IoTCenterDevice: " + deviceId,
permissions: permissions);
return await authorizationApi.CreateAuthorizationAsync(request);
```
#### Device Dashboard
<img src="wwwroot/assets/readme/homepage-device-dashboard.png" alt="drawing">
On right top corner are following elements:
- `select` with devices, default option `virtual_device` is preselected (if exist in devices List), after changing option measurements are reloaded
for selected device
- `button` for write emulated data to InfluxDB
- `button` for refresh
Emulated data are written to InfluxDB by `GetWriteApi` - [WriteEmulatedData](/Data/InfluxModel#L617):
```csharp
var writeApi = influxDbClient.GetWriteApi();
while (lastTime < toTime)
{
lastTime += 60000; // emulate next minute
var point = PointData.Measurement("environment")
.Tag("clientId", deviceId)
.Field("Temperature", _generateValue(30, 0, 40, lastTime))
.Field("Humidity", _generateValue(60, 0, 99, lastTime))
.Field("Pressure", _generateValue(20, 970, 1050, lastTime))
.Field("CO2", Convert.ToInt32(_generateValue(1, 400, 3000, lastTime)))
.Field("TVOC", Convert.ToInt32(_generateValue(1, 250, 2000, lastTime)))
.Tag("TemperatureSensor", "virtual_TemperatureSensor")
.Tag("HumiditySensor", "virtual_HumiditySensor")
.Tag("PressureSensor", "virtual_PressureSensor")
.Tag("CO2Sensor", "virtual_CO2Sensor")
.Tag("TVOCSensor", "virtual_TVOCSensor")
.Tag("GPSSensor", "virtual_GPSSensor")
.Timestamp(Convert.ToInt64(lastTime), WritePrecision.Ms);
writeApi.WritePoint(point, bucket, orgId);
}
```
Each `RadzenRadialGauge` contains data from InfluxDB using `GetWriteApi` -
[FetchDataMean](/Data/InfluxModel#L419):
```csharp
var fluxQuery = $"from(bucket: \"{bucket}\")"
+ $" |> range(start: -{timeRange})"
+ $" |> filter(fn: (r) => (r[\"_measurement\"] == \"{measurement}\"))"
+ $" |> filter(fn: (r) => (r.clientId == \"{deviceId}\"))"
+ $" |> filter(fn: (r) => (r[\"_field\"] == \"{field}\"))"
+ " |> mean()";
return (await influxDbClient.GetQueryApi().QueryAsync(fluxQuery, orgId)).FirstOrDefault();
```
Measurements in `RadzenDataGrid` are getting from InfluxDB using `GetQueryApi` -
[FetchMeasurements](/Data/InfluxModel#L461):
```csharp
var fluxQuery = $"deviceData = from(bucket: \"{bucket}\")"
+ " |> range(start: -30d)"
+ " |> filter(fn: (r) => (r[\"_measurement\"] == \"environment\"))"
+ $" |> filter(fn: (r) => (r.clientId == \"{deviceId}\"))"
+ "measurements = deviceData"
+ " |> keep(columns: [\"_field\", \"_value\", \"_time\"])"
+ " |> group(columns: [\"_field\"])"
+ "counts = measurements |> count()"
+ " |> keep(columns: [\"_field\", \"_value\"])"
+ " |> rename(columns: {_value: \"count\" })"
+ "maxValues = measurements |> max ()"
+ " |> toFloat()"
+ " |> keep(columns: [\"_field\", \"_value\"])"
+ " |> rename(columns: {_value: \"maxValue\"})"
+ "minValues = measurements |> min ()"
+ " |> toFloat()"
+ " |> keep(columns: [\"_field\", \"_value\"])"
+ " |> rename(columns: {_value: \"minValue\"})"
+ "maxTimes = measurements |> max (column: \"_time\")"
+ " |> keep(columns: [\"_field\", \"_time\" ])"
+ " |> rename(columns: {_time : \"maxTime\" })"
+ "j = (tables=<-, t) => join(tables: {tables, t}, on:[\"_field\"])"
+ "counts"
+ "|> j(t: maxValues)"
+ "|> j(t: minValues)"
+ "|> j(t: maxTimes)"
+ "|> yield(name: \"measurements\")";
return await influxDbClient.GetQueryApi().QueryAsync(fluxQuery, orgId);
```
### Device
#### Device Detail
<img src="wwwroot/assets/readme/device-detail.png" alt="drawing">
On right top corner are following elements:
- `select` with buckets list, default option `iot_center` is preselected, after changing option devices are reloaded
for selected bucket
- `select` with devices, default option `virtual_device` is preselected (if exist in devices List), after changing option measurements are reloaded
for selected device
- `button` for write emulated data to InfluxDB, after write, data and measurements are reloaded
- `button` for refresh
Device Configuration `RadzenCard` contains basic device information. Registration time is getting from InfluxDB using
`GetWriteApi` - [FetchDeviceCreatedAt](/Data/InfluxModel#L169):
```csharp
var query = $"from(bucket: \"{bucket}\")" +
" |> range(start: -30d)" +
" |> filter(fn: (r) => r[\"_measurement\"] == \"deviceauth\"" +
" and r[\"_field\"] == \"createdAt\"" +
$" and r.deviceId == \"{deviceId}\")" +
" |> keep(columns: [\"_field\", \"_value\"])" +
" |> last()";
var result = await influxDbClient.GetQueryApi().QueryAsync(query, orgId);
```
Measurements in `RadzenDataGrid` are getting from InfluxDB using `GetQueryApi` -
[FetchMeasurements](/Data/InfluxModel#L461):
#### Measurements Detail
<img src="wwwroot/assets/readme/device-measurements.png" alt="drawing">
On right top corner are following elements:
- `select` with time range list, default option `Past 1d` is preselected, after changing, data are reloaded
- `button` for refresh
Data in `RadzenChart` and `RadzenDataGrid` are getting from InfluxDB using `Client.Linq` -
[FetchDataLinq](/Data/InfluxModel#L556):
```csharp
var query = from s in InfluxDBQueryable<MeasurementPoint>.Queryable(bucket, Client.Org, influxDbClient.GetQueryApiSync())
where s.DeviceId == deviceId
where s.Timestamp > timestamp
where s.Timestamp.AggregateWindow(TimeSpan.FromSeconds(aggregate), null, "mean")
orderby s.Timestamp
select s;
return query.ToList();
```
### Buckets
<img src="wwwroot/assets/readme/buckets.png" alt="drawing">
On right top corner are following elements:
- `input` and `button` for adding new bucket
- `button` for refresh buckets
For adding new bucket, to `input` enter bucket name, click to button for create. Bucket is created in InfluxDB via
`GetBucketsApi` with it's authorization via `GetAuthorizationsApi` - [CreateBucket](/Data/InfluxModel#L83):
```csharp
var retention = new BucketRetentionRules(BucketRetentionRules.TypeEnum.Expire, 3600);
var bucket = await influxDbClient.GetBucketsApi().CreateBucketAsync(name, retention, orgId);
var resource = new PermissionResource(PermissionResource.TypeBuckets, bucket.Id, null, orgId);
var read = new Permission(Permission.ActionEnum.Read, resource);
var write = new Permission(Permission.ActionEnum.Write, resource);
await influxDbClient.GetAuthorizationsApi().CreateAuthorizationAsync(orgId, new List<Permission> {read, write});
```
Load buckets to `RadzenDataGrid` from InfluxDB using `GetBucketsApi` - [FetchBuckets](/Data/InfluxModel#L137):
```csharp
var bucketList = await influxDbClient.GetBucketsApi().FindBucketsAsync();
```
Each row contains `button` for deleting and clone bucket. Bucket is removed or cloned via `GetBucketsApi`-
[DeleteBucket](/Data/InfluxModel#L57):
```csharp
await influxDbClient.GetBucketsApi().DeleteBucketAsync(bucket);
```
[CloneBucket](/Data/InfluxModel#L70):
```csharp
await influxDbClient.GetBucketsApi().CloneBucketAsync(name, bucket);
```
<br clear="right"/>
</div>