InitRoutine.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.RT.SCCore;
  6. using Aitex.Triton160.RT.Device;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Triton160.Common;
  9. using System.Diagnostics;
  10. using Aitex.Core.RT.IOCore;
  11. using Aitex.Core.RT.Fsm;
  12. using Aitex.Triton160.RT.Module;
  13. using Aitex.Core.Util;
  14. using Aitex.Core.RT.Log;
  15. using System.IO;
  16. using Aitex.Triton160.RT.PLC;
  17. using Aitex.Core.RT.Routine;
  18. namespace Aitex.Triton160.RT.Routine.RT
  19. {
  20. public class InitRoutine : SeqenecRoutine
  21. {
  22. private Entity plcEntity = null;
  23. private DeviceEntity deviceEntity = null;
  24. private PMEntity pmEntity = null;
  25. enum INIT
  26. {
  27. PLC,
  28. DEVICE,
  29. PM,
  30. }
  31. public string Module { get; set; }
  32. public string Name { get; set; }
  33. public InitRoutine(string module, string name)
  34. {
  35. Module = module;
  36. Name = name;
  37. }
  38. public bool Initalize()
  39. {
  40. if (SC.GetValue<bool>(SCName.System_IsSimulatorMode))
  41. plcEntity = Singleton<PLCDummyEntity>.Instance;
  42. else
  43. plcEntity = Singleton<PLCEntity>.Instance;
  44. deviceEntity = Singleton<DeviceEntity>.Instance;
  45. pmEntity = Singleton<PMEntity>.Instance;
  46. return true;
  47. }
  48. public Result Start(params object[] objs)
  49. {
  50. Reset();
  51. LOG.Write(String.Format("Routine {0} {1} Start",Module,Name));
  52. return Result.RUN;
  53. }
  54. public Result Monitor()
  55. {
  56. try
  57. {
  58. LaunchMoudle<Entity>(INIT.PLC, plcEntity, "IOLayer");
  59. LaunchMoudle<DeviceEntity>(INIT.DEVICE, deviceEntity, "DeviceLayer");
  60. LaunchMoudle<PMEntity>(INIT.PM, pmEntity, "PM");
  61. }
  62. catch(RoutineBreakException)
  63. {
  64. return Result.RUN;
  65. }
  66. catch(RoutineFaildException)
  67. {
  68. return Result.FAIL;
  69. }
  70. return Result.DONE;
  71. }
  72. public void Abort()
  73. {
  74. if (pmEntity != null)
  75. pmEntity.Terminate();
  76. if (plcEntity != null)
  77. plcEntity.Terminate();
  78. }
  79. private void LaunchMoudle<T>(INIT id, T entity, string name) where T : Entity
  80. {
  81. Tuple<bool, Result> ret = ExecuteAndWait(id, () =>
  82. {
  83. if (!entity.Initialize())
  84. {
  85. LOG.Error(String.Format("系统启动:{0}启动失败.", name));
  86. return false;
  87. }
  88. return true;
  89. },() =>
  90. {
  91. return entity.Running;
  92. }, 10 * 1000);
  93. if (ret.Item1)
  94. {
  95. if (ret.Item2 == Result.FAIL)
  96. {
  97. throw (new RoutineFaildException());
  98. }
  99. else if (ret.Item2 == Result.TIMEOUT) //timeout
  100. {
  101. LOG.Error(String.Format("系统启动:{0}启动超时失败,超时时间为{1}秒", name, Timeout));
  102. throw(new RoutineFaildException());
  103. }
  104. else
  105. throw (new RoutineBreakException());
  106. }
  107. }
  108. }
  109. }