20230724_MBJC_upperpc/Models/ClientModel.cs
2024-02-21 10:24:56 +08:00

204 lines
7.4 KiB
C#

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
{
// <summary>
/// 客户端
/// </summary>
public class ClientModel : NotifyBase
{
SocketInfo SI = new SocketInfo();
public Client _client { set; get; } = new Client();
private bool isConnected;
/// <summary>
/// Socket通信是否连接
/// </summary>
public bool IsConnected
{
get { return isConnected; }
set { isConnected = value; this.DoNotify(); }
}
/// <summary>
/// 打开客户端连接
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
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");
string[] msgs = message.Split(',');
BeaconModel beacon = new BeaconModel();
beacon.ID = int.Parse(msgs[1]);
beacon.Datetime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)).AddSeconds(int.Parse(msgs[2]));
beacon.Ralative_Heading_Angle = float.Parse(msgs[3]);
beacon.Ralative_Pitch_Angle = float.Parse(msgs[4]);
beacon.Position_Distance = float.Parse(msgs[5]);
beacon.Propagationtime = float.Parse(msgs[6]);
beacon.BasicSite_JD = float.Parse(msgs[7]);
beacon.BasicSite_WD = float.Parse(msgs[8]);
beacon.BasicSite_Depth = float.Parse(msgs[9]);
beacon.BasicSite_Heading_Angle = float.Parse(msgs[10]);
beacon.BasicSite_Pitch_Angle = float.Parse(msgs[11]);
beacon.BasicSite_Roll_Angle = float.Parse(msgs[12]);
beacon.BasicSite_Heading_Speed = float.Parse(msgs[13]);
beacon.BasicSite_Pitch_Speed = float.Parse(msgs[14]);
beacon.BasicSite_Roll_Speed = float.Parse(msgs[15]);
beacon.BasicSite_Forword_A = float.Parse(msgs[16]);
beacon.BasicSite_Right_A = float.Parse(msgs[17]);
beacon.BasicSite_Sky_A = float.Parse(msgs[18]);
beacon.Beacon_JD = float.Parse(msgs[19]);
beacon.Beacon_WD = float.Parse(msgs[20]);
beacon.Beacon_Depth = float.Parse(msgs[21]);
beacon.Beacon_Roll_Angle = float.Parse(msgs[22]);
beacon.Beacon_Pitch_Angle = float.Parse(msgs[23]);
beacon.Beacon_Heading_Angle = float.Parse(msgs[24]);
beacon.Temp = float.Parse(msgs[25]);
//数据存储
DBHelper.insertData("beaconmodel", beacon);
switch (beacon.ID)
{
case 1:
MainWindow.viewModel.Beacon1.Beacon = beacon;
MainWindow.viewModel.Beacon1.DoFresh();
break;
case 2:
MainWindow.viewModel.Beacon2.Beacon = beacon;
break;
case 3:
MainWindow.viewModel.Beacon3.Beacon = beacon;
break;
case 4:
MainWindow.viewModel.Beacon4.Beacon = beacon;
break;
case 5:
MainWindow.viewModel.Beacon5.Beacon = beacon;
break;
case 6:
MainWindow.viewModel.Beacon6.Beacon = beacon;
break;
case 7:
MainWindow.viewModel.Beacon7.Beacon = beacon;
break;
case 8:
MainWindow.viewModel.Beacon8.Beacon = beacon;
break;
default:
break;
}
}
catch (Exception ex)
{
return;
}
}
}
#endregion
/// <summary>
/// 发送消息
/// </summary>
public void SendData(byte[] SendData)
{
_client.SendData(SendData);
}
}
public class SocketInfo : NotifyBase
{
private string ip;
/// <summary>
/// IP
/// </summary>
public string IP
{
get { return ip; }
set { ip = value; this.DoNotify(); }
}
private int port;
/// <summary>
/// 端口号
/// </summary>
public int Port
{
get { return port; }
set { port = value; this.DoNotify(); }
}
}
}