示例#1
0
        private void MakeDiscoverable(string name)
        {
            if (_ssdpServer == null)
            {
                var _configuration = new UPNPConfiguration()
                {
                    UdpListnerPort  = _uPnPListenerPort,
                    FriendlyName    = name,
                    Manufacture     = "Tampa IoT Dev",
                    ModelName       = Constants.TankBotModelName,
                    DefaultPageHtml = @"<html>
<head>
<title>SoccerBot</title>
<link rel=""stylesheet"" href=""https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"" integrity=""sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"" crossorigin=""anonymous"">
</head>
<body>
<h1>Tampa IoT TankBot SSDP (uPnP) Listener Page</h1>
</body>
</html>"
                };

                try
                {
                    _ssdpServer = NetworkServices.GetSSDPServer();
                    _ssdpServer.MakeDiscoverable(9500, _configuration);
                }
                catch (Exception ex)
                {
                    _logger.NotifyUserError("ConnectionManager_MakeDiscoverable", ex.Message);
                }
            }
        }
示例#2
0
        public void ReceiveData()
        {
            _cancelListenerSource = new CancellationTokenSource();
            _listenerTask         = new Task(async() =>
            {
                var running = true;
                while (running)
                {
                    try
                    {
                        var readTask = _reader.ReadAsync(_readBuffer, 0, MAX_BUFFER_SIZE);
                        readTask.Wait(_cancelListenerSource.Token);
                        var bytesRead = await readTask;

                        var byteBuffer = _readBuffer.ToByteArray(0, bytesRead);
                        _parser.Parse(byteBuffer);
                    }
                    catch (OperationCanceledException)
                    {
                        running = false;
                        /* Task Cancellation */
                    }
                    catch (Exception ex)
                    {
                        running = false;
                        _logger.NotifyUserError("Client_Listening", ex.Message);
                    }
                }
            });

            _listenerTask.Start();
        }
示例#3
0
        public async override Task <bool> ConnectAsync()
        {
            try
            {
                await _port.OpenAsync();

                State   = States.Connecting;
                _reader = new StreamReader(_port.InputStream);
                _writer = new StreamWriter(_port.OutputStream);

                StartListening(_reader);
                State = States.Connected;
                return(true);
            }
            catch (Exception ex)
            {
                _logger.NotifyUserError("SerialPortChannel_ConnectAsync", ex.Message);
                if (_reader != null)
                {
                    _reader.Dispose();
                    _reader = null;
                }

                if (_writer != null)
                {
                    _writer.Dispose();
                    _writer = null;
                }

                return(false);
            }
        }
示例#4
0
 public async void Listen()
 {
     try
     {
         // keep reading the serial input
         while (true)
         {
             await ReadAsync(_readCancellationTokenSource.Token);
         }
     }
     catch (TaskCanceledException)
     {
         _logger.NotifyUserInfo("SerialChannel_Listen", "Task Cancelled - Closing");
     }
     catch (Exception ex)
     {
         _logger.NotifyUserError("SerialChannel_Listen", ex.Message);
     }
     finally
     {
         // Cleanup once complete
         if (_dataReaderObject != null)
         {
             _dataReaderObject.DetachStream();
             _dataReaderObject = null;
         }
     }
 }
示例#5
0
        public override async Task <bool> ConnectAsync()
        {
            _socket = new StreamSocket();

            State = States.Connecting;

            try
            {
                await _socket.ConnectAsync(_deviceService.ConnectionHostName, _deviceService.ConnectionServiceName);

                _writer = new DataWriter(_socket.OutputStream);

                var dataReader = new DataReader(_socket.InputStream);
                dataReader.InputStreamOptions = InputStreamOptions.Partial;
                ReceiveDataAsync(dataReader);
                State = States.Connected;
                ConnectCommand.Enabled = false;
                _logger.NotifyUserInfo("BT Channel", "Connected!");
                return(true);
            }
            catch (Exception ex)
            {
                _logger.NotifyUserError("BT Channel", ex.Message);
                return(false);
            }
        }
示例#6
0
        private async void SendMessage(mBlockOutgoingMessage msg)
        {
            lock (_messageHandlers)
            {
                if (msg.Handler != null)
                {
                    _messageHandlers.Add(msg);
                }
            }

            try
            {
                OutgoingMessages.Add(msg);
                _logger.NotifyUserInfo("mBlock", $">>> {msg.MessageSerialNumber:X2}. " + msg.MessageHexString);
                await _channel.WriteBuffer(msg.Buffer);
            }
            catch (Exception ex)
            {
                _logger.NotifyUserError("mBlock_SendMessage", ex.Message);
                _logger.NotifyUserInfo("mBlock", "DISCONNECTED!");
                _channel.State = States.Disconnected;
            }
        }
示例#7
0
        public async void StartListening()
        {
            try
            {
                _listener = new StreamSocketListener();
                _listener.ConnectionReceived += _listener_ConnectionReceived;

                _logger.NotifyUserInfo("TCPIP Listener", $"Started Listening on Port {_port}");
                await _listener.BindServiceNameAsync(_port.ToString());
            }
            catch (Exception ex)
            {
                if (_listener != null)
                {
                    _listener.Dispose();
                    _listener = null;
                }
                _logger.NotifyUserError("TCPIP Listener", ex.Message);
            }
        }
示例#8
0
        public void ClientConnected(StreamSocket socket)
        {
            _tankBot.PlayTone(400);
            _tankBot.SetLED(0, NamedColors.Green);

            lock (_clients)
            {
                var client = Client.Create(socket, _logger);
                client.MessageRecevied += Client_MessageRecevied;
                int attempts = 0;
                while (!_clients.TryAdd(client.Id, client) && attempts++ < 5)
                {
                    SpinWait.SpinUntil(() => false, 5);
                }

                if (attempts == 5)
                {
                    _logger.NotifyUserError("Server_ClientConnected", "Could not add client after 5 attempts.");
                }

                client.StartListening();
            }
        }
示例#9
0
        private async void SendMessage(mBlockOutgoingMessage msg)
        {
            lock (_messageHandlers)
            {
                if (msg.Handler != null)
                {
                    _messageHandlers.Add(msg);
                }
            }

            try
            {
                OutgoingMessages.Add(msg);
                //                Debug.WriteLine(String.Format("{0:000000} >>>", (DateTime.Now - _start).TotalMilliseconds) + msg.MessageHexString);
                _logger.NotifyUserInfo("mBlock", ">>> " + msg.MessageHexString);
                await _channel.WriteBuffer(msg.Buffer);
            }
            catch (Exception ex)
            {
                _logger.NotifyUserError("mBlock_SendMessage", ex.Message);
                _logger.NotifyUserInfo("mBlock", "DISCONNECTED!");
                _channel.State = States.Disconnected;
            }
        }