462 lines
20 KiB
C#
462 lines
20 KiB
C#
using System;
|
||
using System.Globalization;
|
||
using System.Linq;
|
||
using System.Text.Json;
|
||
using System.Text.RegularExpressions;
|
||
using HHWMS.Web.Models;
|
||
|
||
namespace HHWMS.Web.Services.DeviceData
|
||
{
|
||
public class CncMessageProcessor
|
||
{
|
||
private const decimal SpindleSpeedDeadband = 50m;
|
||
private const decimal FeedRateDeadband = 20m;
|
||
private static readonly TimeSpan MaxDeadbandSilence = TimeSpan.FromMinutes(1);
|
||
|
||
public ProcessingResult Process(MqttMessageRecord message, IUnitWork unitwork)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(message?.Payload))
|
||
{
|
||
return ProcessingResult.NotHandled();
|
||
}
|
||
|
||
using var document = JsonDocument.Parse(message.Payload);
|
||
var root = document.RootElement;
|
||
if (root.ValueKind != JsonValueKind.Object || !LooksLikeCncPayload(root))
|
||
{
|
||
return ProcessingResult.NotHandled();
|
||
}
|
||
|
||
var payloadTime = ReadTimestamp(root) ?? (message.ReceiveTime == default ? DateTime.Now : message.ReceiveTime);
|
||
var statusInfo = root.TryGetProperty("statusInfo", out var statusElement) && statusElement.ValueKind == JsonValueKind.Object
|
||
? statusElement
|
||
: default;
|
||
var speedFeed = root.TryGetProperty("spindleSpeedAndFeedRate", out var speedFeedElement) && speedFeedElement.ValueKind == JsonValueKind.Object
|
||
? speedFeedElement
|
||
: default;
|
||
var programCurrent = root.TryGetProperty("programCurrent", out var programElement) && programElement.ValueKind == JsonValueKind.Object
|
||
? programElement
|
||
: default;
|
||
|
||
var deviceId = ResolveDeviceId(root, message.Topic);
|
||
var alarmMessages = ReadAlarmMessages(root);
|
||
var alarmStatus = ReadInt(root, "alarmStatus") ?? 0;
|
||
var emergency = ReadInt(statusInfo, "Emergency") ?? 0;
|
||
var alarmFlag = ReadInt(statusInfo, "Alarm") ?? 0;
|
||
var status = ResolveStatus(statusInfo, alarmStatus, alarmFlag, emergency);
|
||
var programName = ReadString(programCurrent, "name");
|
||
var programNumber = ReadInt(programCurrent, "number");
|
||
var currentProduceCount = ReadInt(root, "currentProduceCount");
|
||
var spindleSpeed = ReadDecimal(speedFeed, "speed");
|
||
var feedRate = ReadDecimal(speedFeed, "feedRate");
|
||
var setSpindleSpeed = ReadDecimal(root, "setSpindleSpeed");
|
||
var setFeedRate = ReadDecimal(root, "setFeedRate");
|
||
var runtimeMinutes = ParseFanucTimeMinutes(ReadString(root, "runtime"));
|
||
var cuttimeMinutes = ParseFanucTimeMinutes(ReadString(root, "cuttime"));
|
||
var cycletimeMinutes = ParseFanucTimeMinutes(ReadString(root, "cycletime"));
|
||
var workMode = ReadString(statusInfo, "WorkMode");
|
||
var runStatus = ReadString(statusInfo, "RunStatus");
|
||
var todayProduce = GetTodayProduce(unitwork, deviceId, payloadTime, currentProduceCount);
|
||
|
||
var realtime = new
|
||
{
|
||
currentProduceCount,
|
||
todayProduce,
|
||
programName,
|
||
programNumber,
|
||
workMode,
|
||
runStatus,
|
||
status,
|
||
alarmMessages,
|
||
runtimeMinutes,
|
||
cuttimeMinutes,
|
||
cycletimeMinutes,
|
||
spindleSpeed,
|
||
feedRate,
|
||
setSpindleSpeed,
|
||
setFeedRate,
|
||
cncType = ReadNestedString(root, "sysInfo", "cncType"),
|
||
axes = ReadNestedInt(root, "sysInfo", "axes"),
|
||
payloadTime
|
||
};
|
||
var realtimeJson = JsonSerializer.Serialize(realtime);
|
||
|
||
var currentState = unitwork.Find<DeviceCurrentState>(x => x.DeviceId == deviceId).FirstOrDefault();
|
||
if (IsDeadbandHit(currentState, status, programName, currentProduceCount, alarmMessages, spindleSpeed, feedRate))
|
||
{
|
||
return new ProcessingResult
|
||
{
|
||
Handled = true,
|
||
Updated = false,
|
||
DeviceId = deviceId,
|
||
DeviceType = "cnc",
|
||
Status = status,
|
||
PayloadTime = payloadTime,
|
||
Message = "CNC ignored by deadband",
|
||
ParsedData = realtimeJson
|
||
};
|
||
}
|
||
|
||
UpsertCurrentState(unitwork, currentState, deviceId, status, payloadTime, alarmMessages, realtimeJson);
|
||
UpsertTrendPoint(unitwork, deviceId, "cncProduceCount", payloadTime, currentProduceCount);
|
||
UpsertTrendPoint(unitwork, deviceId, "spindleSpeed", payloadTime, spindleSpeed);
|
||
UpsertTrendPoint(unitwork, deviceId, "feedRate", payloadTime, feedRate);
|
||
UpsertTrendPoint(unitwork, deviceId, "runtimeMinutes", payloadTime, runtimeMinutes);
|
||
UpsertTrendPoint(unitwork, deviceId, "cuttimeMinutes", payloadTime, cuttimeMinutes);
|
||
UpsertProductionDaily(unitwork, deviceId, payloadTime, currentProduceCount, runtimeMinutes, cuttimeMinutes, status);
|
||
SyncCncAlarm(unitwork, deviceId, alarmMessages, payloadTime);
|
||
|
||
return new ProcessingResult
|
||
{
|
||
Handled = true,
|
||
Updated = true,
|
||
DeviceId = deviceId,
|
||
DeviceType = "cnc",
|
||
Status = status,
|
||
PayloadTime = payloadTime,
|
||
Message = "CNC state updated",
|
||
ParsedData = realtimeJson
|
||
};
|
||
}
|
||
|
||
private static bool LooksLikeCncPayload(JsonElement root)
|
||
{
|
||
return root.TryGetProperty("statusInfo", out _)
|
||
|| root.TryGetProperty("programCurrent", out _)
|
||
|| root.TryGetProperty("currentProduceCount", out _)
|
||
|| root.TryGetProperty("spindleSpeedAndFeedRate", out _);
|
||
}
|
||
|
||
private static string ResolveDeviceId(JsonElement root, string topic)
|
||
{
|
||
var explicitId = ReadString(root, "deviceId")
|
||
?? ReadString(root, "device_id")
|
||
?? ReadString(root, "client_id")
|
||
?? ReadString(root, "machineId")
|
||
?? ReadString(root, "machine_id");
|
||
if (!string.IsNullOrWhiteSpace(explicitId))
|
||
{
|
||
return explicitId.StartsWith("CNC-", StringComparison.OrdinalIgnoreCase) ? explicitId : $"CNC-{NormalizeDeviceCode(explicitId)}";
|
||
}
|
||
|
||
var topicCode = Regex.Match(topic ?? string.Empty, @"(?:CNC[-_/]?)?(\d{1,3})(?!\d)", RegexOptions.IgnoreCase);
|
||
if (topicCode.Success)
|
||
{
|
||
return $"CNC-{NormalizeDeviceCode(topicCode.Groups[1].Value)}";
|
||
}
|
||
|
||
return string.IsNullOrWhiteSpace(topic) ? "CNC-UNKNOWN" : $"cnc:{topic}";
|
||
}
|
||
|
||
private static string NormalizeDeviceCode(string raw)
|
||
{
|
||
var digits = Regex.Match(raw ?? string.Empty, @"\d{1,3}");
|
||
return digits.Success ? int.Parse(digits.Value, CultureInfo.InvariantCulture).ToString("D3", CultureInfo.InvariantCulture) : raw;
|
||
}
|
||
|
||
private static string ResolveStatus(JsonElement statusInfo, int alarmStatus, int alarmFlag, int emergency)
|
||
{
|
||
if (alarmStatus > 0 || alarmFlag > 0 || emergency > 0)
|
||
{
|
||
return "alarm";
|
||
}
|
||
|
||
var motion = ReadInt(statusInfo, "Motion") ?? 0;
|
||
var mstb = ReadInt(statusInfo, "MSTB") ?? 0;
|
||
var runStatus = ReadString(statusInfo, "RunStatus");
|
||
if ((runStatus == "3" && motion == 1) || runStatus == "2" || mstb == 1)
|
||
{
|
||
return "running";
|
||
}
|
||
|
||
return "idle";
|
||
}
|
||
|
||
private static string[] ReadAlarmMessages(JsonElement root)
|
||
{
|
||
if (!root.TryGetProperty("alarm", out var alarm) || alarm.ValueKind != JsonValueKind.Array)
|
||
{
|
||
return Array.Empty<string>();
|
||
}
|
||
|
||
return alarm.EnumerateArray()
|
||
.Select(x => x.ValueKind == JsonValueKind.String ? x.GetString() : x.ToString())
|
||
.Where(x => string.IsNullOrWhiteSpace(x) == false)
|
||
.ToArray();
|
||
}
|
||
|
||
private static void UpsertCurrentState(IUnitWork unitwork, DeviceCurrentState currentState, string deviceId, string status, DateTime payloadTime, string[] alarmMessages, string realtimeJson)
|
||
{
|
||
var alarmSummary = alarmMessages.Length > 0 ? string.Join(";", alarmMessages) : null;
|
||
if (currentState == null)
|
||
{
|
||
unitwork.Add(new DeviceCurrentState
|
||
{
|
||
DeviceId = deviceId,
|
||
DeviceName = deviceId,
|
||
DeviceType = "cnc",
|
||
Status = status,
|
||
LastPayloadTime = payloadTime,
|
||
LastUpdateTime = DateTime.Now,
|
||
AlarmSummary = alarmSummary,
|
||
RealtimeJson = realtimeJson
|
||
});
|
||
return;
|
||
}
|
||
|
||
currentState.DeviceName = string.IsNullOrWhiteSpace(currentState.DeviceName) ? deviceId : currentState.DeviceName;
|
||
currentState.DeviceType = "cnc";
|
||
currentState.Status = status;
|
||
currentState.LastPayloadTime = payloadTime;
|
||
currentState.LastUpdateTime = DateTime.Now;
|
||
currentState.AlarmSummary = alarmSummary;
|
||
currentState.RealtimeJson = realtimeJson;
|
||
unitwork.Update(currentState);
|
||
}
|
||
|
||
private static void UpsertTrendPoint(IUnitWork unitwork, string deviceId, string metricName, DateTime payloadTime, int? value)
|
||
{
|
||
if (value != null)
|
||
{
|
||
UpsertTrendPoint(unitwork, deviceId, metricName, payloadTime, (decimal)value.Value);
|
||
}
|
||
}
|
||
|
||
private static void UpsertTrendPoint(IUnitWork unitwork, string deviceId, string metricName, DateTime payloadTime, decimal? value)
|
||
{
|
||
if (value == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var bucketTime = new DateTime(payloadTime.Year, payloadTime.Month, payloadTime.Day, payloadTime.Hour, payloadTime.Minute, 0);
|
||
var point = unitwork.Find<DeviceMetricTrendPoint>(x => x.DeviceId == deviceId && x.MetricName == metricName && x.BucketTime == bucketTime).FirstOrDefault();
|
||
if (point == null)
|
||
{
|
||
unitwork.Add(new DeviceMetricTrendPoint
|
||
{
|
||
DeviceId = deviceId,
|
||
MetricName = metricName,
|
||
BucketTime = bucketTime,
|
||
AvgValue = value.Value,
|
||
MaxValue = value.Value,
|
||
MinValue = value.Value,
|
||
SampleCount = 1
|
||
});
|
||
return;
|
||
}
|
||
|
||
var count = Math.Max(point.SampleCount, 1);
|
||
point.AvgValue = ((point.AvgValue * count) + value.Value) / (count + 1);
|
||
point.MaxValue = Math.Max(point.MaxValue, value.Value);
|
||
point.MinValue = Math.Min(point.MinValue, value.Value);
|
||
point.SampleCount = count + 1;
|
||
unitwork.Update(point);
|
||
}
|
||
|
||
private static void UpsertProductionDaily(IUnitWork unitwork, string deviceId, DateTime payloadTime, int? currentProduceCount, decimal? runtimeMinutes, decimal? cuttimeMinutes, string status)
|
||
{
|
||
if (currentProduceCount == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var productionDate = payloadTime.Date;
|
||
var daily = unitwork.Find<DeviceProductionDaily>(x => x.DeviceId == deviceId && x.ProductionDate == productionDate).FirstOrDefault();
|
||
if (daily == null)
|
||
{
|
||
unitwork.Add(new DeviceProductionDaily
|
||
{
|
||
DeviceId = deviceId,
|
||
ProductionDate = productionDate,
|
||
StartCounter = currentProduceCount.Value,
|
||
EndCounter = currentProduceCount.Value,
|
||
ProduceCount = 0,
|
||
RuntimeMinutes = ToIntMinutes(runtimeMinutes),
|
||
CuttimeMinutes = ToIntMinutes(cuttimeMinutes),
|
||
AlarmMinutes = status == "alarm" ? 1 : 0,
|
||
OfflineMinutes = 0
|
||
});
|
||
return;
|
||
}
|
||
|
||
daily.EndCounter = currentProduceCount.Value;
|
||
daily.ProduceCount = Math.Max(0, daily.EndCounter - daily.StartCounter);
|
||
daily.RuntimeMinutes = Math.Max(daily.RuntimeMinutes, ToIntMinutes(runtimeMinutes));
|
||
daily.CuttimeMinutes = Math.Max(daily.CuttimeMinutes, ToIntMinutes(cuttimeMinutes));
|
||
unitwork.Update(daily);
|
||
}
|
||
|
||
private static int? GetTodayProduce(IUnitWork unitwork, string deviceId, DateTime payloadTime, int? currentProduceCount)
|
||
{
|
||
if (currentProduceCount == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var productionDate = payloadTime.Date;
|
||
var daily = unitwork.Find<DeviceProductionDaily>(x => x.DeviceId == deviceId && x.ProductionDate == productionDate).FirstOrDefault();
|
||
return daily == null ? 0 : Math.Max(0, currentProduceCount.Value - daily.StartCounter);
|
||
}
|
||
|
||
private static void SyncCncAlarm(IUnitWork unitwork, string deviceId, string[] alarmMessages, DateTime payloadTime)
|
||
{
|
||
var activeAlarm = unitwork.Find<DeviceAlarmRecord>(x => x.DeviceId == deviceId && x.AlarmCode == "CNC_ALARM" && x.IsActive).FirstOrDefault();
|
||
if (alarmMessages.Length > 0)
|
||
{
|
||
var alarmText = string.Join(";", alarmMessages);
|
||
if (activeAlarm == null)
|
||
{
|
||
unitwork.Add(new DeviceAlarmRecord
|
||
{
|
||
DeviceId = deviceId,
|
||
AlarmCode = "CNC_ALARM",
|
||
AlarmMessage = alarmText,
|
||
StartTime = payloadTime,
|
||
LastSeenTime = payloadTime,
|
||
IsActive = true,
|
||
RepeatCount = 1
|
||
});
|
||
return;
|
||
}
|
||
|
||
activeAlarm.AlarmMessage = alarmText;
|
||
activeAlarm.LastSeenTime = payloadTime;
|
||
activeAlarm.RepeatCount += 1;
|
||
unitwork.Update(activeAlarm);
|
||
return;
|
||
}
|
||
|
||
if (activeAlarm != null)
|
||
{
|
||
activeAlarm.IsActive = false;
|
||
activeAlarm.EndTime = payloadTime;
|
||
activeAlarm.LastSeenTime = payloadTime;
|
||
unitwork.Update(activeAlarm);
|
||
}
|
||
}
|
||
|
||
private static bool IsDeadbandHit(DeviceCurrentState currentState, string nextStatus, string programName, int? produceCount, string[] alarmMessages, decimal? spindleSpeed, decimal? feedRate)
|
||
{
|
||
if (currentState == null || string.IsNullOrWhiteSpace(currentState.RealtimeJson) || currentState.Status != nextStatus)
|
||
{
|
||
return false;
|
||
}
|
||
if (currentState.LastUpdateTime != null && DateTime.Now - currentState.LastUpdateTime.Value > MaxDeadbandSilence)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
using var document = JsonDocument.Parse(currentState.RealtimeJson);
|
||
var root = document.RootElement;
|
||
var oldAlarm = ReadStringArray(root, "alarmMessages");
|
||
return string.Equals(ReadString(root, "programName"), programName, StringComparison.OrdinalIgnoreCase)
|
||
&& ReadInt(root, "currentProduceCount") == produceCount
|
||
&& string.Join("|", oldAlarm) == string.Join("|", alarmMessages)
|
||
&& IsWithinDeadband(ReadDecimal(root, "spindleSpeed"), spindleSpeed, SpindleSpeedDeadband)
|
||
&& IsWithinDeadband(ReadDecimal(root, "feedRate"), feedRate, FeedRateDeadband);
|
||
}
|
||
|
||
private static bool IsWithinDeadband(decimal? oldValue, decimal? newValue, decimal threshold)
|
||
{
|
||
if (oldValue == null && newValue == null)
|
||
{
|
||
return true;
|
||
}
|
||
if (oldValue == null || newValue == null)
|
||
{
|
||
return false;
|
||
}
|
||
return Math.Abs(oldValue.Value - newValue.Value) < threshold;
|
||
}
|
||
|
||
private static DateTime? ReadTimestamp(JsonElement root)
|
||
{
|
||
var raw = ReadString(root, "timestamp");
|
||
if (DateTime.TryParse(raw, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var timestamp))
|
||
{
|
||
return timestamp;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private static decimal? ParseFanucTimeMinutes(string time)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(time))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var match = Regex.Match(time, @"(\d+)\s*H\s*(\d+)\s*M\s*(\d+)\s*S", RegexOptions.IgnoreCase);
|
||
if (!match.Success)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var hours = decimal.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
|
||
var minutes = decimal.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
|
||
var seconds = decimal.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture);
|
||
return Math.Round(hours * 60 + minutes + seconds / 60, 2);
|
||
}
|
||
|
||
private static int ToIntMinutes(decimal? value)
|
||
{
|
||
return value == null ? 0 : (int)Math.Round(value.Value, MidpointRounding.AwayFromZero);
|
||
}
|
||
|
||
private static string ReadString(JsonElement element, string propertyName)
|
||
{
|
||
if (element.ValueKind == JsonValueKind.Undefined || element.ValueKind == JsonValueKind.Null)
|
||
{
|
||
return null;
|
||
}
|
||
return element.TryGetProperty(propertyName, out var property) && property.ValueKind != JsonValueKind.Null
|
||
? property.ToString()
|
||
: null;
|
||
}
|
||
|
||
private static int? ReadInt(JsonElement element, string propertyName)
|
||
{
|
||
var value = ReadDecimal(element, propertyName);
|
||
return value == null ? null : (int)value.Value;
|
||
}
|
||
|
||
private static decimal? ReadDecimal(JsonElement element, string propertyName)
|
||
{
|
||
if (element.ValueKind == JsonValueKind.Undefined || element.ValueKind == JsonValueKind.Null)
|
||
{
|
||
return null;
|
||
}
|
||
if (!element.TryGetProperty(propertyName, out var property) || property.ValueKind == JsonValueKind.Null)
|
||
{
|
||
return null;
|
||
}
|
||
if (property.ValueKind == JsonValueKind.Number && property.TryGetDecimal(out var number))
|
||
{
|
||
return number;
|
||
}
|
||
return decimal.TryParse(property.ToString(), NumberStyles.Number, CultureInfo.InvariantCulture, out var value) ? value : null;
|
||
}
|
||
|
||
private static string ReadNestedString(JsonElement root, string objectName, string propertyName)
|
||
{
|
||
return root.TryGetProperty(objectName, out var nested) && nested.ValueKind == JsonValueKind.Object ? ReadString(nested, propertyName) : null;
|
||
}
|
||
|
||
private static int? ReadNestedInt(JsonElement root, string objectName, string propertyName)
|
||
{
|
||
return root.TryGetProperty(objectName, out var nested) && nested.ValueKind == JsonValueKind.Object ? ReadInt(nested, propertyName) : null;
|
||
}
|
||
|
||
private static string[] ReadStringArray(JsonElement root, string propertyName)
|
||
{
|
||
if (!root.TryGetProperty(propertyName, out var property) || property.ValueKind != JsonValueKind.Array)
|
||
{
|
||
return Array.Empty<string>();
|
||
}
|
||
|
||
return property.EnumerateArray().Select(x => x.ToString()).ToArray();
|
||
}
|
||
}
|
||
}
|