예제 #1
0
        private async Task SendPingsOnSchedule(CancellationToken cancellationToken)
        {
            try {
                BlynkLogManager.LogMethodBegin(nameof(SendPingsOnSchedule));
                while (!cancellationToken.IsCancellationRequested)
                {
                    try {
                        if (this.Connected)
                        {
                            BlynkLogManager.LogInformation("ping");
                            var pingCommand = new BlynkCommand(BlynkCommandType.BLYNK_CMD_PING, NextMessageId, false);

                            pingCommand.Append(( Int16 )0);

                            await this.SendAsync(pingCommand.ToArray(), cancellationToken);
                        }
                        else
                        {
                            BlynkLogManager.LogInformation("not connected");
                        }
                        await Task.Delay(this.PingIntervalInMilliseconds, cancellationToken);
                    }
                    catch (TaskCanceledException) { }
                    catch (Exception ex) {
                        BlynkLogManager.LogException("Background ping sender", ex);
                    }
                }
                BlynkLogManager.LogInformation("canceled");
            }
            finally {
                BlynkLogManager.LogMethodEnd(nameof(SendPingsOnSchedule));
            }
        }
예제 #2
0
        internal async Task <bool> SendResponseAsync(UInt16 originalMessageId, BlynkResponse response = BlynkResponse.OK)
        {
            try {
                BlynkLogManager.LogMethodBegin(nameof(SendResponseAsync));
                var command = new BlynkCommand(BlynkCommandType.BLYNK_CMD_RESPONSE, originalMessageId, false);

                command.Append(( byte )0)
                .Append(( byte )response);

                return(await this.SendAsync(command.ToArray(), backgroundCancellationTokenSource.Token));
            }
            finally {
                BlynkLogManager.LogMethodEnd(nameof(SendResponseAsync));
            }
        }
예제 #3
0
        public async Task <bool> ConnectAsync(CancellationToken cancellationToken)
        {
            try {
                BlynkLogManager.LogMethodBegin(nameof(ConnectAsync));
                this.messageID = 1;

                await this.tcpClient.ConnectAsync(this.host, this.port);

                if (this.tcpClient.Connected)
                {
                    this.tcpClient.NoDelay = true;
                    this.tcpStream         = this.tcpClient.GetStream();

                    //if( this.withSSL ) {
                    //	this.sslStream = new SslStream( this.tcpStream );
                    //	await this.sslStream.AuthenticateAsClientAsync( this.host );
                    //}

                    using (var loginCommand = new BlynkCommand(BlynkCommandType.BLYNK_CMD_LOGIN, this.NextMessageId)) {
                        loginCommand.Append(this.authentication, this.authentication.Length);

                        var dummy2 = Task.Run(() => this.GetMessageOnSchedule(this.backgroundCancellationTokenSource.Token));

                        await this.SendAsync(loginCommand.ToArray(), cancellationToken);
                    }

                    var dummy = Task.Run(() => this.SendPingsOnSchedule(this.backgroundCancellationTokenSource.Token));

                    return(true);
                }
                return(false);
            }
            finally {
                BlynkLogManager.LogMethodEnd(nameof(ConnectAsync));
            }
        }