OCSMessage.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. }
  30. public class OCSMessage
  31. {
  32. const byte STX = 0x02;
  33. const byte ETX = 0x03;
  34. public string Id { get; set; }
  35. public eKind Kind { get; set; }
  36. //SubCode 만들기 위한 Bool
  37. public bool IsSubCode1 { get; set; } = false;
  38. public bool IsSubCode2 { get; set; } = false;
  39. public bool IsSubCode3 { get; set; } = false;
  40. public string RevID { get; set; }
  41. public string SendID { get; set; }
  42. public string Tag { get; set; }
  43. public string SubCode { get; set; }
  44. public string CheckSum { get; set; }
  45. public IList<string> ViaRouteList { get; set; }
  46. public OCSMessage()
  47. {
  48. this.ViaRouteList = new List<string>();
  49. }
  50. public string LogFormat()
  51. {
  52. if ( this.ViaRouteList.Count <= 0 )
  53. return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.SubCode}] / CheckSum [{this.CheckSum}] / {this.ViaRouteList.Count}";
  54. else
  55. {
  56. StringBuilder viaList = new StringBuilder();
  57. var ll = this.ViaRouteList.ToList();
  58. ll.ForEach( x => { viaList.Append( x ); viaList.Append( ";" ); } );
  59. return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.SubCode}] / CheckSum [{this.CheckSum}] / {this.ViaRouteList.Count} / {viaList}";
  60. }
  61. }
  62. public int SubCodeToInt()
  63. {
  64. int result = 0;
  65. if ( string.IsNullOrEmpty( this.SubCode ) )
  66. {
  67. StringBuilder sb = new StringBuilder();
  68. sb.Append( this.IsSubCode1 ? "1" : "0" );
  69. sb.Append( this.IsSubCode2 ? "1" : "0" );
  70. sb.Append( this.IsSubCode3 ? "1" : "0" );
  71. int.TryParse( sb.ToString(), out result );
  72. }
  73. else
  74. int.TryParse( this.SubCode, out result );
  75. return result;
  76. }
  77. string MakeSubcode()
  78. {
  79. if ( !string.IsNullOrEmpty( this.SubCode ) )
  80. {
  81. return this.SubCode;
  82. }
  83. else
  84. {
  85. StringBuilder sb = new StringBuilder();
  86. sb.Append( this.IsSubCode1 ? "1" : "0" );
  87. sb.Append( this.IsSubCode2 ? "1" : "0" );
  88. sb.Append( this.IsSubCode3 ? "1" : "0" );
  89. return this.SubCode = sb.ToString();
  90. }
  91. }
  92. public MemoryBuffer ToMemoryBuffer()
  93. {
  94. var mb = new MemoryBuffer();
  95. mb.Append( STX );
  96. mb.AppendAscii( RevID );
  97. mb.AppendAscii( SendID );
  98. mb.AppendAscii( this.Kind.ToString() );
  99. mb.AppendAscii( this.Tag ); //4자리로 맞춘다.
  100. mb.AppendAscii( MakeSubcode() );
  101. if ( this.Kind == eKind.M)
  102. {
  103. var viaCountj = this.ViaRouteList.Count.ToString( "0000" );
  104. foreach(var item in this.ViaRouteList )
  105. {
  106. mb.AppendAscii( item );
  107. }
  108. }
  109. this.CheckSum = Convert.ToString( GetCheckSum( mb.ToBytes ), 16 ).ToLower();
  110. mb.AppendAscii( this.CheckSum );
  111. mb.Append( ETX );
  112. return mb;
  113. }
  114. public byte GetCheckSum()
  115. {
  116. var mb = new MemoryBuffer( 18 );
  117. mb.Append( STX );
  118. mb.AppendAscii( RevID );
  119. mb.AppendAscii( SendID );
  120. mb.AppendAscii( this.Kind.ToString() );
  121. mb.AppendAscii( this.Tag );
  122. mb.AppendAscii( MakeSubcode() );
  123. return GetCheckSum( mb.ToBytes );
  124. }
  125. byte GetCheckSum( byte[] bs )
  126. {
  127. byte rb = 0;
  128. foreach ( var item in bs )
  129. {
  130. if ( item == STX ) continue;
  131. rb += item;
  132. }
  133. return (byte)( rb & 0xf ); //&0xff 수정.
  134. }
  135. }
  136. }