OCSMessage.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. string checkSum = string.Empty;
  45. public IList<string> ViaRouteList { get; set; }
  46. public OCSMessage()
  47. {
  48. this.ViaRouteList = new List<string>();
  49. }
  50. public string LogFormat()
  51. {
  52. return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.SubCode}] / CheckSum [{this.CheckSum}]";
  53. }
  54. public int SubCodeToInt()
  55. {
  56. int result = 0;
  57. if ( string.IsNullOrEmpty( this.SubCode ) )
  58. {
  59. StringBuilder sb = new StringBuilder();
  60. sb.Append( this.IsSubCode1 ? "1" : "0" );
  61. sb.Append( this.IsSubCode2 ? "1" : "0" );
  62. sb.Append( this.IsSubCode3 ? "1" : "0" );
  63. int.TryParse( sb.ToString(), out result );
  64. }
  65. else
  66. int.TryParse( this.SubCode, out result );
  67. return result;
  68. }
  69. string MakeSubcode()
  70. {
  71. if ( !string.IsNullOrEmpty( this.SubCode ) )
  72. {
  73. return this.SubCode;
  74. }
  75. else
  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. return this.SubCode = sb.ToString();
  82. }
  83. }
  84. public MemoryBuffer ToMemoryBuffer()
  85. {
  86. var mb = new MemoryBuffer();
  87. mb.Append( STX );
  88. mb.AppendAscii( RevID );
  89. mb.AppendAscii( SendID );
  90. mb.AppendAscii( this.Kind.ToString() );
  91. mb.AppendAscii( this.Tag );
  92. mb.AppendAscii( MakeSubcode() );
  93. if ( this.Kind == eKind.M)
  94. {
  95. var viaCountj = this.ViaRouteList.Count.ToString( "0000" );
  96. foreach(var item in this.ViaRouteList )
  97. {
  98. mb.AppendAscii( item );
  99. }
  100. }
  101. this.CheckSum = Convert.ToString( GetCheckSum( mb.ToBytes ), 16 ).ToLower();
  102. mb.AppendAscii( this.CheckSum );
  103. mb.Append( ETX );
  104. return mb;
  105. }
  106. public byte GetCheckSum()
  107. {
  108. var mb = new MemoryBuffer( 18 );
  109. mb.Append( STX );
  110. mb.AppendAscii( RevID );
  111. mb.AppendAscii( SendID );
  112. mb.AppendAscii( this.Kind.ToString() );
  113. mb.AppendAscii( this.Tag );
  114. mb.AppendAscii( MakeSubcode() );
  115. //string checkSum = Convert.ToString( GetCheckSum( mb.ToBytes ), 16 ).ToLower();
  116. return GetCheckSum( mb.ToBytes );
  117. }
  118. byte GetCheckSum( byte[] bs )
  119. {
  120. byte rb = 0;
  121. foreach ( var item in bs )
  122. {
  123. if ( item == STX ) continue;
  124. rb += item;
  125. }
  126. return (byte)( rb & 0xf ); //&0xff 수정.
  127. }
  128. }
  129. }