OCSMessage.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GSG.NET.Utils;
  7. namespace OHVConnector
  8. {
  9. public enum eKind
  10. {
  11. Unknown, // Not Define
  12. E, //Error
  13. S, //State
  14. B, //Battery
  15. T, //
  16. C,
  17. P,
  18. I,//수동명령보고,응답
  19. O,
  20. A,//Alive Check
  21. L,
  22. U,
  23. F,
  24. M,//Move
  25. R,
  26. H,
  27. Z,
  28. }
  29. public class OCSMessage
  30. {
  31. const byte STX = 0x02;
  32. const byte ETX = 0x03;
  33. public string Id { get; set; }
  34. public eKind Kind { get; set; }
  35. //SubCode 만들기 위한 Bool
  36. public bool IsSubCode1 { get; set; } = false;
  37. public bool IsSubCode2 { get; set; } = false;
  38. public bool IsSubCode3 { get; set; } = false;
  39. public string RevID { get; set; }
  40. public string SendID { get; set; }
  41. public string Tag { get; set; }
  42. public string SubCode { get; set; }
  43. public string CheckSum { get; set; }
  44. public IList<string> ViaRouteList { get; set; }
  45. public OCSMessage()
  46. {
  47. this.ViaRouteList = new List<string>();
  48. }
  49. public string LogFormat()
  50. {
  51. if ( this.ViaRouteList.Count <= 0 )
  52. return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.SubCode}] / CheckSum [{this.CheckSum}] / {this.ViaRouteList.Count}";
  53. else
  54. {
  55. StringBuilder viaList = new StringBuilder();
  56. var ll = this.ViaRouteList.ToList();
  57. ll.ForEach( x => { viaList.Append( x ); viaList.Append( ";" ); } );
  58. return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.SubCode}] / CheckSum [{this.CheckSum}] / {this.ViaRouteList.Count} / {viaList}";
  59. }
  60. }
  61. public int SubCodeToInt()
  62. {
  63. int result = 0;
  64. if ( string.IsNullOrEmpty( this.SubCode ) )
  65. {
  66. StringBuilder sb = new StringBuilder();
  67. sb.Append( this.IsSubCode1 ? "1" : "0" );
  68. sb.Append( this.IsSubCode2 ? "1" : "0" );
  69. sb.Append( this.IsSubCode3 ? "1" : "0" );
  70. int.TryParse( sb.ToString(), out result );
  71. }
  72. else
  73. int.TryParse( this.SubCode, out result );
  74. return result;
  75. }
  76. string MakeSubcode()
  77. {
  78. if ( !string.IsNullOrEmpty( this.SubCode ) )
  79. {
  80. return this.SubCode;
  81. }
  82. else
  83. {
  84. StringBuilder sb = new StringBuilder();
  85. sb.Append( this.IsSubCode1 ? "1" : "0" );
  86. sb.Append( this.IsSubCode2 ? "1" : "0" );
  87. sb.Append( this.IsSubCode3 ? "1" : "0" );
  88. return this.SubCode = sb.ToString();
  89. }
  90. }
  91. public MemoryBuffer ToMemoryBuffer()
  92. {
  93. var mb = new MemoryBuffer();
  94. mb.Append( STX );
  95. mb.AppendAscii( RevID );
  96. mb.AppendAscii( SendID );
  97. mb.AppendAscii( this.Kind.ToString() );
  98. mb.AppendAscii( this.Tag ); //4자리로 맞춘다.
  99. mb.AppendAscii( MakeSubcode() );
  100. if ( this.Kind == eKind.M)
  101. {
  102. var viaCountj = this.ViaRouteList.Count.ToString( "0000" );
  103. foreach(var item in this.ViaRouteList )
  104. {
  105. mb.AppendAscii( item );
  106. }
  107. }
  108. this.CheckSum = Convert.ToString( GetCheckSum( mb.ToBytes ), 16 ).ToLower();
  109. mb.AppendAscii( this.CheckSum );
  110. mb.Append( ETX );
  111. return mb;
  112. }
  113. public byte GetCheckSum()
  114. {
  115. var mb = new MemoryBuffer( 18 );
  116. mb.Append( STX );
  117. mb.AppendAscii( RevID );
  118. mb.AppendAscii( SendID );
  119. mb.AppendAscii( this.Kind.ToString() );
  120. mb.AppendAscii( this.Tag );
  121. mb.AppendAscii( MakeSubcode() );
  122. return GetCheckSum( mb.ToBytes );
  123. }
  124. byte GetCheckSum( byte[] bs )
  125. {
  126. byte rb = 0;
  127. foreach ( var item in bs )
  128. {
  129. if ( item == STX ) continue;
  130. rb += item;
  131. }
  132. return (byte)( rb & 0xf ); //&0xff 수정.
  133. }
  134. }
  135. }