JAKAServer.cs
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using Dolen.Communication.TCP;
using System;
using System.Collections.Generic;
using System.Linq;
namespace JAKA
{
public class JAKAServer
{
Dictionary<string, JAKABean> JAKAs;
TCPServer TCPServer;
public Dictionary<string, JakaCmd> RobotCmds = new Dictionary<string, JakaCmd>();
public JAKAServer(string[] clientips, string serverIp = "0.0.0.0", int port = 5000)
{
loadCmds();
JAKAs = new Dictionary<string, JAKABean>();
TCPServer = new TCPServer(serverIp, port);
TCPServer.RecvMsgEvent += TCPServer_RecvMsgEvent;
TCPServer.ClientChangedEvent += TCPServer_ClientChangedEvent;
foreach (string ip in clientips)
{
JAKAs.Add(ip, new JAKABean(ip));
}
}
public static string _回原 = "_回原点";
void loadCmds()
{
// string[] pointNames= Enum.GetNames(typeof(JakaPoint));
foreach (var item in Enum.GetNames(typeof(JakaPoint)))
{
JakaPoint point = (JakaPoint)Enum.Parse(typeof(JakaPoint), item);
if (point.Equals(JakaPoint.A下皮带末端) || point.Equals(JakaPoint.B下皮带末端))
RobotCmds.Add(item, new JakaCmd(JakaCmdType.MoveAndRound, point, item));
else
RobotCmds.Add(item, new JakaCmd(JakaCmdType.Move, point, item));
RobotCmds.Add($"{item}{_回原}", new JakaCmd(JakaCmdType.Home, point, $"{item}{_回原}"));
}
//foreach (var item in Enum.GetNames(typeof(JakaPoint)))
//{
// JakaPoint point = (JakaPoint)Enum.Parse(typeof(JakaPoint), item);
// if (point.Equals(JakaPoint.A下皮带末端) || point.Equals(JakaPoint.B下皮带末端))
// RobotCmds.Add(item, new JakaCmd(JakaCmdType.MoveAndRound, point, item));
// else
// RobotCmds.Add(item, new JakaCmd(JakaCmdType.Move, point, item));
//}
}
private void TCPServer_ClientChangedEvent(List<Client> clientKey)
{
foreach (string ip in JAKAs.Keys)
{
Client client = clientKey.Find(s => s.IP.Equals(ip));
if (client != null)
{
JAKAs[ip].RemoteEndPoint = client.EndPoint;
JAKAs[ip].RobotData.Online = true;
}
else
{
JAKAs[ip].RemoteEndPoint = "";
JAKAs[ip].RobotData.Online = false;
}
}
}
string getCmdName(string msg)
{
JakaCmd jakaCmd = RobotCmds.Values.ToList().Find(s => s.CheckIsCurCmd(msg));
if (jakaCmd != null) return jakaCmd.CmdName;
return msg;
}
string parseRecvMsg(string msg)
{
return $"{getCmdName(msg)};{JakaCmd.ParseMsg(msg)}";
}
private void TCPServer_RecvMsgEvent(string clientKey, string msg)
{
try
{
string ip = clientKey.Split(':')[0];
if (msg?.Contains("get") ?? false) return;
JAKABean jAKA = JAKAs[ip];
if (jAKA != null)
{
jAKA.RobotData.RecvMsg = parseRecvMsg(msg.Replace(" ", ""));
}
}
catch
{
}
}
public void Start()
{
TCPServer.Start();
foreach (JAKABean item in JAKAs.Values)
{
item.Start();
}
}
public void Stop()
{
TCPServer.Stop();
}
public void AssociateControl(UCRobot uCRobot, JAKABean jAKABean)
{
uCRobot.Regidter(jAKABean, this);
}
public JAKABean GetJAKABean(string ip)
{
return JAKAs[ip];
}
/// <summary>
/// 发送命令
/// </summary>
/// <param name="endpoint"></param>
/// <param name="cmd"></param>
public void SendCmd(string endpoint, string cmdKey,int roundVal=0)
{
string ip = endpoint.Split(':')[0];
if (JAKAs.ContainsKey(ip))
{
JAKAs[ip].RobotData.RecvMsg = "";
JAKAs[ip].RobotData.CurCmd = cmdKey;
TCPServer.SendMsg(endpoint, RobotCmds[cmdKey].ToCopy(roundVal).ToCmd());
}
}
}
}