You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
5.6 KiB

5 months ago
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;
5 months ago
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<IBaseHandler, Decoder, Encoder>
{
private static readonly ILog Log = LogManager.GetLogger(typeof(TBoxServer));
5 months ago
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; }
5 months ago
public RestartMsg RestartMsg { get; set; }
5 months ago
public Msg.TBox.VinMsg[] VinMsgs { get; } = new Msg.TBox.VinMsg[3];
#endregion
#region func
5 months ago
#region Send
/// <summary>
///
/// </summary>
/// <param name="@lock">0断开连接 1加锁 2解锁 3无效</param>
public void StartSendLock(byte @lock)
{
LockMsg.Lock = @lock;
if (TaskInit.TaskMap.TryGetValue(LockTask.TaskName, out var task))
{
task.Start();
}
}
/// <summary>
///
/// </summary>
public void StopSendLock()
{
if (TaskInit.TaskMap.TryGetValue(LockTask.TaskName, out var task))
{
task.Stop();
}
}
/// <summary>
///
/// </summary>
/// <param name="vin"></param>
public void StartSendVin(string vin)
{
SendVin = vin;
EncodeVin(vin);
if (TaskInit.TaskMap.TryGetValue(VinTask.TaskName, out var task))
{
task.Start();
}
}
/// <summary>
///
/// </summary>
/// <param name="vin"></param>
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,
5 months ago
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,
5 months ago
B2 = (byte)vin[14 + 0],
B3 = (byte)vin[14 + 1],
B4 = (byte)vin[14 + 2],
};
}
/// <summary>
///
/// </summary>
public void StopSendVin()
{
if (TaskInit.TaskMap.TryGetValue(VinTask.TaskName, out var task))
{
task.Stop();
}
}
/// <summary>
/// "0无指令 1重启指令"
/// </summary>
/// <param name="restart"></param>
public void SendRestart(byte restart)
{
TBoxServerMgr.Server?.SessionMgr.Broadcast(new RestartMsg(restart));
}
5 months ago
#endregion
5 months ago
#region Read
/// <summary>
/// 读取vin
/// </summary>
/// <returns></returns>
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
5 months ago
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;
}
5 months ago
#endregion
}