public void OnGet() { HttpContext.Request.Cookies.TryGetValue("timezone", out var timezone); MeasuresApiConnector.GetMeasures(HttpContext, out IEnumerable <Measure> measures); foreach (Measure measure in measures) { string device = "ESP32_CAM" + measure.LocationID.ToString() + ":1.8." + measure.MeasureTypeID.ToString(); List <LineSeriesData> data = ViewData[device] as List <LineSeriesData>; if (data == null) { ViewData[device] = new List <LineSeriesData>(); } (ViewData[device] as List <LineSeriesData>).Add(new LineSeriesData { X = measure.TimeStamp .AddMinutes(Convert.ToDouble(timezone)) .Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)) .TotalMilliseconds, Y = measure.Value, Id = measure.Id.ToString() }); } var chartOptions = new Highstock { ID = "chart", Chart = new Chart { Type = ChartType.Line, ZoomType = ChartZoomType.X, BackgroundColor = "transparent" }, /*Title = new Title * { * Text = "Snow depth at Vikjafjellet, Norway" * },*/ RangeSelector = new RangeSelector { Enabled = true, AllButtonsEnabled = true, Selected = 3, Buttons = new List <RangeSelectorButton> { new RangeSelectorButton { Type = "week", Count = 2, Text = "2" + _modelLocalizer["w"].Value }, new RangeSelectorButton { Type = "week", Count = 1, Text = "1" + _modelLocalizer["w"].Value }, new RangeSelectorButton { Type = "day", Count = 3, Text = "3" + _modelLocalizer["d"].Value }, new RangeSelectorButton { Type = "day", Count = 1, Text = "1" + _modelLocalizer["d"].Value }, new RangeSelectorButton { Type = "hour", Count = 6, Text = "6" + _modelLocalizer["hr"].Value }, new RangeSelectorButton { Type = "hour", Count = 1, Text = "1" + _modelLocalizer["hr"].Value } } }, XAxis = new List <XAxis> { new XAxis { Title = new XAxisTitle { Text = _modelLocalizer["Date"].Value }, DateTimeLabelFormats = new Hashtable { } } }, YAxis = new List <YAxis> { new YAxis { Title = new YAxisTitle { Text = _modelLocalizer["Consumption"].Value + " [kWh]" } } }, Legend = new Legend { Enabled = true }, Tooltip = new Tooltip { HeaderFormat = "<b>{series.name}</b>", PointFormat = "{point.x:%H:%M %e/%m} <b>{point.y: .0f} kWh<b>" }, PlotOptions = new PlotOptions { Series = new PlotOptionsSeries { Marker = new PlotOptionsSeriesMarker { Enabled = true }, ShowInLegend = true, TurboThreshold = 5000, Selected = true, StickyTracking = false, Visible = true } }, Series = new List <Series>(), Credits = new Credits { Enabled = false }, Lang = new Lang { RangeSelectorFrom = _modelLocalizer["From"].Value, RangeSelectorTo = _modelLocalizer["To"].Value, ViewFullscreen = _modelLocalizer["View in full screen"].Value, PrintChart = _modelLocalizer["Print chart"].Value, DownloadPNG = _modelLocalizer["Download PNG image"].Value, DownloadJPEG = _modelLocalizer["Download JPEG image"].Value, DownloadPDF = _modelLocalizer["Download PDF document"].Value, DownloadSVG = _modelLocalizer["Download SVG vector image"].Value, }, Exporting = new Exporting { Buttons = new ExportingButtons { ContextButton = new ExportingButtonsContextButton { Theme = new ExportingButtonsContextButtonTheme { Fill = "none" } } } } }; foreach (var x in ViewData.Select((Data, idx) => new { idx, Data })) { chartOptions.Series.Add(new LineSeries { Name = x.Data.Key, Data = x.Data.Value as List <LineSeriesData>, ShowInLegend = true, ShowInNavigator = true, LegendIndex = x.idx }); } var renderer = new HighstockRenderer(chartOptions).RenderHtml(); ViewData["renderer"] = renderer.Replace("var ChartOptions = {", "var ChartOptions = {\"legend\":{\"enabled\": true},"); }
public static async Task Process(Event @event) { if (@event.OnHold) { return; } string title; string message; string thingId; Gateway gateway; long locationId; IEnumerable <Measure> measures; long recentMeasuresSum, oldMeasuresSum, olderMeasuresSum, delta; switch (@event.EventType) { case Models.EventType.Threshold: thingId = "EMSfIIoT:" + @event.Variable.Split(":").First(); gateway = BoschIoTSuiteApiConnector.gateways.Where(gateway => gateway.thingId.Equals(thingId)).First(); locationId = gateway.attributes.configuration.devices.Where(device => device.deviceId.Equals(@event.Variable)).First().locationId; MeasuresApiConnector.GetDeviceLastMeasures(locationId, out measures); recentMeasuresSum = measures.Sum(measure => measure.Value); MeasuresApiConnector.GetDeviceLastMeasures(locationId, out measures, 1); oldMeasuresSum = measures.Sum(measure => measure.Value); delta = recentMeasuresSum - oldMeasuresSum; title = "Device threshold"; switch (@event.EventValueType) { case EventValueType.EqualTo: if (delta == @event.EventValue) { message = $"Your device {@event.Variable} read equaled the defined threshold {@event.EventValue}"; break; } else { return; } case EventValueType.GreaterThan: if (delta > @event.EventValue) { message = $"Your device {@event.Variable} read surpassed the defined threshold {@event.EventValue}"; break; } else { return; } case EventValueType.GreaterThanOrEqualTo: if (delta >= @event.EventValue) { message = $"Your device {@event.Variable} read surpassed or equaled the defined threshold {@event.EventValue}"; break; } else { return; } case EventValueType.LessThan: if (delta < @event.EventValue) { message = $"Your device {@event.Variable} read falled behind the defined threshold {@event.EventValue}"; break; } else { return; } case EventValueType.LessThanOrEqualTo: if (delta <= @event.EventValue) { message = $"Your device {@event.Variable} read falled behind or equaled the defined threshold {@event.EventValue}"; break; } else { return; } case EventValueType.NotEqualTo: if (delta != @event.EventValue) { message = $"Your device {@event.Variable} read differed the the defined threshold {@event.EventValue}"; break; } else { return; } default: return; } break; case Models.EventType.Inactive: gateway = BoschIoTSuiteApiConnector.gateways.Where(gateway => gateway.thingId.Equals(@event.Variable)).First(); locationId = gateway.attributes.configuration.devices.First().locationId; MeasuresApiConnector.GetDeviceLastMeasures(locationId, out var lastRead); DateTime lastMeasureDate = lastRead.First().TimeStamp; if (lastMeasureDate.AddMinutes(@event.EventValue) < DateTime.UtcNow) { message = $"Your gateway {@event.Variable} last communication was {Math.Floor(DateTime.UtcNow.Subtract(lastMeasureDate).TotalMinutes)} minutes ago"; title = "Gateway inactive"; break; } else { return; } case Models.EventType.Algorithm: thingId = "EMSfIIoT:" + @event.Variable.Split(":").First(); gateway = BoschIoTSuiteApiConnector.gateways.Where(gateway => gateway.thingId.Equals(thingId)).First(); locationId = gateway.attributes.configuration.devices.Where(device => device.deviceId.Equals(@event.Variable)).First().locationId; double percentage; double multiplier; string timeSpan; switch (@event.EventFrequency) { case EventFrequency.Every15Minutes: multiplier = 1 / 4.0; timeSpan = "15 minutes"; break; case EventFrequency.Every30Minutes: multiplier = 1 / 2.0; timeSpan = "30 minutes"; break; case EventFrequency.EveryHour: multiplier = 1; timeSpan = "hour"; break; case EventFrequency.Every6Hours: multiplier = 6; timeSpan = "6 hours"; break; case EventFrequency.EveryDay: multiplier = 24; timeSpan = "day"; break; case EventFrequency.EveryWeek: multiplier = 24 * 7; timeSpan = "week"; break; default: return; } MeasuresApiConnector.GetDeviceLastMeasures(locationId, out measures); recentMeasuresSum = measures.Sum(measure => measure.Value); MeasuresApiConnector.GetDeviceMeasures(locationId, out measures, Convert.ToInt32(multiplier * 60)); MeasuresApiConnector.GetDeviceLastMeasures(locationId, out measures, measures.Count()); oldMeasuresSum = measures.Sum(measure => measure.Value); MeasuresApiConnector.GetDeviceMeasures(locationId, out measures, Convert.ToInt32(2 * multiplier * 60)); MeasuresApiConnector.GetDeviceLastMeasures(locationId, out measures, measures.Count()); olderMeasuresSum = measures.Sum(measure => measure.Value); if (oldMeasuresSum != olderMeasuresSum) { percentage = Convert.ToDouble(recentMeasuresSum - oldMeasuresSum) / Convert.ToDouble(oldMeasuresSum - olderMeasuresSum); } else { percentage = double.PositiveInfinity; } if (Math.Abs(percentage) > @event.EventValue) { message = $"Your device {@event.Variable} read from the last {timeSpan} differed the previous one more than the defined threshold of {@event.EventValue}%. Difference was {percentage}%."; } else { return; } title = "Device threshold"; break; default: return; } await SendNotificationToUser("APINotification", title, message, @event); await _events.PutEventOnHold(@event.Id); }