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)); public bool Connected { get; set; } #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; } public string? BatteryNo() { #region MyRegion /*byte leng = BatteryOneSn.Length; if (leng > 8) { byte[] bytes = new byte[leng]; List lstByte = new List(); lstByte.Add(BatteryOneSn.Sn1); lstByte.Add(BatteryOneSn.Sn2); lstByte.Add(BatteryOneSn.Sn3); lstByte.Add(BatteryOneSn.Sn4); lstByte.Add(BatteryOneSn.Sn5); lstByte.Add(BatteryOneSn.Sn6); lstByte.Add(BatteryTwoSn.Sn1); lstByte.Add(BatteryTwoSn.Sn2); lstByte.Add(BatteryTwoSn.Sn3); lstByte.Add(BatteryTwoSn.Sn4); lstByte.Add(BatteryTwoSn.Sn5); lstByte.Add(BatteryTwoSn.Sn6); lstByte.Add(BatteryTwoSn.Sn7); lstByte.Add(BatteryThreeSn.Sn1); lstByte.Add(BatteryThreeSn.Sn2); lstByte.Add(BatteryThreeSn.Sn3); lstByte.Add(BatteryThreeSn.Sn4); lstByte.Add(BatteryThreeSn.Sn5); lstByte.Add(BatteryThreeSn.Sn6); lstByte.Add(BatteryThreeSn.Sn7); lstByte.Add(BatteryFourSn.Sn1); lstByte.Add(BatteryFourSn.Sn2); lstByte.Add(BatteryFourSn.Sn3); lstByte.Add(BatteryFourSn.Sn4); lstByte.Add(BatteryFourSn.Sn5); lstByte.Add(BatteryFourSn.Sn6); lstByte.Add(BatteryFourSn.Sn7); for (int i = 0; i < leng; i++) { bytes[i] = lstByte[i]; } if (BatteryOneSn.Sn1 != 0 && BatteryOneSn.Sn2 != 0 && BatteryOneSn.Sn3 != 0 && BatteryOneSn.Sn4 != 0 && BatteryOneSn.Sn5 != 0 && BatteryOneSn.Sn6 != 0 && BatteryTwoSn.Sn1 != 0 && BatteryTwoSn.Sn2 != 0 && BatteryTwoSn.Sn3 != 0 && BatteryTwoSn.Sn4 != 0 && BatteryTwoSn.Sn5 != 0 && BatteryTwoSn.Sn6 != 0) { return Encoding.ASCII.GetString(bytes); } }*/ #endregion List list = new() { BatteryOneSn.Sn1, BatteryOneSn.Sn2, BatteryOneSn.Sn3, BatteryOneSn.Sn4, BatteryOneSn.Sn5, BatteryOneSn.Sn6, BatteryTwoSn.Sn1, BatteryTwoSn.Sn2, BatteryTwoSn.Sn3, BatteryTwoSn.Sn4, BatteryTwoSn.Sn5, BatteryTwoSn.Sn6, }; var count = list.Where(i => i <= 0).Count(); if (count > 6) { return null; } byte[] bytes = new byte[12]; bytes[0] = BatteryOneSn.Sn1; bytes[1] = BatteryOneSn.Sn2; bytes[2] = BatteryOneSn.Sn3; bytes[3] = BatteryOneSn.Sn4; bytes[4] = BatteryOneSn.Sn5; bytes[5] = BatteryOneSn.Sn6; bytes[6] = BatteryTwoSn.Sn1; bytes[7] = BatteryTwoSn.Sn2; bytes[8] = BatteryTwoSn.Sn3; bytes[9] = BatteryTwoSn.Sn4; bytes[10] = BatteryTwoSn.Sn5; bytes[11] = BatteryTwoSn.Sn6; return Encoding.ASCII.GetString(bytes); } #endregion public void Reset() { StopSendLock(); StopSendVin(); LockMsg.Lock = 3; SendVin = null; SendVinMsg[0] = null; SendVinMsg[1] = null; SendVinMsg[2] = null; VinMsgs[0] = null; VinMsgs[1] = null; VinMsgs[2] = null; BatteryOneSn = null; BatteryTwoSn = null; BatteryThreeSn = null; BatteryFourSn = null; BatteryInfo1 = null; LockStatusMsg = null; SocMsg = null; SohMsg = null; StatusMsg = null; SubMileMsg = null; TotalMileMsg = null; VersionMsg = null; RestartMsg = null; Connected = false; } #endregion }