Пример #1
0
        private const int maxEventDescriptors = 42;                     // Value determined by how many max length event descriptors (91 chars)
                                                                        // stored as a JSON string can fit in 8K (max allowed for local settings)

        void IBackgroundTask.Run(IBackgroundTaskInstance taskInstance)
        {
            BackgroundTaskDeferral deferral       = taskInstance.GetDeferral();
            GeovisitTriggerDetails triggerDetails = (GeovisitTriggerDetails)taskInstance.TriggerDetails;

            // Handle Visit reports
            GetVisitReports(triggerDetails);
            deferral.Complete();
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _deferral = taskInstance.GetDeferral();
            GeovisitTriggerDetails visit = (GeovisitTriggerDetails)taskInstance.TriggerDetails;
            var client  = DigitServiceBuilder.Get();
            var reports = visit.ReadReports();
            var msg     = string.Join("\n", reports.Select(r => $"{r.Timestamp} {r.StateChange} {r.Position.Coordinate.Point}").ToArray());
            await client.LogAsync($"Geovisit:\n{msg}");

            _deferral.Complete();
        }
Пример #3
0
        private void GetVisitReports(GeovisitTriggerDetails triggerDetails)
        {
            // First fill the _visitBackgroundEvents with previous existing reports saved in local settings.
            _visitBackgroundEvents.Clear();
            FillReportCollectionWithExistingReports();

            // Read reports from the triggerDetails.
            IReadOnlyList <Geovisit> reports = triggerDetails.ReadReports();

            string visitItemEvent = null;
            int    numReports     = reports.Count;

            DateTimeFormatter formatterLongTime = new DateTimeFormatter("{hour.integer}:{minute.integer(2)}:{second.integer(2)}", new[] { "en-US" }, "US", CalendarIdentifiers.Gregorian, ClockIdentifiers.TwentyFourHour);

            foreach (Geovisit report in reports)
            {
                visitItemEvent = formatterLongTime.Format(report.Timestamp);

                visitItemEvent += " " + report.StateChange.ToString();

                // Check if the report has a valid position.
                if (report.Position != null)
                {
                    visitItemEvent += " (" +
                                      report.Position.Coordinate.Point.Position.Latitude.ToString() + "," +
                                      report.Position.Coordinate.Point.Position.Longitude.ToString() +
                                      ")";
                }

                // Now add each new visit report to _VisitBackgroundEvents.
                AddVisitReport(visitItemEvent);
            }

            if (numReports != 0)
            {
                // Save all the reports back to the local settings.
                SaveExistingReports();

                // NOTE: Other notification mechanisms can be used here, such as Badge and/or Tile updates.
                DoToast(numReports, visitItemEvent);
            }
        }
Пример #4
0
        private void ProcessVisitReports(GeovisitTriggerDetails triggerDetails)
        {
            // Load the values saved from last time.
            JsonArray visitBackgroundEvents = Helpers.LoadSavedJson("BackgroundVisitEventCollection");

            // Read reports from the triggerDetails.
            IReadOnlyList <Geovisit> reports = triggerDetails.ReadReports();

            string description = null;

            foreach (Geovisit report in reports)
            {
                description = GenerateVisitDescription(report);

                // Add to the collection, trimming old events if necessary.
                if (visitBackgroundEvents.Count == maxEventDescriptors)
                {
                    visitBackgroundEvents.RemoveAt(maxEventDescriptors - 1);
                }
                visitBackgroundEvents.Insert(0, JsonValue.CreateStringValue(description));
            }

            int numReports = reports.Count;

            if (numReports > 0)
            {
                // Save the modified results for next time.
                _values["BackgroundVisitEventCollection"] = visitBackgroundEvents.Stringify();

                if (numReports > 1)
                {
                    description = $"There are {numReports} new geofence events";
                }
                Helpers.DisplayToast(description);
            }
        }