DEVICE.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.Util;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.Device;
  9. namespace Aitex.Core.RT.Device
  10. {
  11. //[Obsolete("DEVICE不再使用")]
  12. public class DEVICE
  13. {
  14. public static IDeviceManager Manager { set; private get; }
  15. public delegate bool? DeviceFunc(out string reason, int time, params object[] param);
  16. private static object _locker = new object();
  17. private static Dictionary<string, DeviceFunc> deviceFuncs = new Dictionary<string, DeviceFunc>();
  18. public static string Module = "Device";
  19. //[Obsolete("DEVICE不再使用")]
  20. public static void Register(string name, DeviceFunc func)
  21. {
  22. lock (_locker)
  23. {
  24. if(!deviceFuncs.ContainsKey(name))
  25. deviceFuncs.Add(name, func);
  26. }
  27. }
  28. public static void UnRegister(string name)
  29. {
  30. lock (_locker)
  31. {
  32. deviceFuncs.Remove(name);
  33. }
  34. }
  35. public static bool CanDo(string name)
  36. {
  37. return deviceFuncs.ContainsKey(name);
  38. }
  39. public static bool? Do(string name, int time, bool isClientCmd, params object[] param)
  40. {
  41. bool? ret = false;
  42. string reason;
  43. string cmd = name;
  44. lock (_locker)
  45. {
  46. try
  47. {
  48. ret = deviceFuncs[name].Invoke(out reason, time, param);
  49. string result = string.Format("Execute:{0},{1}", name, reason);
  50. if (ret.HasValue && !ret.Value)
  51. {
  52. string msg = String.Format("Failed to do {0}, {1}", name, reason);
  53. EV.PostMessage(Module, EventEnum.GuiCmdExecFailed, Module, msg);
  54. }
  55. else
  56. {
  57. EV.PostMessage(Module, EventEnum.GuiCmdExecSucc, Module, result);
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. LOG.Write(ex, string.Format("Device:执行{0}命令发生异常",name));
  63. ret = false;
  64. }
  65. }
  66. return ret;
  67. }
  68. public static List<IDevice> GetAllDevice( )
  69. {
  70. if (Manager != null)
  71. return Manager.GetAllDevice();
  72. return null;
  73. }
  74. public static T GetDevice<T>(string name) where T : class, IDevice
  75. {
  76. if(Manager != null)
  77. return Manager.GetDevice<T>(name);
  78. return null;
  79. }
  80. public static object GetDevice(string name)
  81. {
  82. if (Manager != null)
  83. return Manager.GetDevice(name);
  84. return null;
  85. }
  86. public static List<T> GetDevice<T>() where T : class, IDevice
  87. {
  88. if (Manager != null)
  89. return Manager.GetDevice<T>();
  90. return null;
  91. }
  92. public static object GetOptionDevice(string name, Type type)
  93. {
  94. if (Manager != null)
  95. return Manager.GetOptionDevice(name, type);
  96. return null;
  97. }
  98. }
  99. }