83 lines
2.8 KiB
C#
83 lines
2.8 KiB
C#
using DeviceCommand.Base;
|
||
using Model.Model;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net.Http;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace DeviceCommand.Devices
|
||
{
|
||
/// <summary>
|
||
/// CTS3-6-300-8IS0 设备:通过 EnovaDataController 接收下位机 POST 上报的通道数据
|
||
/// </summary>
|
||
public class CTS3 : EnovaDataReporter
|
||
{
|
||
/// <summary>
|
||
/// 最近一次收到的通道数据快照(key = ChannelCode)
|
||
/// </summary>
|
||
public IReadOnlyDictionary<string, EnovaChannelData> LatestChannelData => _latestChannelData;
|
||
private readonly Dictionary<string, EnovaChannelData> _latestChannelData = new Dictionary<string, EnovaChannelData>();
|
||
private readonly object _dataLock = new object();
|
||
|
||
/// <summary>
|
||
/// 业务侧可订阅此事件以获得更友好的回调(仅本设备数据)
|
||
/// </summary>
|
||
public event EventHandler<EnovaChannelDataReceivedEventArgs>? OnDataUpdated;
|
||
|
||
public CTS3(HttpClient httpClient) : base(httpClient)
|
||
{
|
||
// 订阅基类事件:当 Controller 调用 HandleIncomingChannelData 时会触发
|
||
ChannelDataReceived += OnChannelDataReceivedInternal;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 指定 DeviceCode 的便捷构造,便于多台 CTS3 共存时按 deviceCode 精确匹配
|
||
/// </summary>
|
||
public CTS3(HttpClient httpClient, string deviceCode) : this(httpClient)
|
||
{
|
||
DeviceCode = deviceCode ?? string.Empty;
|
||
}
|
||
|
||
private void OnChannelDataReceivedInternal(object? sender, EnovaChannelDataReceivedEventArgs e)
|
||
{
|
||
if (e?.DataList == null || e.DataList.Count == 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
// 1. 缓存最近一次的通道快照
|
||
lock (_dataLock)
|
||
{
|
||
foreach (var data in e.DataList)
|
||
{
|
||
if (data == null) continue;
|
||
string key = data.ChannelCode ?? string.Empty;
|
||
_latestChannelData[key] = data;
|
||
}
|
||
}
|
||
|
||
// 2. 业务异常拦截示例:通道错误状态
|
||
foreach (var data in e.DataList)
|
||
{
|
||
if (data == null) continue;
|
||
if (data.ChannelState == "错误" || data.ChannelState == "0x04")
|
||
{
|
||
// TODO: 触发本地报警逻辑
|
||
// Logger.LoggerHelper.WarnWithNotify($"通道 {data.ChannelCode} 故障");
|
||
}
|
||
}
|
||
|
||
// 3. 转发给业务订阅者
|
||
OnDataUpdated?.Invoke(this, e);
|
||
}
|
||
|
||
public override void Unregister()
|
||
{
|
||
ChannelDataReceived -= OnChannelDataReceivedInternal;
|
||
base.Unregister();
|
||
}
|
||
}
|
||
}
|