public byte[] GetBedCommandMessage(BedControlCommand command) //todo а не слишком ли высокий уровень абстракции я сюда спустил? { switch (command) { case BedControlCommand.Start: { return(GetWriteRegisterMessage(BedMovingPosition, new byte[] { 0x00, 0xAA })); } case BedControlCommand.Pause: { return(GetWriteRegisterMessage(BedMovingPosition, new byte[] { 0x00, 0xAA })); } case BedControlCommand.EmergencyStop: { return(GetWriteRegisterMessage(BedMovingPosition, new byte[] { 0x00, 0xAB })); } case BedControlCommand.Reverse: { return(GetWriteRegisterMessage(BedMovingPosition, new byte[] { 0x00, 0xAC })); } default: { throw new ArgumentException("Unknow type of command"); } } }
//todo fix sending public async Task ExecuteCommandAsync(BedControlCommand command) { AssertInitParams(); var isFree = await _semaphoreSlim .WaitAsync(_config.Timeout) .ConfigureAwait(false); if (!isFree) { throw new DeviceConnectionException( $"Не удалось подключиться к инверсионному столу в течение {_config.Timeout.TotalMilliseconds}"); } await Task.Factory.StartNew(() => { try { AssertConnection(); var message = new BedMessage(BedMessageEventType.Write); var sendMessage = message.GetBedCommandMessage(command); _udpSendingClient.Send(sendMessage, sendMessage.Length); //todo зачем после каждой отправки мы ожидаем ответа? Мы делаем из UDP свой TCP _udpReceivingClient.Receive(ref _remoteRecievedIpEndPoint); } catch (DeviceConnectionException) { IsConnected = false; throw; } catch (SocketException e) { IsConnected = false; throw new DeviceConnectionException("Ошибка соединения с инверсионным столом", e); } catch (ObjectDisposedException e) { IsConnected = false; throw new DeviceConnectionException("Ошибка соединения с инверсионным столом", e); } catch (Exception e) { IsConnected = false; throw new DeviceProcessingException("Ошибка отправки команды инверсионному столу", e); } finally { _semaphoreSlim.Release(); } }) .ConfigureAwait(false); }
public void ExecuteCommand(BedControlCommand command) { try { lock (_lockObject) { var device = GetBedUsbDevice(); if (device == null) { return; } byte[] message = null; switch (command) { case BedControlCommand.Start: message = new byte[] { 0x29, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff }; break; case BedControlCommand.Pause: message = new byte[] { 0x2a, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff }; break; case BedControlCommand.Reverse: message = new byte[] { 0x2c, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff }; break; case BedControlCommand.EmergencyStop: message = new byte[] { 0x2b, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff }; break; } device.write(message); device.close(); } } catch (Exception ex) { //error message } }
public Task ExecuteCommandAsync(BedControlCommand command) { return(Task.Delay(_config.DefaultDelay)); }