RobotService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Team.FFFeederService;
  7. using Team.FFFeederService.Interfaces;
  8. using TeamAAS_VP.Core.Robots;
  9. using TeamAAS_VP.Interfaces;
  10. using TeamAAS_VP.Models;
  11. using TouchSocket.Core;
  12. using TouchSocket.Sockets;
  13. namespace TeamAAS_VP.Services
  14. {
  15. public class RobotService : IRobotService, IDisposable
  16. {
  17. private readonly Dictionary<Guid, IRobot> _robotCollection;
  18. private readonly object _sync = new object();
  19. public RobotService()
  20. {
  21. _robotCollection = new Dictionary<Guid, IRobot>();
  22. }
  23. /// <summary>
  24. /// 创建机器人
  25. /// </summary>
  26. /// <param name="id"></param>
  27. /// <param name="robotInfo"></param>
  28. /// <returns></returns>
  29. /// <exception cref="ArgumentNullException"></exception>
  30. public bool CreateRobot(Guid id, RobotInfo robotInfo)
  31. {
  32. if (robotInfo == null) throw new ArgumentNullException(nameof(robotInfo));
  33. var robot = CreateRobotInstance(robotInfo);
  34. robot.Id = id;
  35. lock (_sync)
  36. {
  37. if (_robotCollection.ContainsKey(id))
  38. {
  39. var currentRobot = _robotCollection[id];
  40. try
  41. {
  42. currentRobot.Dispose();
  43. }
  44. catch
  45. {
  46. // 忽略单个机器人 Dispose 异常,继续替换
  47. }
  48. }
  49. _robotCollection[id] = robot;
  50. }
  51. return true;
  52. }
  53. /// <summary>
  54. /// 创建机器人
  55. /// </summary>
  56. /// <param name="id"></param>
  57. /// <param name="robotInfo"></param>
  58. /// <returns></returns>
  59. public Task<bool> CreateRobotAsync(Guid id, RobotInfo robotInfo)
  60. {
  61. return Task.Run(() =>
  62. {
  63. return CreateRobot(id, robotInfo);
  64. });
  65. }
  66. public IRobot GetRobot(Guid id)
  67. {
  68. lock (_sync)
  69. {
  70. if (_robotCollection.TryGetValue(id, out var robot))
  71. {
  72. return robot;
  73. }
  74. }
  75. return null;
  76. }
  77. public Task<IRobot> GetRobotAsync(Guid id)
  78. {
  79. return Task.Run(() =>
  80. {
  81. return GetRobot(id);
  82. });
  83. }
  84. public void UnRegisterRobot(Guid id)
  85. {
  86. IRobot robot = null;
  87. lock (_sync)
  88. {
  89. if (_robotCollection.TryGetValue(id, out robot))
  90. {
  91. _robotCollection.Remove(id);
  92. }
  93. }
  94. if (robot != null)
  95. {
  96. try
  97. {
  98. robot.Dispose();
  99. }
  100. catch
  101. {
  102. // 忽略释放异常
  103. }
  104. }
  105. }
  106. public Task UnRegisterRobotAsync(Guid id)
  107. {
  108. return Task.Run(()=> UnRegisterRobot(id));
  109. }
  110. /// <summary>
  111. /// 获取所有机器人
  112. /// </summary>
  113. /// <returns></returns>
  114. public IReadOnlyCollection<IRobot> GetAllRobots()
  115. {
  116. lock (_sync)
  117. {
  118. return _robotCollection.Values.ToList().AsReadOnly();
  119. }
  120. }
  121. public Task<IReadOnlyCollection<IRobot>> GetAllRobotsAsync()
  122. {
  123. return Task.Run(() =>
  124. {
  125. return GetAllRobots();
  126. });
  127. }
  128. /// <summary>
  129. /// 尝试获取机器人
  130. /// </summary>
  131. /// <param name="id"></param>
  132. /// <param name="robot"></param>
  133. /// <returns></returns>
  134. public bool TryGetRobot(Guid id, out IRobot robot)
  135. {
  136. lock (_sync)
  137. {
  138. return _robotCollection.TryGetValue(id, out robot);
  139. }
  140. }
  141. public Task<(bool found, IRobot robot)> TryGetRobotAsync(Guid id)
  142. {
  143. return Task.Run(() =>
  144. {
  145. IRobot robot;
  146. bool found;
  147. lock (_sync)
  148. {
  149. found = _robotCollection.TryGetValue(id, out robot);
  150. }
  151. return (found, robot);
  152. });
  153. }
  154. /// <summary>
  155. /// 检查是否存在
  156. /// </summary>
  157. /// <param name="id"></param>
  158. /// <returns></returns>
  159. public bool ContainsRobot(Guid id)
  160. {
  161. lock (_sync)
  162. {
  163. return _robotCollection.ContainsKey(id);
  164. }
  165. }
  166. public Task<bool> ContainsRobotAsync(Guid id)
  167. {
  168. return Task.Run(() =>
  169. {
  170. return ContainsRobot(id);
  171. });
  172. }
  173. /// <summary>
  174. /// 移除机器人并释放资源,返回是否成功移除
  175. /// </summary>
  176. /// <param name="id"></param>
  177. /// <returns></returns>
  178. public bool RemoveRobot(Guid id)
  179. {
  180. IRobot robot = null;
  181. lock (_sync)
  182. {
  183. if (_robotCollection.TryGetValue(id, out robot))
  184. {
  185. _robotCollection.Remove(id);
  186. }
  187. }
  188. if (robot != null)
  189. {
  190. try
  191. {
  192. robot.Dispose();
  193. }
  194. catch
  195. {
  196. // 忽略释放异常
  197. }
  198. return true;
  199. }
  200. return false;
  201. }
  202. public Task<bool> RemoveRobotAsync(Guid id)
  203. {
  204. return Task.Run(() =>
  205. {
  206. return RemoveRobot(id);
  207. });
  208. }
  209. public IRobot GetRobotByNumber(int robotNo)
  210. {
  211. if (TryFindRobotByNumber(robotNo, out _, out var robot))
  212. {
  213. return robot;
  214. }
  215. return null;
  216. }
  217. public Task<IRobot> GetRobotByNumberAsync(int robotNo)
  218. {
  219. return Task.Run(() =>
  220. {
  221. return GetRobotByNumber(robotNo);
  222. });
  223. }
  224. public Task UnRegisterRobotByNumberAsync(int robotNo)
  225. {
  226. return Task.Run(() => UnRegisterRobotByNumber(robotNo));
  227. }
  228. public void UnRegisterRobotByNumber(int robotNo)
  229. {
  230. if (TryFindRobotByNumber(robotNo, out var id, out var robot))
  231. {
  232. // 复用已有的方法以保持行为一致(线程安全、异常处理)
  233. UnRegisterRobot(id);
  234. }
  235. }
  236. /// <summary>
  237. /// 移除所有机器人
  238. /// </summary>
  239. public void RemoveAllRobots()
  240. {
  241. //依次移除每个机器人,确保资源释放
  242. List<Guid> robotIds;
  243. lock (_sync)
  244. {
  245. robotIds = _robotCollection.Keys.ToList();
  246. }
  247. foreach (var id in robotIds)
  248. {
  249. RemoveRobot(id);
  250. }
  251. }
  252. /// <summary>
  253. /// 初始化所有机器人
  254. /// </summary>
  255. /// <param name="robots"></param>
  256. /// <returns></returns>
  257. public (bool IsSucceed, string Message) InitializeAllRobots(RobotInfo[] robots)
  258. {
  259. //清除现有机器人集合
  260. RemoveAllRobots();
  261. //根据传入的机器人信息数组,依次创建并注册机器人实例
  262. foreach (var robotInfo in robots)
  263. {
  264. var robot = CreateRobotInstance(robotInfo);
  265. lock (_sync)
  266. {
  267. _robotCollection[robot.Id] = robot;
  268. }
  269. }
  270. bool allConnected = true;
  271. StringBuilder errorMessages = new StringBuilder();
  272. //获取所有机器人并连接
  273. var allRobots = GetAllRobots();
  274. foreach (var robot in allRobots)
  275. {
  276. try
  277. {
  278. robot.Connect();
  279. }
  280. catch
  281. {
  282. //忽略单个机器人连接异常,继续连接其他机器人
  283. }
  284. if (!robot.IsConnected)
  285. {
  286. allConnected = false;
  287. errorMessages.AppendLine($"机器人[{robot.Name}]连接失败。");
  288. }
  289. }
  290. if (allConnected)
  291. {
  292. return (true, "所有机器人初始化并连接成功。");
  293. }
  294. else
  295. {
  296. return (false, errorMessages.ToString());
  297. }
  298. }
  299. /// <summary>
  300. /// 初始化所有机器人 (异步)
  301. /// </summary>
  302. /// <param name="robots"></param>
  303. /// <returns></returns>
  304. public Task<(bool IsSucceed, string Message)> InitializeAllRobotsAsync(RobotInfo[] robots)
  305. {
  306. return Task.Run(() =>
  307. {
  308. return InitializeAllRobots(robots);
  309. });
  310. }
  311. // 私有工厂方法,避免重复代码
  312. private IRobot CreateRobotInstance(RobotInfo robotInfo)
  313. {
  314. if (robotInfo == null) throw new ArgumentNullException(nameof(robotInfo));
  315. IRobot robot;
  316. if (robotInfo.RobotBrand == Enums.RobotBrand.EPSON)
  317. {
  318. robot = new EpsonRobot(robotInfo);
  319. }
  320. else if (robotInfo.RobotBrand == Enums.RobotBrand.FANUC)
  321. {
  322. robot = new FanucRobot(robotInfo);
  323. }
  324. else if (robotInfo.RobotBrand == Enums.RobotBrand.Schneider)
  325. {
  326. robot = new SchneiderRobot(robotInfo);
  327. }
  328. else if (robotInfo.RobotBrand == Enums.RobotBrand.XYZ_Platform || robotInfo.RobotBrand == Enums.RobotBrand.XYZU_Platform)
  329. {
  330. robot = new XYZ_Platform(robotInfo);
  331. }
  332. else
  333. {
  334. robot = new EpsonRobot(robotInfo);
  335. }
  336. return robot;
  337. }
  338. /// <summary>
  339. /// 更新机器人编号
  340. /// </summary>
  341. /// <param name="id"></param>
  342. /// <param name="newNumber"></param>
  343. public void UpdateRobotNumber(Guid id, int newNumber)
  344. {
  345. IRobot robot;
  346. lock (_sync)
  347. {
  348. if (_robotCollection.TryGetValue(id, out robot))
  349. {
  350. robot.RobotNo = newNumber;
  351. }
  352. }
  353. }
  354. // 私有帮助方法:通过机器人编号查找字典中的条目(线程安全)
  355. private bool TryFindRobotByNumber(int robotNo, out Guid id, out IRobot robot)
  356. {
  357. lock (_sync)
  358. {
  359. foreach (var kvp in _robotCollection)
  360. {
  361. var r = kvp.Value;
  362. if (r == null) continue;
  363. object rawValue = null;
  364. var type = r.GetType();
  365. // 1. 直接在机器人对象上查找常见编号属性
  366. var prop = type.GetProperty("RobotNo") ??
  367. type.GetProperty("RobotNumber") ??
  368. type.GetProperty("Number") ??
  369. type.GetProperty("No");
  370. if (prop != null)
  371. {
  372. try
  373. {
  374. rawValue = prop.GetValue(r);
  375. }
  376. catch
  377. {
  378. rawValue = null;
  379. }
  380. }
  381. // 2. 如果未找到,尝试从 RobotInfo / Info 属性中获取编号
  382. if (rawValue == null)
  383. {
  384. var infoProp = type.GetProperty("RobotInfo") ?? type.GetProperty("Info");
  385. if (infoProp != null)
  386. {
  387. try
  388. {
  389. var infoObj = infoProp.GetValue(r);
  390. if (infoObj != null)
  391. {
  392. var infoType = infoObj.GetType();
  393. var numberProp = infoType.GetProperty("RobotNo") ??
  394. infoType.GetProperty("RobotNumber") ??
  395. infoType.GetProperty("Number") ??
  396. infoType.GetProperty("No");
  397. if (numberProp != null)
  398. {
  399. try
  400. {
  401. rawValue = numberProp.GetValue(infoObj);
  402. }
  403. catch
  404. {
  405. rawValue = null;
  406. }
  407. }
  408. }
  409. }
  410. catch
  411. {
  412. // 忽略反射读取错误
  413. rawValue = null;
  414. }
  415. }
  416. }
  417. if (rawValue != null)
  418. {
  419. int parsed;
  420. try
  421. {
  422. if (rawValue is int) parsed = (int)rawValue;
  423. else parsed = Convert.ToInt32(rawValue);
  424. }
  425. catch
  426. {
  427. // 不能转换则跳过
  428. continue;
  429. }
  430. if (parsed == robotNo)
  431. {
  432. id = kvp.Key;
  433. robot = r;
  434. return true;
  435. }
  436. }
  437. }
  438. }
  439. id = Guid.Empty;
  440. robot = null;
  441. return false;
  442. }
  443. // 实现 IDisposable,释放所有机器人并清空集合
  444. public void Dispose()
  445. {
  446. List<IRobot> robots;
  447. lock (_sync)
  448. {
  449. robots = _robotCollection.Values.ToList();
  450. _robotCollection.Clear();
  451. }
  452. foreach (var r in robots)
  453. {
  454. try
  455. {
  456. r.Dispose();
  457. }
  458. catch
  459. {
  460. // 忽略单个释放异常
  461. }
  462. }
  463. GC.SuppressFinalize(this);
  464. }
  465. }
  466. }