OCSMessage.cs 7.3 KB

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