using System.Text; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.AutoTask; using HybirdFrameworkDriver.TcpServer; using log4net; using Service.TBox.Handler; using Service.TBox.Msg.Host; using Service.TBox.Msg.TBox; using Service.TBox.MyTask; using Decoder = Service.TBox.Codec.Decoder; using Encoder = Service.TBox.Codec.Encoder; using VinMsg = Service.TBox.Msg.Host.VinMsg; namespace Service.TBox.Server; [Scope] public class TBoxServer : TcpServer { private static readonly ILog Log = LogManager.GetLogger(typeof(TBoxServer)); #region HostMsg public LockMsg LockMsg = new LockMsg(3); public VinMsg[] SendVinMsg = new VinMsg[3]; public string SendVin { get; set; } #endregion #region TBoxMsg public BatteryOneSn? BatteryOneSn { get; set; } public BatteryTwoSn? BatteryTwoSn { get; set; } public BatteryThreeSn BatteryThreeSn { get; set; } public BatteryFourSn BatteryFourSn { get; set; } public BatteryInfo1 BatteryInfo1 { get; set; } public LockStatusMsg LockStatusMsg { get; set; } public SocMsg SocMsg { get; set; } public SohMsg SohMsg { get; set; } public StatusMsg StatusMsg { get; set; } public SubMileMsg SubMileMsg { get; set; } public TotalMileMsg TotalMileMsg { get; set; } public VersionMsg VersionMsg { get; set; } public RestartMsg RestartMsg { get; set; } public Msg.TBox.VinMsg[] VinMsgs { get; } = new Msg.TBox.VinMsg[3]; #endregion #region func #region Send /// /// /// /// 0:断开连接 1:加锁 2:解锁 3:无效 public void StartSendLock(byte @lock) { LockMsg.Lock = @lock; if (TaskInit.TaskMap.TryGetValue(LockTask.TaskName, out var task)) { task.Start(); } } /// /// /// public void StopSendLock() { if (TaskInit.TaskMap.TryGetValue(LockTask.TaskName, out var task)) { task.Stop(); } } /// /// /// /// public void StartSendVin(string vin) { SendVin = vin; EncodeVin(vin); if (TaskInit.TaskMap.TryGetValue(VinTask.TaskName, out var task)) { task.Start(); } } /// /// /// /// private void EncodeVin(string vin) { if (vin.Length != 17) { Log.Info("vin length is not 17"); return; } SendVinMsg[0] = new VinMsg() { Seq = 1, B2 = (byte)vin[0], B3 = (byte)vin[1], B4 = (byte)vin[2], B5 = (byte)vin[3], B6 = (byte)vin[4], B7 = (byte)vin[5], B8 = (byte)vin[6], }; SendVinMsg[1] = new VinMsg() { Seq = 2, B2 = (byte)vin[7+0], B3 = (byte)vin[7+1], B4 = (byte)vin[7+2], B5 = (byte)vin[7+3], B6 = (byte)vin[7+4], B7 = (byte)vin[7+5], B8 = (byte)vin[7+6], }; SendVinMsg[2] = new VinMsg() { Seq = 3, B2 = (byte)vin[14+0], B3 = (byte)vin[14+1], B4 = (byte)vin[14+2], }; } /// /// /// public void StopSendVin() { if (TaskInit.TaskMap.TryGetValue(VinTask.TaskName, out var task)) { task.Stop(); } } /// /// "0:无指令 1:重启指令" /// /// public void SendRestart(byte restart) { TBoxServerMgr.Server?.SessionMgr.Broadcast(new RestartMsg(restart)); } #endregion #region Read /// /// 读取vin /// /// public string? ReadVin() { if (VinMsgs[0] != null && VinMsgs[1] != null && VinMsgs[2] != null) { byte[] bytes = new byte[17]; bytes[0] = VinMsgs[0].B2; bytes[1] = VinMsgs[0].B3; bytes[2] = VinMsgs[0].B4; bytes[3] = VinMsgs[0].B5; bytes[4] = VinMsgs[0].B6; bytes[5] = VinMsgs[0].B7; bytes[6] = VinMsgs[0].B8; bytes[7] = VinMsgs[1].B2; bytes[8] = VinMsgs[1].B3; bytes[9] = VinMsgs[1].B4; bytes[10] = VinMsgs[1].B5; bytes[11] = VinMsgs[1].B6; bytes[12] = VinMsgs[1].B7; bytes[13] = VinMsgs[1].B8; bytes[14] = VinMsgs[2].B2; bytes[15] = VinMsgs[2].B3; bytes[16] = VinMsgs[2].B4; return Encoding.ASCII.GetString(bytes); } return null; } #endregion #endregion }