public async void Run(IBackgroundTaskInstance taskInstance) { _deferral = taskInstance.GetDeferral(); var details = (BluetoothLEAdvertisementWatcherTriggerDetails)taskInstance.TriggerDetails; var client = DigitServiceBuilder.Get(); if (details.Advertisements != null && details.Advertisements.Count > 0) { var packets = details.Advertisements.Select(v => new DigitAdvertisementPacket(v)); var latest = packets.OrderByDescending(v => v.Timestamp).First(); if (!latest.TimeKnown.HasValue || !latest.TimeKnown.Value) { var opts = new DigitBLEOptions(); if (opts.IsConfigured) { var bleClient = new DigitBLEClient(opts); try { await bleClient.SetTime(DateTime.Now); await client.LogAsync($"Sent current time to watch (advertisement requested)", 1); } catch (DigitBLEExpcetion e) { await client.LogAsync($"Could not send time (advertisement requested): ${e.Message}", 3); } } else { await client.LogAsync($"Adv Task error: no device configured.", 3); } } if (new AdvertisementWatcherManager().RegisterAdvertisementWatcherTask()) { await client.LogAsync($"Re-registered advertisement watcher task"); } } _deferral.Complete(); }
public async void Run(IBackgroundTaskInstance taskInstance) { _deferral = taskInstance.GetDeferral(); var client = DigitServiceBuilder.Get(); try { RawNotification notification = (RawNotification)taskInstance.TriggerDetails; await client.LogAsync($"Received push {notification.Content}"); var opts = new DigitBLEOptions(); var content = JsonConvert.DeserializeObject <PushPayload>(notification.Content); switch (content.Action) { case PushActions.MeasureBattery: if (opts.IsConfigured) { var bleClient = new DigitBLEClient(opts); var batteryService = new BatteryService(bleClient, client); await batteryService.AddBatteryMeasurement(); } else { await client.LogAsync($"Push error: no device configured.", 3); } break; case PushActions.SendTime: if (opts.IsConfigured) { var bleClient = new DigitBLEClient(opts); try { await bleClient.SetTime(DateTime.Now); } catch (DigitBLEExpcetion e) { await client.LogAsync($"Could not send time: {e.Message}", 3); } } else { await client.LogAsync($"Push error: no device configured.", 3); } break; case PushActions.SendLocation: var locationService = new LocationService(client); await locationService.SendCurrentLocation(); break; default: break; } } catch (Exception e) { await client.LogAsync($"Unhandled background exception: {e.Message}", 3); } _deferral.Complete(); }