ConfigService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using TeamAAS_VP.Core;
  10. using TeamAAS_VP.Interfaces;
  11. using TeamAAS_VP.Models;
  12. using TeamAAS_VP.Models.Calibration;
  13. using TeamAAS_VP.Models.Feeder;
  14. namespace TeamAAS_VP.Services
  15. {
  16. /// <summary>
  17. /// 配置参数服务实现类
  18. /// </summary>
  19. public class ConfigService : IConfigService
  20. {
  21. private static class ConfigPaths
  22. {
  23. // 将路径集中管理于本类,当前使用现有 FilePath 值以保持兼容
  24. public static readonly string CamerasConfigurationPath = "..//Config//CameraConfiguration.cfg";
  25. public static readonly string RobotsConfigurationPath = "..//Config//RobotsConfiguration.cfg";
  26. public static readonly string FeedersConfigurationPath = "..//Config//FeedersConfiguration.cfg";
  27. public static readonly string PlcConfigurationPath = "..//Config//PlcConfiguration.cfg";
  28. public static readonly string BgTcpIpConfigurationPath = "..//Config//BgTcpIpConfiguration.cfg";
  29. public static readonly string BgModbusTcpConfigurationPath = "..//Config//BgModbusTcpConfiguration.cfg";
  30. public static readonly string SystemConfigurationPath = "..//Config//SystemConfiguration.cfg";
  31. // Feeder清料任务配置路径
  32. public static readonly string FeederClearanceTasksConfigurationPath = "..//Config//FeederClearanceTasksConfiguration.cfg";
  33. }
  34. private readonly object _sync = new object();
  35. private ObservableCollection<CameraInfo> Cameras { get; set; }
  36. private ObservableCollection<RobotInfo> Robots { get; set; }
  37. private ObservableCollection<FeederInfo> Feeders { get; set; }
  38. private ObservableCollection<PlcInfo> Plcs { get; set; }
  39. private BgTcpIP BgCommunicate { get; set; }
  40. private BgModbusTcp BgModbusCommunicate { get; set; }
  41. private ObservableCollection<FeederClearWork> FeederClearanceTasks { get; set; }
  42. public ConfigService()
  43. {
  44. //LoadAll();
  45. }
  46. public void LoadAll()
  47. {
  48. lock (_sync)
  49. {
  50. // 读取各配置,如果文件不存在,则保持 DeviceConfig 默认值
  51. if (File.Exists(ConfigPaths.CamerasConfigurationPath))
  52. Cameras = FileHelper.ReadJsonFile<ObservableCollection<CameraInfo>>(ConfigPaths.CamerasConfigurationPath);
  53. else
  54. {
  55. Cameras = new ObservableCollection<CameraInfo>();
  56. FileHelper.WriteJsonFile(Cameras, ConfigPaths.CamerasConfigurationPath);
  57. }
  58. if (File.Exists(ConfigPaths.RobotsConfigurationPath))
  59. Robots = FileHelper.ReadJsonFile<ObservableCollection<RobotInfo>>(ConfigPaths.RobotsConfigurationPath);
  60. else
  61. {
  62. Robots = new ObservableCollection<RobotInfo>();
  63. FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
  64. }
  65. if (File.Exists(ConfigPaths.FeedersConfigurationPath))
  66. Feeders = FileHelper.ReadJsonFile<ObservableCollection<FeederInfo>>(ConfigPaths.FeedersConfigurationPath);
  67. else
  68. {
  69. Feeders = new ObservableCollection<FeederInfo>();
  70. FileHelper.WriteJsonFile(Feeders, ConfigPaths.FeedersConfigurationPath);
  71. }
  72. if (File.Exists(ConfigPaths.PlcConfigurationPath))
  73. Plcs = FileHelper.ReadJsonFile<ObservableCollection<PlcInfo>>(ConfigPaths.PlcConfigurationPath);
  74. else
  75. {
  76. Plcs = new ObservableCollection<PlcInfo>();
  77. FileHelper.WriteJsonFile(Plcs, ConfigPaths.PlcConfigurationPath);
  78. }
  79. if (File.Exists(ConfigPaths.BgTcpIpConfigurationPath))
  80. BgCommunicate = FileHelper.ReadJsonFile<BgTcpIP>(ConfigPaths.BgTcpIpConfigurationPath);
  81. else
  82. {
  83. BgCommunicate = new BgTcpIP();
  84. FileHelper.WriteJsonFile(BgCommunicate, ConfigPaths.BgTcpIpConfigurationPath);
  85. }
  86. if (File.Exists(ConfigPaths.BgModbusTcpConfigurationPath))
  87. BgModbusCommunicate = FileHelper.ReadJsonFile<BgModbusTcp>(ConfigPaths.BgModbusTcpConfigurationPath);
  88. else
  89. {
  90. BgModbusCommunicate = new BgModbusTcp();
  91. FileHelper.WriteJsonFile(BgModbusCommunicate, ConfigPaths.BgModbusTcpConfigurationPath);
  92. }
  93. if (!File.Exists(ConfigPaths.FeederClearanceTasksConfigurationPath))
  94. {
  95. FeederClearanceTasks = new ObservableCollection<FeederClearWork>();
  96. FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
  97. }
  98. else
  99. {
  100. // 读取 Feeder清料任务 配置
  101. FeederClearanceTasks = FileHelper.ReadJsonFile<ObservableCollection<FeederClearWork>>(ConfigPaths.FeederClearanceTasksConfigurationPath);
  102. }
  103. }
  104. }
  105. public Task LoadAllAsync()
  106. {
  107. return Task.Run(() => LoadAll());
  108. }
  109. public void SaveAll()
  110. {
  111. lock (_sync)
  112. {
  113. FileHelper.WriteJsonFile(Cameras, ConfigPaths.CamerasConfigurationPath);
  114. FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
  115. FileHelper.WriteJsonFile(Feeders, ConfigPaths.FeedersConfigurationPath);
  116. FileHelper.WriteJsonFile(Plcs, ConfigPaths.PlcConfigurationPath);
  117. FileHelper.WriteJsonFile(BgCommunicate, ConfigPaths.BgTcpIpConfigurationPath);
  118. FileHelper.WriteJsonFile(BgModbusCommunicate, ConfigPaths.BgModbusTcpConfigurationPath);
  119. }
  120. }
  121. public Task SaveAllAsync()
  122. {
  123. return Task.Run(() => SaveAll());
  124. }
  125. #region Cameras
  126. public IReadOnlyCollection<CameraInfo> GetAllCameras()
  127. {
  128. lock (_sync) { return Cameras.ToList().AsReadOnly(); }
  129. }
  130. public CameraInfo GetCamera(Guid id)
  131. {
  132. lock (_sync) { return Cameras.FirstOrDefault(c => c.Id == id); }
  133. }
  134. public CameraInfo AddOrUpdateCamera(CameraInfo camera)
  135. {
  136. if (camera == null) return null;
  137. lock (_sync)
  138. {
  139. var exist = Cameras.FirstOrDefault(c => c.Id == camera.Id);
  140. if (exist != null)
  141. {
  142. var idx = Cameras.IndexOf(exist);
  143. Cameras[idx] = camera;
  144. }
  145. else
  146. {
  147. camera.CameraNo = Cameras.Count + 1;
  148. Cameras.Add(camera);
  149. }
  150. }
  151. FileHelper.WriteJsonFile(Cameras, ConfigPaths.CamerasConfigurationPath);
  152. return camera;
  153. }
  154. public bool RemoveCamera(Guid id)
  155. {
  156. bool result = false;
  157. lock (_sync)
  158. {
  159. var exist = Cameras.FirstOrDefault(c => c.Id == id);
  160. if (exist != null) result = Cameras.Remove(exist);
  161. //重新编号
  162. int no = 1;
  163. //对所有相机按CameraNo排序
  164. var sortedCameras = Cameras.OrderBy(c => c.CameraNo).ToList();
  165. foreach (var cam in sortedCameras)
  166. {
  167. cam.CameraNo = no;
  168. no++;
  169. }
  170. Cameras.Clear();
  171. foreach (var cam in sortedCameras)
  172. {
  173. Cameras.Add(cam);
  174. }
  175. }
  176. FileHelper.WriteJsonFile(Cameras, ConfigPaths.CamerasConfigurationPath);
  177. return result;
  178. }
  179. public bool ContainsCamera(Guid id) { lock (_sync) { return Cameras.Any(c => c.Id == id); } }
  180. #endregion
  181. #region Robots
  182. public IReadOnlyCollection<RobotInfo> GetAllRobots() { lock (_sync) { return Robots.ToList().AsReadOnly(); } }
  183. public RobotInfo GetRobot(Guid id) { lock (_sync) { return Robots.FirstOrDefault(r => r.Id == id); } }
  184. public RobotInfo AddOrUpdateRobot(RobotInfo robot)
  185. {
  186. if (robot == null) return null;
  187. lock (_sync)
  188. {
  189. var exist = Robots.FirstOrDefault(r => r.Id == robot.Id);
  190. if (exist != null)
  191. {
  192. var idx = Robots.IndexOf(exist);
  193. Robots[idx] = robot;
  194. }
  195. else
  196. {
  197. robot.RobotNo = Robots.Count + 1;
  198. Robots.Add(robot);
  199. }
  200. }
  201. FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
  202. return robot;
  203. }
  204. public bool RemoveRobot(Guid id)
  205. {
  206. bool result = false;
  207. lock (_sync)
  208. {
  209. var e = Robots.FirstOrDefault(r => r.Id == id);
  210. if (e != null)
  211. result = Robots.Remove(e);
  212. //重新编号
  213. int no = 1;
  214. //对所有机器人按RobotNo排序
  215. var sortedRobots = Robots.OrderBy(r => r.RobotNo).ToList();
  216. foreach (var r in sortedRobots)
  217. {
  218. r.RobotNo = no;
  219. no++;
  220. }
  221. Robots.Clear();
  222. foreach (var r in sortedRobots)
  223. {
  224. Robots.Add(r);
  225. }
  226. }
  227. FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
  228. return result;
  229. }
  230. public bool ContainsRobot(Guid id) { lock (_sync) { return Robots.Any(r => r.Id == id); } }
  231. /// <summary>
  232. /// 修改指定机器人id的步进距离
  233. /// </summary>
  234. /// <param name="id">机器人唯一标识符。</param>
  235. /// <param name="stepDistance">步进距离</param>
  236. /// <returns></returns>
  237. public bool UpdateRobotStepDistance(Guid id, double stepDistance)
  238. {
  239. lock (_sync)
  240. {
  241. var robot = Robots.FirstOrDefault(r => r.Id == id);
  242. if (robot != null)
  243. {
  244. robot.StepDistance = stepDistance;
  245. FileHelper.WriteJsonFile(Robots, ConfigPaths.RobotsConfigurationPath);
  246. return true;
  247. }
  248. else
  249. {
  250. return false;
  251. }
  252. }
  253. }
  254. /// <summary>
  255. /// 获取指定机器人Id的步进距离
  256. /// </summary>
  257. /// <param name="id"></param>
  258. /// <returns></returns>
  259. public double GetRobotStepDistance(Guid id)
  260. {
  261. lock (_sync)
  262. {
  263. var robot = Robots.FirstOrDefault(r=> r.Id == id);
  264. if (robot != null)
  265. {
  266. return robot.StepDistance;
  267. }
  268. else
  269. {
  270. return 1.0;
  271. }
  272. }
  273. }
  274. #endregion
  275. #region Feeders
  276. public IReadOnlyCollection<FeederInfo> GetAllFeeders() { lock (_sync) { return Feeders.ToList().AsReadOnly(); } }
  277. public FeederInfo GetFeeder(Guid id) { lock (_sync) { return Feeders.FirstOrDefault(f => f.Id == id); } }
  278. public FeederInfo AddOrUpdateFeeder(FeederInfo feeder)
  279. {
  280. if (feeder == null) return null;
  281. lock (_sync)
  282. {
  283. var exist = Feeders.FirstOrDefault(f => f.Id == feeder.Id);
  284. if (exist != null)
  285. {
  286. var idx = Feeders.IndexOf(exist);
  287. Feeders[idx] = feeder;
  288. }
  289. else
  290. {
  291. feeder.FeederNo = Feeders.Count + 1;
  292. Feeders.Add(feeder);
  293. }
  294. }
  295. FileHelper.WriteJsonFile(Feeders, ConfigPaths.FeedersConfigurationPath);
  296. return feeder;
  297. }
  298. public bool RemoveFeeder(Guid id)
  299. {
  300. bool result = false;
  301. lock (_sync)
  302. {
  303. var e = Feeders.FirstOrDefault(f => f.Id == id);
  304. if (e != null) result = Feeders.Remove(e);
  305. //重新编号
  306. int no = 1;
  307. //对所有供料器按FeederNo排序
  308. var sortedFeeders = Feeders.OrderBy(f => f.FeederNo).ToList();
  309. foreach (var f in sortedFeeders)
  310. {
  311. f.FeederNo = no;
  312. no++;
  313. }
  314. Feeders.Clear();
  315. foreach (var f in sortedFeeders)
  316. {
  317. Feeders.Add(f);
  318. }
  319. }
  320. FileHelper.WriteJsonFile(Feeders, ConfigPaths.FeedersConfigurationPath);
  321. return result;
  322. }
  323. public bool ContainsFeeder(Guid id) { lock (_sync) { return Feeders.Any(f => f.Id == id); } }
  324. #endregion
  325. #region PLC
  326. public IReadOnlyCollection<PlcInfo> GetAllPlcs() { lock (_sync) { return Plcs.ToList().AsReadOnly(); } }
  327. public PlcInfo GetPlc(Guid id) { lock (_sync) { return Plcs.FirstOrDefault(p => p.Id == id); } }
  328. public PlcInfo AddOrUpdatePlc(PlcInfo plc)
  329. {
  330. if (plc == null) return null;
  331. lock (_sync)
  332. {
  333. var exist = Plcs.FirstOrDefault(p => p.Id == plc.Id);
  334. if (exist != null)
  335. {
  336. var idx = Plcs.IndexOf(exist);
  337. Plcs[idx] = plc;
  338. }
  339. else
  340. {
  341. plc.PlcNo = Plcs.Count + 1;
  342. Plcs.Add(plc);
  343. }
  344. }
  345. FileHelper.WriteJsonFile(Plcs, ConfigPaths.PlcConfigurationPath);
  346. return plc;
  347. }
  348. public bool RemovePlc(Guid id)
  349. {
  350. bool result = false;
  351. lock (_sync)
  352. {
  353. var e = Plcs.FirstOrDefault(p => p.Id == id);
  354. if (e != null) result = Plcs.Remove(e);
  355. //重新编号
  356. int no = 1;
  357. //对所有PLC按PlcNo排序
  358. var sortedPlcs = Plcs.OrderBy(p => p.PlcNo).ToList();
  359. foreach (var p in sortedPlcs)
  360. {
  361. p.PlcNo = no;
  362. no++;
  363. }
  364. Plcs.Clear();
  365. foreach (var p in sortedPlcs)
  366. {
  367. Plcs.Add(p);
  368. }
  369. }
  370. FileHelper.WriteJsonFile(Plcs, ConfigPaths.PlcConfigurationPath);
  371. return result;
  372. }
  373. public bool ContainsPlc(Guid id) { lock (_sync) { return Plcs.Any(p => p.Id == id); } }
  374. #endregion
  375. #region BgTcp
  376. public BgTcpIP GetBgTcp() { lock (_sync) { return BgCommunicate; } }
  377. public void SetBgTcp(BgTcpIP cfg) { lock (_sync) { BgCommunicate = cfg; } }
  378. public void SaveBgTcp() { lock (_sync) { FileHelper.WriteJsonFile(BgCommunicate, ConfigPaths.BgTcpIpConfigurationPath); } }
  379. #endregion
  380. #region BgModbus
  381. public BgModbusTcp GetBgModbusTcp() { lock (_sync) { return BgModbusCommunicate; } }
  382. public void SetBgModbusTcp(BgModbusTcp cfg) { lock (_sync) { BgModbusCommunicate = cfg; } }
  383. public void SaveBgModbusTcp() { lock (_sync) { FileHelper.WriteJsonFile(BgModbusCommunicate, ConfigPaths.BgModbusTcpConfigurationPath); } }
  384. #endregion
  385. #region 系统参数
  386. /// <summary>
  387. /// 获取系统设置
  388. /// </summary>
  389. /// <returns></returns>
  390. public SystemConfiguration GetSystemConfiguration()
  391. {
  392. lock (_sync)
  393. {
  394. var config = new SystemConfiguration();
  395. if (File.Exists(ConfigPaths.SystemConfigurationPath))
  396. {
  397. config = FileHelper.ReadJsonFile<SystemConfiguration>(ConfigPaths.SystemConfigurationPath);
  398. }
  399. return config;
  400. }
  401. }
  402. /// <summary>
  403. /// 保存系统设置
  404. /// </summary>
  405. public void SaveSystemConfiguration(SystemConfiguration systemConfiguration)
  406. {
  407. lock (_sync)
  408. {
  409. FileHelper.WriteJsonFile(systemConfiguration, ConfigPaths.SystemConfigurationPath);
  410. }
  411. }
  412. #endregion
  413. #region Feeder清料任务
  414. /// <summary>
  415. /// 获取所有清料任务
  416. /// </summary>
  417. /// <returns></returns>
  418. public IReadOnlyCollection<FeederClearWork> GetAllClearanceTasks() { lock (_sync) { return FeederClearanceTasks.ToList().AsReadOnly(); } }
  419. /// <summary>
  420. /// 新增或更新清料任务
  421. /// </summary>
  422. /// <param name="task"></param>
  423. /// <returns></returns>
  424. public FeederClearWork AddOrUpdateClearanceTask(FeederClearWork task)
  425. {
  426. if (task == null) return null;
  427. lock (_sync)
  428. {
  429. var exist = FeederClearanceTasks.FirstOrDefault(t => t.Id == task.Id);
  430. if (exist != null)
  431. {
  432. var idx = FeederClearanceTasks.IndexOf(exist);
  433. FeederClearanceTasks[idx] = task;
  434. }
  435. else
  436. {
  437. // 设置任务编号为当前最大编号加1
  438. task.TaskCode = FeederClearanceTasks.Count > 0 ? FeederClearanceTasks.Max(t => t.TaskCode) + 1 : 1;
  439. FeederClearanceTasks.Add(task);
  440. }
  441. }
  442. FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
  443. return task;
  444. }
  445. /// <summary>
  446. /// 移除清料任务
  447. /// </summary>
  448. /// <param name="id"></param>
  449. /// <returns></returns>
  450. public bool RemoveClearanceTask(Guid id)
  451. {
  452. lock (_sync)
  453. {
  454. var exist = FeederClearanceTasks.FirstOrDefault(t => t.Id == id);
  455. if (exist != null)
  456. {
  457. FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
  458. return FeederClearanceTasks.Remove(exist);
  459. }
  460. else
  461. return false;
  462. }
  463. }
  464. /// <summary>
  465. /// 清除所有清料任务
  466. /// </summary>
  467. public void ClearAllClearanceTasks()
  468. {
  469. lock (_sync)
  470. {
  471. FeederClearanceTasks.Clear();
  472. }
  473. FileHelper.WriteJsonFile(FeederClearanceTasks, ConfigPaths.FeederClearanceTasksConfigurationPath);
  474. }
  475. /// <summary>
  476. /// 判断是否包含指定标识的清料任务
  477. /// </summary>
  478. /// <param name="id"></param>
  479. /// <returns></returns>
  480. public bool ContainsClearanceTask(Guid id)
  481. {
  482. lock (_sync) { return FeederClearanceTasks.Any(t => t.Id == id); }
  483. }
  484. /// <summary>
  485. /// 根据标识获取单个清料任务信息
  486. /// </summary>
  487. /// <param name="id"></param>
  488. /// <returns></returns>
  489. public FeederClearWork GetClearanceTask(Guid id)
  490. {
  491. lock (_sync) { return FeederClearanceTasks.FirstOrDefault(t => t.Id == id); }
  492. }
  493. /// <summary>
  494. /// 判断是否包含指定编号的清料任务
  495. /// </summary>
  496. /// <param name="taskNumber"></param>
  497. /// <returns></returns>
  498. public bool ContainsClearanceTaskByNumber(int taskNumber)
  499. {
  500. lock (_sync) { return FeederClearanceTasks.Any(t => t.TaskCode == taskNumber); }
  501. }
  502. /// <summary>
  503. /// 根据编号获取单个清料任务信息
  504. /// </summary>
  505. /// <param name="taskNumber"></param>
  506. /// <returns></returns>
  507. public FeederClearWork GetClearanceTaskByNumber(int taskNumber)
  508. {
  509. lock (_sync) { return FeederClearanceTasks.FirstOrDefault(t => t.TaskCode == taskNumber); }
  510. }
  511. #endregion
  512. public void Dispose()
  513. {
  514. SaveAll();
  515. GC.SuppressFinalize(this);
  516. }
  517. }
  518. }