PlcService.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using OxyPlot;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using TeamAAS_VP.Core.Robots;
  8. using TeamAAS_VP.Interfaces;
  9. using TeamAAS_VP.Models;
  10. namespace TeamAAS_VP.Core.PLCs
  11. {
  12. public class PlcService
  13. {
  14. private readonly Dictionary<Guid, IPlc> _plcCollection;
  15. public PlcService()
  16. {
  17. _plcCollection = new Dictionary<Guid, IPlc>();
  18. }
  19. public bool CreatePlc(Guid id, PlcInfo plcInfo)
  20. {
  21. IPlc plc;
  22. if (plcInfo.CommunicationType == Enums.CommunicationType.OPC_UA_Client)
  23. {
  24. plc = new OPCuaClientPLC(plcInfo);
  25. }
  26. else if (plcInfo.CommunicationType == Enums.CommunicationType.Mitsubishi_MC)
  27. {
  28. plc = new MitsubishiMcPLC(plcInfo);
  29. }
  30. else
  31. {
  32. return false;
  33. }
  34. if (_plcCollection.ContainsKey(id))
  35. {
  36. var currenplc = _plcCollection[id];
  37. currenplc.Dispose();
  38. }
  39. _plcCollection[id] = plc;
  40. return true;
  41. }
  42. public Task<bool> CreateRobotAsync(Guid id, PlcInfo plcInfo)
  43. {
  44. IPlc plc;
  45. if (plcInfo.CommunicationType == Enums.CommunicationType.OPC_UA_Client)
  46. {
  47. plc = new OPCuaClientPLC(plcInfo);
  48. }
  49. else if (plcInfo.CommunicationType == Enums.CommunicationType.Mitsubishi_MC)
  50. {
  51. plc = new MitsubishiMcPLC(plcInfo);
  52. }
  53. else
  54. {
  55. return Task.FromResult(false);
  56. }
  57. if (_plcCollection.ContainsKey(id))
  58. {
  59. var currenplc = _plcCollection[id];
  60. currenplc.Dispose();
  61. }
  62. _plcCollection[id] = plc;
  63. return Task.FromResult(true);
  64. }
  65. public IPlc GetPlc(Guid id)
  66. {
  67. if (!_plcCollection.ContainsKey(id))
  68. {
  69. return null;
  70. }
  71. return _plcCollection[id];
  72. }
  73. public Task<IPlc> GetPlcAsync(Guid id)
  74. {
  75. if (!_plcCollection.ContainsKey(id))
  76. {
  77. return null;
  78. }
  79. return Task.FromResult(_plcCollection[id]);
  80. }
  81. public void UnRegisterPlc(Guid id)
  82. {
  83. if (_plcCollection.ContainsKey(id))
  84. {
  85. var plc = _plcCollection[id];
  86. plc.Dispose();
  87. }
  88. }
  89. public Task UnRegisterPlcAsync(Guid id)
  90. {
  91. if (_plcCollection.ContainsKey(id))
  92. {
  93. var plc = _plcCollection[id];
  94. plc.Dispose();
  95. }
  96. return Task.CompletedTask;
  97. }
  98. }
  99. }