using _20230724_MBJC_upperpc.Common;
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;
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.Position_Heading_Angle = float.Parse(msgs[3]);
beacon.Position_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 = float.Parse(msgs[10]);
beacon.Beacon_JD = float.Parse(msgs[11]);
beacon.Beacon_WD = float.Parse(msgs[12]);
beacon.Beacon_Depth = float.Parse(msgs[13]);
beacon.Beacon_Roll_Angle = float.Parse(msgs[14]);
beacon.Beacon_Pitch_Angle = float.Parse(msgs[15]);
beacon.Beacon_Heading_Angle = float.Parse(msgs[16]);
beacon.Temp = float.Parse(msgs[17]);
switch (beacon.ID)
{
case 1:
MainWindow.viewModel.Beacon1.Beacon = beacon;
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
///
/// 发送消息
///
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(); }
}
}
}