AutoManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using System.Threading;
  3. using GSG.NET.Concurrent;
  4. using GSG.NET.Logging;
  5. using OHV.Common.Shareds;
  6. using VehicleControlSystem.ControlLayer.IO;
  7. using Prism.Events;
  8. using OHV.SqliteDAL;
  9. using OHV.Common.Model;
  10. using OHV.Common.Events;
  11. using VehicleControlSystem.ControlLayer;
  12. namespace VehicleControlSystem.Managers
  13. {
  14. /// <summary>
  15. /// Vehicle 전체 상태를 관리.
  16. /// </summary>
  17. public class AutoManager : IDisposable
  18. {
  19. static Logger logger = Logger.GetLogger();
  20. Thread threadWorker = null;
  21. bool isThreadAlive = false;
  22. IIO iO = null;
  23. bool IsErrorProcessing = false;
  24. IEventAggregator eventAggregator = null;
  25. SqliteManager sql = null;
  26. private eOperatationMode operationMode;
  27. public eOperatationMode OperationModeProperty
  28. {
  29. get { return operationMode; }
  30. set
  31. {
  32. if (operationMode == value) return;
  33. operationMode = value;
  34. logger.D($"[AutoManager] OperationMode - {value}");
  35. this.OperationModeChanged?.Invoke(this.operationMode);
  36. }
  37. }
  38. private delegate void OperationModeChanging();
  39. public event Action OperationModeChanged;
  40. private eAutoModeState autoModeState;
  41. public eAutoModeState AutoModeStateProperty
  42. {
  43. get { return autoModeState; }
  44. set
  45. {
  46. if (autoModeState == value) return;
  47. autoModeState = value;
  48. logger.D($"[AutoManager] AutoModeState - {value}");
  49. }
  50. }
  51. private eLampState lampState;
  52. public eLampState LampStateProperty
  53. {
  54. get { return lampState; }
  55. set
  56. {
  57. if (lampState == value) return;
  58. lampState = value;
  59. this.LampStateChange(value);
  60. }
  61. }
  62. private eBuzzerKind buzzerState;
  63. public eBuzzerKind BuzzerStateProperty
  64. {
  65. get { return buzzerState; }
  66. set { buzzerState = value; }
  67. }
  68. public AutoManager(IIO io, IEventAggregator ea, SqliteManager sql)
  69. {
  70. this.iO = io;
  71. this.eventAggregator = ea;
  72. this.sql = sql;
  73. }
  74. #region Vehicle Events
  75. private void Vehicle_OnChargingFull()
  76. {
  77. LampStateChange(eLampState.AutoRunNChargingFull);
  78. }
  79. private void Vehicle_OnCharging()
  80. {
  81. LampStateChange(eLampState.Charging);
  82. }
  83. private void Vehicle_OnMoveFinish()
  84. {
  85. BuzzerOnOff(false);
  86. logger.D("Vehicle Move Finish");
  87. }
  88. private void Vehicle_OnMoving()
  89. {
  90. BuzzerOnOff(true, eBuzzerKind.Moving);
  91. logger.D("Vehicle Moving");
  92. }
  93. private void Vehicle_OnMoveReady()
  94. {
  95. BuzzerOnOff(true, eBuzzerKind.StartWarn);
  96. logger.D("Vehicle Move Ready");
  97. }
  98. #endregion
  99. public void Init(Vehicle vehicle)
  100. {
  101. this.OperationModeProperty = eOperatationMode.ManualMode;
  102. this.AutoModeStateProperty = eAutoModeState.Stop;
  103. this.isThreadAlive = true;
  104. this.threadWorker = ThreadUtils.Invoke(this.ThreadWork);
  105. vehicle.OnMoveReady += Vehicle_OnMoveReady;
  106. vehicle.OnMoving += Vehicle_OnMoving;
  107. vehicle.OnMoveFinish += Vehicle_OnMoveFinish;
  108. vehicle.OnCharging += Vehicle_OnCharging;
  109. vehicle.OnChargingFull += Vehicle_OnChargingFull;
  110. }
  111. #region Lamp & Buzzer
  112. void LampStateChange(eLampState state)
  113. {
  114. this.iO.OutputOff("OUT_TOWER_LAMP_RED");
  115. this.iO.OutputOff("OUT_TOWER_LAMP_GREEN");
  116. this.iO.OutputOff("OUT_TOWER_LAMP_BLUE");
  117. switch (state)
  118. {
  119. case eLampState.Alarm:
  120. this.iO.OutputOff("OUT_TOWER_LAMP_RED");
  121. break;
  122. case eLampState.Charging:
  123. this.iO.OutputOff("OUT_TOWER_LAMP_GREEN");
  124. break;
  125. case eLampState.AutoRunNChargingFull:
  126. this.iO.OutputOff("OUT_TOWER_LAMP_BLUE");
  127. break;
  128. default:
  129. break;
  130. }
  131. }
  132. void BuzzerOnOff(bool isOn, eBuzzerKind kind = eBuzzerKind.Alarm)
  133. {
  134. this.iO.OutputOff("OUT_BUZZER_00");
  135. this.iO.OutputOff("OUT_BUZZER_01");
  136. this.iO.OutputOff("OUT_BUZZER_02");
  137. if (!isOn)
  138. return;
  139. switch (kind)
  140. {
  141. case eBuzzerKind.Alarm:
  142. this.iO.OutputOn("OUT_BUZZER_00");
  143. break;
  144. case eBuzzerKind.StartWarn:
  145. this.iO.OutputOn("OUT_BUZZER_01");
  146. break;
  147. case eBuzzerKind.Moving:
  148. this.iO.OutputOn("OUT_BUZZER_02");
  149. break;
  150. default:
  151. break;
  152. }
  153. }
  154. #endregion
  155. void ThreadWork()
  156. {
  157. while (this.isThreadAlive)
  158. {
  159. try
  160. {
  161. Thread.Sleep(5);
  162. DoWork();
  163. }
  164. catch (Exception ex)
  165. {
  166. logger.E($"{GetType().Name} - Thread Exception : {ex.StackTrace}");
  167. }
  168. }
  169. logger.D("[AutoManager] - Work Thread Dispose");
  170. }
  171. public void DoWork()
  172. {
  173. switch (this.OperationModeProperty)
  174. {
  175. case eOperatationMode.ManualMode:
  176. break;
  177. case eOperatationMode.AutoMode:
  178. switch (this.AutoModeStateProperty)
  179. {
  180. case eAutoModeState.ErrorStop:
  181. if ( !IsErrorProcessing )
  182. this.AutoModeStateProperty = eAutoModeState.WaitStop;
  183. break;
  184. case eAutoModeState.WaitStop:
  185. this.AutoModeStateProperty = eAutoModeState.Stop;
  186. break;
  187. case eAutoModeState.Stop:
  188. this.OperationModeProperty = eOperatationMode.ManualMode;
  189. break;
  190. case eAutoModeState.StartRun:
  191. this.AutoModeStateProperty = eAutoModeState.Run;
  192. this.LampStateProperty = eLampState.AutoRunNChargingFull;
  193. break;
  194. case eAutoModeState.Run:
  195. break;
  196. default:
  197. break;
  198. }
  199. break;
  200. case eOperatationMode.InitialMode:
  201. break;
  202. default:
  203. break;
  204. }
  205. }
  206. public void ProcessAlarm(int alarmID)
  207. {
  208. this.AutoModeStateProperty = eAutoModeState.ErrorStop;
  209. this.LampStateProperty = eLampState.Alarm;
  210. this.sql.CommandDAL.Clean();
  211. this.sql.SubCmdDAL.Clean();
  212. HisAlarm hisAlarm = new HisAlarm();
  213. var alarm = sql.AlarmDAL.GetK(alarmID);
  214. if (alarm == null)
  215. {
  216. hisAlarm.AlarmId = alarmID;
  217. hisAlarm.Text = "Not Define Alarm";
  218. }
  219. else
  220. {
  221. hisAlarm.AlarmId = alarmID;
  222. hisAlarm.Text = alarm.Name + " - " + alarm.Text;
  223. hisAlarm.Solution = alarm.Solution;
  224. //hisAlarm.Alarm = alarm;
  225. }
  226. sql.HisAlarmDAL.Add(hisAlarm);
  227. if (IsErrorProcessing)
  228. return;
  229. var msg = new GUIMessageEventArgs
  230. {
  231. Kind = GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange,
  232. MessageKey = MessageKey.Alarm,
  233. MessageText = hisAlarm.Text,
  234. Args = hisAlarm,
  235. };
  236. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Publish(msg);
  237. }
  238. #region IDisposable Support
  239. private bool disposedValue = false; // 중복 호출을 검색하려면
  240. protected virtual void Dispose(bool disposing)
  241. {
  242. if (!disposedValue)
  243. {
  244. if (disposing)
  245. {
  246. // TODO: 관리되는 상태(관리되는 개체)를 삭제합니다.
  247. this.isThreadAlive = false;
  248. if (!this.threadWorker.Join(3000))
  249. ThreadUtils.Kill(this.threadWorker);
  250. }
  251. // TODO: 관리되지 않는 리소스(관리되지 않는 개체)를 해제하고 아래의 종료자를 재정의합니다.
  252. // TODO: 큰 필드를 null로 설정합니다.
  253. disposedValue = true;
  254. }
  255. }
  256. // TODO: 위의 Dispose(bool disposing)에 관리되지 않는 리소스를 해제하는 코드가 포함되어 있는 경우에만 종료자를 재정의합니다.
  257. // ~AutoManager()
  258. // {
  259. // // 이 코드를 변경하지 마세요. 위의 Dispose(bool disposing)에 정리 코드를 입력하세요.
  260. // Dispose(false);
  261. // }
  262. // 삭제 가능한 패턴을 올바르게 구현하기 위해 추가된 코드입니다.
  263. public void Dispose()
  264. {
  265. // 이 코드를 변경하지 마세요. 위의 Dispose(bool disposing)에 정리 코드를 입력하세요.
  266. Dispose(true);
  267. // TODO: 위의 종료자가 재정의된 경우 다음 코드 줄의 주석 처리를 제거합니다.
  268. // GC.SuppressFinalize(this);
  269. }
  270. #endregion
  271. }
  272. }