コード例 #1
0
        /// <summary>
        /// Queries for the most current event
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="maxAge">Timespan containing max age of "current"</param>
        /// <returns></returns>
        public static async Task <MotionActivityEvent> GetCurrentActivity(this IMotionActivityManager activity, TimeSpan?maxAge = null)
        {
            maxAge = maxAge ?? TimeSpan.FromMinutes(5);
            var end    = DateTimeOffset.UtcNow;
            var start  = end.Subtract(maxAge.Value);
            var result = (await activity.Query(start, end)).OrderBy(x => x.Timestamp).FirstOrDefault();

            return(result);
        }
コード例 #2
0
        public TestMotionActivityViewModel(IMotionActivityManager manager, IDialogs dialogs)
        {
            var tm = manager as TestMotionActivityManager;

            this.IsGeneratingData = tm?.IsGeneratingTestData ?? false;
            this.ActivityType     = tm?.GeneratingActivityType ?? MotionActivityType.Automotive;
            this.Confidence       = tm?.GeneratingConfidence ?? MotionActivityConfidence.High;
            this.IntervalSeconds  = Convert.ToInt32(tm?.GeneratingInterval?.TotalSeconds ?? 10d);

            this.SelectActivityType = dialogs.PickEnumValueCommand <MotionActivityType>(
                "Select Activity Type",
                x => this.ActivityType = x
                );
            this.SelectConfidence = dialogs.PickEnumValueCommand <MotionActivityConfidence>(
                "Select Confidence",
                x => this.Confidence = x
                );

            this.Toggle = ReactiveCommand.Create(
                () =>
            {
                if (tm.IsGeneratingTestData)
                {
                    tm.StopGeneratingTestData();
                    this.IsGeneratingData = false;
                }
                else
                {
                    tm.StartGeneratingTestData(
                        this.ActivityType,
                        TimeSpan.FromSeconds(this.IntervalSeconds),
                        this.Confidence
                        );
                    this.IsGeneratingData = true;
                }
            },
                this.WhenAny(
                    x => x.IntervalSeconds,
                    (ts) =>
            {
                if (tm == null)
                {
                    return(false);
                }

                if (ts.GetValue() <= 0)
                {
                    return(false);
                }

                return(true);
            }
                    )
                );
        }
コード例 #3
0
        //public static async Task<IList<MotionActivityTimeBlock>> GetTimeBlocksForRange(this IMotionActivity activity,
        //                                                                               DateTimeOffset start,
        //                                                                               DateTimeOffset? end = null,
        //                                                                               MotionActivityConfidence minConfidence = MotionActivityConfidence.Medium)
        //{
        //    var list = new List<MotionActivityTimeBlock>();
        //    var result = await activity.Query(start, end);
        //    var set = result
        //        .Where(x => x.Confidence >= minConfidence)
        //        .OrderBy(x => x.Timestamp)
        //        .ToList();

        //    if (set.Count > 1)
        //    {
        //        MotionActivityEvent firstEvent = null;
        //        foreach (var item in set)
        //        {
        //            if (firstEvent == null)
        //                firstEvent = item;
        //            else if (!firstEvent.Types.HasFlag(item.Types)) // has to have 1 of the types
        //            {
        //                var block = new MotionActivityTimeBlock(item.Types, firstEvent.Timestamp, item.Timestamp);
        //                list.Add(block);

        //                // first event of next time block
        //                firstEvent = item;
        //            }

        //        }
        //    }

        //    return list;
        //}


        //public static async Task<IDictionary<MotionActivityType, TimeSpan>> GetTotalsForRange(this IMotionActivity activity,
        //                                                                                      DateTimeOffset start,
        //                                                                                      DateTimeOffset? end = null,
        //                                                                                      MotionActivityConfidence minConfidence = MotionActivityConfidence.Medium)
        //{
        //    var dict = new Dictionary<MotionActivityType, TimeSpan>();
        //    var result = await activity.Query(start, end);
        //    var set = result
        //        .Where(x => x.Confidence >= minConfidence)
        //        .OrderBy(x => x.Timestamp)
        //        .ToList();

        //    if (set.Count > 1)
        //    {
        //        MotionActivityEvent lastEvent = null;
        //        foreach (var item in set)
        //        {
        //            if (lastEvent == null)
        //                lastEvent = item;
        //            else
        //            {
        //                if (!dict.ContainsKey(item.Types))
        //                    dict.Add(item.Types, TimeSpan.Zero);

        //                var ts = item.Timestamp.Subtract(lastEvent.Timestamp);
        //                dict[item.Types] += ts;
        //            }

        //        }
        //    }
        //    return dict;
        //}


        /// <summary>
        /// Queries for the most current event and checks against type & confidence
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="type"></param>
        /// <param name="maxAge"></param>
        /// <param name="minConfidence"></param>
        /// <returns></returns>
        public static async Task <bool> IsCurrentActivity(this IMotionActivityManager activity, MotionActivityType type, TimeSpan?maxAge = null, MotionActivityConfidence minConfidence = MotionActivityConfidence.Medium)
        {
            var result = await activity.GetCurrentActivity(maxAge);

            //if (result == default(MotionActivityEvent))
            //return false;
            if (result.Confidence < minConfidence)
            {
                return(false);
            }

            return(result.Types.HasFlag(type));
        }
