Beispiel #1
0
        public async Task Loop()
        {
            string      errMsg = string.Empty;
            BoardStatus oldStatus = null, latestStatus;

            if (!TestBoard(_config.PortName, ref _ic, ref version, ref errMsg))
            {
                AppLog.Error("{0}. {1}", "Connect to board fail", errMsg);
                await Task.Delay(ERROR_DELAY);

                return;
            }
            PrintBoardInfo(_ic, version);

            AppLog.Info("Authenticating...");
            _jwt = await Login(_ic, _config.Secret);

            AppLog.Info("Authenticated");

            await Subscribe(_jwt);

            while (true)
            {
                latestStatus = ReadBoardStatus(ref errMsg);
                if (latestStatus is null || !string.IsNullOrEmpty(errMsg))
                {
                    AppLog.Error("{0}. {1}", "Can't read board's status", errMsg);
                    break;
                }

                if (latestStatus != oldStatus)
                {
                    Console.WriteLine("Previous status:");
                    Console.WriteLine(oldStatus);
                    Console.WriteLine("New status:");
                    Console.WriteLine(latestStatus);
                    SendStatus(latestStatus);
                    oldStatus = latestStatus;
                }

                if (IsCommandAvailable())
                {
                    lock (CommandsQueue)
                    {
                        var safe = CommandsQueue.Dequeue();
                        if (!ExecuteCommand(safe, ref errMsg))
                        {
                            AppLog.Error("{0}. {1}", "Execute command fail", errMsg);
                        }
                        else
                        {
                            AppLog.Info("Opened Safe:{0}", safe);
                        }
                    }
                }

                await Task.Delay(LOOP_DELAY);
            }
        }
        private void Bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = sender as BackgroundWorker;

            while (true)
            {
                if (bw.CancellationPending)
                {
                    break;
                }
                Thread.Sleep(5);

                //如果没有指令,继续等待
                if (CommandsQueue.Count == 0)
                {
                    Thread.Sleep(50);
                    continue;
                }

                //从队列中将指令取出
                CommandInfo info  = CommandsQueue.Dequeue() as CommandInfo;
                string      value = info.Command.Trim('\0');
                //SetValue(value, info.Client.Client);

                //如果设备未连接,通知客户端
                if (testDevice == null || !testDevice.IsOpen)
                {
                    ServerResultMessage _ServerResultMessage = new ServerResultMessage()
                    {
                        CommandType = CommandType.None,
                        ResultCode  = ResultCode.fail,
                        Data        = "device is disconnect.",
                    };
                    SendData(info, JsonConvert.SerializeObject(_ServerResultMessage));
                    Thread.Sleep(1000);
                    continue;
                }

                ResetType result = ResetType.None;
Start:
                try
                {
                    Command(value, info);
                }
                catch (MotorException ex)
                {
                    Loghelper.WriteLog("发送指令:", ex);
                    switch (result)
                    {
                    case ResetType.None:
                        lock (deviceLock)
                            result = device.ResetComUSB();
                        if (result == ResetType.USBSuccess || result == ResetType.Success)
                        {
                            goto Start;
                        }
                        break;

                    case ResetType.USBSuccess:
                        lock (deviceLock)
                            result = device.ResetCom();
                        if (result == ResetType.Success)
                        {
                            goto Start;
                        }
                        break;

                    case ResetType.Error:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Loghelper.WriteLog("下发错误", ex);
                    ServerResultMessage _ServerResultMessage = new ServerResultMessage()
                    {
                        CommandType = CommandType.None,
                        ResultCode  = ResultCode.fail,
                        Data        = ex.Message,
                    };
                    SendData(info, JsonConvert.SerializeObject(_ServerResultMessage));
                }
                //SetValue(string.Format("队列还剩【{0}】条指令未下发", CommandsQueue.Count), null);
            }
        }