/// <summary> /// Gets the current devices for plugin from Homeseer /// </summary> /// <returns>Current devices for plugin</returns> /// <exception cref="HspiException"></exception> private IDictionary <string, DeviceClass> GetCurrentDevices() { var deviceEnumerator = HS.GetDeviceEnumerator() as clsDeviceEnumeration; if (deviceEnumerator == null) { throw new HspiException(Invariant($"{Name} failed to get a device enumerator from HomeSeer.")); } var currentDevices = new Dictionary <string, DeviceClass>(); do { ShutdownCancellationToken.ThrowIfCancellationRequested(); DeviceClass device = deviceEnumerator.GetNext(); if ((device != null) && (device.get_Interface(HS) != null) && (device.get_Interface(HS).Trim() == Name)) { string address = device.get_Address(HS); currentDevices.Add(address, device); } } while (!deviceEnumerator.Finished); return(currentDevices); }
private async Task RecordTrackedDevices() { var collector = await GetInfluxDBMeasurementsCollector().ConfigureAwait(false); if (collector != null) { var deviceEnumerator = HS.GetDeviceEnumerator() as clsDeviceEnumeration; do { DeviceClass device = deviceEnumerator.GetNext(); if (device != null) { if (collector.IsTracked(device.get_Ref(HS), null)) { await RecordDeviceValue(collector, HS, device).ConfigureAwait(false); } } ShutdownCancellationToken.ThrowIfCancellationRequested(); }while (!deviceEnumerator.Finished); } }
/// <summary> /// Fetches the WU Data and update devices. /// </summary> /// <param name="existingDevices">The existing devices in HS.</param> /// <param name="token">The token.</param> /// <returns>Task</returns> private async Task FetchAndUpdateDevices(IDictionary <string, DeviceClass> existingDevices, CancellationToken token) { if (string.IsNullOrWhiteSpace(pluginConfig.APIKey) || string.IsNullOrWhiteSpace(pluginConfig.StationId)) { Trace.TraceWarning("Configuration not setup to fetch weather data"); return; } Trace.TraceInformation(Invariant($"Starting data fetch from WU Weather from station:{pluginConfig.StationId}")); LogConfiguration(); WUWeatherService service = new WUWeatherService(pluginConfig.APIKey); XmlDocument rootXmlDocument = await service.GetDataForStationAsync(pluginConfig.StationId, token).ConfigureAwait(false); XPathNavigator rootNavigator = rootXmlDocument.CreateNavigator(); foreach (var deviceDefinition in WUWeatherData.DeviceDefinitions) { ShutdownCancellationToken.ThrowIfCancellationRequested(); existingDevices.TryGetValue(deviceDefinition.Name, out var rootDevice); if (rootDevice == null) { // no root device exists yet so children won't exist continue; } Unit currentUnit = pluginConfig.Unit; XPathExpression childExpression = deviceDefinition.PathData.GetPath(currentUnit); XPathNodeIterator childNodeIter = rootNavigator.Select(childExpression); if (childNodeIter != null && childNodeIter.MoveNext()) { XmlElement childElement = childNodeIter.Current.UnderlyingObject as XmlElement; if (childElement == null) { Trace.TraceWarning(Invariant($"{deviceDefinition.Name} has invalid type in xml document.")); continue; } deviceDefinition.UpdateDeviceData(HS, rootDevice, childElement); var childNavigator = childElement.CreateNavigator(); DateTimeOffset?lastUpdate = deviceDefinition.LastUpdateTime; foreach (var childDeviceDefinition in deviceDefinition.Children) { string childAddress = CreateChildAddress(deviceDefinition.Name, childDeviceDefinition.Name); existingDevices.TryGetValue(childAddress, out var childDevice); if (childDevice != null) { ShutdownCancellationToken.ThrowIfCancellationRequested(); try { XPathExpression subExpression = childDeviceDefinition.PathData.GetPath(this.pluginConfig.Unit); XPathNodeIterator elements = childNavigator.Select(subExpression); childDeviceDefinition.UpdateDeviceData(HS, childDevice, elements); if (lastUpdate.HasValue) { childDevice.set_Last_Change(HS, lastUpdate.Value.DateTime); } var scaledNumberDeviceData = childDeviceDefinition as ScaledNumberDeviceData; if (scaledNumberDeviceData != null) { childDevice.set_ScaleText(HS, scaledNumberDeviceData.GetDeviceSuffix(currentUnit)); } } catch (OperationCanceledException) { } catch (Exception ex) { Trace.TraceError(Invariant($"Failed to update [{childAddress}] with {ex.GetFullMessage()}")); } } } } } Trace.TraceInformation(Invariant($"Finished Processing update from station:{pluginConfig.StationId}")); }