OCSMessage.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. G, //MTL 이동 명령
  29. W,
  30. K, //총 주행 거리.
  31. D, //시간 동기화.
  32. }
  33. public class OCSMessage
  34. {
  35. const byte STX = 0x02;
  36. const byte ETX = 0x03;
  37. public string Id { get; set; }
  38. public eKind Kind { get; set; }
  39. //SubCode 만들기 위한 Bool
  40. public bool IsSubCode1 { get; set; } = false;
  41. public bool IsSubCode2 { get; set; } = false;
  42. public bool IsSubCode3 { get; set; } = false;
  43. public string RevID { get; set; }
  44. public string SendID { get; set; }
  45. public string Tag { get; set; }
  46. public string SubCode { get; set; }
  47. /// <summary>
  48. /// Battery SOH 상태 보고 추가.
  49. /// 000 3자리로 맞춘다.
  50. /// </summary>
  51. public string BatterySOH { get; set; }
  52. public string CheckSum { get; set; }
  53. public IList<string> ViaRouteList { get; set; }
  54. public OCSMessage()
  55. {
  56. this.ViaRouteList = new List<string>();
  57. }
  58. public string LogFormat()
  59. {
  60. if ( this.Kind == eKind.B )
  61. return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.MakeSubcode()}] / SOH[{this.BatterySOH}] / CheckSum [{this.GetCheckSum()}]";
  62. if ( this.ViaRouteList.Count <= 0 )
  63. return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.MakeSubcode()}] / CheckSum [{this.GetCheckSum()}] / {this.ViaRouteList.Count}";
  64. else
  65. {
  66. StringBuilder viaList = new StringBuilder();
  67. var ll = this.ViaRouteList.ToList();
  68. ll.ForEach( x => { viaList.Append( x ); viaList.Append( ";" ); } );
  69. return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.MakeSubcode()}] / CheckSum [{this.GetCheckSum()}] / {this.ViaRouteList.Count} / {viaList}";
  70. }
  71. }
  72. public int SubCodeToInt()
  73. {
  74. int result = 0;
  75. if ( string.IsNullOrEmpty( this.SubCode ) )
  76. {
  77. StringBuilder sb = new StringBuilder();
  78. sb.Append( this.IsSubCode1 ? "1" : "0" );
  79. sb.Append( this.IsSubCode2 ? "1" : "0" );
  80. sb.Append( this.IsSubCode3 ? "1" : "0" );
  81. int.TryParse( sb.ToString(), out result );
  82. }
  83. else
  84. int.TryParse( this.SubCode, out result );
  85. return result;
  86. }
  87. string MakeSubcode()
  88. {
  89. if ( !string.IsNullOrEmpty( this.SubCode ) )
  90. {
  91. return this.SubCode;
  92. }
  93. else
  94. {
  95. StringBuilder sb = new StringBuilder();
  96. sb.Append( this.IsSubCode1 ? "1" : "0" );
  97. sb.Append( this.IsSubCode2 ? "1" : "0" );
  98. sb.Append( this.IsSubCode3 ? "1" : "0" );
  99. return this.SubCode = sb.ToString();
  100. }
  101. }
  102. public MemoryBuffer ToMemoryBuffer()
  103. {
  104. var mb = new MemoryBuffer();
  105. mb.Append( STX );
  106. mb.AppendAscii( RevID );
  107. mb.AppendAscii( SendID );
  108. mb.AppendAscii( this.Kind.ToString() );
  109. mb.AppendAscii( this.Tag ); //4자리로 맞춘다.
  110. mb.AppendAscii( MakeSubcode() );
  111. if ( this.Kind == eKind.M )
  112. {
  113. var viaCountj = this.ViaRouteList.Count.ToString( "0000" );
  114. foreach ( var item in this.ViaRouteList )
  115. {
  116. mb.AppendAscii( item );
  117. }
  118. }
  119. if ( this.Kind == eKind.B )
  120. {
  121. if ( string.IsNullOrEmpty( this.BatterySOH ) )
  122. mb.AppendAscii( "000" );
  123. else
  124. mb.AppendAscii( this.BatterySOH.PadLeft( 3, '0' ) );
  125. }
  126. this.CheckSum = Convert.ToString( GetCheckSum( mb.ToBytes ), 16 ).ToLower();
  127. mb.AppendAscii( this.CheckSum );
  128. mb.Append( ETX );
  129. return mb;
  130. }
  131. public byte[] ToCRC8Byte4Send()
  132. {
  133. var mb = new MemoryBuffer();
  134. mb.Append( STX );
  135. mb.AppendAscii( RevID );
  136. mb.AppendAscii( SendID );
  137. mb.AppendAscii( this.Kind.ToString() );
  138. mb.AppendAscii( this.Tag ); //4자리로 맞춘다.
  139. mb.AppendAscii( MakeSubcode() );
  140. if ( this.Kind == eKind.M )
  141. {
  142. var viaCountj = this.ViaRouteList.Count.ToString( "0000" );
  143. foreach ( var item in this.ViaRouteList )
  144. {
  145. mb.AppendAscii( item );
  146. }
  147. }
  148. if ( this.Kind == eKind.B )
  149. {
  150. if ( string.IsNullOrEmpty( this.BatterySOH ) )
  151. mb.AppendAscii( "000" );
  152. else
  153. mb.AppendAscii( this.BatterySOH.PadLeft( 3, '0' ) );
  154. }
  155. return mb.ToBytes;
  156. }
  157. /// <summary>
  158. /// OCS 응답 Msg 는 Recived, Send 가 바뀌어서 옴
  159. /// 강제로 바꿔서 맞춤.
  160. /// </summary>
  161. /// <returns></returns>
  162. public byte[] ToCRC8Byte4Received()
  163. {
  164. var mb = new MemoryBuffer();
  165. mb.Append( STX );
  166. mb.AppendAscii( SendID );
  167. mb.AppendAscii( RevID );
  168. mb.AppendAscii( this.Kind.ToString() );
  169. mb.AppendAscii( this.Tag ); //4자리로 맞춘다.
  170. mb.AppendAscii( MakeSubcode() );
  171. if ( this.Kind == eKind.M )
  172. {
  173. var viaCountj = this.ViaRouteList.Count.ToString( "0000" );
  174. foreach ( var item in this.ViaRouteList )
  175. {
  176. mb.AppendAscii( item );
  177. }
  178. }
  179. if ( this.Kind == eKind.B )
  180. {
  181. if ( string.IsNullOrEmpty( this.BatterySOH ) )
  182. mb.AppendAscii( "000" );
  183. else
  184. mb.AppendAscii( this.BatterySOH.PadLeft( 3, '0' ) );
  185. }
  186. return mb.ToBytes;
  187. }
  188. public byte GetCheckSum()
  189. {
  190. var mb = new MemoryBuffer( 18 );
  191. mb.Append( STX );
  192. mb.AppendAscii( RevID );
  193. mb.AppendAscii( SendID );
  194. mb.AppendAscii( this.Kind.ToString() );
  195. mb.AppendAscii( this.Tag );
  196. mb.AppendAscii( MakeSubcode() );
  197. return GetCheckSum( mb.ToBytes );
  198. }
  199. byte GetCheckSum( byte[] bs )
  200. {
  201. byte rb = 0;
  202. foreach ( var item in bs )
  203. {
  204. if ( item == STX ) continue;
  205. rb += item;
  206. }
  207. return (byte)( rb & 0xf ); //&0xff 수정.
  208. }
  209. }
  210. }