Esempio n. 1
0
        private async Task <ErrorCode> SetFloatValueAsyncInternal(byte key, float value, bool sync)
        {
            if (CommandSet == null)
            {
                Log.Logger?.Error("commandSet not populated", this);
                return(ErrorCode.DeviceNotInitialized);
            }
            if (key > CommandSet.Length || key == 0)
            {
                Log.Logger?.Error("key not within commandSetLength", this);
                return(ErrorCode.StellantriebInvalidKey);
            }

            CommandInternal command = commandSet[key - 1];

            if (command.CommandLength == Command.Length.None || command.CommandLength == Command.Length.NoneAsText)
            {
                Log.Logger?.Error("%s: key not supported", this);
                return(ErrorCode.StellantriebInvalidKey);
            }
            if (command.AccessMode != Command.Access.WriteAccess)
            {
                Log.Logger?.Error("key not writable", this);
                return(ErrorCode.StellantriebValueReadOnly);
            }
            if (command.IsControlValue)
            {
                Log.Logger?.Error("value with key %u is not floating point, which was requested", this);
                return(ErrorCode.StellantriebInvalidKey);
            }

            command.bufferValid = false;

            BusRequest request = new BusRequest();

            request.Write(key);

            switch (command.CommandLength)
            {
            case Command.Length.Byte:
                request.Write((byte)(value / command.Factor));
                return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError);

            case Command.Length.Short:
                request.Write((Int16)(value / command.Factor));
                return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError);

            case Command.Length.Long:
                request.Write((Int32)(value / command.Factor));
                return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError);

            case Command.Length.Float:
                request.Write(value / command.Factor);
                return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError);

            default:
                return(ErrorCode.Unspecified);
            }
        }
Esempio n. 2
0
        private async Task <ErrorCode> PopulateCommandSetAsyncInternal(bool sync)
        {
            (ErrorCode commandSetLengthError, uint commandSetLength) = sync ?
                                                                       GetCommandsetLength() :
                                                                       await GetCommandsetLengthAsync();

            if (commandSetLengthError != ErrorCode.Success)
            {
                Log.Logger?.Error("unable to retrieve command set length", this);
                return(commandSetLengthError);
            }

            if (commandSetLength == 0)
            {
                Log.Logger?.Error("command set length of 0 is unsupported", this);
                return(ErrorCode.StellantriebCommandLengthZero);
            }

            CommandInternal[] tempCommandSet = new CommandInternal[commandSetLength];


            for (int i = 0; i < commandSetLength; ++i)
            {
                BusRequest request = new BusRequest();
                request.Write((byte)(i + 1));
                request.Write((byte)CommandKey.CommandInfoGet);
                request.Write((byte)CommandKey.CommandInfoGet);
                request.Write((byte)CommandKey.CommandInfoGet);

                BusTransceiveResult result = sync ? Transceive(request, 6) : await TransceiveAsync(request, 6);

                if (!result.Success)
                {
                    return(result.TransportError);
                }

                tempCommandSet[i] = new CommandInternal(
                    (Command.Access)result.Response.ReadByte(),
                    (Command.Length)result.Response.ReadByte(),
                    result.Response.ReadSingle());
            }

            commandSet = tempCommandSet;

            return(ErrorCode.Success);
        }
Esempio n. 3
0
        private async Task <ErrorCode> RetrieveCommandNamesAsyncInternal(bool sync)
        {
            if (commandSet == null)
            {
                ErrorCode populateCommandSetError = sync ?
                                                    PopulateCommandSet() :
                                                    await PopulateCommandSetAsync();

                if (populateCommandSetError != ErrorCode.Success)
                {
                    Log.Logger?.Error("unable to retrieve command set", this);
                    return(populateCommandSetError);
                }
            }

            for (int i = 0; i < commandSet.Length; ++i)
            {
                (ErrorCode commandNameError, string commandName) = sync ?
                                                                   GetCommandName((byte)(i + 1)) :
                                                                   await GetCommandNameAsync((byte)(i + 1));

                if (commandNameError != ErrorCode.Success)
                {
                    Log.Logger?.Error("unable to retrieve command name", this);
                    return(commandNameError);
                }

                Command oldCommand = commandSet[i];

                commandSet[i] = new CommandInternal(
                    commandName,
                    oldCommand.AccessMode,
                    oldCommand.CommandLength,
                    oldCommand.Factor);
            }

            return(ErrorCode.Success);
        }
