using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using IStation.Model; using IStation.Untity; namespace IStation.DataDockingSocket { public class HandleHelper : IDataDockingSocket.IHandleHelper { /// /// 验证 /// public bool Valid(byte[] bytes) { SZJTKTLogHelper.Info($"字节长度:{bytes.Count()}"); if (bytes == null || bytes.Count() < 1) return false; var appParas = AppParasHelper.Get(); SZJTKTLogHelper.Info($"获取参数"); if (appParas == null) return false; var bytePrefix = new byte[8]; Array.Copy(bytes, 0, bytePrefix, 0, 8); var prefix = Encoding.ASCII.GetString(bytePrefix); SZJTKTLogHelper.Info($"前缀:{prefix}"); if (prefix != appParas.Prefix) return false; byte[] byteProductID = new byte[4]; Array.Copy(bytes, 9, byteProductID, 0, 4); var productId = HandleDataHelper.Bytes2Int4(byteProductID); SZJTKTLogHelper.Info($"设备编号:{productId}"); if (productId.ToString() != appParas.ProductID) return false; return true; } /// /// 处理注册码 /// public void HandleRegisterCode(IMonitorDataDockingSession session) { try { if (session == null) { SZJTKTLogHelper.Error("session == null"); return; } if (!session.IsConnected) { SZJTKTLogHelper.Error("!session.IsConnected"); return; } if (SessionHelper.Cache != null) { if (SessionHelper.Cache != session) { if (SessionHelper.Cache.IsConnected) { SessionHelper.Cache.Close($"{session.SessionName} 中,产生新的会话!"); } } } SessionHelper.Cache = session; var appParas = AppParasHelper.Get(); if (appParas == null) { SZJTKTLogHelper.Error("Socket 数据对接中,SZJT-KT 缺少数据对接配置文件"); return; } } catch (Exception ex) { SZJTKTLogHelper.Error("HandleRegisterCode():" + ex.Message); } } /// /// 处理心跳包 /// public void HandleHeartbeat(IMonitorDataDockingSession session) { } /// /// 处理数据 /// public void HandleData ( IMonitorDataDockingSession session, byte[] bytes, List mappers, Action> receive ) { if (session == null) return; if (!session.IsConnected) return; if (bytes == null) return; if (mappers == null || mappers.Count < 1) return; var appParas = AppParasHelper.Get(); if (appParas == null) { LogHelper.Error("Socket 数据对接中,SZJT-KT 缺少数据对接配置文件"); return; } SZJTKTLogHelper.Info($"开始处理数据:{BitConverter.ToString(bytes)}"); var src_list = HandleDataHelper.HandleData(bytes); if (src_list != null && src_list.Count > 0) { var receive_list = new List(); foreach (var mapper in mappers) { var src = src_list.Find(x => x.SignId == mapper.SignId); if (src != null) { var record = new Model.MonitorDataDockingReceiveRecord(); record.SysId = mapper.SysId; record.RecordType = src.RecordType; record.SrcTime = src.SrcTime; record.SrcValue = src.SrcValue; receive_list.Add(record); } } receive(receive_list); } if (appParas.Src) { SZJTKTLogHelper.Src(bytes); } if (appParas.Debug) { SZJTKTLogHelper.Debug(src_list); } } } }