Ejemplo n.º 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));
            }
        }
Ejemplo n.º 2
0
        internal async Task <bool> ReceiveAsync(CancellationToken cancellationToken)
        {
            byte[] receiveBuffer = null;
            try {
                BlynkLogManager.LogMethodBegin(nameof(ReceiveAsync));
                if (this.tcpStream.DataAvailable)
                {
                    BlynkLogManager.LogInformation("data available");

                    receiveBuffer = Utility.ArrayManager <byte> .GetArray(this.tcpClient.Available);                     // new byte[ this.tcpClient.Available ];

                    int count = await this.tcpStream.ReadAsync(receiveBuffer, 0, receiveBuffer.Length, cancellationToken);

                    var commands = BlynkMessageParser.GetBlynkMessages(receiveBuffer, count);

                    return(await this.ProcessMessagesAsync(commands));
                }
                //else {
                //	Console.Write( "." );
                //}
                return(false);
            }
            finally {
                if (receiveBuffer != null)
                {
                    Utility.ArrayManager <byte> .ReleaseArray(receiveBuffer);

                    receiveBuffer = null;
                }

                BlynkLogManager.LogMethodEnd(nameof(ReceiveAsync));
            }
        }
Ejemplo n.º 3
0
        internal async Task GetMessageOnSchedule(CancellationToken cancellationToken)
        {
            try {
                BlynkLogManager.LogMethodBegin(nameof(GetMessageOnSchedule));
                while (!cancellationToken.IsCancellationRequested)
                {
                    try {
                        if (this.Connected)
                        {
                            var receivedMessages = await this.ReceiveAsync(cancellationToken);

                            if (receivedMessages)
                            {
                                BlynkLogManager.LogInformation("received");
                            }
                            await Task.Delay(this.BackgroundReadIntervalInMilliseconds, cancellationToken);
                        }
                        else
                        {
                            BlynkLogManager.LogInformation("not connected");
                            await Task.Delay(10 *this.BackgroundReadIntervalInMilliseconds, cancellationToken);
                        }
                    }
                    catch (TaskCanceledException) { }
                    catch (Exception ex) {
                        BlynkLogManager.LogException("Background message receiver", ex);
                    }
                }
                BlynkLogManager.LogInformation("canceled");
            }
            finally {
                BlynkLogManager.LogMethodEnd(nameof(GetMessageOnSchedule));
            }
        }
