AutoManager.cs 9.5 KB

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