IDeviceFactory.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using Aitex.Core.RT.Log;
  4. using System.Collections;
  5. using Aitex.Core.RT.Device;
  6. namespace Aitex.Core.RT.Device
  7. {
  8. public class DeviceFactory<T> : IEnumerable
  9. where T:class, IDevice
  10. {
  11. private object locker = new object();
  12. protected Dictionary<string, T> devices = null;
  13. public string Type { get; protected set; }
  14. public T this[string name]
  15. {
  16. get
  17. {
  18. T value = null;
  19. lock (locker)
  20. {
  21. if (devices.ContainsKey(name))
  22. value = devices[name];
  23. }
  24. return value;
  25. }
  26. set
  27. {
  28. devices[name] = value;
  29. }
  30. }
  31. public IEnumerator GetEnumerator()
  32. {
  33. return devices.Values.GetEnumerator();
  34. }
  35. public List<T> GetAll()
  36. {
  37. return new List<T>(devices.Values);
  38. }
  39. /// <summary>
  40. /// in future, all device define by xml
  41. /// </summary>
  42. public DeviceFactory()
  43. {
  44. devices = new Dictionary<string, T>();
  45. }
  46. public bool Initialize()
  47. {
  48. lock (locker)
  49. {
  50. Type = DeviceType.UnknownType;
  51. Product();
  52. foreach (var device in devices)
  53. {
  54. if ((device.Value != null) && !device.Value.Initialize())
  55. {
  56. //LOG.Warning("Device {0} initialize false", 2, device.Key);
  57. }
  58. }
  59. }
  60. return true;
  61. }
  62. public void Terminate()
  63. {
  64. lock (locker)
  65. {
  66. foreach (T device in devices.Values)
  67. {
  68. if (device != null)
  69. {
  70. device.Terminate();
  71. }
  72. }
  73. }
  74. }
  75. protected virtual void Product()
  76. {
  77. }
  78. }
  79. }