private void _bluetooth_DataReceived(object sender, EventArgs e) { //DeviceStream stream = new DeviceStream(_bluetooth); //Message message = await Transport.Read<Message>(stream); byte[] buffer = _bluetooth.ReadAll(); MessageHeader header = StructSerializer.DeserializeStruct <MessageHeader>(buffer); if (header.Type == MessageType.Command) { CommandMessage command = StructSerializer.DeserializeStruct <CommandMessage>(buffer); Console.WriteLine("command {0} = {1},{2}", command.Device.Hash, command.Device.IValue, command.Device.DValue); //IComponent component = _components.SingleOrDefault(i => i.Name) } }
private async Task ListenAsync() { uint headerSize = (uint)Marshal.SizeOf(typeof(MessageHeader)); byte[] headerBuffer = new byte[headerSize]; DataReader reader = new DataReader(_stream.InputStream); while (true) { await reader.LoadAsync(headerSize); reader.ReadBytes(headerBuffer); MessageHeader header = StructSerializer.DeserializeStruct <MessageHeader>(headerBuffer); if (header.Type == MessageType.Telemetry) { uint telemetrySize = (uint)Marshal.SizeOf(typeof(TelemetryMessage)); byte[] telemetryBuffer = new byte[telemetrySize - headerSize]; await reader.LoadAsync(telemetrySize - headerSize); reader.ReadBytes(telemetryBuffer); var z = new byte[headerBuffer.Length + telemetryBuffer.Length]; headerBuffer.CopyTo(z, 0); telemetryBuffer.CopyTo(z, headerBuffer.Length); TelemetryMessage telemetry = StructSerializer.DeserializeStruct <TelemetryMessage>(z); CurrentValues.Update(telemetry); Debug.WriteLine("Telemetry"); for (int i = 0; i < TelemetryMessage.EntryLength; i++) { Debug.WriteLine(" {0} = {1},{2}", telemetry.Devices[i].Hash, telemetry.Devices[i].IValue, telemetry.Devices[i].DValue); } Debug.WriteLine(""); for (int i = 0; i < TelemetryMessage.EntryLength; i++) { if (telemetry.Devices[i].Hash == 0) { continue; } //If we are not watching this hash, ignore it if (!_telemetry.ContainsKey(telemetry.Devices[i].Hash)) { continue; } ObservableCollection <Data> collection = _telemetry[telemetry.Devices[i].Hash]; object value; TypeCode type = _telemetryHashToType[telemetry.Devices[i].Hash]; switch (type) { case TypeCode.Int32: value = telemetry.Devices[i].IValue; break; case TypeCode.Single: value = telemetry.Devices[i].DValue; break; default: throw new NotImplementedException(); } Data data = new Data { DateTime = DateTime.Now, Value = value }; CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { collection.Add(data); }); } } } }