Beispiel #1
0
        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;
        }
Beispiel #2
0
 public void Save()
 {
     InvokeHelper.Invoke(() =>
     {
         ValueChanged?.Invoke(null);
     });
 }
Beispiel #3
0
        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);
            }
        }
Beispiel #4
0
 public void NotifyPropertyChanged(params string[] changedProperties)
 {
     if (changedProperties != null)
     {
         foreach (string property in changedProperties)
         {
             InvokeHelper.Invoke(() =>
             {
                 OnPropertyChanged(property);
             });
         }
     }
 }
Beispiel #5
0
 public void Append(SensorItem e)
 {
     try
     {
         InvokeHelper.Invoke(() =>
         {
             Value = e;
             History.Add(e);
             ValueChanged?.Invoke(e);
             NotifyPropertyChanged("History");
         });
     }
     catch (Exception x) {
         Debug.WriteLine(x);
     }
 }
Beispiel #6
0
        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);
            }
        }