电子药箱通讯服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EleMedKitData.cs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Serilog;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using TouchSocket.Sockets;
  8. namespace Rdh.SocketServer.Client.Models
  9. {
  10. public class EleMedKitData : IBetweenAndRequestInfo
  11. {
  12. private static readonly Byte[] _headBytes;
  13. private static readonly Byte[] _tailBytes;
  14. private static readonly Encoding _electronicMedicineKitEncoding;
  15. private String _message;
  16. private BaseEleMedKitProtocol _protocolDataInfo;
  17. private String _contentSource;
  18. public EleMedKitData()
  19. {
  20. }
  21. static EleMedKitData()
  22. {
  23. _electronicMedicineKitEncoding = Encoding.GetEncoding("gb2312");
  24. _headBytes = _electronicMedicineKitEncoding.GetBytes(EleMedKitProtocolFlags.Start);
  25. _tailBytes = _electronicMedicineKitEncoding.GetBytes(EleMedKitProtocolFlags.End);
  26. }
  27. public static Byte[] HeadBytes
  28. {
  29. get => _headBytes;
  30. }
  31. public static Byte[] TailBytes
  32. {
  33. get => _tailBytes;
  34. }
  35. public String? Message
  36. {
  37. get => _message;
  38. set
  39. {
  40. _message = value;
  41. }
  42. }
  43. public String? ContentSource
  44. {
  45. get => _contentSource;
  46. }
  47. public BaseEleMedKitProtocol? ProtocolDataInfo
  48. {
  49. get => _protocolDataInfo;
  50. }
  51. public void OnParsingBody(byte[] body)
  52. {
  53. if (body == null || body.Length == 0)
  54. {
  55. return;
  56. }
  57. try
  58. {
  59. _contentSource = _electronicMedicineKitEncoding.GetString(body).Replace("\u0000", String.Empty);
  60. Log.Debug("接收电子药箱消息:" + _contentSource);
  61. String[] lines = EleMedKitProtocolHelper.GetContentLines(_contentSource);
  62. BaseEleMedKitProtocol protocol = EleMedKitProtocolHelper.GetRequestEleMedKitProtocol(lines[0]);
  63. if (protocol.GetType() == typeof(UnSupportProtocol))
  64. {
  65. _message = "无效的数据";
  66. }
  67. else
  68. {
  69. if (protocol.ParseContent(lines))
  70. {
  71. _protocolDataInfo = protocol;
  72. }
  73. else
  74. {
  75. _message = "数据解析失败";
  76. }
  77. }
  78. }
  79. catch (Exception ex)
  80. {
  81. _message = "发生异常:" + ex.Message;
  82. }
  83. }
  84. public bool OnParsingEndCode(byte[] endCode)
  85. {
  86. return _tailBytes.SequenceEqual(endCode);
  87. }
  88. public bool OnParsingStartCode(byte[] startCode)
  89. {
  90. return _headBytes.SequenceEqual(startCode);
  91. }
  92. }
  93. }