Example #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);
            }
        }
Example #2
0
        private RestRequest MakeRequest(BoardStatus status)
        {
            RestRequest restRequest = new RestRequest("status", Method.POST);

            restRequest.RequestFormat = DataFormat.Json;
            restRequest.AddBody(new DTO.Status(status, _ic));
            return(restRequest);
        }
Example #3
0
 public void SendStatus(BoardStatus boardStatus)
 {
     if (_socket.Io().ReadyState != Manager.ReadyStateEnum.OPEN)
     {
         throw new Exception("Socket isn't ready.");
     }
     _socket.Emit("status", JsonConvert.SerializeObject(boardStatus));
 }
Example #4
0
        public BoardStatus ReadBoardStatus(ref string errMsg)
        {
            try
            {
                BoardStatus boardStatus = new BoardStatus();
                if (!hldMainBoard.OpenSerialPort(_config.PortName, BAUD_RATE, ref errMsg))
                {
                    return(null);
                }

                boardStatus.PowerStatus = hldMainBoard.GetPowerStatus(ref errMsg);
                if (!string.IsNullOrEmpty(errMsg))
                {
                    return(null);
                }
                else if (boardStatus.PowerStatus != 0)
                {
                    errMsg = "Power is down";
                    return(null);
                }

                var list = GetAllSafeStatus(ref errMsg);
                if (!string.IsNullOrEmpty(errMsg))
                {
                    return(null);
                }

                boardStatus.SafeStatuss = list;
                return(boardStatus);
            }
            catch (Exception ex)
            {
                errMsg = ex.ToString();
                return(null);
            }
            finally
            {
                CloseSerialPort();
            }
        }