Esempio n. 4
0
        private async Task <ErrorCode> SetIntValueAsyncInternal(byte key, int value, bool sync)
        {
            if (CommandSet == null)
            {
                Log.Logger?.Error("commandSet not populated", this);
                return(ErrorCode.DeviceNotInitialized);
            }
            if (key > CommandSet.Length || key == 0)
            {
                Log.Logger?.Error("key not within commandSetLength", this);
                return(ErrorCode.StellantriebInvalidKey);
            }

            CommandInternal command = commandSet[key - 1];

            if (command.CommandLength == Command.Length.None || command.CommandLength == Command.Length.NoneAsText)
            {
                Log.Logger?.Error("%s: key not supported", this);
                return(ErrorCode.StellantriebInvalidKey);
            }
            if (command.AccessMode != Command.Access.WriteAccess)
            {
                Log.Logger?.Error("key not writable", this);
                return(ErrorCode.StellantriebValueReadOnly);
            }
            if (!command.IsControlValue)
            {
                Log.Logger?.Error("value with key %u is floating point, which was not requested", this);
                return(ErrorCode.StellantriebInvalidKey);
            }

            command.bufferValid = false;

            BusRequest request = new BusRequest();

            request.Write(key);

            switch (command.CommandLength)
            {
            case Command.Length.Byte:
                request.Write((byte)(value));
                return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError);

            case Command.Length.Short:
                request.Write((short)(value));
                return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError);

            case Command.Length.Long:
                request.Write((Int32)(value));
                return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError);

            case Command.Length.Float:
                // this actually does not make sense. But the problem ist that the configuration is
                // invalid in the first place.
                request.Write((float)value);
                return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError);

            default:
                return(ErrorCode.Unspecified);
            }
        }
Esempio n. 5
0
        private async Task <(ErrorCode, int)> GetIntValueAsyncInternal(byte key, bool sync)
        {
            if (CommandSet == null)
            {
                Log.Logger?.Error("commandSet not populated", this);
                return(ErrorCode.DeviceNotInitialized, 0);
            }
            if (key > CommandSet.Length || key == 0)
            {
                Log.Logger?.Error("key not within commandSetLength", this);
                return(ErrorCode.StellantriebInvalidKey, 0);
            }

            CommandInternal command = commandSet[key - 1];

            if (command.CommandLength == Command.Length.None || command.CommandLength == Command.Length.NoneAsText)
            {
                Log.Logger?.Error("%s: key not supported", this);
                return(ErrorCode.StellantriebInvalidKey, 0);
            }
            if (!command.IsControlValue)
            {
                Log.Logger?.Error("value with key %u is not a control value, which was requested", this);
                return(ErrorCode.StellantriebInvalidKey, 0);
            }

            if (!command.bufferValid)
            {
                BusRequest request = new BusRequest();
                request.Write(key);

                switch (command.CommandLength)
                {
                case Command.Length.Byte:
                {
                    BusTransceiveResult result = sync ? Transceive(request, 1) : await TransceiveAsync(request, 1);

                    if (!result.Success)
                    {
                        return(result.TransportError, 0);
                    }
                    else
                    {
                        command.intBuffer = (int)result.Response.ReadByte();
                    }
                    break;
                }

                case Command.Length.Short:
                {
                    BusTransceiveResult result = sync ? Transceive(request, 2) : await TransceiveAsync(request, 2);

                    if (!result.Success)
                    {
                        return(result.TransportError, 0);
                    }
                    else
                    {
                        command.intBuffer = (int)result.Response.ReadInt16();
                    }
                    break;
                }

                case Command.Length.Long:
                {
                    BusTransceiveResult result = sync ? Transceive(request, 4) : await TransceiveAsync(request, 4);

                    if (!result.Success)
                    {
                        return(result.TransportError, 0);
                    }
                    else
                    {
                        command.intBuffer = (int)result.Response.ReadInt32();
                    }
                    break;
                }

                case Command.Length.Float:
                {
                    BusTransceiveResult result = sync ? Transceive(request, 4) : await TransceiveAsync(request, 4);

                    if (!result.Success)
                    {
                        return(result.TransportError, 0);
                    }
                    else
                    {
                        // this actually does not make sense. But the problem ist that the configuration is
                        // invalid in the first place.
                        command.intBuffer = (int)result.Response.ReadSingle();
                    }
                    break;
                }

                default:
                    return(ErrorCode.Unspecified, 0);
                }

                // protocol definition: writable values are bufferable
                if (command.AccessMode == Command.Access.WriteAccess)
                {
                    command.bufferValid = true;
                }
            }
            return(ErrorCode.Success, command.intBuffer);
        }