コード例 #1
0
        private async Task ProcessMessage(string message)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(message))
                {
                    return;
                }

                var json = JsonObject.Parse(message);

                var highestInent = GetHighestInent(json);

                if (string.IsNullOrWhiteSpace(highestInent))
                {
                    return;
                }

                switch (highestInent.ToLower())
                {
                case "stop":
                    _twoMotorsDriver.Stop();
                    _isMovingForward = false;
                    break;

                case "moveforward":
                    _twoMotorsDriver.MoveForward();
                    _isMovingForward = true;
                    break;

                case "movebackward":
                    _twoMotorsDriver.MoveBackward();
                    _isMovingForward = false;
                    break;

                case "turn":

                    var direction = GetHighestEntity(json);
                    if (!string.IsNullOrWhiteSpace(direction))
                    {
                        if (direction == "right")
                        {
                            await _twoMotorsDriver.TurnRightAsync();
                        }
                        else
                        {
                            await _twoMotorsDriver.TurnLeftAsync();
                        }
                    }
                    _isMovingForward = false;
                    break;
                }
            }
            catch (Exception ex)
            {
                await WriteLog(ex.Message);
            }
        }
コード例 #2
0
        private async void DoWork(object sender, DoWorkEventArgs e)
        {
            while (_worker != null && !_worker.CancellationPending)
            {
                var result = await ServiceBusHelper.ReceiveAndDeleteMessage();

                switch (result)
                {
                case "SMR":
                    await _uln2003Driver.TurnAsync(90, TurnDirection.Right);

                    break;

                case "SML":
                    await _uln2003Driver.TurnAsync(90, TurnDirection.Left);

                    break;

                case "F":
                    _twoMotorsDriver.MoveForward();
                    isMovingForward = true;
                    break;

                case "S":
                    _twoMotorsDriver.Stop();
                    isMovingForward = false;
                    break;

                case "R":
                    await _twoMotorsDriver.TurnRightAsync();

                    isMovingForward = false;
                    break;

                case "L":
                    await _twoMotorsDriver.TurnLeftAsync();

                    isMovingForward = false;
                    break;

                case "B":
                    _twoMotorsDriver.MoveBackward();
                    isMovingForward = false;
                    break;

                case "TR":
                    await _twoMotorsDriver.TurnRightAsync(45);

                    isMovingForward = false;
                    break;

                case "TL":
                    await _twoMotorsDriver.TurnLeftAsync(45);

                    isMovingForward = false;
                    break;

                case "BR":
                    await _twoMotorsDriver.TurnRightAsync(100);

                    isMovingForward = false;
                    break;

                case "BL":
                    await _twoMotorsDriver.TurnLeftAsync(100);

                    isMovingForward = false;
                    break;
                }

                await Task.Delay(1000);
            }
        }