using System; using System.Linq; using System.Text.Json; using System.Text.Json.Nodes; using System.Threading.Tasks; using HHWMS.Web.Models; using Microsoft.AspNetCore.SignalR; namespace HHWMS.Web { public class DashboardHub : Hub { private readonly IUnitWork _unitWork; public DashboardHub(IUnitWork unitWork) { _unitWork = unitWork; } public Task GetSnapshot() { var productionDate = DateTime.Today; var dailyProductions = _unitWork.Find(x => x.ProductionDate == productionDate).ToList(); var productionStartDate = productionDate.AddDays(-6); var recentProductions = _unitWork.Find(x => x.ProductionDate >= productionStartDate && x.ProductionDate <= productionDate).ToList(); var states = _unitWork.Find(x => true) .OrderBy(x => x.DeviceType) .ThenBy(x => x.DeviceId) .ToList(); var devices = states .Select(x => new { x.DeviceId, x.DeviceName, x.DeviceType, x.Status, x.LastPayloadTime, x.LastUpdateTime, x.AlarmSummary, Realtime = ParseRealtimeJson(x, dailyProductions.FirstOrDefault(d => d.DeviceId == x.DeviceId)), DailyProduction = BuildDailyProduction(x.DeviceId, productionDate, recentProductions), TrendPoints = BuildRecentTrendPoints(x.DeviceId, x.DeviceType, _unitWork) }) .ToList(); var alarms = _unitWork.Find(x => x.IsActive) .OrderByDescending(x => x.LastSeenTime ?? x.StartTime) .Take(20) .Select(x => new { x.DeviceId, x.AlarmCode, x.AlarmMessage, x.StartTime, x.LastSeenTime, x.RepeatCount }) .ToList(); return Task.FromResult(new { generatedAt = DateTime.Now, devices, alarms }); } private static object ParseRealtimeJson(DeviceCurrentState state, DeviceProductionDaily daily) { if (string.IsNullOrWhiteSpace(state.RealtimeJson)) { return null; } try { var node = JsonNode.Parse(state.RealtimeJson); if (node is JsonObject obj && state.DeviceType == "cnc" && daily != null) { obj["todayProduce"] = daily.ProduceCount; } return node == null ? null : JsonSerializer.Deserialize(node.ToJsonString()); } catch { return state.RealtimeJson; } } private static object BuildDailyProduction(string deviceId, DateTime endDate, System.Collections.Generic.IEnumerable productions) { var rows = productions.Where(x => x.DeviceId == deviceId).ToList(); var dates = Enumerable.Range(0, 7).Select(offset => endDate.AddDays(offset - 6).Date).ToList(); return new { dates = dates.Select(x => x.ToString("MM/dd")).ToArray(), produce = dates.Select(date => rows.FirstOrDefault(x => x.ProductionDate.Date == date)?.ProduceCount ?? 0).ToArray(), runtime = dates.Select(date => { var minutes = rows.FirstOrDefault(x => x.ProductionDate.Date == date)?.RuntimeMinutes ?? 0; return Math.Round(minutes / 60m, 2); }).ToArray() }; } private static object BuildRecentTrendPoints(string deviceId, string deviceType, IUnitWork unitWork) { if (deviceType != "power") { return Array.Empty(); } var startTime = DateTime.Now.AddHours(-1); var metrics = new[] { "currentA", "currentB", "currentC", "estimatedPower" }; var windowPoints = unitWork.Find(x => x.DeviceId == deviceId && x.BucketTime >= startTime && metrics.Contains(x.MetricName)).ToList(); var leadingPoints = metrics .Select(metric => unitWork.Find(x => x.DeviceId == deviceId && x.MetricName == metric && x.BucketTime < startTime) .OrderByDescending(x => x.BucketTime) .FirstOrDefault()) .Where(x => x != null) .ToList(); return leadingPoints.Concat(windowPoints) .OrderBy(x => x.BucketTime) .Select(x => new { x.DeviceId, x.MetricName, x.BucketTime, x.AvgValue, x.MaxValue, x.MinValue, x.SampleCount }) .ToList(); } } }