/// <summary> /// Check driver id is unique /// </summary> public void CheckDriverId() { var dict = new Dictionary <string, int>();//Driver Id with Count foreach (var driver in this.manifest.Drivers) { var key = driver.DriverId; if (!dict.ContainsKey(key)) { dict[key] = 0; } dict[key]++; } var duplicates = (from row in dict where row.Value > 1 select row).ToList(); foreach (var did in duplicates) { MvLog.WarnNs(this, "Duplicate driver id: {0}", did.Key); } if (duplicates.Count != 0) { var did = duplicates.FirstOrDefault().Key; throw new MacWrongDriverException("Exist non-unique driver id " + did); } }
protected virtual void DisposeSelf() { if (this.Task != null) { this.Task.Dispose(); } if (this.Task != null) { //預期Task是能在迴圈開始時, 自己確認是否終止 //一般會以 disposed 和 IsCancellationRequested 作為持續的判斷 try { using (this.Task) if (!this.Task.Wait(3 * 1000)) { if (this.Task.Status < TaskStatus.Running) { MvLog.WarnNs(this, "MvTask is no start"); } else if (this.Task.Status == TaskStatus.Running) { MvLog.WarnNs(this, "MvTask can not cancel"); } } } catch (OperationCanceledException) { } this.Task = null; } }
public int HalClose() { //關閉所有HAL foreach (var kv in this.HalDevices) { try { this.HalClose(kv.Value); } catch (Exception ex) { MvLog.WarnNs(this, ex); } } //釋放資源 foreach (var kv in this.Resources) { try { if (kv.Value == null) { continue; } kv.Value.Dispose(); } catch (Exception ex) { MvLog.WarnNs(this, ex); } } return(0); }
public void SaveStateMachine(string file) { try { XmlDocument doc = new XmlDocument(); XmlElement xStateMachine = doc.CreateElement("StateMachine"); doc.AppendChild(xStateMachine); xStateMachine.SetAttribute("Name", this.Name); xStateMachine.SetAttribute("Class", this.GetType().ToString()); //匯出STATE 狀態 foreach (KeyValuePair <string, State> s in states) { s.Value.GetElements(ref doc, ref xStateMachine); } //匯出Transition 狀態 //foreach (KeyValuePair<Enum, Transition> t in transitions) //{ // t.Value.GetElements(ref doc, ref xStateMachine); //} foreach (Transition t in transitions) { t.GetElements(ref doc, ref xStateMachine); } doc.Save(file); } catch (Exception ex) { MvLog.Write(ex, MvLoggerEnumLevel.Warn); } }
public void ResourceDispose() { foreach (var rsc in this.Resources) { try { rsc.Value.Dispose(); } catch (Exception ex) { MvLog.WarnNs(this, ex); } } }
public void Close() { try { if (this.TcpClient != null) { using (var obj = this.TcpClient) { obj.Disconnect(); } } } catch (Exception ex) { MvLog.WarnNs(this, ex); } }
public void CleanEvent() { try { foreach (Delegate d in this.evtDataReceive.GetInvocationList()) { this.evtDataReceive -= (EventHandler <WacohForceMessageEventArgs>)d; } } catch (Exception ex) { MvLog.Write(ex); } }
public int Close() { // 若 mobjCore 不為空 if (mobjCore != null) { // 將 mobjCore 斷開連線 try { mobjCore.Disconnect(); } catch (Exception ex) { MvLog.WarnNs(this, ex); } mobjCore = null; } return(0); }
public bool ChangeState(State s) { bool trulyChange = false; if (FlageNormalStatus) { if (!s.Equals(CurrentState)) { trulyChange = true; MvLog.Info(ID + " change state from {0} to {1}", this.CurrentState.StateName, s.StateName); CurrentState = s; } } return(trulyChange); }
/// <summary> /// transitions runTrigger with step name /// </summary> /// <param name="param"></param> /// <returns>-1 is action can't run</returns> public EnumStateMachineMsgType SmProcTransition(IStateParam param) { try { bool executeFlag = false; Transition runTriggerSingle = null; //第一步檢查有沒有此TRANSITION if (transitions.Find(x => x.TransName.Equals(param.TransName)) == null) { throw new StateMachineException(ID + " No Find Transition: " + param.TransName); } //找出同名Transition var trans = (from t in transitions where t.TransName == param.TransName select t).ToList(); var currTrans = trans.Where(t => t.FromState == this.CurrentState).ToList();//過濾from state //確認所有Guard是否成立 foreach (var tran in currTrans) { if (tran.RunGuard(this)) { //若確實只找到一個成立, 記錄下來, 超過一個不可執行 if (runTriggerSingle == null) { runTriggerSingle = tran; } else { throw new PluralTriggerSameTimeException(this.GetType().ToString() + " : current is " + this.CurrentState.StateName + " with Transition : " + param.TransName + ", trigger more than one same time");//只能有一個transition成立 } } } if (runTriggerSingle != null) { executeFlag = true; runTriggerSingle.RunTrigger(param, this); } else if (currTrans.Count == 0 && trans.Where(t => t is DeferralTransition).Count() == 0) { //如果要求的transaction不在current state, 且不具有Deferral Transaction, 就拋出例外 //不應存在無法trigger的transition throw new NotCurrentStateException(this.GetType().ToString() + " : current is " + this.CurrentState.StateName + " does not have Transition : " + param.TransName); } else { //否則: 這個State Machine 的 Deferral Transition 具有 param.TranName //在這裡代表沒有transition被觸發, 因此需要判斷是否要存回Queue裡 //State Machine中, 並沒有定義 Defferal Guard 失敗時的行為, 因此暫以 Normal Transition 的行為作準則 // 不回存(or, 其中一個成立) // 1. Trigger 成功 // 2. Current State 具同名 Transition (不論 Guard 成功與否) // 即=>回存(and, 全部成立) // 1. Trigger失敗 // 2. Curr State 不具同名 Transition // 3. 其它State具有同名 DeferralTransition //找出同名且不在Current State裡的Defferal的數量 var countDefferalOfNotInCurr = (from row in trans where row is DeferralTransition select row).Count(); if (currTrans.Count == 0 && countDefferalOfNotInCurr > 0) { SaveDeferralEvent(param);//TODO: 確認加回最後一個是否合理 } //Deferral 請先確認是否有在CURRENT STATE有在的話,不管GUARD的狀況與否都不會重新放回JOB LIST //bool noTransitionInCurrentState = true; //foreach (Transition t in trans.Where(t => t is DeferralTransition)) //{ // if (((DeferralTransition)t).IsInCurrentState(this)) // { // noTransitionInCurrentState = false; // } //} //if (noTransitionInCurrentState) //{ // SaveDeferralEvent(param); //} //else //{ // //執行Defferal transition on not current state (save to event queue) // foreach (Transition t in trans.Where(t => t is DeferralTransition)) // { // if (t.RunGuard(this)) // { // if (runTriggerSingle == null) // { // runTriggerSingle = (DeferralTransition)t; // } // else // { // throw new PluralTriggerSameTimeException(this.GetType().ToString() + " : current is " + this.CurrentState.StateName + " with Transition : " + param.TransName + ", trigger more than one same time"); // } // } // } // runTriggerSingle.RunTrigger(param, this); //} } if (executeFlag) { return(EnumStateMachineMsgType.CommandSuccessful); } else { //run deferral or strange transition /*foreach(Transition t in (from transObj in transitions where transObj.TransName.Equals(param.TransName.ToString()) && !currentState.transs.Contains(transObj) select transObj)) * { * t.RunTrigger(param, this); * }*/ return(EnumStateMachineMsgType.GuardFail); } } catch (PluralTriggerSameTimeException ex) { PopupAlarm(this, EnumStateMachineMsgType.PluralTriggerSameTime, "TransitionPluralTrigger"); MvLog.Write(ex, MvLoggerEnumLevel.Warn); return(EnumStateMachineMsgType.PluralTriggerSameTime); } catch (NotCurrentStateException ex) { //Transition nocurTrans = GetDynamicExceptionTransition(CurrentState, EnumStateMachineMsgType.NotCurrentStateTransition); //nocurTrans.RunTrigger(new StateParameter(), this); MvLog.Write(ex, MvLoggerEnumLevel.Error); return(EnumStateMachineMsgType.StateMachineException); } catch (StateMachineException ex) { System.Diagnostics.Debug.WriteLine(this.ID + ":Error"); MvLog.Write(ex, MvLoggerEnumLevel.Error); throw ex; } catch (Exception ex) { MvLog.Write(ex, MvLoggerEnumLevel.Error); throw ex; } }