public async Task <string> CommandGetVariable(string propertyName) { if (_UARTService == null || !_UARTService.IsDeviceReady) { return(string.Empty); } //Generate package DataPacket packetToSend = new GetPropertyDataPacket(propertyName); //Timeout handling TaskCompletionSource <DataPacket> signalEventPoke = new TaskCompletionSource <DataPacket>(); UARTPacketEventHandler handler = null; handler += (sender, e) => { if (e.ResponsePacket.MessageId == packetToSend.MessageId) { signalEventPoke.SetResult(e.ResponsePacket); _UARTService.UARTPacketReceived -= handler; } }; _UARTService.UARTPacketReceived += handler; if (await _UARTService.SendPacket(packetToSend)) { SendPropertyDataPacket reception = await TimeoutAsyncEvent.TimeoutAfter(signalEventPoke.Task, TIMEOUT_UART_ANSWER) as SendPropertyDataPacket; _UARTService.UARTPacketReceived -= handler; if (reception != null) { await CommandConfirmVariable(reception.MessageId, reception.PropertyName); } return(reception?.PropertyValue); } else { //TODO LOG Log.Error("PokeAliveCommand failed."); _UARTService.UARTPacketReceived -= handler; } return(string.Empty); }
private async void Event_UARTPacketReceived(object sender, UARTPacketEventArgs e) { if (e.ResponsePacket.Source != DataSource.FourByFour) { return; } switch (e.ResponsePacket.Command) { //Automatically send a pong answer case FourByFourConstants.CMD_PING: await CommandSendPong(e.ResponsePacket.MessageId); break; //Automatically send a confirmation case FourByFourConstants.CMD_SEND_PROPERTY: SendPropertyDataPacket sendValueDataPacket = e.ResponsePacket as SendPropertyDataPacket; if (sendValueDataPacket != null) { NewResponseVariableChangedReceived(sendValueDataPacket.PropertyName, sendValueDataPacket.PropertyValue); await CommandConfirmVariable(e.ResponsePacket.MessageId, sendValueDataPacket.PropertyName); } break; //A confirm action is part of the CommandSendVariable case FourByFourConstants.CMD_CONFIRM_PROPERTY: //A confirm action is part of the CommandLaunchAction case FourByFourConstants.CMD_CONFIRM_ACTION: //A pong command will never be sent on its own. case FourByFourConstants.CMD_PONG: //4x4 will never get data from the UI. case FourByFourConstants.CMD_GET_PROPERTY: //4x4 will never launch an action on the UI, it's the other way around. case FourByFourConstants.CMD_LAUNCH_ACTION: break; } }