| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- using System;
- using System.Threading;
- using GSG.NET.Concurrent;
- using GSG.NET.Logging;
- using OHV.Common.Shareds;
- using VehicleControlSystem.ControlLayer.IO;
- using Prism.Events;
- using OHV.SqliteDAL;
- using OHV.Common.Model;
- using OHV.Common.Events;
- using VehicleControlSystem.ControlLayer;
- using GSG.NET.Utils;
- using System.Collections.Generic;
- using System.Linq;
- namespace VehicleControlSystem.Managers
- {
- /// <summary>
- /// Vehicle 전체 상태를 관리.
- /// </summary>
- public class AutoManager : IDisposable
- {
- static Logger logger = Logger.GetLogger();
- Thread threadWorker = null;
- bool isThreadAlive = false;
- IIO iO = null;
- private bool isErrorProcessing = false;
- public bool IsErrorProcessing
- {
- get { return this.isErrorProcessing; }
- set
- {
- if ( this.isErrorProcessing == value ) return;
- this.isErrorProcessing = value;
- if ( value )
- {
- if ( this.activeAlarm == null ) return;
- this.OnOccurAlarm?.BeginInvoke( this.activeAlarm.AlarmId, null, null );
- }
- else
- {
- if ( this.activeAlarm == null ) return;
- this.OnClearAlarm?.BeginInvoke( this.activeAlarm.AlarmId, null, null );
- this.activeAlarm = null;
- this.BuzzerOnOff( false );
- }
- }
- }
- public event Action<int> OnOccurAlarm;
- public event Action<int> OnClearAlarm;
- Alarm activeAlarm = null;
- List<Alarm> Alarms { get; set; }
- IEventAggregator eventAggregator = null;
- SqliteManager sql = null;
- private eOperatationMode operationMode;
- public eOperatationMode OperationModeProperty
- {
- get { return operationMode; }
- set
- {
- if ( operationMode == value ) return;
- operationMode = value;
- logger.D( $"[AutoManager] OperationMode - {value}" );
- this.OnOperationModeChanged?.Invoke( value );
- if ( value == eOperatationMode.AutoMode )
- this.iO.OutputOff( "OUT_TEACH_MODE" );
- else
- this.iO.OutputOn( "OUT_TEACH_MODE" );
- }
- }
- public event Action<eOperatationMode> OnOperationModeChanged;
- private eAutoModeState autoModeState;
- public eAutoModeState AutoModeStateProperty
- {
- get { return autoModeState; }
- set
- {
- if ( autoModeState == value ) return;
- autoModeState = value;
- logger.D( $"[AutoManager] AutoModeState - {value}" );
- switch ( value )
- {
- case eAutoModeState.ErrorStop:
- this.LampStateProperty = eLampState.Alarm;
- break;
- case eAutoModeState.StartRun:
- this.LampStateProperty = eLampState.AutoRunNChargingFull;
- break;
- case eAutoModeState.Run:
- this.LampStateProperty = eLampState.AutoRunNChargingFull;
- break;
- case eAutoModeState.Stop:
- this.LampStateProperty = eLampState.Alarm;
- break;
- case eAutoModeState.WaitStop:
- this.LampStateProperty = eLampState.Alarm;
- break;
- default:
- break;
- }
- }
- }
- private eLampState lampState = eLampState.None;
- public eLampState LampStateProperty
- {
- get { return lampState; }
- set
- {
- if ( lampState == value ) return;
- lampState = value;
- this.LampStateChange( value );
- }
- }
- private eBuzzerKind buzzerState;
- public eBuzzerKind BuzzerStateProperty
- {
- get { return buzzerState; }
- set { buzzerState = value; }
- }
- public AutoManager( IIO io, IEventAggregator ea, SqliteManager sql, List<Alarm> al )
- {
- this.iO = io;
- this.eventAggregator = ea;
- this.sql = sql;
- this.Alarms = al;
- }
- #region Vehicle Events
- private void Vehicle_OnChargingFull()
- {
- if ( this.OperationModeProperty == eOperatationMode.AutoMode )
- LampStateChange( eLampState.AutoRunNChargingFull );
- else
- LampStateChange( eLampState.Alarm );
- }
- private void Vehicle_OnCharging()
- {
- LampStateChange( eLampState.Charging );
- }
- private void Vehicle_OnMoveFinish()
- {
- BuzzerOnOff( false );
- logger.D( "Vehicle Move Finish" );
- }
- private void Vehicle_OnMoving()
- {
- BuzzerOnOff( true, eBuzzerKind.Moving );
- logger.D( "Vehicle Moving" );
- }
- private void Vehicle_OnMoveReady()
- {
- BuzzerOnOff( true, eBuzzerKind.StartWarn );
- logger.D( "Vehicle Move Ready" );
- }
- #endregion
- public void Init( Vehicle vehicle )
- {
- this.OperationModeProperty = eOperatationMode.ManualMode;
- this.AutoModeStateProperty = eAutoModeState.Stop;
- this.isThreadAlive = true;
- this.threadWorker = ThreadUtils.Invoke( this.ThreadWork );
- vehicle.OnMoveReady += Vehicle_OnMoveReady;
- vehicle.OnMoving += Vehicle_OnMoving;
- vehicle.OnMoveFinish += Vehicle_OnMoveFinish;
- vehicle.OnCharging += Vehicle_OnCharging;
- vehicle.OnChargingFull += Vehicle_OnChargingFull;
- }
- #region Lamp & Buzzer
- void LampStateChange( eLampState state )
- {
- this.iO.OutputOff( "OUT_TOWER_LAMP_RED" );
- this.iO.OutputOff( "OUT_TOWER_LAMP_GREEN" );
- switch ( state )
- {
- case eLampState.Alarm:
- this.iO.OutputOn( "OUT_TOWER_LAMP_RED" );
- break;
- case eLampState.Charging:
- this.iO.OutputOn( "OUT_TOWER_LAMP_RED" );
- this.iO.OutputOn( "OUT_TOWER_LAMP_GREEN" );
- break;
- case eLampState.AutoRunNChargingFull:
- this.iO.OutputOn( "OUT_TOWER_LAMP_GREEN" );
- //Blue 없음
- //this.iO.OutputOff("OUT_TOWER_LAMP_BLUE");
- break;
- default:
- break;
- }
- }
- void BuzzerOnOff( bool isOn, eBuzzerKind kind = eBuzzerKind.Alarm )
- {
- this.iO.OutputOff( "OUT_BUZZER_00" );
- this.iO.OutputOff( "OUT_BUZZER_01" );
- this.iO.OutputOff( "OUT_BUZZER_02" );
- if ( !isOn )
- return;
- switch ( kind )
- {
- case eBuzzerKind.Alarm:
- this.iO.OutputOn( "OUT_BUZZER_00" );
- break;
- case eBuzzerKind.StartWarn:
- this.iO.OutputOn( "OUT_BUZZER_01" );
- break;
- case eBuzzerKind.Moving:
- this.iO.OutputOn( "OUT_BUZZER_02" );
- break;
- default:
- break;
- }
- }
- #endregion
- void ThreadWork()
- {
- while ( this.isThreadAlive )
- {
- try
- {
- Thread.Sleep( 5 );
- DoWork();
- }
- catch ( Exception ex )
- {
- logger.E( $"{GetType().Name} - Thread Exception : {ex.StackTrace}" );
- }
- }
- logger.D( "[AutoManager] - Work Thread Dispose" );
- }
- public void DoWork()
- {
- switch ( this.OperationModeProperty )
- {
- case eOperatationMode.ManualMode:
- break;
- case eOperatationMode.AutoMode:
- switch ( this.AutoModeStateProperty )
- {
- case eAutoModeState.ErrorStop:
- this.AutoModeStateProperty = eAutoModeState.WaitStop;
- break;
- case eAutoModeState.WaitStop:
- this.AutoModeStateProperty = eAutoModeState.Stop;
- break;
- case eAutoModeState.Stop:
- this.OperationModeProperty = eOperatationMode.ManualMode;
- break;
- case eAutoModeState.StartRun:
- this.AutoModeStateProperty = eAutoModeState.Run;
- break;
- case eAutoModeState.Run:
- break;
- default:
- break;
- }
- break;
- case eOperatationMode.InitialMode:
- break;
- default:
- break;
- }
- }
- public void ProcessAlarm( int alarmID )
- {
- this.AutoModeStateProperty = eAutoModeState.ErrorStop;
- this.LampStateProperty = eLampState.Alarm;
- this.BuzzerStateProperty = eBuzzerKind.Alarm;
- HisAlarm hisAlarm = new HisAlarm();
- var alarm = this.Alarms.Where( x => x.AlarmId == alarmID ).FirstOrDefault();
- if ( alarm == null )
- {
- hisAlarm.AlarmId = alarmID;
- hisAlarm.Text = "Not Define Alarm";
- alarm = new Alarm();
- alarm.AlarmId = alarmID;
- alarm.Text = "Not Define Alarm";
- logger.E( $"[{this.GetType().Name}] - Not Defiine Alarm No {alarmID}" );
- }
- else
- {
- hisAlarm.AlarmId = alarmID;
- hisAlarm.Text = alarm.Name + " - " + alarm.Text;
- hisAlarm.Solution = alarm.Solution;
- //hisAlarm.Alarm = alarm;
- }
- sql.HisAlarmDAL.Insert( hisAlarm );
- logger.I( $"[Alarm Occur] - ID : {alarmID} / Text : {hisAlarm.Text}" );
- //UI 로는 처음 발생한 Error 만 전송한다.
- if ( IsErrorProcessing )
- return;
- this.BuzzerOnOff( true, eBuzzerKind.Alarm );
- this.sql.CommandDAL.Clean();
- this.sql.SubCmdDAL.Clean();
- this.activeAlarm = ObjectCopyUtils.DeepClone<Alarm>( alarm );
- IsErrorProcessing = true;
- var msg = new GUIMessageEventArgs
- {
- Kind = GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange,
- MessageKey = MessageKey.Alarm,
- MessageText = hisAlarm.Text,
- Args = hisAlarm,
- };
- this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Publish( msg );
- }
- #region IDisposable Support
- private bool disposedValue = false; // 중복 호출을 검색하려면
- protected virtual void Dispose( bool disposing )
- {
- if ( !disposedValue )
- {
- if ( disposing )
- {
- // TODO: 관리되는 상태(관리되는 개체)를 삭제합니다.
- this.isThreadAlive = false;
- if ( !this.threadWorker.Join( 3000 ) )
- ThreadUtils.Kill( this.threadWorker );
- }
- // TODO: 관리되지 않는 리소스(관리되지 않는 개체)를 해제하고 아래의 종료자를 재정의합니다.
- // TODO: 큰 필드를 null로 설정합니다.
- disposedValue = true;
- }
- }
- // TODO: 위의 Dispose(bool disposing)에 관리되지 않는 리소스를 해제하는 코드가 포함되어 있는 경우에만 종료자를 재정의합니다.
- // ~AutoManager()
- // {
- // // 이 코드를 변경하지 마세요. 위의 Dispose(bool disposing)에 정리 코드를 입력하세요.
- // Dispose(false);
- // }
- // 삭제 가능한 패턴을 올바르게 구현하기 위해 추가된 코드입니다.
- public void Dispose()
- {
- // 이 코드를 변경하지 마세요. 위의 Dispose(bool disposing)에 정리 코드를 입력하세요.
- Dispose( true );
- // TODO: 위의 종료자가 재정의된 경우 다음 코드 줄의 주석 처리를 제거합니다.
- // GC.SuppressFinalize(this);
- }
- #endregion
- }
- }
|