Scheduler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using GSG.NET.Concurrent;
  9. using GSG.NET.Logging;
  10. using GSG.NET.Utils;
  11. using OHV.Common.Events;
  12. using OHV.Common.Model;
  13. using OHV.SqliteDAL;
  14. using Prism.Events;
  15. namespace VehicleControlSystem.Managers
  16. {
  17. public class Scheduler : IDisposable
  18. {
  19. static Logger logger = Logger.GetLogger();
  20. private List<Command> commands;
  21. public List<Command> CommandsList
  22. {
  23. get { return commands; }
  24. set { commands = value; }
  25. }
  26. Thread thread = null;
  27. bool isThreadAlive = false;
  28. IEventAggregator eventAggregator;
  29. AutoManager autoManager;
  30. SqliteManager sql = null;
  31. public Scheduler( IEventAggregator ea, AutoManager autoManager, SqliteManager sqlite )
  32. {
  33. this.CommandsList = new List<Command>();
  34. eventAggregator = ea;// CommonServiceLocator.ServiceLocator.Current.GetInstance<IEventAggregator>();
  35. eventAggregator.GetEvent<VCSMessagePubSubEvent>().Subscribe( OnReceivedMessageEvent );
  36. this.autoManager = autoManager;
  37. this.sql = sqlite;
  38. }
  39. private void OnReceivedMessageEvent( VCSMessageEventArgs obj )
  40. {
  41. this.AddCommand( obj.Command );
  42. //var m = new GUIMessageEventArgs() { MessageText = obj.MessageText, MessageKey = obj.MessageKey };
  43. //m.Command = ObjectCopyUtils.DeepClone(obj.Command);
  44. //GUIMessagePublish(m);
  45. }
  46. void GUIMessagePublish( GUIMessageEventArgs args )
  47. {
  48. eventAggregator.GetEvent<GUIMessagePubSubEvent>().Publish( args );
  49. }
  50. public void Init()
  51. {
  52. this.isThreadAlive = true;
  53. this.thread = ThreadUtils.Invoke( this.ExcuteCommand );
  54. }
  55. public bool AddCommand( Command cmd )
  56. {
  57. if ( this.CommandsList == null ) return false;
  58. try
  59. {
  60. this.CommandsList.Add( cmd );
  61. }
  62. catch ( Exception ex )
  63. {
  64. logger.E( ex );
  65. return false;
  66. }
  67. return true;
  68. }
  69. bool RemoveCommand( Command cmd )
  70. {
  71. if ( !this.CommandsList.Remove( cmd ) )
  72. {
  73. logger.D( $"Scheduler - [Command] : RemoveCommand fail ID-{cmd.CommandID} / Result-{cmd.Result} " );
  74. return false;
  75. }
  76. logger.D( $"Scheduler - [Command] : RemoveCommand OK ID-{cmd.CommandID} / Result-{cmd.Result} " );
  77. return true;
  78. }
  79. void ExcuteCommand()
  80. {
  81. while ( isThreadAlive )
  82. {
  83. Thread.Sleep( 500 );
  84. try
  85. {
  86. var state = this.autoManager.OperationModeProperty;
  87. switch ( state )
  88. {
  89. case OHV.Common.Shareds.eOperatationMode.ManualMode:
  90. break;
  91. case OHV.Common.Shareds.eOperatationMode.AutoMode:
  92. {
  93. switch ( this.autoManager.AutoModeStateProperty )
  94. {
  95. case OHV.Common.Shareds.eAutoModeState.ErrorStop:
  96. break;
  97. case OHV.Common.Shareds.eAutoModeState.StartRun:
  98. break;
  99. case OHV.Common.Shareds.eAutoModeState.Run:
  100. Command cmd = sql.CommandDAL.GetCmd();
  101. if ( cmd == null ) continue;
  102. DoWork( cmd );
  103. break;
  104. case OHV.Common.Shareds.eAutoModeState.Stop:
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. break;
  111. case OHV.Common.Shareds.eOperatationMode.InitialMode:
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117. catch ( Exception ex )
  118. {
  119. logger.E( ex );
  120. }
  121. }
  122. }
  123. private void DoWork( Command cmd )
  124. {
  125. switch ( cmd.State )
  126. {
  127. case OHV.Common.Shareds.eCommandState.Queued:
  128. if ( sql.SubCmdDAL.Count <= 0 ) //
  129. {
  130. switch ( cmd.Type )
  131. {
  132. case OHV.Common.Shareds.eCommandType.Move:
  133. case OHV.Common.Shareds.eCommandType.MoveNLoad:
  134. case OHV.Common.Shareds.eCommandType.MoveNUnload:
  135. CreateSubCommand( cmd, SubCmd.eType.Move );
  136. break;
  137. case OHV.Common.Shareds.eCommandType.Charging:
  138. break;
  139. default:
  140. break;
  141. }
  142. this.sql.CommandDAL.UpdateState( cmd.CommandID, OHV.Common.Shareds.eCommandState.Processing );
  143. }
  144. break;
  145. case OHV.Common.Shareds.eCommandState.Processing:
  146. {
  147. var subCmd = sql.SubCmdDAL.GetSubCmd();
  148. if ( subCmd == null ) // Move 명령을 수행 완료 했다고 판단.
  149. {
  150. if ( cmd.IsSecondCommanded )
  151. {
  152. sql.CommandDAL.UpdateState( cmd.CommandID, OHV.Common.Shareds.eCommandState.Complete );
  153. break;
  154. }
  155. if ( cmd.Type == OHV.Common.Shareds.eCommandType.MoveNLoad )
  156. {
  157. if ( this.CreateSubCommand( cmd, SubCmd.eType.Load ) )
  158. cmd.IsSecondCommanded = true;
  159. break;
  160. }
  161. else if ( cmd.Type == OHV.Common.Shareds.eCommandType.MoveNUnload )
  162. {
  163. if ( this.CreateSubCommand( cmd, SubCmd.eType.Unload ) )
  164. cmd.IsSecondCommanded = true;
  165. break;
  166. }
  167. else if ( cmd.Type == OHV.Common.Shareds.eCommandType.Charging )
  168. {
  169. if ( this.CreateSubCommand( cmd, SubCmd.eType.Charge ) )
  170. cmd.IsSecondCommanded = true;
  171. break;
  172. }
  173. else //Move Command complete
  174. {
  175. sql.CommandDAL.UpdateState( cmd.CommandID, OHV.Common.Shareds.eCommandState.Complete );
  176. break;
  177. }
  178. }
  179. else
  180. {
  181. if ( subCmd.CmdID.Equals( cmd.CommandID ) ) //진행중 판단
  182. break;
  183. }
  184. }
  185. break;
  186. case OHV.Common.Shareds.eCommandState.Complete:
  187. sql.CommandDAL.Delete( cmd );
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. private bool CreateSubCommand( Command cmd, SubCmd.eType type )
  194. {
  195. //명령을 만들기 전에 Vehicle 의 상태를 확인하여 만들자.
  196. switch ( type )
  197. {
  198. case SubCmd.eType.Move:
  199. break;
  200. case SubCmd.eType.Load:
  201. break;
  202. case SubCmd.eType.Unload:
  203. break;
  204. case SubCmd.eType.Charge:
  205. break;
  206. default:
  207. break;
  208. }
  209. var sub = new SubCmd
  210. {
  211. CmdType = SubCmd.eCmdType.Auto,
  212. Type = type,
  213. TargetID = cmd.TargetID,
  214. CmdID = cmd.CommandID,
  215. };
  216. sql.SubCmdDAL.Add( sub );
  217. logger.I( $"{sub.ID} Sub Command Create - Type={sub.Type}, TargetID={sub.TargetID}, CommandID={sub.CmdID}" );
  218. return true;
  219. }
  220. public bool CreateTransferCommand()
  221. {
  222. Command cmd = new Command
  223. {
  224. };
  225. sql.CommandDAL.Add( cmd );
  226. logger.I( $"{cmd.CommandID} Command Create - Type={cmd.Type}, TargetID={cmd.TargetID}" );
  227. return true;
  228. }
  229. #region IDisposable Support
  230. private bool disposedValue = false; // 중복 호출을 검색하려면
  231. protected virtual void Dispose( bool disposing )
  232. {
  233. if ( !disposedValue )
  234. {
  235. if ( disposing )
  236. {
  237. // TODO: 관리되는 상태(관리되는 개체)를 삭제합니다.
  238. this.isThreadAlive = false;
  239. if ( !this.thread.Join( 3000 ) )
  240. ThreadUtils.Kill( this.thread );
  241. }
  242. // TODO: 관리되지 않는 리소스(관리되지 않는 개체)를 해제하고 아래의 종료자를 재정의합니다.
  243. // TODO: 큰 필드를 null로 설정합니다.
  244. disposedValue = true;
  245. }
  246. }
  247. // TODO: 위의 Dispose(bool disposing)에 관리되지 않는 리소스를 해제하는 코드가 포함되어 있는 경우에만 종료자를 재정의합니다.
  248. // ~Scheduler()
  249. // {
  250. // // 이 코드를 변경하지 마세요. 위의 Dispose(bool disposing)에 정리 코드를 입력하세요.
  251. // Dispose(false);
  252. // }
  253. // 삭제 가능한 패턴을 올바르게 구현하기 위해 추가된 코드입니다.
  254. public void Dispose()
  255. {
  256. // 이 코드를 변경하지 마세요. 위의 Dispose(bool disposing)에 정리 코드를 입력하세요.
  257. Dispose( true );
  258. // TODO: 위의 종료자가 재정의된 경우 다음 코드 줄의 주석 처리를 제거합니다.
  259. // GC.SuppressFinalize(this);
  260. }
  261. #endregion
  262. }
  263. }