OCSMessage.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 byte 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}]";
  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 );
  91. mb.AppendAscii( MakeSubcode() );
  92. //string checkSum = Convert.ToString( GetCheckSum( mb.ToBytes ), 16 ).ToLower();
  93. //mb.AppendAscii( checkSum );
  94. mb.Append( GetCheckSum( mb.ToBytes ) );
  95. mb.Append( ETX );
  96. return mb;
  97. }
  98. public byte GetCheckSum()
  99. {
  100. var mb = new MemoryBuffer( 18 );
  101. mb.Append( STX );
  102. mb.AppendAscii( RevID );
  103. mb.AppendAscii( SendID );
  104. mb.AppendAscii( this.Kind.ToString() );
  105. mb.AppendAscii( this.Tag );
  106. mb.AppendAscii( MakeSubcode() );
  107. //string checkSum = Convert.ToString( GetCheckSum( mb.ToBytes ), 16 ).ToLower();
  108. return GetCheckSum( mb.ToBytes );
  109. }
  110. byte GetCheckSum( byte[] bs )
  111. {
  112. byte rb = 0;
  113. foreach ( var item in bs )
  114. {
  115. if ( item == STX ) continue;
  116. rb += item;
  117. }
  118. return (byte)( rb & 0xf ); //&0xff 수정.
  119. }
  120. }
  121. }