@page "/" @using ExampleBlazor.Data @using InfluxDB.Client.Api.Domain @using InfluxDB.Client.Core.Flux.Domain @using Radzen @using System.Globalization @inject IJSRuntime JsRuntime Index

ClientSettings


DeviceRegistration

@if (_bucketList == null) { } else { }

DeviceDashboard

@if (DeviceList == null) { } else { }

@if (_chartDataList != null) { foreach (var data in _chartDataList) {
@data.Name
} }
@code { protected override async Task OnInitializedAsync() { _chartDataList = new List { new("Temperature", "°C", 0, 50, StateHasChanged), new("Humidity", "%", 0, 100, StateHasChanged), new("Pressure", "hPa", 970, 1050, StateHasChanged), new("CO2", "ppm", 400, 3000, StateHasChanged), new("TVOC", "ppm", 200, 2000, StateHasChanged) }; _client = new Client(InfluxModel.Client.Url, InfluxModel.Client.Token, InfluxModel.Client.Org); _bucketList = await InfluxModel.FetchBuckets(); if (_bucketList.FirstOrDefault(bucket => bucket.Name == "iot_center") != null) SelectedBucket = "iot_center"; } #region ---------------------------- ClientSettings ---------------------------- private Client? _client; private ButtonStyle _checkConnectionButtonStyle = ButtonStyle.Light; private ButtonStyle CheckConnectionButtonStyle { get => _checkConnectionButtonStyle; set { _checkConnectionButtonStyle = value; switch (value) { case ButtonStyle.Success: _buttonIcon = "check_circle"; break; case ButtonStyle.Danger: _buttonIcon = "report"; break; default: _buttonIcon = "save"; break; } StateHasChanged(); } } private string _buttonIcon = "save"; private bool _checkConnectionBusy; private bool CheckConnectionBusy { get => _checkConnectionBusy; set { _checkConnectionBusy = value; StateHasChanged(); } } private async void CheckConnection() { CheckConnectionBusy = true; if (await InfluxModel.CheckClient(_client)) { InfluxModel.Client.Org = _client?.Org; InfluxModel.Client.Token = _client?.Token; InfluxModel.Client.Url = _client?.Url; CheckConnectionButtonStyle = ButtonStyle.Success; } else { CheckConnectionButtonStyle = ButtonStyle.Danger; } CheckConnectionBusy = false; } #endregion ---------------------------- ClientSettings ---------------------------- #region ---------------------------- DeviceRegistration ---------------------------- private List? _bucketList; private string? _selectedBucket; string? SelectedBucket { get => _selectedBucket; set { _selectedBucket = value; FetchDevices(value).ConfigureAwait(false); } } private List? _deviceList; private List? DeviceList { get => _deviceList; set { _deviceList = value; StateHasChanged(); if (value == null || value.Count < 1) SelectedDevice = ""; else if (string.IsNullOrEmpty(SelectedDevice)) { var device = value.FirstOrDefault(dev => dev!.Values.FirstOrDefault(rec => rec.Key == "deviceId").Value.ToString() == "virtual_device"); if (device != null) SelectedDevice = device.Values.FirstOrDefault(rec => rec.Key == "deviceId").Value.ToString(); } } } private string? _newDevice = ""; private string? NewDevice { get => _newDevice; set { _newDevice = value; StateHasChanged(); } } private async Task FetchDevices(string? value) { if (!string.IsNullOrEmpty(value)) { var tables = await InfluxModel.FetchDeviceList(value); if (tables != null) { var records = tables.Select(table => table.Records .FirstOrDefault(rec => rec.Values.ContainsKey("deviceId"))).ToList(); DeviceList = records; } } else { DeviceList = new List()!; } } private bool _registerDeviceBusy; private bool RegisterDeviceBusy { get => _registerDeviceBusy; set { _registerDeviceBusy = value; StateHasChanged(); } } private async void RegisterDevice() { RegisterDeviceBusy = true; if (!string.IsNullOrEmpty(SelectedBucket) && !string.IsNullOrEmpty(NewDevice)) { await InfluxModel.CreateDevice(NewDevice, "virtual", SelectedBucket); await FetchDevices(SelectedBucket); NewDevice = ""; } else if (string.IsNullOrEmpty(SelectedBucket)) await JsRuntime.InvokeAsync("confirm", "Please select bucket."); else if (string.IsNullOrEmpty(NewDevice)) await JsRuntime.InvokeAsync("confirm", "DeviceId cannot be empty."); RegisterDeviceBusy = false; } private bool _removeDeviceBusy; private bool RemoveDeviceBusy { get => _removeDeviceBusy; set { _removeDeviceBusy = value; StateHasChanged(); } } private async Task RemoveDevice(FluxRecord record) { if (SelectedBucket != null) { RemoveDeviceBusy = true; if (await InfluxModel.RemoveDevice(record, SelectedBucket)) await FetchDevices(SelectedBucket); } RemoveDeviceBusy = false; } private bool _refreshDevicesBusy; private bool RefreshDevicesBusy { get => _refreshDevicesBusy; set { _refreshDevicesBusy = value; StateHasChanged(); } } private async Task RefreshDevices() { RefreshDevicesBusy = true; _bucketList = await InfluxModel.FetchBuckets(); if (string.IsNullOrEmpty(SelectedBucket) && _bucketList.FirstOrDefault(bucket => bucket.Name == "iot_center") != null) SelectedBucket = "iot_center"; RefreshDevicesBusy = false; } #endregion ---------------------------- DeviceRegistration ---------------------------- #region ---------------------------- DeviceDashboard ---------------------------- private string? _selectedDevice; private string? SelectedDevice { get => _selectedDevice; set { _selectedDevice = value; StateHasChanged(); RefreshData(); } } private bool _writeEmulatedDataBusy; private bool WriteEmulatedDataBusy { get => _writeEmulatedDataBusy; set { _writeEmulatedDataBusy = value; StateHasChanged(); } } private async void WriteEmulatedData() { WriteEmulatedDataBusy = true; if (SelectedBucket != null && !string.IsNullOrEmpty(SelectedDevice)) { await InfluxModel.WriteEmulatedData(SelectedDevice, SelectedBucket); RefreshData(); } WriteEmulatedDataBusy = false; } private bool _refreshDataBusy; private bool RefreshDataBusy { get => _refreshDataBusy; set { _refreshDataBusy = value; StateHasChanged(); } } private async void RefreshData() { RefreshDataBusy = true; if (_chartDataList != null) foreach (var data in _chartDataList) await data.FetchDataMean(SelectedBucket, SelectedDevice); if (SelectedBucket != null && !string.IsNullOrEmpty(SelectedDevice)) Measurements = await InfluxModel.FetchMeasurements(SelectedBucket, SelectedDevice); else Measurements = new List(); RefreshDataBusy = false; } private List? _chartDataList; private List? _measurements; private List? Measurements { get => _measurements; set { _measurements = value; StateHasChanged(); } } #endregion ---------------------------- DeviceDashboard ---------------------------- string FormatDateTime(object? value) { return value != null ? Convert.ToDateTime(value.ToString()).ToString(CultureInfo.InvariantCulture) : string.Empty; } }