OCSMessage.cs 3.9 KB

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