using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GSG.NET.Utils; namespace OHVConnector { public enum eKind { Unknown, // Not Define E, //Error S, //State B, //Battery T, // C, P, I,//수동명령보고,응답 O, A,//Alive Check L, U, F, M,//Move R, H, Z, G, //MTL 이동 명령 W, K, //총 주행 거리. } public class OCSMessage { const byte STX = 0x02; const byte ETX = 0x03; public string Id { get; set; } public eKind Kind { get; set; } //SubCode 만들기 위한 Bool public bool IsSubCode1 { get; set; } = false; public bool IsSubCode2 { get; set; } = false; public bool IsSubCode3 { get; set; } = false; public string RevID { get; set; } public string SendID { get; set; } public string Tag { get; set; } public string SubCode { get; set; } /// /// Battery SOH 상태 보고 추가. /// 000 3자리로 맞춘다. /// public string BatterySOH { get; set; } public string CheckSum { get; set; } public IList ViaRouteList { get; set; } public OCSMessage() { this.ViaRouteList = new List(); } public string LogFormat() { if ( this.Kind == eKind.B ) return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.MakeSubcode()}] / SOH[{this.BatterySOH}] / CheckSum [{this.GetCheckSum()}]"; if ( this.ViaRouteList.Count <= 0 ) return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.MakeSubcode()}] / CheckSum [{this.GetCheckSum()}] / {this.ViaRouteList.Count}"; else { StringBuilder viaList = new StringBuilder(); var ll = this.ViaRouteList.ToList(); ll.ForEach( x => { viaList.Append( x ); viaList.Append( ";" ); } ); return $"{this.Id} - {this.RevID} <- {this.SendID} : kind [{this.Kind.ToString()}] / Tag [{this.Tag}] / SubCode [{this.MakeSubcode()}] / CheckSum [{this.GetCheckSum()}] / {this.ViaRouteList.Count} / {viaList}"; } } public int SubCodeToInt() { int result = 0; if ( string.IsNullOrEmpty( this.SubCode ) ) { StringBuilder sb = new StringBuilder(); sb.Append( this.IsSubCode1 ? "1" : "0" ); sb.Append( this.IsSubCode2 ? "1" : "0" ); sb.Append( this.IsSubCode3 ? "1" : "0" ); int.TryParse( sb.ToString(), out result ); } else int.TryParse( this.SubCode, out result ); return result; } string MakeSubcode() { if ( !string.IsNullOrEmpty( this.SubCode ) ) { return this.SubCode; } else { StringBuilder sb = new StringBuilder(); sb.Append( this.IsSubCode1 ? "1" : "0" ); sb.Append( this.IsSubCode2 ? "1" : "0" ); sb.Append( this.IsSubCode3 ? "1" : "0" ); return this.SubCode = sb.ToString(); } } public MemoryBuffer ToMemoryBuffer() { var mb = new MemoryBuffer(); mb.Append( STX ); mb.AppendAscii( RevID ); mb.AppendAscii( SendID ); mb.AppendAscii( this.Kind.ToString() ); mb.AppendAscii( this.Tag ); //4자리로 맞춘다. mb.AppendAscii( MakeSubcode() ); if ( this.Kind == eKind.M ) { var viaCountj = this.ViaRouteList.Count.ToString( "0000" ); foreach ( var item in this.ViaRouteList ) { mb.AppendAscii( item ); } } if ( this.Kind == eKind.B ) { if ( string.IsNullOrEmpty( this.BatterySOH ) ) mb.AppendAscii( "000" ); else mb.AppendAscii( this.BatterySOH.PadLeft( 3, '0' ) ); } this.CheckSum = Convert.ToString( GetCheckSum( mb.ToBytes ), 16 ).ToLower(); mb.AppendAscii( this.CheckSum ); mb.Append( ETX ); return mb; } public byte[] ToCRC8Byte4Send() { var mb = new MemoryBuffer(); mb.Append( STX ); mb.AppendAscii( RevID ); mb.AppendAscii( SendID ); mb.AppendAscii( this.Kind.ToString() ); mb.AppendAscii( this.Tag ); //4자리로 맞춘다. mb.AppendAscii( MakeSubcode() ); if ( this.Kind == eKind.M ) { var viaCountj = this.ViaRouteList.Count.ToString( "0000" ); foreach ( var item in this.ViaRouteList ) { mb.AppendAscii( item ); } } if ( this.Kind == eKind.B ) { if ( string.IsNullOrEmpty( this.BatterySOH ) ) mb.AppendAscii( "000" ); else mb.AppendAscii( this.BatterySOH.PadLeft( 3, '0' ) ); } return mb.ToBytes; } /// /// OCS 응답 Msg 는 Recived, Send 가 바뀌어서 옴 /// 강제로 바꿔서 맞춤. /// /// public byte[] ToCRC8Byte4Received() { var mb = new MemoryBuffer(); mb.Append( STX ); mb.AppendAscii( SendID ); mb.AppendAscii( RevID ); mb.AppendAscii( this.Kind.ToString() ); mb.AppendAscii( this.Tag ); //4자리로 맞춘다. mb.AppendAscii( MakeSubcode() ); if ( this.Kind == eKind.M ) { var viaCountj = this.ViaRouteList.Count.ToString( "0000" ); foreach ( var item in this.ViaRouteList ) { mb.AppendAscii( item ); } } if ( this.Kind == eKind.B ) { if ( string.IsNullOrEmpty( this.BatterySOH ) ) mb.AppendAscii( "000" ); else mb.AppendAscii( this.BatterySOH.PadLeft( 3, '0' ) ); } return mb.ToBytes; } public byte GetCheckSum() { var mb = new MemoryBuffer( 18 ); mb.Append( STX ); mb.AppendAscii( RevID ); mb.AppendAscii( SendID ); mb.AppendAscii( this.Kind.ToString() ); mb.AppendAscii( this.Tag ); mb.AppendAscii( MakeSubcode() ); return GetCheckSum( mb.ToBytes ); } byte GetCheckSum( byte[] bs ) { byte rb = 0; foreach ( var item in bs ) { if ( item == STX ) continue; rb += item; } return (byte)( rb & 0xf ); //&0xff 수정. } } }