143 lines
5.2 KiB
C#
143 lines
5.2 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using HHWMS.Web.Models;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
namespace HHWMS.Web.Services.DeviceData
|
|
{
|
|
public class DashboardPushService
|
|
{
|
|
private readonly IHubContext<DashboardHub> _hubContext;
|
|
|
|
public DashboardPushService(IHubContext<DashboardHub> hubContext)
|
|
{
|
|
_hubContext = hubContext;
|
|
}
|
|
|
|
public async Task PushProcessingResultAsync(ProcessingResult result, IUnitWork unitWork)
|
|
{
|
|
if (result?.Handled != true || result.Updated != true || string.IsNullOrWhiteSpace(result.DeviceId))
|
|
{
|
|
return;
|
|
}
|
|
|
|
await _hubContext.Clients.All.SendAsync("device.updated", new
|
|
{
|
|
DeviceId = result.DeviceId,
|
|
DeviceName = result.DeviceId,
|
|
DeviceType = result.DeviceType,
|
|
Status = result.Status,
|
|
LastPayloadTime = result.PayloadTime,
|
|
LastUpdateTime = DateTime.Now,
|
|
Realtime = ParseJson(result.ParsedData),
|
|
DailyProduction = BuildDailyProduction(result.DeviceId, DateTime.Today, unitWork)
|
|
});
|
|
|
|
var trendPoints = BuildTrendPoints(result, unitWork);
|
|
await _hubContext.Clients.All.SendAsync("trend.updated", new
|
|
{
|
|
deviceId = result.DeviceId,
|
|
points = trendPoints
|
|
});
|
|
|
|
var alarms = unitWork.Find<DeviceAlarmRecord>(x => x.DeviceId == result.DeviceId && x.IsActive)
|
|
.OrderByDescending(x => x.LastSeenTime ?? x.StartTime)
|
|
.Select(x => new
|
|
{
|
|
x.DeviceId,
|
|
x.AlarmCode,
|
|
x.AlarmMessage,
|
|
x.StartTime,
|
|
x.LastSeenTime,
|
|
x.RepeatCount
|
|
})
|
|
.ToList();
|
|
await _hubContext.Clients.All.SendAsync("alarm.updated", new
|
|
{
|
|
deviceId = result.DeviceId,
|
|
alarms
|
|
});
|
|
}
|
|
|
|
private static object ParseJson(string json)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(json))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
return JsonSerializer.Deserialize<object>(json);
|
|
}
|
|
catch
|
|
{
|
|
return json;
|
|
}
|
|
}
|
|
|
|
private static object BuildDailyProduction(string deviceId, DateTime endDate, IUnitWork unitWork)
|
|
{
|
|
var startDate = endDate.Date.AddDays(-6);
|
|
var rows = unitWork.Find<DeviceProductionDaily>(x => x.DeviceId == deviceId && x.ProductionDate >= startDate && x.ProductionDate <= endDate.Date).ToList();
|
|
var dates = Enumerable.Range(0, 7).Select(offset => endDate.Date.AddDays(offset - 6)).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 BuildTrendPoints(ProcessingResult result, IUnitWork unitWork)
|
|
{
|
|
if (result.DeviceType == "power")
|
|
{
|
|
var startTime = DateTime.Now.AddHours(-1);
|
|
var metrics = new[] { "currentA", "currentB", "currentC", "estimatedPower" };
|
|
var windowPoints = unitWork.Find<DeviceMetricTrendPoint>(x => x.DeviceId == result.DeviceId && x.BucketTime >= startTime && metrics.Contains(x.MetricName)).ToList();
|
|
var leadingPoints = metrics
|
|
.Select(metric => unitWork.Find<DeviceMetricTrendPoint>(x => x.DeviceId == result.DeviceId && x.MetricName == metric && x.BucketTime < startTime)
|
|
.OrderByDescending(x => x.BucketTime)
|
|
.FirstOrDefault())
|
|
.Where(x => x != null)
|
|
.ToList();
|
|
|
|
return ToTrendPointDto(leadingPoints.Concat(windowPoints));
|
|
}
|
|
|
|
var points = unitWork.Find<DeviceMetricTrendPoint>(x => x.DeviceId == result.DeviceId)
|
|
.OrderByDescending(x => x.BucketTime)
|
|
.Take(80)
|
|
.OrderBy(x => x.BucketTime)
|
|
.ToList();
|
|
|
|
return ToTrendPointDto(points);
|
|
}
|
|
|
|
private static object ToTrendPointDto(System.Collections.Generic.IEnumerable<DeviceMetricTrendPoint> points)
|
|
{
|
|
return points
|
|
.OrderBy(x => x.BucketTime)
|
|
.Select(x => new
|
|
{
|
|
x.DeviceId,
|
|
x.MetricName,
|
|
x.BucketTime,
|
|
x.AvgValue,
|
|
x.MaxValue,
|
|
x.MinValue,
|
|
x.SampleCount
|
|
})
|
|
.ToList();
|
|
}
|
|
}
|
|
}
|