JAKABean.cs
4.2 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
using log4net;
using System;
using static JAKA.JKTYPE;
namespace JAKA
{
public partial class JAKABean
{
private static ILog log;
/// <summary>
/// 机器人句柄
/// </summary>
private int _handle = 0;
public string IP { get; private set; }
public string RemoteEndPoint { get; set; } = "";
/// <summary>
/// 机器人数据
/// </summary>
public RobotData RobotData;
bool initok = false;
public JAKABean(string robotIp, string logname = "jaka")
{
IP = robotIp;
log = LogManager.GetLogger(logname);
RobotData = new RobotData();
initok = CreateHandler(robotIp);
}
public void Start()
{
if (initok)
{
updateThread = new System.Threading.Thread(UpdateData);
updateThread.IsBackground = true;
updateThread.Start();
}
}
~JAKABean()
{
DestroyHandler();
}
#region 更新数据信息
System.Threading.Thread updateThread;
public delegate void UpdateEventHandler(RobotData robotData);
event UpdateEventHandler UpdateDataEvent;
/// <summary>
/// 刷新频率
/// </summary>
public int ScanRate = 300;
RobotData robotData = new RobotData();
string err = "";
private void UpdateData()
{
try
{
while (true)
{
JKTYPE.ErrorCode error = new JKTYPE.ErrorCode();
if (GetRobotStatus(out robotData.RobotStatus))//GetRobotState(out RobotData.RobotState) &&
{
if (robotData.RobotStatus.errcode != 0)
{
if (GetLastError(ref error))
{
robotData.Errorinfo = $"运行异常:【{error.code}】【{string.Join("", error.message)}】";
if(!err.Equals(robotData.Errorinfo))
{
err = robotData.Errorinfo;
log.Error(err);
}
}
else
{
robotData.Errorinfo = $"运行异常:{robotData.RobotStatus.errcode}";
}
}
else
robotData.Errorinfo = "运行正常";
GetProgramState(out robotData.ProgramState);
if (!RobotData.Equals(robotData))
{
RobotData.RobotStatus = robotData.RobotStatus;
RobotData.Errorinfo = robotData.Errorinfo;
RobotData.ProgramState = robotData.ProgramState;
robotData.Online = RobotData.Online;
robotData.CurCmd = RobotData.CurCmd;
robotData.RecvMsg = RobotData.RecvMsg;
log.Info($"Update Robot Data:【{RobotData.CurCmd}】【{RobotData.RecvMsg}】【{RobotData.Errorinfo}】");
UpdateDataEvent?.Invoke(robotData);
}
}
System.Threading.Thread.Sleep(ScanRate);
}
}
catch (Exception ex)
{
log.Error("UpdateData", ex);
}
}
public void RegisterUpdataEvent(UpdateEventHandler eventHandler)
{
UpdateDataEvent += eventHandler;
}
#endregion
#region 日志打印
private void DebugLog(string txt)
{
log.Debug($"[{IP}][{_handle}] {txt}]");
}
private void ErrorLog(string txt, Exception exception = null)
{
log.Error($"[{IP}][{_handle}] {txt}]", exception);
}
private void InfoLog(string txt)
{
log.Info($"[{IP}][{_handle}] {txt}]");
}
#endregion
}
}