public IList <IDictionary <string, object> > GetDevices() { return(GetDevicesAsync().ResultForSync()); async Task <IList <IDictionary <string, object> > > GetDevicesAsync() { var list = new List <IDictionary <string, object> >(); var statusMap = new Dictionary <int, Task <TasmotaDeviceStatus> >(); var devices = await this.GetTasmotaDevices().ConfigureAwait(false); foreach (var pair in devices) { TasmotaDeviceInfo?data = pair.Value.Data; if (data != null) { statusMap.Add(pair.Key, TasmotaDeviceInterface.GetStatus(data, ShutdownCancellationToken)); } } foreach (var pair in devices) { var data = new Dictionary <string, object> { { "refId", pair.Key } }; var tasmotaData = pair.Value.Data; data.Add("uri", tasmotaData?.Uri?.ToString() ?? string.Empty); try { if (statusMap.TryGetValue(pair.Key, out var task)) { var status = await task.ConfigureAwait(false); data.Add("Version", status.Version ?? string.Empty); data.Add("BuildDateTime", status.BuildDateTime ?? string.Empty); data.Add("BootCount", status.BootCount ?? string.Empty); data.Add("UpTime", status.Uptime ?? string.Empty); data.Add("RestartReason", status.RestartReason ?? string.Empty); } } catch { // Ignore errors while reading tasmota } list.Add(data); } return(list); } }
public override string GetJuiDeviceConfigPage(int devOrFeatRef) { var page = PageFactory.CreateDeviceConfigPage(PlugInData.ConfigPageId, "Tasmota Device Configuration"); var tasmotaDevices = GetTasmotaDevices().ResultForSync(); if (tasmotaDevices.TryGetValue(devOrFeatRef, out var tasmotaDevice)) { var data = tasmotaDevice.Data; var networkInfoView = new GridView("id_network", "Network"); networkInfoView.AddView(new InputView(nameof(TasmotaDeviceInfo.Uri), "Http url of the device", data?.Uri?.ToString() ?? string.Empty, EInputType.Url)); networkInfoView.AddView(new InputView(nameof(TasmotaDeviceInfo.User), "User", data?.User ?? string.Empty, EInputType.Text)); networkInfoView.AddView(new InputView(nameof(TasmotaDeviceInfo.Password), "Password", data?.Password ?? string.Empty, EInputType.Password)); page = page.WithView(networkInfoView); try { if (data != null) { var tasmotaStatus = TasmotaDeviceInterface.GetStatus(data, ShutdownCancellationToken).ResultForSync(); var possibleFeatures = tasmotaStatus.GetPossibleFeatures(); var telePeriod = TasmotaDeviceInterface.GetTelePeriod(data, ShutdownCancellationToken).ResultForSync(); var groups = possibleFeatures.GroupBy((x) => x.SourceType); var settingView = new GridView("id_settings", "Settings"); settingView.AddView(new InputView(TelePeriodId, TelePeriodId, telePeriod.ToString(CultureInfo.InvariantCulture), EInputType.Number)); page = page.WithView(settingView); foreach (var group in groups) { var groupView = new GridView("id_" + group.Key.ToString(), group.Key.ToString()); AddFeatureEnabledOptions(groupView, group, data?.EnabledFeatures ?? ImmutableHashSet <TasmotaDeviceFeature> .Empty); page = page.WithView(groupView); } } } catch (Exception ex) { page.WithLabel("featureerror", Invariant($"Unable to contact device with {ex.GetFullMessage()}, cannot set feature of device.")); } } else { page.WithLabel("notfound", "No Configuration Found"); } return(page.Page.ToJsonString());