| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- 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, //총 주행 거리.
- D, //시간 동기화.
- }
- 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; }
- /// <summary>
- /// Battery SOH 상태 보고 추가.
- /// 000 3자리로 맞춘다.
- /// </summary>
- public string BatterySOH { get; set; }
- public string CheckSum { get; set; }
- public IList<string> ViaRouteList { get; set; }
- public OCSMessage()
- {
- this.ViaRouteList = new List<string>();
- }
- 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;
- }
- /// <summary>
- /// OCS 응답 Msg 는 Recived, Send 가 바뀌어서 옴
- /// 강제로 바꿔서 맞춤.
- /// </summary>
- /// <returns></returns>
- 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 수정.
- }
- }
- }
|