using _20230724_MBJC_upperpc.Common; using _20230724_MBJC_upperpc.DataAccess; using _20230724_MBJC_upperpc.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace _20230724_MBJC_upperpc.Models { // /// 客户端 /// public class ClientModel : NotifyBase { SocketInfo SI = new SocketInfo(); public Client _client { set; get; } = new Client(); private bool isConnected; /// /// Socket通信是否连接 /// public bool IsConnected { get { return isConnected; } set { isConnected = value; this.DoNotify(); } } /// /// 打开客户端连接 /// /// /// /// public bool DoConnect(SocketInfo socketInfo) { //if (!tools.TelnetPort(socketInfo.IP, socketInfo.Port)) // return false; SI.IP = socketInfo.IP; SI.Port = socketInfo.Port; _client.InitSocket(socketInfo.IP, socketInfo.Port); //初始化设备的IP和端口号 IsConnected = _client.Connect(); if (IsConnected) { _client.pushSockets = ReceiveMess; tools.Logging("连接客户端成功,IP:" + socketInfo.IP + "端口号:" + socketInfo.Port); } else { tools.Logging("连接客户端失败,IP:" + socketInfo.IP + "端口号:" + socketInfo.Port); } return IsConnected; } public bool DisConnect() { _client.Stop(); IsConnected = false; return IsConnected; } #region Socket通信接收 private void ReceiveMess(Sockets sks) { if (sks.ex != null) { if (sks.ClientDispose == true) { //由于未知原因引发异常.导致客户端下线. 比如网络故障.或服务器断开连接. //SetClientState(string.Format("客户端下线.!异常消息:{0}\r\n", sks.ex)); } else { //SetClientState(string.Format("异常消息:{0}\r\n", sks.ex)); } //timerConnect.Enabled = true; } else if (sks.Offset == 0) { //客户端主动下线 // SetClientState("客户端下线.!"); } else { byte[] buffer = new byte[sks.Offset]; Array.Copy(sks.RecBuffer, buffer, sks.Offset); try { //数据解析 string message = Encoding.UTF8.GetString(buffer); if (!message.StartsWith("$SMBXS")) return; //保存原始数据 tools.AddLgoToTXT("原始报文.txt", tools.Save_Path + System.DateTime.Now.ToString("yyyy_MM_dd") + @"\", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ---- " + message + "\r\n"); DateTime date = DateTime.Now; string[] msgs = message.Split(','); BeaconModel beacon = new BeaconModel(); beacon.ID = int.Parse(msgs[1]); beacon.Datetime = string.IsNullOrEmpty(msgs[2]) ? date : TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)).AddSeconds(int.Parse(msgs[2])); beacon.Ralative_Heading_Angle = string.IsNullOrEmpty(msgs[3]) ? 0 : float.Parse(msgs[3]); beacon.Ralative_Pitch_Angle = string.IsNullOrEmpty(msgs[4]) ? 0 : float.Parse(msgs[4]); beacon.Position_Distance = string.IsNullOrEmpty(msgs[5]) ? 0 : float.Parse(msgs[5]); beacon.Propagationtime = string.IsNullOrEmpty(msgs[6]) ? 0 : float.Parse(msgs[6]); beacon.BasicSite_JD = string.IsNullOrEmpty(msgs[7]) ? 0 : float.Parse(msgs[7]); beacon.BasicSite_WD = string.IsNullOrEmpty(msgs[8]) ? 0 : float.Parse(msgs[8]); beacon.BasicSite_Depth = string.IsNullOrEmpty(msgs[9]) ? 0 : float.Parse(msgs[9]); beacon.BasicSite_Heading_Angle = string.IsNullOrEmpty(msgs[10]) ? 0 : float.Parse(msgs[10]); beacon.BasicSite_Pitch_Angle = string.IsNullOrEmpty(msgs[11]) ? 0 : float.Parse(msgs[11]); beacon.BasicSite_Roll_Angle = string.IsNullOrEmpty(msgs[12]) ? 0 : float.Parse(msgs[12]); beacon.BasicSite_Heading_Speed = string.IsNullOrEmpty(msgs[13]) ? 0 : float.Parse(msgs[13]); beacon.BasicSite_Pitch_Speed = string.IsNullOrEmpty(msgs[14]) ? 0 : float.Parse(msgs[14]); beacon.BasicSite_Roll_Speed = string.IsNullOrEmpty(msgs[15]) ? 0 : float.Parse(msgs[15]); beacon.BasicSite_Forword_A = string.IsNullOrEmpty(msgs[16]) ? 0 : float.Parse(msgs[16]); beacon.BasicSite_Right_A = string.IsNullOrEmpty(msgs[17]) ? 0 : float.Parse(msgs[17]); beacon.BasicSite_Sky_A = string.IsNullOrEmpty(msgs[18]) ? 0 : float.Parse(msgs[18]); beacon.Beacon_JD = string.IsNullOrEmpty(msgs[19]) ? 0 : float.Parse(msgs[19]); beacon.Beacon_WD = string.IsNullOrEmpty(msgs[20]) ? 0 : float.Parse(msgs[20]); beacon.Beacon_Depth = string.IsNullOrEmpty(msgs[21]) ? 0 : float.Parse(msgs[21]); beacon.Beacon_Roll_Angle = string.IsNullOrEmpty(msgs[22]) ? 0 : float.Parse(msgs[22]); beacon.Beacon_Pitch_Angle = string.IsNullOrEmpty(msgs[23]) ? 0 : float.Parse(msgs[23]); beacon.Beacon_Heading_Angle = string.IsNullOrEmpty(msgs[24]) ? 0 : float.Parse(msgs[24]); beacon.Temp = string.IsNullOrEmpty(msgs[25]) ? 0 : float.Parse(msgs[25]); //数据存储 DBHelper.insertData("beaconmodel", beacon); switch (beacon.ID) { case 25: MainWindow.viewModel.Beacon1.Beacon = beacon; break; case 27: MainWindow.viewModel.Beacon2.Beacon = beacon; break; case 28: MainWindow.viewModel.Beacon3.Beacon = beacon; break; case 30: MainWindow.viewModel.Beacon4.Beacon = beacon; break; case 49: MainWindow.viewModel.Beacon5.Beacon = beacon; break; case 98: MainWindow.viewModel.Beacon6.Beacon = beacon; break; case 32: MainWindow.viewModel.Beacon7.Beacon = beacon; break; case 44: MainWindow.viewModel.Beacon8.Beacon = beacon; break; default: break; } } catch (Exception ex) { return; } } } #endregion /// /// 发送消息 /// public void SendData(byte[] SendData) { _client.SendData(SendData); } } public class SocketInfo : NotifyBase { private string ip; /// /// IP /// public string IP { get { return ip; } set { ip = value; this.DoNotify(); } } private int port; /// /// 端口号 /// public int Port { get { return port; } set { port = value; this.DoNotify(); } } } }