Esempio n. 1
0
        private async Task <ErrorCode> SetBusAddressAsyncInternal(uint uuid, int busAddress, bool sync)
        {
            var request = new BusRequest();

            request.Write((byte)0x00);
            request.Write((byte)0x00);
            request.Write(uuid);
            request.Write((byte)0x00);
            request.Write((byte)busAddress);

            var result = sync ? TransceiveBroadcast(request, 1) : await TransceiveBroadcastAsync(request, 1);

            if (result.Success)
            {
                if (result.Response.ReadByte() == 1)
                {
                    return(ErrorCode.Success);
                }
                else
                {
                    return(ErrorCode.DeviceRejectedBusAddress);
                }
            }
            else
            {
                return(result.TransportError);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Sends a broadcast to all devices which does nothing, but can be used to wake up devices which were put
        /// to sleep with GoToSleep.
        /// </summary>
        /// <returns>Error code describing the result of the call.</returns>
        public ErrorCode SendNopBroadcast()
        {
            var request = new BusRequest();

            request.Write((byte)0x00);
            return(SendBroadcast(request));
        }
Esempio n. 3
0
        /// <summary>
        /// Sends a broadcast to all devices which does nothing, but can be used to wake up devices which were put
        /// to sleep with GoToSleep.
        /// </summary>
        /// <returns>A task representing the asynchronous operation.
        /// Contains an error code describing the result of the call.</returns>
        public Task <ErrorCode> SendNopBroadcastAsync()
        {
            var request = new BusRequest();

            request.Write((byte)0x00);
            return(SendBroadcastAsync(request));
        }
Esempio n. 4
0
        private async Task <(ErrorCode, string)> GetCommandNameAsyncInternal(byte key, bool sync)
        {
            (ErrorCode commandNameLengthError, byte length) = sync ?
                                                              GetCommandNameLength(key) :
                                                              await GetCommandNameLengthAsync(key);

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

            BusRequest request = new BusRequest();

            request.Write(key);
            request.Write((byte)CommandKey.CommandInfoGetName);
            request.Write((byte)CommandKey.CommandInfoGetName);
            request.Write((byte)CommandKey.CommandInfoGetName);

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

            if (result.Success)
            {
                return(ErrorCode.Success, Encoding.UTF8.GetString(result.Response.ReadBytes(length)));
            }
            else
            {
                return(result.TransportError, null);
            }
        }
Esempio n. 5
0
 private async Task SendRequestAsync(string queueName, BusRequest request,
                                     CancellationToken cancellationToken = default)
 {
     await using var sender = _client.CreateSender(queueName);
     var input = JsonSerializer.SerializeToUtf8Bytes(request, request.GetType());
     await sender.SendMessageAsync(new ServiceBusMessage(input), cancellationToken);
 }
Esempio n. 6
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. 7
0
        /// <summary>
        /// Disable bus neighbours of available devices.
        /// </summary>
        /// <returns>Error code describing the result of the call.</returns>
        public ErrorCode DisableBusNeighbours()
        {
            var broadcast = new BusRequest();

            broadcast.Write((byte)0x00);
            broadcast.Write((byte)0x02);

            return(SendBroadcast(broadcast));
        }
Esempio n. 8
0
        /// <summary>
        /// Disable bus neighbours of available devices.
        /// </summary>
        /// <returns>A task representing the asynchronous operation.
        /// Contains an error code describing the result of the call.</returns>
        public Task <ErrorCode> DisableBusNeighboursAsync()
        {
            var broadcast = new BusRequest();

            broadcast.Write((byte)0x00);
            broadcast.Write((byte)0x02);

            return(SendBroadcastAsync(broadcast));
        }
Esempio n. 9
0
        /// <summary>
        /// Requests all available devices to enter a powerdown mode.
        /// </summary>
        /// <returns>Error code describing the result of the call.</returns>
        public ErrorCode GoToSleep()
        {
            var broadcast = new BusRequest();

            broadcast.Write((byte)0x00);
            broadcast.Write((byte)0x06);

            return(SendBroadcast(broadcast));
        }
Esempio n. 10
0
        /// <summary>
        /// Requests all available devices to enter a powerdown mode.
        /// </summary>
        /// <returns>A task representing the asynchronous operation.
        /// Contains an error code describing the result of the call.</returns>
        public Task <ErrorCode> GoToSleepAsync()
        {
            var broadcast = new BusRequest();

            broadcast.Write((byte)0x00);
            broadcast.Write((byte)0x06);

            return(SendBroadcastAsync(broadcast));
        }
Esempio n. 11
0
        /// <summary>
        /// Resets the bus address of all available devices.
        /// </summary>
        /// <returns>A task representing the asynchronous operation.
        /// Contains an error code describing the result of the call.</returns>
        public Task <ErrorCode> ResetAllBusAddressesAsync()
        {
            var broadcast = new BusRequest();

            broadcast.Write((byte)0x00);
            broadcast.Write((byte)0x03);

            return(SendBroadcastAsync(broadcast));
        }
Esempio n. 12
0
        /// <summary>
        /// Resets the bus address of all available devices.
        /// </summary>
        /// <returns>Error code describing the result of the call.</returns>
        public ErrorCode ResetAllBusAddresses()
        {
            var broadcast = new BusRequest();

            broadcast.Write((byte)0x00);
            broadcast.Write((byte)0x03);

            return(SendBroadcast(broadcast));
        }
Esempio n. 13
0
        /// <summary>
        /// Pings the device with the given UUID.
        /// </summary>
        /// <param name="uuid">UUID of the device to ping.</param>
        /// <returns>Error code describing the result of the call.</returns>
        public ErrorCode SendUuidPing(uint uuid)
        {
            var request = new BusRequest();

            request.Write((byte)0x00);
            request.Write((byte)0x00);
            request.Write(uuid);

            return(TransceiveBroadcast(request, 0).TransportError);
        }
Esempio n. 14
0
        /// <summary>
        /// Pings the device with the given UUID.
        /// </summary>
        /// <param name="uuid">UUID of the device to ping.</param>
        /// <returns>A task representing the asynchronous operation.
        /// Contains an error code describing the result of the call.</returns>
        public async Task <ErrorCode> SendUuidPingAsync(uint uuid)
        {
            var request = new BusRequest();

            request.Write((byte)0x00);
            request.Write((byte)0x00);
            request.Write(uuid);

            return((await TransceiveBroadcastAsync(request, 0)).TransportError);
        }
Esempio n. 15
0
        private async Task <ErrorCode> ResetBusAddressAsyncInternal(uint uuid, bool sync)
        {
            var request = new BusRequest();

            request.Write((byte)0x00);
            request.Write((byte)0x00);
            request.Write(uuid);
            request.Write((byte)0x01);

            var result = sync ? TransceiveBroadcast(request, 0) : await TransceiveBroadcastAsync(request, 0);

            return(result.TransportError);
        }
Esempio n. 16
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. 17
0
        private async Task <(ErrorCode, uint uuid)> SendBroadcastPingAsyncInternal(bool sync)
        {
            var request = new BusRequest();

            request.Write((byte)0x00);
            request.Write((byte)0x00);

            var result = sync ? TransceiveBroadcast(request, 4) : await TransceiveBroadcastAsync(request, 4);

            if (result.Success)
            {
                return(result.TransportError, result.Response.ReadUInt32());
            }
            else
            {
                return(result.TransportError, 0);
            }
        }
Esempio n. 18
0
        private async Task <(ErrorCode, byte)> GetCommandNameLengthAsyncInternal(byte key, bool sync)
        {
            BusRequest request = new BusRequest();

            request.Write(key);
            request.Write((byte)CommandKey.CommandInfoGetNameLength);
            request.Write((byte)CommandKey.CommandInfoGetNameLength);
            request.Write((byte)CommandKey.CommandInfoGetNameLength);

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

            if (result.Success)
            {
                return(ErrorCode.Success, result.Response.ReadByte());
            }
            else
            {
                return(result.TransportError, 0);
            }
        }
Esempio n. 19
0
        private async Task <(ErrorCode, int busAddress)> ReceiveBusAddressAsyncInternal(uint uuid, bool sync)
        {
            var request = new BusRequest();

            request.Write((byte)0x00);
            request.Write((byte)0x00);
            request.Write(uuid);
            request.Write((byte)0x00);

            var result = sync ? TransceiveBroadcast(request, 1) : await TransceiveBroadcastAsync(request, 1);

            if (result.Success)
            {
                return(result.TransportError, result.Response.ReadByte());
            }
            else
            {
                return(result.TransportError, 0);
            }
        }
Esempio n. 20
0
 /// <summary>
 /// Transmits a broadcast.
 /// </summary>
 /// <param name="broadcast">Broadcast containing the packet data, excluding address and checksum.</param>
 /// <returns>A task representing the asynchronous operation.
 /// Contains an error code describing the result of the call.</returns>
 protected Task <ErrorCode> SendBroadcastAsync(BusRequest broadcast)
 {
     return(SendBroadcastAsyncInternal(broadcast, sync: false));
 }
Esempio n. 21
0
 /// <summary>
 /// Transmits a broadcast.
 /// </summary>
 /// <param name="broadcast">Broadcast containing the packet data, excluding address and checksum.</param>
 /// <returns>Error code describing the result of the call.</returns>
 protected ErrorCode SendBroadcast(BusRequest broadcast)
 {
     return(SendBroadcastAsyncInternal(broadcast, sync: true).GetAwaiter().GetResult());
 }
Esempio n. 22
0
 /// <summary>
 /// Transmit a broadcast and attempt to receive a response. This function is equivalent to calling
 /// TransceiveAsync(), passing BroadcastAddress as the target address.
 /// </summary>
 /// <param name="broadcastRequest">Request containing the packet data, excluding address and checksum.</param>
 /// <param name="responseSize">Expected response data size, not counting address and checksum.</param>
 /// <param name="attempts">Number of unsuccessful transceive attempts before an error code
 /// is returned.</param>
 /// <returns>A task representing the asynchronous operation.
 /// Contains an object containing an error code and the received data.</returns>
 protected Task <BusTransceiveResult> TransceiveBroadcastAsync(BusRequest broadcastRequest, int responseSize, int attempts = DefaultAttempts)
 {
     return(TransceiveAsyncInternal(BroadcastAddress, broadcastRequest, responseSize, attempts, sync: false));
 }
Esempio n. 23
0
 /// <summary>
 /// Transmit a broadcast and attempt to receive a response. This function is equivalent to calling
 /// Transceive(), passing BroadcastAddress as the target address.
 /// </summary>
 /// <param name="broadcastRequest">Request containing the packet data, excluding address and checksum.</param>
 /// <param name="responseSize">Expected response data size, not counting address and checksum.</param>
 /// <param name="attempts">Number of unsuccessful transceive attempts before an error code
 /// is returned.</param>
 /// <returns>An object containing an error code and the received data.</returns>
 protected BusTransceiveResult TransceiveBroadcast(BusRequest broadcastRequest, int responseSize, int attempts = DefaultAttempts)
 {
     return(TransceiveAsyncInternal(BroadcastAddress, broadcastRequest, responseSize, attempts, sync: true).GetAwaiter().GetResult());
 }
Esempio n. 24
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);
            }
        }