async Task AddSensor(IDevice device) { if (isAddingSensor) return; isAddingSensor = true; try { SensorModel sensor = null; sensor = await SensorKitExtensions.BeginInvokeOnMainThreadAsync<SensorModel>(()=>{ sensor = new SensorModel() { Id = device.Id, Name = device.Name, Information = Registry.Public.FirstOrDefault(s => s.Model == SensorInformation.TryParseSensorType(device.Name)), IsLive = true }; Data.Add(sensor); return sensor; }); if (sensor != null) { InvokeHelper.Invoke(() => { NotifyPropertyChanged("Data"); }); } }catch(Exception x) { Debug.WriteLine(x); } isAddingSensor = false; }
public void Save() { InvokeHelper.Invoke(() => { ValueChanged?.Invoke(null); }); }
public async Task Subscribe() { try { Debug.WriteLine($"***SUBSCRIBING {Data.Name}..."); if (Data.Id != null) { if (_device == null) { var cancellationTokenSource = new CancellationTokenSource(5000); _device = await SensorKit.Instance.Adapter.ConnectToKnownDeviceAsync(Data.Id, default(ConnectParameters), cancellationTokenSource.Token); } if (_device != null && (uart == null || _uartTX == null || _uartRX == null)) { _connectedTime = DateTime.Now; uart = await _device.GetServiceAsync(NORDIC_UART_SERVICE); if (uart != null) { _uartRX = await uart.GetCharacteristicAsync(NORDIC_UART_RX); _uartTX = await uart.GetCharacteristicAsync(NORDIC_UART_TX); _uartRX.ValueUpdated += (s, e) => { var received = Encoding.UTF8.GetString(e.Characteristic.Value, 0, e.Characteristic.Value.Length); ParsePacket(received); }; IsConnected = true; await _uartRX.StartUpdatesAsync(); } } if (_uartTX != null) { await _uartTX.WriteAsync(Encoding.UTF8.GetBytes(SYNC_COMMAND_HISTORY)); } InvokeHelper.Invoke(() => { Data.NotifyPropertyChanged("IsSubscribed"); }); } }catch (Exception x) { Debug.WriteLine(x); } }
public void NotifyPropertyChanged(params string[] changedProperties) { if (changedProperties != null) { foreach (string property in changedProperties) { InvokeHelper.Invoke(() => { OnPropertyChanged(property); }); } } }
public void Append(SensorItem e) { try { InvokeHelper.Invoke(() => { Value = e; History.Add(e); ValueChanged?.Invoke(e); NotifyPropertyChanged("History"); }); } catch (Exception x) { Debug.WriteLine(x); } }
public static Task <T> BeginInvokeOnMainThreadAsync <T>(Func <T> a) { var tcs = new TaskCompletionSource <T>(); InvokeHelper.Invoke(() => { try { var result = a(); tcs.SetResult(result); } catch (Exception ex) { tcs.SetException(ex); } }); return(tcs.Task); }
public void Append(SensorItem e) { try { InvokeHelper.Invoke(() => { Value = e; History.Add(e); ValueChanged?.Invoke(e); NotifyPropertyChanged("History"); NotifyPropertyChanged("LastSummary"); NotifyPropertyChanged("LastDribble"); NotifyPropertyChanged("DribbleHistory"); }); Task.Run(() => PostToApi(this, e)); } catch (Exception x) { Debug.WriteLine(x); } }