MotionManager.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Newtonsoft.Json;
  6. using TeamAAS.Global.Devices;
  7. using TeamAAS.Motion.AdapterFactorys;
  8. using TeamAAS.Motion.Models;
  9. namespace TeamAAS.Motion
  10. {
  11. public class MotionManager
  12. {
  13. private static MotionManager _instance;
  14. public static MotionManager Instance => _instance ?? (_instance = new MotionManager());
  15. private DevicePluginManager _devicePluginManager = new DevicePluginManager();
  16. public DevicePluginManager DevicePluginManager
  17. {
  18. get => _devicePluginManager;
  19. set => _devicePluginManager = value;
  20. }
  21. private readonly object _sync = new object();
  22. public List<object> Devices { get; } = new List<object>();
  23. public List<MotionDeviceConfig> Configs { get; private set; } = new List<MotionDeviceConfig>();
  24. public static string ConfigPath
  25. {
  26. get
  27. {
  28. var sysCfg = TeamAAS.PathHelper.SystemConfigurationFile;
  29. var dir = string.IsNullOrEmpty(sysCfg)
  30. ? AppDomain.CurrentDomain.BaseDirectory
  31. : Path.GetDirectoryName(sysCfg);
  32. return Path.Combine(dir ?? "", "MotionDevices.json");
  33. }
  34. }
  35. public void LoadConfig()
  36. {
  37. lock (_sync)
  38. {
  39. CloseAllUnsafe();
  40. try
  41. {
  42. if (File.Exists(ConfigPath))
  43. {
  44. var json = File.ReadAllText(ConfigPath);
  45. Configs = JsonConvert.DeserializeObject<List<MotionDeviceConfig>>(json)
  46. ?? new List<MotionDeviceConfig>();
  47. }
  48. else
  49. {
  50. Configs = new List<MotionDeviceConfig>();
  51. }
  52. }
  53. catch
  54. {
  55. Configs = new List<MotionDeviceConfig>();
  56. }
  57. foreach (var cfg in Configs)
  58. {
  59. var dev = CreateDevice(cfg);
  60. if (dev != null)
  61. Devices.Add(dev);
  62. }
  63. }
  64. }
  65. public bool SaveConfig()
  66. {
  67. lock (_sync)
  68. {
  69. try
  70. {
  71. var dir = Path.GetDirectoryName(ConfigPath);
  72. if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
  73. Directory.CreateDirectory(dir);
  74. File.WriteAllText(ConfigPath, JsonConvert.SerializeObject(Configs, Formatting.Indented));
  75. return true;
  76. }
  77. catch (Exception ex)
  78. {
  79. AppLogger.Error("运动设备配置保存失败", ex, nameof(MotionManager));
  80. }
  81. return false;
  82. }
  83. }
  84. public IMotionCard GetMotion(string name)
  85. => Devices.OfType<IMotionCard>()
  86. .FirstOrDefault(d => string.Equals(d.Name, name, StringComparison.OrdinalIgnoreCase));
  87. public IIOCard GetIO(string name)
  88. => Devices.OfType<IIOCard>()
  89. .FirstOrDefault(d => string.Equals(d.Name, name, StringComparison.OrdinalIgnoreCase));
  90. public void CloseAll()
  91. {
  92. lock (_sync) { CloseAllUnsafe(); }
  93. }
  94. private void CloseAllUnsafe()
  95. {
  96. foreach (var d in Devices)
  97. {
  98. try
  99. {
  100. if (d is IMotionCard motion)
  101. motion.Close();
  102. else
  103. (d as IConnectableDevice)?.Close();
  104. }
  105. catch { }
  106. }
  107. Devices.Clear();
  108. }
  109. public object CreateDevice(MotionDeviceConfig cfg)
  110. {
  111. if (cfg == null) return null;
  112. var factory = DevicePluginManager.GetFactory(cfg);
  113. if (factory == null)
  114. {
  115. AppLogger.Info($"未找到运动适配器: {cfg.AdapterKey}");
  116. return null;
  117. }
  118. return factory.Create(cfg);
  119. }
  120. public bool AddDevice(MotionDeviceConfig cfg)
  121. {
  122. if (cfg == null || string.IsNullOrWhiteSpace(cfg.Name)) return false;
  123. lock (_sync)
  124. {
  125. if (Configs.Any(c => string.Equals(c.Name, cfg.Name, StringComparison.OrdinalIgnoreCase)))
  126. return false;
  127. var dev = CreateDevice(cfg);
  128. if (dev == null) return false;
  129. Configs.Add(cfg);
  130. Devices.Add(dev);
  131. return SaveConfig();
  132. }
  133. }
  134. public bool RemoveDevice(MotionDeviceConfig cfg)
  135. {
  136. if (cfg == null) return false;
  137. lock (_sync)
  138. {
  139. var motion = GetMotion(cfg.Name);
  140. try { motion?.Close(); } catch { }
  141. if (motion != null)
  142. Devices.Remove(motion);
  143. Configs.RemoveAll(c => string.Equals(c.Name, cfg.Name, StringComparison.OrdinalIgnoreCase));
  144. return SaveConfig();
  145. }
  146. }
  147. }
  148. }