AutoManager.cs 9.8 KB

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