JAKABean.cs
7.4 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using log4net;
using System;
using System.Threading;
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; } = "";
private string progFileName { get; set; } = "lizhen1011";
/// <summary>
/// 机器人数据
/// </summary>
public RobotData RobotData;
bool initok = false;
public JAKABean(string robotIp, string logname = "jaka")
{
IP = robotIp;
log = LogManager.GetLogger(logname);
RobotData = new RobotData();
try
{
initok = CreateHandler(robotIp);
}
catch (Exception ex)
{
log.Error("Init JAKABean", ex);
}
}
public void Start()
{
if (initok)
{
// Load(progFileName);
started = true;
updateThread = new System.Threading.Thread(UpdateData);
updateThread.IsBackground = true;
updateThread.Start();
//updateTimer.Enabled = true;
//updateTimer.Interval = 300;
//updateTimer.Elapsed += UpdateTimer_Elapsed; ;
//updateTimer.AutoReset = true;
}
}
object updateLoc = new object();
private void UpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (Monitor.TryEnter(updateLoc))
{
try
{
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}】");
if (robotData != null)
UpdateDataEvent?.Invoke(robotData);
}
}
}
catch (Exception ex)
{
log.Error("UpdateData", ex);
}
finally
{
Monitor.Exit(updateLoc);
}
}
}
System.Timers.Timer updateTimer = new System.Timers.Timer();
~JAKABean()
{
started = false;
if (initok)
DestroyHandler();
}
#region 更新数据信息
System.Threading.Thread updateThread;
bool started = false;
public delegate void UpdateEventHandler(RobotData robotData);
event UpdateEventHandler UpdateDataEvent;
/// <summary>
/// 刷新频率
/// </summary>
public int ScanRate = 1000;
RobotData robotData = new RobotData();
string err = "";
private void UpdateData()
{
while (started)
{
try
{
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}】");
if (robotData != null)
UpdateDataEvent?.Invoke(robotData);
}
}
}
catch (Exception ex)
{
log.Error("UpdateData", ex);
}
System.Threading.Thread.Sleep(ScanRate);
}
}
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
}
}