ProductService.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ToolBlock;
  3. using Newtonsoft.Json;
  4. using NPOI.SS.Formula.Functions;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using System.Windows.Documents;
  11. using Team.FFFeederService.Interfaces;
  12. using TeamAAS_VP.Core;
  13. using TeamAAS_VP.Interfaces;
  14. using TeamAAS_VP.Models;
  15. using TeamAAS_VP.Models.Product;
  16. namespace TeamAAS_VP.Services
  17. {
  18. public class ProductService : IProductService
  19. {
  20. private readonly Dictionary<Guid, ProductModel> _products = new Dictionary<Guid, ProductModel>();
  21. private readonly object _sync = new object();
  22. private Guid _currentProduct = Guid.Empty;
  23. IConfigService _configService;
  24. IFeederService _feederService;
  25. // 可配置的路径与文件名
  26. // 默认把产品放到公共应用数据目录下的 TeamAAS\Products
  27. public const string DefaultProductsSubPath = "..//Products";
  28. /// <summary>
  29. /// 模板Toolblock路径
  30. /// </summary>
  31. public const string TemplateToolBlock = "..//Vision Template//TemplateToolBlock.vpp";
  32. /// <summary>
  33. /// 产品模板文件夹路径
  34. /// </summary>
  35. public const string ProductTemplatePath = "..//Product Template";
  36. /// <summary>
  37. /// 产品文件夹路径
  38. /// </summary>
  39. public string ProductsRootPath { get; set; }
  40. /// <summary>
  41. /// 产品切换事件
  42. /// </summary>
  43. public event Action<ProductModel> OnProductChanged;
  44. /// <summary>
  45. /// 构造函数
  46. /// </summary>
  47. /// <param name="configService"></param>
  48. public ProductService(IConfigService configService, IFeederService feederService)
  49. {
  50. _configService = configService;
  51. _feederService = feederService;
  52. ProductsRootPath = DefaultProductsSubPath;
  53. }
  54. public bool Initialize()
  55. {
  56. // 清空现有产品
  57. RemoveAllProducts();
  58. // 获取所有产品列表
  59. var productList = GetAllProductList();
  60. lock (_sync)
  61. {
  62. foreach (var (id, name, filePath) in productList)
  63. {
  64. var product = LoadProduct(filePath);
  65. if (product != null)
  66. {
  67. _products[id] = product;
  68. }
  69. }
  70. }
  71. return true;
  72. }
  73. public Task<bool> InitializeAsync()
  74. {
  75. return Task.Run(() => Initialize());
  76. }
  77. /// <summary>
  78. /// 获取本地所有产品列表
  79. /// </summary>
  80. /// <returns></returns>
  81. public IEnumerable<(Guid id,string name,string filePath)> GetAllProductList()
  82. {
  83. if (!Directory.Exists(ProductsRootPath))
  84. {
  85. Directory.CreateDirectory(ProductsRootPath);
  86. }
  87. //在产品目录下有一个ProductList.cfg文件,里面保存了所有产品的ID和名称
  88. string productListFilePath = Path.Combine(ProductsRootPath, "ProductList.cfg");
  89. if (!File.Exists(productListFilePath))
  90. {
  91. return Enumerable.Empty<(Guid id, string name, string filePath)>();
  92. }
  93. // 读取文件内容,这是一个Json文件,里面保存了所有产品的ID和名称
  94. Dictionary<Guid, string> list = FileHelper.ReadJsonFile<Dictionary<Guid, string>>(productListFilePath);
  95. var result = new List<(Guid id, string name, string filePath)>();
  96. foreach (var kvp in list)
  97. {
  98. var id = kvp.Key;
  99. var name = kvp.Value;
  100. //产品路径为:ProductsRootPath//kvp.Value//kvp.Value.cfg
  101. var filePath = Path.Combine(ProductsRootPath, name, name + ".cfg");
  102. result.Add((id, name, filePath));
  103. }
  104. return result;
  105. }
  106. /// <summary>
  107. /// 保存产品清单
  108. /// </summary>
  109. public void SaveProductList()
  110. {
  111. if (!Directory.Exists(ProductsRootPath))
  112. {
  113. Directory.CreateDirectory(ProductsRootPath);
  114. }
  115. //在产品目录下有一个ProductList.cfg文件,里面保存了所有产品的ID和名称
  116. string productListFilePath = Path.Combine(ProductsRootPath, "ProductList.cfg");
  117. Dictionary<Guid, string> list = new Dictionary<Guid, string>();
  118. lock (_sync)
  119. {
  120. foreach (var kvp in _products)
  121. {
  122. list[kvp.Key] = kvp.Value.Name;
  123. }
  124. }
  125. // 保存为Json文件
  126. FileHelper.WriteJsonFile(list, productListFilePath);
  127. }
  128. /// <summary>
  129. /// 异步保存产品清单
  130. /// </summary>
  131. /// <returns></returns>
  132. public Task SaveProductListAsync()
  133. {
  134. return Task.Run(() => SaveProductList());
  135. }
  136. public bool CreateProduct(ProductModel product)
  137. {
  138. if (product == null) throw new ArgumentNullException(nameof(product));
  139. //如果当前存在同名产品,则创建失败
  140. lock (_sync)
  141. {
  142. if (_products.Values.Any(p => p.Name == product.Name))
  143. {
  144. return false;
  145. }
  146. }
  147. //对产品的编号进行自增
  148. lock (_sync)
  149. {
  150. if (_products.Count == 0)
  151. {
  152. product.NumberCode = 1;
  153. }
  154. else
  155. {
  156. product.NumberCode = _products.Values.Max(p => p.NumberCode) + 1;
  157. }
  158. }
  159. if (product.ID == Guid.Empty) product.ID = Guid.NewGuid();
  160. lock (_sync)
  161. {
  162. _products[product.ID] = product;
  163. }
  164. //保存产品清单
  165. SaveProductList();
  166. // 创建后自动保存到磁盘
  167. SaveProduct(product.ID);
  168. return true;
  169. }
  170. public Task<bool> CreateProductAsync(ProductModel product)
  171. {
  172. return Task.Run(() => CreateProduct(product));
  173. }
  174. public ProductModel GetProduct(Guid id)
  175. {
  176. lock (_sync)
  177. {
  178. _products.TryGetValue(id, out var p);
  179. return p;
  180. }
  181. }
  182. public Task<ProductModel> GetProductAsync(Guid id)
  183. {
  184. var p = GetProduct(id);
  185. return Task.FromResult(p);
  186. }
  187. public IReadOnlyCollection<ProductModel> GetAllProducts()
  188. {
  189. lock (_sync)
  190. {
  191. return _products.Values.ToList().AsReadOnly();
  192. }
  193. }
  194. public Task<IReadOnlyCollection<ProductModel>> GetAllProductsAsync()
  195. {
  196. var list = GetAllProducts();
  197. return Task.FromResult(list);
  198. }
  199. public bool TryGetProduct(Guid id, out ProductModel product)
  200. {
  201. lock (_sync)
  202. {
  203. return _products.TryGetValue(id, out product);
  204. }
  205. }
  206. public Task<(bool found, ProductModel product)> TryGetProductAsync(Guid id)
  207. {
  208. ProductModel p;
  209. bool f;
  210. lock (_sync)
  211. {
  212. f = _products.TryGetValue(id, out p);
  213. }
  214. return Task.FromResult((f, p));
  215. }
  216. public bool ContainsProduct(Guid id)
  217. {
  218. lock (_sync)
  219. {
  220. return _products.ContainsKey(id);
  221. }
  222. }
  223. public Task<bool> ContainsProductAsync(Guid id)
  224. {
  225. var v = ContainsProduct(id);
  226. return Task.FromResult(v);
  227. }
  228. /// <summary>
  229. /// 判断服务中是否包含指定名称的产品(同步)。
  230. /// </summary>
  231. /// <param name="name">产品的名称</param>
  232. /// <returns>若包含则返回 true;否则返回 false。</returns>
  233. public bool ContainsProductByName(string name)
  234. {
  235. lock (_sync)
  236. {
  237. return _products.Values.Any(p => p.Name == name);
  238. }
  239. }
  240. /// <summary>
  241. /// 判断服务中是否包含指定名称的产品(异步)。
  242. /// </summary>
  243. /// <param name="name">产品的名称</param>
  244. /// <returns>若包含则返回 true;否则返回 false。</returns>
  245. public Task<bool> ContainsProductByNameAsync(string name)
  246. {
  247. return Task.FromResult(ContainsProductByName(name));
  248. }
  249. public bool RemoveProduct(Guid id)
  250. {
  251. bool result;
  252. lock (_sync)
  253. {
  254. result= _products.Remove(id);
  255. }
  256. // 删除后可选择删除对应的文件(视需求而定)
  257. SaveProductList();
  258. return result;
  259. }
  260. public Task<bool> RemoveProductAsync(Guid id)
  261. {
  262. var r = RemoveProduct(id);
  263. return Task.FromResult(r);
  264. }
  265. public void RemoveAllProducts()
  266. {
  267. lock (_sync)
  268. {
  269. _products.Clear();
  270. }
  271. }
  272. // 仍保留按路径保存的版本(向后兼容)
  273. public void SaveProduct(Guid id, string filePath)
  274. {
  275. var p = GetProduct(id);
  276. if (p == null) return;
  277. SaveProduct(p, filePath);
  278. }
  279. public Task SaveProductAsync(Guid id, string filePath)
  280. {
  281. return Task.Run(() => SaveProduct(id, filePath));
  282. }
  283. // 新增:不需要传入路径的保存,使用约定目录和文件名
  284. public void SaveProduct(Guid id)
  285. {
  286. // 获取产品
  287. var p = GetProduct(id);
  288. if (p == null) return;
  289. // 获取产品文件路径,产品路径为:ProductsRootPath//p.Name//p.Name.cfg
  290. string filePath = Path.Combine(ProductsRootPath, p.Name, p.Name + ".cfg");
  291. SaveProduct(p, filePath);
  292. }
  293. public void SaveProduct(ProductModel p,string filePath)
  294. {
  295. if (p == null) throw new ArgumentNullException(nameof(p));
  296. var dir = Path.GetDirectoryName(filePath);
  297. if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  298. FileHelper.WriteJsonFile(p, filePath);
  299. foreach (var item in p.CameraProcedures)
  300. {
  301. foreach (var item1 in item.ProcedureModels)
  302. {
  303. string prcPath = dir + "//" + item.Camera + "//" + item1.Name + ".vpp";
  304. if (!Directory.Exists(Path.GetDirectoryName(prcPath)))
  305. {
  306. Directory.CreateDirectory(Path.GetDirectoryName(prcPath));
  307. }
  308. if (item1.ToolBlock != null)
  309. {
  310. CogSerializer.SaveObjectToFile(item1.ToolBlock, prcPath);
  311. }
  312. else
  313. {
  314. //判断路径下是否存在toolblock
  315. if (File.Exists(prcPath))
  316. {
  317. item1.ToolBlock = CogSerializer.LoadObjectFromFile(prcPath) as CogToolBlock;
  318. }
  319. else
  320. {
  321. if (File.Exists(TemplateToolBlock))
  322. {
  323. //保存模板
  324. item1.ToolBlock = CogSerializer.LoadObjectFromFile(TemplateToolBlock) as CogToolBlock;
  325. CogSerializer.SaveObjectToFile(item1.ToolBlock, prcPath);
  326. }
  327. }
  328. }
  329. }
  330. }
  331. }
  332. public Task SaveProductAsync(Guid id)
  333. {
  334. return Task.Run(() => SaveProduct(id));
  335. }
  336. public ProductModel LoadProduct(string filePath)
  337. {
  338. if (!File.Exists(filePath)) return null;
  339. var prd = FileHelper.ReadJsonFile<ProductModel>(filePath);
  340. return prd;
  341. }
  342. public ProductModel LoadProduct(Guid id)
  343. {
  344. var p = GetProduct(id);
  345. if (p == null) return null;
  346. // 获取产品文件路径,产品路径为:ProductsRootPath//p.Name//p.Name.cfg
  347. string filePath = Path.Combine(ProductsRootPath, p.Name, p.Name + ".cfg");
  348. var dir = Path.GetDirectoryName(filePath);
  349. if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  350. // 加载每个相机的流程ToolBlock
  351. foreach (var camera in p.CameraProcedures)
  352. {
  353. foreach (var prc in camera.ProcedureModels)
  354. {
  355. string prcPath = dir + "//" + camera.Camera + "//" + prc.Name + ".vpp";
  356. prc.ToolBlock = CogSerializer.LoadObjectFromFile(prcPath) as CogToolBlock;
  357. }
  358. }
  359. return p;
  360. }
  361. public Task<ProductModel> LoadProductAsync(Guid id)
  362. {
  363. return Task.Run(() => LoadProduct(id));
  364. }
  365. public (Guid id, string name) LoadLastProduct()
  366. {
  367. // 获取最后打开的产品ID
  368. var sysConfig = _configService.GetSystemConfiguration();
  369. if (sysConfig == null || sysConfig.LastOpenedProductId == Guid.Empty)
  370. {
  371. return (Guid.Empty, string.Empty);
  372. }
  373. var lastProductId = sysConfig.LastOpenedProductId;
  374. //判断是否存在产品
  375. if (!ContainsProduct(lastProductId))
  376. {
  377. return (Guid.Empty, string.Empty);
  378. }
  379. // 尝试从磁盘加载
  380. var loaded = LoadProduct(lastProductId);
  381. if (loaded != null)
  382. {
  383. SetCurrentProduct(lastProductId);
  384. return (lastProductId, loaded.Name ?? string.Empty);
  385. }
  386. // 如果未能加载,返回空结果
  387. return (Guid.Empty, string.Empty);
  388. }
  389. public Task<(Guid id, string name)> LoadLastProductAsync()
  390. {
  391. // 可以使用 Task.FromResult,因为内部是同步且快速的文件检测/读取
  392. return Task.Run(() => LoadLastProduct());
  393. }
  394. public void SetCurrentProduct(Guid id)
  395. {
  396. lock (_sync)
  397. {
  398. if (_products.ContainsKey(id))
  399. {
  400. _currentProduct = id;
  401. _products[id].IsLoad = true;
  402. //将其他的产品设置为未加载状态
  403. foreach (var kvp in _products)
  404. {
  405. if (kvp.Key != id)
  406. {
  407. kvp.Value.IsLoad = false;
  408. }
  409. }
  410. var sysConfig = _configService.GetSystemConfiguration();
  411. sysConfig.LastOpenedProductId = id;
  412. _configService.SaveSystemConfiguration(sysConfig);
  413. OnCurrentProductChanged(_products[id]);
  414. }
  415. }
  416. }
  417. public ProductModel GetCurrentProduct()
  418. {
  419. lock (_sync)
  420. {
  421. if (_currentProduct == Guid.Empty) return null;
  422. _products.TryGetValue(_currentProduct, out var p);
  423. return p;
  424. }
  425. }
  426. public Guid CopyProduct(Guid id, string newName)
  427. {
  428. var p = GetProduct(id);
  429. if (p == null) return Guid.Empty;
  430. var clone = p.Clone();
  431. clone.ID = Guid.NewGuid();
  432. clone.Name = newName;
  433. clone.DateTime = DateTime.Now;
  434. CreateProduct(clone);
  435. return clone.ID;
  436. }
  437. public Task<Guid> CopyProductAsync(Guid id, string newName)
  438. {
  439. return Task.Run(() => CopyProduct(id, newName));
  440. }
  441. /// <summary>
  442. /// 永久删除产品
  443. /// </summary>
  444. /// <param name="id"></param>
  445. public void DeleteProduct(Guid id)
  446. {
  447. //获取产品
  448. var p = GetProduct(id);
  449. //如果产品不存在,直接返回
  450. if (p == null) return;
  451. //获取产品文件夹路径,产品路径为:ProductsRootPath//p.Name
  452. string productDirPath = Path.Combine(ProductsRootPath, p.Name);
  453. //移除产品
  454. RemoveProduct(id);
  455. //删除产品文件夹及其内容
  456. DeleteDirectoryRecursively(productDirPath);
  457. }
  458. /// <summary>
  459. /// 永久删除产品
  460. /// </summary>
  461. /// <param name="id"></param>
  462. public Task DeleteProductAsync(Guid id)
  463. {
  464. return Task.Run(() => DeleteProduct(id));
  465. }
  466. /// <summary>
  467. /// 对指定产品进行重命名
  468. /// </summary>
  469. /// <param name="id"></param>
  470. /// <param name="newName"></param>
  471. public void RenameProduct(Guid id, string newName)
  472. {
  473. // 获取产品
  474. var p = GetProduct(id);
  475. if (p == null) return;
  476. // 获取旧产品文件夹路径,产品路径为:ProductsRootPath//p.Name
  477. string oldProductDirPath = Path.Combine(ProductsRootPath, p.Name);
  478. // 更新产品名称
  479. p.Name = newName;
  480. // 获取新产品文件夹路径
  481. string newProductDirPath = Path.Combine(ProductsRootPath, newName);
  482. // 重命名文件夹
  483. if (Directory.Exists(oldProductDirPath))
  484. {
  485. Directory.Move(oldProductDirPath, newProductDirPath);
  486. }
  487. // 保存产品清单
  488. SaveProductList();
  489. //对产品进行保存
  490. SaveProduct(id);
  491. }
  492. /// <summary>
  493. /// 对指定产品进行重命名
  494. /// </summary>
  495. /// <param name="id"></param>
  496. /// <param name="newName"></param>
  497. /// <returns></returns>
  498. public Task RenameProductAsync(Guid id, string newName)
  499. {
  500. return Task.Run(() => RenameProduct(id, newName));
  501. }
  502. /// <summary>
  503. /// 更新产品
  504. /// </summary>
  505. /// <param name="product"></param>
  506. public void UpdateProduct(ProductModel product)
  507. {
  508. if (product == null) throw new ArgumentNullException(nameof(product));
  509. lock (_sync)
  510. {
  511. if (_products.ContainsKey(product.ID))
  512. {
  513. _products[product.ID] = product;
  514. }
  515. }
  516. // 更新后自动保存到磁盘
  517. SaveProduct(product.ID);
  518. }
  519. /// <summary>
  520. /// 更新产品
  521. /// </summary>
  522. /// <param name="product"></param>
  523. /// <returns></returns>
  524. public Task UpdateProductAsync(ProductModel product)
  525. {
  526. return Task.Run(() => UpdateProduct(product));
  527. }
  528. /// <summary>
  529. /// 根据指定的模板名称创建产品
  530. /// </summary>
  531. /// <param name="templateName"></param>
  532. /// <param name="productName"></param>
  533. /// <returns></returns>
  534. public ProductModel CreateProductByTemplateName(string templateName, string productName)
  535. {
  536. // 产品模板路径为:ProductTemplatePath//templateName//templateName.cfg
  537. string templateFilePath = Path.Combine(ProductTemplatePath, templateName, templateName + ".cfg");
  538. var templateProduct = LoadProduct(templateFilePath);
  539. if (templateProduct == null) return null;
  540. var newProduct = templateProduct.Clone();
  541. newProduct.Name = productName;
  542. newProduct.ID= Guid.NewGuid();
  543. CreateProduct(newProduct);
  544. return GetProduct(newProduct.ID);
  545. }
  546. /// <summary>
  547. /// 根据指定的模板名称异步创建产品
  548. /// </summary>
  549. /// <param name="templateName"></param>
  550. /// <param name="productName"></param>
  551. /// <returns></returns>
  552. public Task<ProductModel> CreateProductByTemplateNameAsync(string templateName, string productName)
  553. {
  554. return Task.Run(() => CreateProductByTemplateName(templateName, productName));
  555. }
  556. /// <summary>
  557. /// 增加相机时,需要对产品的相机流程进行增加
  558. /// </summary>
  559. /// <param name="cameraId"></param>
  560. /// <param name="cameraName"></param>
  561. public void AddCameraToProduct(Guid cameraId, string cameraName)
  562. {
  563. //获取所有产品
  564. var products= GetAllProducts();
  565. if (products == null) return;
  566. //遍历所有产品,增加相机流程
  567. foreach (var item in products)
  568. {
  569. item.AddCameraProcedure(new CameraProcedure(cameraId, cameraName));
  570. //单个产品的配置文件路径
  571. string productFilePath = Path.Combine(ProductsRootPath, item.Name, item.Name + ".cfg");
  572. //保存产品
  573. FileHelper.WriteJsonFile(item, productFilePath);
  574. }
  575. }
  576. /// <summary>
  577. /// 增加相机时,需要对产品的相机流程进行增加
  578. /// </summary>
  579. /// <param name="cameraId"></param>
  580. /// <param name="cameraName"></param>
  581. /// <returns></returns>
  582. public Task AddCameraToProductAsync(Guid cameraId, string cameraName)
  583. {
  584. return Task.Run(() => AddCameraToProduct(cameraId, cameraName));
  585. }
  586. /// <summary>
  587. /// 删除相机时,需要对产品的相机流程进行删除
  588. /// </summary>
  589. /// <param name="cameraId"></param>
  590. public void RemoveCameraFromProduct(Guid cameraId)
  591. {
  592. var products= GetAllProducts();
  593. if (products == null) return;
  594. //遍历所有产品,删除相机流程
  595. foreach (var item in products)
  596. {
  597. item.RemoveCameraProcedure(cameraId);
  598. //单个产品的配置文件路径
  599. string productFilePath = Path.Combine(ProductsRootPath, item.Name, item.Name + ".cfg");
  600. //保存产品
  601. FileHelper.WriteJsonFile(item, productFilePath);
  602. }
  603. }
  604. /// <summary>
  605. /// 删除相机时,需要对产品的相机流程进行删除
  606. /// </summary>
  607. /// <param name="cameraId"></param>
  608. /// <returns></returns>
  609. public Task RemoveCameraFromProductAsync(Guid cameraId)
  610. {
  611. return Task.Run(() => RemoveCameraFromProduct(cameraId));
  612. }
  613. /// <summary>
  614. /// 修改相机名称时,需要对产品的相机流程进行修改
  615. /// </summary>
  616. /// <param name="cameraId"></param>
  617. /// <param name="newCameraName"></param>
  618. public void UpdateCameraNameInProduct(Guid cameraId, string newCameraName)
  619. {
  620. var products= GetAllProducts();
  621. if (products == null) return;
  622. //遍历所有产品,修改相机名称
  623. foreach (var item in products)
  624. {
  625. //旧名称
  626. string oldCameraName = string.Empty;
  627. var cameraProcedure = item.CameraProcedures.FirstOrDefault(cp => cp.ID == cameraId);
  628. if (cameraProcedure != null)
  629. {
  630. oldCameraName = cameraProcedure.Camera;
  631. cameraProcedure.Camera = newCameraName;
  632. }
  633. //单个产品的配置文件路径
  634. string productFilePath = Path.Combine(ProductsRootPath, item.Name, item.Name + ".cfg");
  635. //保存产品
  636. FileHelper.WriteJsonFile(item, productFilePath);
  637. if (!string.IsNullOrEmpty(oldCameraName))
  638. {
  639. //改之前相机流程文件夹路径
  640. string cameraDirPath = Path.Combine(ProductsRootPath, item.Name, oldCameraName);
  641. //如果相机名称发生变化,重命名相机流程文件夹
  642. if (oldCameraName != newCameraName)
  643. {
  644. string newCameraDirPath = Path.Combine(ProductsRootPath, item.Name, newCameraName);
  645. if (Directory.Exists(cameraDirPath))
  646. {
  647. Directory.Move(cameraDirPath, newCameraDirPath);
  648. }
  649. }
  650. }
  651. }
  652. }
  653. /// <summary>
  654. /// 修改相机名称时,需要对产品的相机流程进行修改
  655. /// </summary>
  656. /// <param name="cameraId"></param>
  657. /// <param name="newCameraName"></param>
  658. /// <returns></returns>
  659. public Task UpdateCameraNameInProductAsync(Guid cameraId, string newCameraName)
  660. {
  661. return Task.Run(() => UpdateCameraNameInProduct(cameraId, newCameraName));
  662. }
  663. /// <summary>
  664. /// 增加机器人时,需要对产品的机器人点位进行增加
  665. /// </summary>
  666. /// <param name="robotId"></param>
  667. /// <param name="robotPointName"></param>
  668. public void AddRobotPointToProduct(Guid robotId, string robotPointName)
  669. {
  670. //获取所有产品
  671. var products = GetAllProducts();
  672. if (products == null) return;
  673. //遍历所有产品,增加机器人点位表
  674. foreach (var item in products)
  675. {
  676. item.RobotPoint.AddRobot(robotId, robotPointName);
  677. //单个产品的配置文件路径
  678. string productFilePath = Path.Combine(ProductsRootPath, item.Name, item.Name + ".cfg");
  679. //保存产品
  680. FileHelper.WriteJsonFile(item, productFilePath);
  681. }
  682. }
  683. /// <summary>
  684. /// 增加机器人时,需要对产品的机器人点位进行增加
  685. /// </summary>
  686. /// <param name="robotId"></param>
  687. /// <param name="robotPointName"></param>
  688. /// <returns></returns>
  689. public Task AddRobotPointToProductAsync(Guid robotId, string robotPointName)
  690. {
  691. return Task.Run(() => AddRobotPointToProduct(robotId, robotPointName));
  692. }
  693. /// <summary>
  694. /// 删除机器人时,需要对产品的机器人点位进行删除
  695. /// </summary>
  696. /// <param name="robotId"></param>
  697. public void RemoveRobotPointFromProduct(Guid robotId)
  698. {
  699. var products = GetAllProducts();
  700. if (products == null) return;
  701. //遍历所有产品,删除机器人点位表
  702. foreach (var item in products)
  703. {
  704. item.RobotPoint.RemoveRobot(robotId);
  705. //单个产品的配置文件路径
  706. string productFilePath = Path.Combine(ProductsRootPath, item.Name, item.Name + ".cfg");
  707. //保存产品
  708. FileHelper.WriteJsonFile(item, productFilePath);
  709. }
  710. }
  711. /// <summary>
  712. /// 删除机器人时,需要对产品的机器人点位进行删除
  713. /// </summary>
  714. /// <param name="robotId"></param>
  715. /// <returns></returns>
  716. public Task RemoveRobotPointFromProductAsync(Guid robotId)
  717. {
  718. return Task.Run(() => RemoveRobotPointFromProduct(robotId));
  719. }
  720. /// <summary>
  721. /// 修改机器人名称时,需要对产品的机器人点位进行修改
  722. /// </summary>
  723. /// <param name="robotId"></param>
  724. /// <param name="newRobotPointName"></param>
  725. public void UpdateRobotPointNameInProduct(Guid robotId, string newRobotPointName)
  726. {
  727. var products = GetAllProducts();
  728. if (products == null) return;
  729. //遍历所有产品,修改机器人点位名称
  730. foreach (var item in products)
  731. {
  732. var robotPointStore = item.RobotPoint.Robots.FirstOrDefault(rp => rp.Id == robotId);
  733. if (robotPointStore != null)
  734. {
  735. robotPointStore.Name = newRobotPointName;
  736. }
  737. //单个产品的配置文件路径
  738. string productFilePath = Path.Combine(ProductsRootPath, item.Name, item.Name + ".cfg");
  739. //保存产品
  740. FileHelper.WriteJsonFile(item, productFilePath);
  741. }
  742. }
  743. /// <summary>
  744. /// 修改机器人名称时,需要对产品的机器人点位进行修改
  745. /// </summary>
  746. /// <param name="robotId"></param>
  747. /// <param name="newRobotPointName"></param>
  748. /// <returns></returns>
  749. public Task UpdateRobotPointNameInProductAsync(Guid robotId, string newRobotPointName)
  750. {
  751. return Task.Run(() => UpdateRobotPointNameInProduct(robotId, newRobotPointName));
  752. }
  753. /// <summary>
  754. /// 加载指定产品流程的视觉工具
  755. /// </summary>
  756. /// <param name="productid"></param>
  757. /// <param name="cameraid"></param>
  758. /// <param name="prcname"></param>
  759. public void LoadProcedureVisionTools(Guid productid, Guid cameraid, string prcname)
  760. {
  761. //获取产品
  762. var product= GetProduct(productid);
  763. if (product == null) return;
  764. //获取相机流程
  765. var cameraProcedure = product.CameraProcedures.FirstOrDefault(cp => cp.ID == cameraid);
  766. if (cameraProcedure == null) return;
  767. //获取指定名称的流程
  768. var procedure = cameraProcedure.ProcedureModels.FirstOrDefault(pm => pm.Name == prcname);
  769. if (procedure == null) return;
  770. //获取流程视觉工具路径
  771. string vpppath= Path.Combine(ProductsRootPath, product.Name, cameraProcedure.Camera, prcname + ".vpp");
  772. if (File.Exists(vpppath))
  773. {
  774. procedure.ToolBlock = CogSerializer.LoadObjectFromFile(vpppath) as CogToolBlock;
  775. }
  776. else
  777. {
  778. //如果不存在,则加载一个模板工具
  779. if (File.Exists(TemplateToolBlock))
  780. {
  781. procedure.ToolBlock = CogSerializer.LoadObjectFromFile(TemplateToolBlock) as CogToolBlock;
  782. //保存到指定路径
  783. CogSerializer.SaveObjectToFile(procedure.ToolBlock, vpppath);
  784. }
  785. else
  786. {
  787. throw new FileNotFoundException("模板工具文件不存在,无法创建新的视觉工具。", TemplateToolBlock);
  788. }
  789. }
  790. }
  791. /// <summary>
  792. /// 加载指定产品流程的视觉工具
  793. /// </summary>
  794. /// <param name="productid"></param>
  795. /// <param name="cameraid"></param>
  796. /// <param name="prcname"></param>
  797. /// <returns></returns>
  798. public Task<bool> LoadProcedureVisionToolsAsync(Guid productid, Guid cameraid, string prcname)
  799. {
  800. return Task.Run(() =>
  801. {
  802. try
  803. {
  804. LoadProcedureVisionTools(productid, cameraid, prcname);
  805. return true;
  806. }
  807. catch
  808. {
  809. return false;
  810. }
  811. });
  812. }
  813. /// <summary>
  814. /// 移除指定文件夹路径下的所有文件和文件夹,及其本身,子文件夹递归删除
  815. /// </summary>
  816. /// <param name="dirPath"></param>
  817. private void DeleteDirectoryRecursively(string dirPath)
  818. {
  819. if (Directory.Exists(dirPath))
  820. {
  821. var dirInfo = new DirectoryInfo(dirPath);
  822. // 删除所有文件
  823. foreach (var file in dirInfo.GetFiles())
  824. {
  825. file.Delete();
  826. }
  827. // 递归删除所有子目录
  828. foreach (var dir in dirInfo.GetDirectories())
  829. {
  830. DeleteDirectoryRecursively(dir.FullName);
  831. }
  832. // 删除当前目录
  833. dirInfo.Delete();
  834. }
  835. }
  836. /// <summary>
  837. /// 产品切换方法,触发事件
  838. /// </summary>
  839. /// <param name="product"></param>
  840. private void OnCurrentProductChanged(ProductModel product)
  841. {
  842. OnProductChanged?.Invoke(product);
  843. }
  844. public void Dispose()
  845. {
  846. RemoveAllProducts();
  847. GC.SuppressFinalize(this);
  848. }
  849. }
  850. }