private static async Task RecordCurrentPrice(Config options, MetricsCollector influxCollector) { var client = new FlickAndroidClient(options.Username, options.Password); var userInfo = await client.GetUserInfo(); var forecastPrices = await client.GetPriceForecast(userInfo.AuthorizedDataContexts.SupplyNodes[0]); var currentPredictedPrice = forecastPrices.Prices.MinBy(price => price.StartsAt); var priceComponents = new Dictionary <string, object>(); foreach (var priceComponent in currentPredictedPrice.Components) { priceComponents[$"{priceComponent.ChargeSetter}_{priceComponent.ChargeMethod}"] = priceComponent.Value; } influxCollector.Write("PredictedPrice.Components", priceComponents, timestamp: currentPredictedPrice.StartsAt.ToUniversalTime()); var total = new Dictionary <string, object> { ["total"] = currentPredictedPrice.Price.Value }; influxCollector.Write("PredictedPrice.Total", total, timestamp: currentPredictedPrice.StartsAt.ToUniversalTime()); Console.WriteLine("Finished recording current power price"); }
private static async Task RecordDetailedHistoricUsage(Config options, MetricsCollector influxCollector) { VerifyLookbackSet(options); var client = new FlickWebClient(options.Username, options.Password); for (var lookback = options.LookBackDays; lookback >= 0; lookback--) { var date = DateTime.Now.AddDays(-lookback); try { Console.WriteLine($"Fetching detailed power usage for {date.Date}"); var powerUsage = await client.FetchDetailedUsageForDay(date).ConfigureAwait(false); foreach (var powerAndPriceInterval in powerUsage) { var fields = new Dictionary <string, object> { ["price"] = powerAndPriceInterval.Price, ["units"] = powerAndPriceInterval.Units, ["total_cost"] = powerAndPriceInterval.Units * powerAndPriceInterval.Price }; influxCollector.Write("DetailedPowerUsage", fields, timestamp: powerAndPriceInterval.Start.ToUniversalTime()); } } catch (Exception exception) { Console.WriteLine($"Skipping {date} due to an exception {exception}"); } } }
private void MessageReceived(string message) { _logger.Information("Message Received: {Message}", message); try { var hubMessage = JsonConvert.DeserializeObject <HubMessage>(message); if (string.IsNullOrWhiteSpace(hubMessage.Value)) { _logger.Warning("Ignoring message from device {DeviceId} with no value specified", hubMessage.DeviceId); return; } var processedMessage = ProcessMessage(hubMessage); if (MessageShouldBeLogged(processedMessage)) { _logger.Debug("Writing Data: {Data}", processedMessage.Data); _logger.Debug("Writing Tags: {Tags}", processedMessage.Tags); _collector.Write(hubMessage.Name, processedMessage.Data, processedMessage.Tags, processedMessage.LoggedAt); } else { _logger.Information("Ignoring measurement"); } } catch (Exception ex) { _logger.Error(ex, "Error deserializing message '{Message}'", message); } }
public override void SendInflux(MetricsCollector collector, string prefix) { collector.Write( $"{prefix}{UID}", new Dictionary <string, object> { { "moisture", MoistureFraction } } ); }
public override void SendInflux(MetricsCollector collector, string prefix) { collector.Write( $"{prefix}{UID}", new Dictionary <string, object> { { "light_raw", RawLightValue }, { "light_norm", NormalizedLightValue } } ); }
protected override void WriteRecord(string records) { var fields = records.Split(",")[1]; var values = fields.Split(); _collector.Write(MeasurementName, new Dictionary <string, object> { { values[1].Split("=")[0], values[1].Split("=")[1] } }, new Dictionary <string, string> { { values[0].Split("=")[0], values[0].Split("=")[1] } }); Interlocked.Increment(ref Counter); }
public void RegisterDepartmentFeedback(string department, int feedback) { var values = new Dictionary <string, object> { { "rating", feedback } }; var tags = new Dictionary <string, string> { { "department", department } }; _collector.Write("user_satisfaction", values, tags); }
private static async Task RecordSimpleHistoricUsage(Config options, MetricsCollector influxCollector) { VerifyLookbackSet(options); var webClient = new FlickWebClient(options.Username, options.Password); var powerUsage = await webClient.GetPowerUsage(DateTime.Now.Subtract(TimeSpan.FromDays(options.LookBackDays)), DateTime.Now).ConfigureAwait(false); foreach (var usageBucket in powerUsage) { var fields = new Dictionary <string, object> { ["usage"] = usageBucket.Value }; influxCollector.Write("PowerUsage", fields, timestamp: usageBucket.StartedAt.ToUniversalTime()); } Console.WriteLine($"Finished recording {powerUsage.Count} power usage buckets"); }
public void Create(Interfaces.TimeseriesData data) { if (data == null || data.DataPoints == null) { return; } foreach (var point in data.DataPoints) { var fields = new Dictionary <string, object>(); fields.Add("Value", point.Value); var tags = new Dictionary <string, string>(); tags.Add("Tag", data.Tag); tags.Add("Source", data.Source); lock (metricsCollector) { metricsCollector.Write(measurement, fields, tags, point.Timestamp); } } }