/// <summary> /// 请求执行任务 /// </summary> /// <param name="type"></param> /// <param name="value"></param> public void Request(TaskActionType type, object value) { if (RunMode == RunModeType.Normal) { TaskQueueObject tqo = new TaskQueueObject(); tqo.ActionType = type; tqo.Value = value; TaskQueues.Enqueue(tqo); } else { try { if (MainService.MainUIObj.IsHandleCreated) { MainService.MainUIObj.Invoke(new MethodInvoker(delegate() { if (MainService.VoiceUIObj != null) { MainService.VoiceUIObj.ChatPanel.AddMachineMsg(SuperObject.Config.DebugModeHintText); } })); } } catch (Exception ex) { System.Console.WriteLine(ex.ToString()); } } }
void _worker_DoWork(object sender, DoWorkEventArgs e) { while (!_scanWorker.CancellationPending) { try { if (RunMode == RunModeType.Normal) { lock (StaticActionList) { if (StaticActionList != null) { TaskQueueObject queueObj = null; TaskQueues.TryDequeue(out queueObj); if (queueObj != null) { foreach (Robot_Actions action in StaticActionList) { //检查是否符合条件 if (IsAcceptRun(queueObj, action)) { //发送指令序列 RunAction(action, DBInstance.GetSteps(action.Id)); } } } } } } } catch (Exception ex) { System.Console.WriteLine(ex.ToString()); } try { Thread.Sleep(15); } catch (Exception ex) { } } }
/// <summary> /// 检查是否同意运行 /// </summary> /// <param name="action"></param> /// <returns></returns> public bool IsAcceptRun(TaskQueueObject queueObj, Robot_Actions action) { if (action != null && queueObj != null && queueObj.Value != null) { if (string.IsNullOrEmpty(action.Condition)) { return(false); } else { bool result = false; switch (queueObj.ActionType) { case TaskActionType.Voice: if (action.Condition.StartsWith("语音=")) { result = action.Condition.Replace("语音=", string.Empty).Equals(queueObj.Value.ToString()); } break; case TaskActionType.Angle: if (action.Condition.StartsWith("角度=")) { string angleRangeStr = action.Condition.Replace("角度=", string.Empty); string[] temps = angleRangeStr.Split(','); if (temps != null && temps.Length >= 2) { short min = 0; short max = 0; try { min = short.Parse(temps[0]); } catch (Exception ex) { } try { max = short.Parse(temps[1]); } catch (Exception ex) { } short angle = short.Parse(queueObj.Value.ToString()); result = angle >= min && angle <= max; } } break; case TaskActionType.Joy: if (action.Condition.StartsWith("手柄=")) { result = action.Condition.Replace("手柄=", string.Empty).Equals(queueObj.Value.ToString()); } break; } return(result); } } else { return(false); } }