Esempio n. 1
0
        static void outputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (_cmd == null)
            {
                return;
            }

            Console.WriteLine(String.Format("output handler {0} : {1}", _state, outLine.Data));
            Log += "\n" + outLine.Data;

            switch (outLine.Data)
            {
            case "_INITIALIZED":
                _state = CmdState.Initialized;
                break;

            case "_INPUTTING":
                _state = CmdState.Inputting;
                break;

            case "_PROCESSING":
                _state = CmdState.Processing;
                break;

            case "_QUIT":
                _state = CmdState.Quit;
                break;

            default:
                break;
            }
        }
Esempio n. 2
0
        public CmdState SendCmdToMcuAndRead(bool syncInnerMsg, string cmd, ref string data, ref List <string> innerText, Func <string, bool> chk = null, int timeout = 20000)
        {
            CmdState cmdState = CmdState.Fail;

            _slimForSerial.WaitOne(Timeout.Infinite);
            data = string.Empty;
            innerText.Clear();
            while (syncInnerMsg && _revQueue.TryDequeue(out data))
            {
            }
            data = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(cmd))
                {
                    Console.WriteLine($"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++{cmd}");
                    SendCmdToMcu(cmd);
                }
                var sw = new Stopwatch();
                sw.Start();
                while (sw.ElapsedMilliseconds < timeout)
                {
                    var str = RecvString(_innerTimeout);
                    if (!string.IsNullOrEmpty(str))
                    {
                        if (chk == null || chk(str))
                        {
                            data = str;
                            break;
                        }
                        else
                        {
                            innerText?.Add(str);
                            if (syncInnerMsg)
                            {
                                _revQueue.Enqueue(str);
                            }
                        }
                    }
                }
                sw.Stop();

                while (syncInnerMsg && !_revQueue.IsEmpty)
                {
                    Thread.Sleep(1);
                }

                if (!string.IsNullOrEmpty(data))
                {
                    cmdState = CmdState.Success;
                }
            }
            catch (Exception e)
            {
                _log.Error($"Exception -> {e.Message}");
            }
            _slimForSerial.Set();

            return(cmdState);
        }
Esempio n. 3
0
    public static string GeneratePlayerCommandsMessage(CmdState cmd)
    {
        NetworkingMessage msg = new NetworkingMessage(NetworkingMessageType.CMDS);

        msg.content = ToJson(cmd);
        return(ToJson(msg));
    }
