private async Task <bool> SyncAsyncInternal(bool sync) { if (!initialized) { return(false); } if (syncSize > 2) { BusRequest request = new BusRequest(); request.Write((byte)0xFF); BusTransceiveResult result = sync ? Transceive(request, syncSize - 2) : await TransceiveAsync(request, syncSize - 2); if (!result.Success) { return(false); } else { if (numberOfDigitalInputs > 0) { digitalInputValue = result.Response.ReadUInt16(); } for (int i = 0; i < numberOfAnalogInputs; ++i) { analogInputs[i] = result.Response.ReadUInt16(); } } } return(true); }
private async Task <bool> SetDigitalOutputAsyncInternal(uint key, bool value, bool sync) { BusRequest request = new BusRequest(); request.Write((byte)(key + 33)); request.Write((byte)(value ? 1 : 0)); BusTransceiveResult result = sync ? Transceive(request, 0) : await TransceiveAsync(request, 0); if (!result.Success) { return(false); } else { if (value) { digitalOutputValue |= (UInt16)(1 << (int)key); } else { digitalOutputValue &= (UInt16)(~(1 << (int)key)); } return(true); } }
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); } }
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); }
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); } }
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); }