using Serilog; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TouchSocket.Sockets; namespace Rdh.SocketServer.Client.Models { public class EleMedKitData : IBetweenAndRequestInfo { private static readonly Byte[] _headBytes; private static readonly Byte[] _tailBytes; private static readonly Encoding _electronicMedicineKitEncoding; private String _message; private BaseEleMedKitProtocol _protocolDataInfo; private String _contentSource; public EleMedKitData() { } static EleMedKitData() { _electronicMedicineKitEncoding = Encoding.GetEncoding("gb2312"); _headBytes = _electronicMedicineKitEncoding.GetBytes(EleMedKitProtocolFlags.Start); _tailBytes = _electronicMedicineKitEncoding.GetBytes(EleMedKitProtocolFlags.End); } public static Byte[] HeadBytes { get => _headBytes; } public static Byte[] TailBytes { get => _tailBytes; } public String? Message { get => _message; set { _message = value; } } public String? ContentSource { get => _contentSource; } public BaseEleMedKitProtocol? ProtocolDataInfo { get => _protocolDataInfo; } public void OnParsingBody(byte[] body) { if (body == null || body.Length == 0) { return; } try { _contentSource = _electronicMedicineKitEncoding.GetString(body).Replace("\u0000", String.Empty); Log.Debug("接收电子药箱消息:" + _contentSource); String[] lines = EleMedKitProtocolHelper.GetContentLines(_contentSource); BaseEleMedKitProtocol protocol = EleMedKitProtocolHelper.GetRequestEleMedKitProtocol(lines[0]); if (protocol.GetType() == typeof(UnSupportProtocol)) { _message = "无效的数据"; } else { if (protocol.ParseContent(lines)) { _protocolDataInfo = protocol; } else { _message = "数据解析失败"; } } } catch (Exception ex) { _message = "发生异常:" + ex.Message; } } public bool OnParsingEndCode(byte[] endCode) { return _tailBytes.SequenceEqual(endCode); } public bool OnParsingStartCode(byte[] startCode) { return _headBytes.SequenceEqual(startCode); } } }