コード例 #4
0
        public ListViewModel(IUserDialogs dialogs, IMotionActivityManager activityManager = null)
        {
            this.activityManager = activityManager;

            this.Load = ReactiveCommand.CreateFromTask(async() =>
            {
                if (this.activityManager == null)
                {
                    await dialogs.Alert("MotionActivity is not supported on this platform");
                    return;
                }

                var result = await this.activityManager.RequestPermission();
                if (result != Shiny.AccessState.Available)
                {
                    await dialogs.Alert("Motion Activity is not available - " + result);
                    return;
                }

                var activities = await activityManager.QueryByDate(this.Date);
                this.Events    = activities
                                 .OrderByDescending(x => x.Timestamp)
                                 .Select(x => new CommandItem
                {
                    Text   = $"({x.Confidence}) {x.Types}",
                    Detail = $"{x.Timestamp.LocalDateTime}"
                })
                                 .ToList();

                this.EventCount = this.Events.Count;
            });
            this.BindBusyCommand(this.Load);

            this.WhenAnyValue(x => x.Date)
            .DistinctUntilChanged()
            .Select(_ => Unit.Default)
            .InvokeCommand((ICommand)this.Load)
            .DisposeWith(this.DestroyWith);
        }
コード例 #5
0
        /// <summary>
        /// Queries for activities for an entire day (beginning to end)
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static Task <IList <MotionActivityEvent> > QueryByDate(this IMotionActivityManager activity, DateTimeOffset date)
        {
            var range = date.GetRangeForDate();

            return(activity.Query(range.Start, range.End));
        }
コード例 #6
0
 /// <summary>
 /// Queries if most recent activity is stationary
 /// </summary>
 /// <param name="activity"></param>
 /// <param name="maxAge"></param>
 /// <param name="minConfidence"></param>
 /// <returns></returns>
 public static Task <bool> IsCurrentStationary(this IMotionActivityManager activity, TimeSpan?maxAge = null, MotionActivityConfidence minConfidence = MotionActivityConfidence.Medium)
 => activity.IsCurrentActivity(MotionActivityType.Stationary, maxAge, minConfidence);
コード例 #7
0
        /// <summary>
        /// Queries for activities for an entire day (beginning to end)
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static Task <IList <MotionActivityEvent> > QueryByDate(this IMotionActivityManager activity, DateTimeOffset date)
        {
            var endOfDay = date.Date.AddDays(1).AddTicks(-1);

            return(activity.Query(date.Date, endOfDay));
        }
コード例 #8
0
 public OtherExtensionsViewModel(IMotionActivityManager activityManager, IUserDialogs dialogs)
 {
     this.activityManager = activityManager;
     this.dialogs         = dialogs;
 }