Esempio n. 4
0
        public static void Start(string envName, string scriptPath, int deviceID, string ckptPath, string outputPath, int sleepInterval)
        {
            try
            {
                if (_state != CmdState.None)
                {
                    Stop();
                }

                Log                  = "";
                _state               = CmdState.None;
                _envname             = envName;
                _scriptpath          = scriptPath;
                _deviceid            = deviceID;
                _ckptpath            = ckptPath;
                _outpath             = outputPath;
                _sleepinterval       = sleepInterval;
                _thread              = new Thread(new ThreadStart(ofThread));
                _thread.IsBackground = true;
                _thread.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 切换巡检状态:0/采集记录(暂停巡检),1/巡检
        /// </summary>
        /// <param name="stateChk">巡检类型</param>
        private void reChecking(int stateChk)
        {
            if (null == this.cmdGather)
            {
                return;
            }
            CmdState st = this.cmdGather.CheckState();

            if (CmdState.Response == st || CmdState.Request == st)
            {
                this.cmdGather.EventWh.Reset();
                this.cmdGather.EventWh.WaitOne(cmdGather.TimeOut, false);
            }
            this.cmdGather.TimeSendInv = new TimeSpan(24, 0, 0);
            this.commimgr.ClearBuffer(this.target);
            this.stateChecking = stateChk;
            string tag = "@设备地址=" + Convert.ToString(this.station);

            if (1 == stateChk)
            {
                this.cmdGather.setCommand("消费", "检测状态", tag);
            }
            else
            {
                this.cmdGather.setCommand(new byte[0]);
            }
            if (0 < stateChk)
            {
                this.cmdGather.TimeSendInv = this.tsinv;
                this.commimgr.SendCommand(this.target, this.cmdGather);
            }
        }
Esempio n. 6
0
        private static void ofThread()
        {
            _state = CmdState.None;

            string           initcommand = generateInitCommand(_scriptpath, _deviceid, _ckptpath, _outpath);
            ProcessStartInfo info        = new ProcessStartInfo(_CMDPATH);

            info.RedirectStandardInput  = true;
            info.RedirectStandardOutput = true;
            info.CreateNoWindow         = false;
            info.UseShellExecute        = false;

            _cmd = Process.Start(info);
            _cmd.OutputDataReceived += outputHandler;
            _cmd.ErrorDataReceived  += outputHandler;

            //..initialization
            Console.WriteLine("activating: " + _envname);
            _cmd.StandardInput.Write(" activate " + _envname + "\n");
            _cmd.StandardInput.Flush();
            _cmd.BeginOutputReadLine();
            Console.WriteLine(_envname + " activated");

            _cmd.StandardInput.WriteLine(initcommand);
            _cmd.StandardInput.Flush();
            while (!(_state == CmdState.Initialized || _state == CmdState.Inputting))
            {
                Thread.Sleep(_sleepinterval);
            }
            Console.WriteLine("initialize completed");
            //..initialization

            //while (_state != CmdState.Quit)
            //{
            //    while (_state != CmdState.Inputting)
            //    {
            //        Thread.Sleep(_sleepinterval);
            //        Console.WriteLine(_state);
            //    }

            //    Thread.Sleep(_sleepinterval);
            //    while (string.IsNullOrEmpty(InputPath))
            //        Thread.Sleep(_sleepinterval);

            //    _cmd.StandardInput.WriteLine(InputPath);
            //    _cmd.StandardInput.Flush();
            //    InputPath = "";
            //    Thread.Sleep(_sleepinterval);
            //    while (_state == CmdState.Processing)
            //        Thread.Sleep(_sleepinterval);
            //}

            _cmd.WaitForExit();
        }
Esempio n. 7
0
        void OnMidiInput(MidiDevice Device, int DeviceIdx, int ControlId, int Data, int Status, int Voice, int Channel)
        {
            Device.latestControlID = ControlId;  //-W2PA used when making PL-1 LEDs respond to control action
            try
            {
                ControllerBinding ctrlBinding;
                if (bindings.TryGetValue(DeviceIdx, out ctrlBinding))
                {
                    MidMessageHandler handlers;
                    CmdState          state = CmdState.NoChange;

                    if (ctrlBinding.CmdBindings.TryGetValue(ControlId, out handlers))
                    {
                        if (handlers != null)
                        {
                            lock (midi_handler_lock)
                            {
                                if (handlers.CmdHandler != null)
                                {
                                    handlers.CmdHandler(Data, Device);
                                    if (Data <= 0)
                                    {
                                        state = CmdState.Off;
                                    }
                                    else
                                    {
                                        state = CmdState.On;
                                    }
                                }
                                else if (handlers.ToggleCmdHandler != null)
                                {
                                    state = handlers.ToggleCmdHandler(Data, Device);
                                }

                                if (state == CmdState.On && handlers.MidiOutCmdDown != null)
                                {
                                    Device.SendMsg(Channel, 0x7F, Status, ControlId, handlers.MidiOutCmdDown);
                                }
                                if (state == CmdState.Off && handlers.MidiOutCmdUp != null)
                                {
                                    Device.SendMsg(Channel, 0x00, Status, ControlId, handlers.MidiOutCmdUp);
                                }
                                if (handlers.MidiOutCmdSetValue != null)
                                {
                                    Device.SendMsg(Channel, Data, Status, ControlId, handlers.MidiOutCmdSetValue);
                                }
                            }
                        }
                    }
                }
            }
            catch { }
        }
Esempio n. 8
0
        public static void Stop()
        {
            if (_thread != null)
            {
                _thread.Abort();
            }

            if (_cmd != null)
            {
                _cmd.Close();
                _cmd = null;
            }

            _state = CmdState.None;
        }
Esempio n. 9
0
        /// <summary>
        /// 切换巡检状态:0/停止巡检,1/启动巡检
        /// </summary>
        /// <param name="stateChk">巡检类型,0停止巡检,其他值会内部自动识别恢复巡检</param>
        private void reChecking(int stateChk)
        {
            if (0 != stateChk)
            {
                stateChk = this.isCardIC ? 2 : 1;
            }
            CmdState st = this.CheckState();

            if (CmdState.Response == st || CmdState.Request == st || CmdState.ReqTimeout == st)
            {
                this.EventWh.Reset();
                this.EventWh.WaitOne(this.TimeOut, false);
            }
            this.TimeSendInv = new TimeSpan(24, 0, 0);
            this.commiMgr.ClearBuffer(this.targetwr);
            this.stateChecking = stateChk;
            string tagdata = "@设备地址=" + this.addrst;

            if (1 == stateChk)
            {
                this.setCommand("卡务中心", "脱机", tagdata);
                this.ResetState();
                this.commiMgr.SendCommand(this.targetwr, this);
                this.EventWh.WaitOne(this.waitTime, false);
                this.setCommand("卡务中心", "读卡", tagdata);
            }
            else if (2 == stateChk)
            {
                this.setCommand("卡务中心", "防冲突", tagdata);
            }
            else
            {
                this.setCommand(new byte[0]);
            }
            this.ResetState();
            if (this.getCommand().Length < 1)
            {
                this.ResponseDatetime = this.ResponseDatetime.AddMilliseconds(1);
            }
            if (0 < stateChk)
            {
                this.TimeSendInv = new TimeSpan(3000000);
                this.commiMgr.SendCommand(this.targetwr, this);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// 切换巡检状态:0/采集记录(暂停巡检),1/巡检
        /// </summary>
        /// <param name="stateChk">巡检类型</param>
        private void reChecking(int stateChk)
        {
            CmdProtocol cmd = this.cmdGather;

            if (null == cmd)
            {
                return;
            }
            CmdState st = cmd.CheckState();

            cmd.TimeSendInv   = new TimeSpan(24, 0, 0);
            cmd.TimeLimit     = new TimeSpan(4 * cmd.TimeOut.Ticks);
            cmd.TimeFailLimit = cmd.TimeLimit;
            if (CmdState.Response == st || CmdState.Request == st || CmdState.ReqTimeout == st)
            {
                cmd.EventWh.Reset();
                cmd.EventWh.WaitOne(cmd.TimeOut, false);
            }
            this.commimgr.ClearBuffer(this.target);
            this.stateChecking = stateChk;
            string tag = "@设备地址=" + Convert.ToString(this.station);

            if (1 == stateChk)
            {
                cmd.setCommand("门禁", "检测状态", tag);
            }
            else
            {
                cmd.setCommand(new byte[0]);
            }
            if (0 < stateChk)
            {
                cmd.TimeSendInv     = this.tsinv;
                cmd.TimeLimit       = TimeSpan.MaxValue;
                cmd.TimeFailLimit   = TimeSpan.MaxValue;
                cmd.FailProAf       = FailAftPro.Ignor;
                cmd.TimeSendInv     = this.tsinv;
                cmd.IsResposeHandle = isResponse;
                this.commimgr.SendCommand(this.target, cmd);
            }
            Debug.WriteLine("巡检类型:" + stateChk);
        }
Esempio n. 11
0
    public bool AddPlayersCommands(CmdState cmd, int playerID)
    {
        bool valid = true;

        foreach (ShipCmdState c in cmd.cmds)
        {
            // TODO
            // CHECK IF SHIP EXISTS
            ShipController sc = GetShipController(c.shipID);

            if (sc.shipOwner == playerID)
            {
                // TODO
                // CHECK IF VALID SHIP CMD

                // CHECK IF CMDS FOR SHIP ALREADY EXIST
                if (GetShipCommand(sc.shipID) != null)
                {
                    valid = false;
                    Debug.LogWarning("Command already set for this ship");
                }

                // SHIP ALIVE?
                // SHIP HAVE ENOUGH ENERGY?
                // CAN SHIP PERFORM ACTION?
                // CAN SHIP PERFORM MOVE?
                shipCommands.Add(new ShipCommand(c));
            }
            else
            {
                valid = false;

                Debug.LogWarning("Invalid ship owner");
            }
        }

        return(valid);
    }
Esempio n. 12
0
        public override CommandState GetNextState()
        {
            switch (m_CurrentState)
            {
                case CmdState.eStart:
                    m_CurrentState = CmdState.eCenterPoint;

                    return new PointInferenceState();

                case CmdState.eCenterPoint:
                    if (m_StateMgr.DataItems.Count > 0)
                    {
                        PositionDataItem Pos = (PositionDataItem)m_StateMgr.DataItems.Back();
                        m_Instance.CenterPoint = Pos.Point;

                        m_CurrentState = CmdState.eRaduis;
                    }

                    return new PointInferenceState();

                default:
                    if (m_StateMgr.DataItems.Count > 1)
                    {
                        PositionDataItem Pos = (PositionDataItem)m_StateMgr.DataItems.Back();
                        m_Instance.SetRaduis(Pos.Point);

                        m_CurrentState = CmdState.eRaduis;
                    }
                    else
                    {
                        Debug.Assert(false);

                        return new PointInferenceState();
                    }

                    return null;
            }
        }
Esempio n. 13
0
    void HandlePlayerCommands(ConnectionObject co, NetworkingMessage inmsg)
    {
        CmdState cmd = NetworkingMessageTranslator.ParseShipCommands(inmsg.content);

        if (state == ServerState.COMMAND)
        {
            bool valid = gr.AddPlayersCommands(cmd, co.serverPlayerID);

            if (!valid)
            {
                //TODO
                // Send msg back to client saying cmd are invalid
            }

            // CHECK IF ALL PLAYER CMDS ARE IN
            // IF SO BEGIN PROCESSING
            if (ReadyToProcess())
            {
                state = ServerState.PROCESSING;
                Debug.Log("Beginning to process gamestate");
            }
        }
    }
Esempio n. 14
0
    public void EndTurn()
    {
        // Send ship commands to server

        if (!canUpdateCmds)
        {
            return;
        }

        uiController.EnableEndUI(false);

        enableTurnTimer = false;
        uiController.EnableTurnTimerText(false);

        List <ShipCmdState> myCommands = new List <ShipCmdState>();

        foreach (ShipCommand c in shipCommands)
        {
            if (GetShipController(c.shipID).shipOwner == playerID)
            {
                myCommands.Add(c.GetShipCmdState());
            }
        }

        CmdState cmd = new CmdState(myCommands);

        canUpdateCmds = false;
        if (endTurnCallback != null)
        {
            endTurnCallback(cmd);
        }
        else
        {
            Debug.LogWarning("End turn callback was null...");
        }
    }
Esempio n. 15
0
    void SendPlayerCommands(CmdState cmd)
    {
        string request = NetworkingMessageTranslator.GeneratePlayerCommandsMessage(cmd);

        SendMsg(request);
    }
Esempio n. 16
0
        protected override bool OnActive()
        {
            m_CurrentState = CmdState.eStart;

            return base.OnActive();
        }
        public void DiffuseCommand(Entity command, Entity commandResult, bool defaultResult, CmdState state)
        {
            commandResult.SetComponentData(new EntityCommandResult {
                IsAuthorized = Convert.ToByte(defaultResult)
            });

            foreach (var ev in AppEvent <StEventDiffuseCommand.IEv> .eventList)
            {
                AppEvent <StEventDiffuseCommand.IEv> .Caller = this;

                ev.OnCommandDiffuse(new StEventDiffuseCommand.Arguments(command, commandResult, state));
            }
        }