Ejemplo n.º 4
0
        internal async Task <bool> ParseMessageAsync(BlynkConnection blynkConnection)
        {
            bool result = true;

            try {
                BlynkLogManager.LogMethodBegin(nameof(ParseMessageAsync));
                BlynkLogManager.LogInformation(string.Format("Message Received command type : {0}", this.BlynkCommandType));

                switch (this.BlynkCommandType)
                {
                case BlynkCommandType.BLYNK_CMD_RESPONSE:
                    blynkConnection.ResponseReceivedNotification?.Invoke(this.ResponseCode);
                    return(result);

                case BlynkCommandType.BLYNK_CMD_PING:
                    return(await blynkConnection.SendResponseAsync(this.MessageId));

                case BlynkCommandType.BLYNK_CMD_BRIDGE:
                    return(await blynkConnection.SendResponseAsync(this.MessageId));

                case BlynkCommandType.BLYNK_CMD_HARDWARE: {
                    var hardwareCommandType = this.GetHardwareCommandType();
                    BlynkLogManager.LogInformation(string.Format("Hardware command type : {0}", hardwareCommandType));

                    switch (hardwareCommandType)
                    {
                    case HardwareCommandType.VirtualRead: {
                        string pinString;

                        this.messageBuffer.Extract(out pinString);

                        var pinNumber = int.Parse(pinString);

                        var pin = blynkConnection.VirtualPinNotification.PinReadRequest?.Invoke(pinNumber);                                          // blynkConnection.ReadVirtualPinRequest?.Invoke( pinNumber );

                        if (pin == null)
                        {
                            return(await blynkConnection.SendResponseAsync(this.MessageId, BlynkResponse.NO_DATA));
                        }
                        else
                        {
                            return(await pin.SendVirtualPinWriteAsync(blynkConnection, this.MessageId, blynkConnection.CancellationToken));
                        }
                    }

                    case HardwareCommandType.VirtualWrite: {
                        string pinNumberAsString;

                        this.messageBuffer.Extract(out pinNumberAsString);

                        var pin = new VirtualPin()
                        {
                            PinNumber = int.Parse(pinNumberAsString)
                        };

                        this.messageBuffer.Extract(pin.Values);

                        blynkConnection.VirtualPinNotification.PinWriteNotification?.Invoke(pin);

                        return(await blynkConnection.SendResponseAsync(this.MessageId));
                    }

                    case HardwareCommandType.DigitalRead: {
                        string pinString;

                        this.messageBuffer.Extract(out pinString);

                        var pinNumber = int.Parse(pinString);

                        var pin = blynkConnection.DigitalPinNotification.PinReadRequest?.Invoke(pinNumber);                                          // blynkConnection.ReadDigitalPinRequest?.Invoke( pinNumber );

                        if (pin == null)
                        {
                            return(await blynkConnection.SendResponseAsync(this.MessageId, BlynkResponse.NO_DATA));
                        }
                        else
                        {
                            return(await pin.SendDigitalPinWriteAsync(blynkConnection, this.MessageId, blynkConnection.CancellationToken));
                        }
                    }

                    case HardwareCommandType.DigitalWrite: {
                        string pinNumberAsString;
                        string valueAsString;

                        this.messageBuffer.Extract(out pinNumberAsString)
                        .Extract(out valueAsString);

                        var pin = new DigitalPin()
                        {
                            PinNumber = int.Parse(pinNumberAsString),
                            Value     = int.Parse(valueAsString) == 1
                        };

                        //blynkConnection.WriteDigitalPinNotification?.Invoke( pin );
                        blynkConnection.DigitalPinNotification.PinWriteNotification?.Invoke(pin);

                        return(await blynkConnection.SendResponseAsync(this.MessageId));
                    }

                    case HardwareCommandType.AnalogRead: {
                        string pinString;

                        this.messageBuffer.Extract(out pinString);

                        var pinNumber = int.Parse(pinString);

                        var pin = blynkConnection.AnalogPinNotification.PinReadRequest?.Invoke(pinNumber);                                          // blynkConnection.ReadAnalogPinRequest( pinNumber );

                        if (pin == null)
                        {
                            return(await blynkConnection.SendResponseAsync(this.MessageId, BlynkResponse.NO_DATA));
                        }
                        else
                        {
                            return(await pin.SendAnalogPinWriteAsync(blynkConnection, this.MessageId, blynkConnection.CancellationToken));
                        }
                    }

                    case HardwareCommandType.AnalogWrite: {
                        string pinNumberAsString;
                        string valueAsString;

                        this.messageBuffer.Extract(out pinNumberAsString)
                        .Extract(out valueAsString);

                        var pin = new AnalogPin()
                        {
                            PinNumber = int.Parse(pinNumberAsString),
                            Value     = short.Parse(valueAsString)
                        };

                        //blynkConnection.WriteAnalogPinNotification?.Invoke( pin );
                        blynkConnection.AnalogPinNotification.PinWriteNotification?.Invoke(pin);

                        return(await blynkConnection.SendResponseAsync(this.MessageId));
                    }

                    case HardwareCommandType.PinMode: {
                        string pin;
                        string mode;
                        while (this.messageBuffer.Position < this.MessageLength)
                        {
                            this.messageBuffer.Extract(out pin)
                            .Extract(out mode);

                            PinMode pinMode = PinMode.Invalid;

                            switch (mode)
                            {
                            case "in":
                                pinMode = PinMode.Input;
                                break;

                            case "out":
                                pinMode = PinMode.Output;
                                break;

                            case "pu":
                                pinMode = PinMode.PullUp;
                                break;

                            case "pd":
                                pinMode = PinMode.PullDown;
                                break;

                            case "pwm":
                                pinMode = PinMode.Pwm;
                                break;
                            }

                            if (pinMode != PinMode.Invalid)
                            {
                                blynkConnection.PinModeNotification?.Invoke(pin, pinMode);
                            }
                        }
                        return(await blynkConnection.SendResponseAsync(this.MessageId));
                    }
                    }
                    break;
                }
                }
            }
            catch (Exception ex) {
                BlynkLogManager.LogException("Error parsing message", ex);
            }
            finally {
                BlynkLogManager.LogMethodEnd(nameof(ParseMessageAsync));
            }
            return(result);
        }