Ejemplo n.º 1
0
        protected Selector <TModel, TSelector> Select(params Expression <Func <TSelector, object> >[] expressions)
        {
            foreach (var expression in expressions)
            {
                var member = (expression.Body as MemberExpression)?.Member;

                if (member == null)
                {
                    var unary = (expression.Body as UnaryExpression)?.Operand;
                    member = (unary as MemberExpression)?.Member;
                }

                if (member == null)
                {
                    continue;
                }

                if (Selections.TryGetValue(member.Name, out var selection))
                {
                    selection.IsSelected = true;
                }

                if (!SelectedProperties.ContainsKey(member.Name))
                {
                    SelectedProperties.Add(member.Name, PropertyInfoMapping[member.Name]);
                }
            }

            return(this);
        }
Ejemplo n.º 2
0
        public async Task OnListItemTapped(FeaturesViewModel vm)
        {
            // Check if the Feature is an Action
            if (vm.FeatureType == FeatureType.Action)
            {
                // NOTE: Currently hard coded to supported actions which will require a new version of the app to support new actions.
                // The server view models need to be changed to support the abstract definition of actions so they can be cerated and supported
                // dynamically without a change to the code
                vm.IsBusy = !vm.IsBusy;
                // persist the change in busy status
                FullFeatureCollection[CurrentDeviceSelection][vm.Index].IsBusy = vm.IsBusy;
                if (vm.FeatureName.Contains("brew") || vm.FeatureName.Contains("Brew"))
                {
                    if (vm.IsBusy)
                    {
                        // if it is now busy, invoke the actions

                        // set brew strength
                        // try to get the brew strength
                        if (!Selections.TryGetValue("Brew Strength", out Tuple <object, Type> brewStrength))
                        {
                            brewStrength = new Tuple <object, Type>(1, typeof(int));
                        }
                        var response1 = await IoTHubDataService.Instance.InvokeFeature(CurrentDeviceId, "changeBrewStrength", Convert.ToString(brewStrength.Item1));

                        if (response1 == null)
                        {
                            vm.IsBusy = false;
                            await App.Current.MainPage.DisplayAlert("Sorry", "Your Device isn't responding. \nPlease ensure it has power and is connected, and try again.", "Ok");
                        }
                        else if (response1.Status.ToLower() != "success")
                        {
                            vm.IsBusy = false;
                            await App.Current.MainPage.DisplayAlert("Sorry", "Your Device has sent an error. \nPlease check it and try again", "Ok");
                        }
                        else
                        {
                            // Invoke brew related feature action
                            var response2 = await IoTHubDataService.Instance.InvokeFeature(CurrentDeviceId, vm.FeatureMethodName, vm.FeatureMethodParameter);

                            // if it was actioned successfully
                            if (response2 == null)
                            {
                                vm.IsBusy = false;
                                await App.Current.MainPage.DisplayAlert("Sorry", "Your Device isn't responding. \nPlease ensure it has power and is connected, and try again.", "Ok");
                            }
                            else if (response2.Status.ToLower() != "success")
                            {
                                vm.IsBusy = false;
                                await App.Current.MainPage.DisplayAlert("Sorry", "Your Device has sent an error. \nPlease check it and try again", "Ok");
                            }
                            else
                            {
                                //set an asynchronous timer based upon the returned DateTimeStamp
                                var UTCCompleteTime = response2.TimeTillCompletion;
                                var currentUTCTime  = DateTime.Now.ToUniversalTime();
                                var completeInMs    = UTCCompleteTime.Subtract(currentUTCTime).TotalMilliseconds > 0 ? UTCCompleteTime.Subtract(currentUTCTime).TotalMilliseconds : 0;

                                TimerStateObjClass StateObj = new TimerStateObjClass {
                                    Vm = vm, SelectedDevice = CurrentDeviceSelection
                                };
                                // Could move Timer to be a field of the deviceViewModel
                                TimerCallback TimerDelegate = new System.Threading.TimerCallback((o) =>
                                {
                                    var state       = (TimerStateObjClass)o;
                                    state.Vm.IsBusy = false;
                                    // Persist the busy state
                                    FullFeatureCollection[state.SelectedDevice][state.Vm.Index].IsBusy = false;
                                    state.TimerReference.Dispose();
                                });
                                vm.Timer = new Timer(TimerDelegate, StateObj, (int)completeInMs + 2000, Timeout.Infinite);
                                StateObj.TimerReference = vm.Timer;
                            }
                        }
                    }
                    else
                    {
                        // Cancel the action
                        var response2 = await IoTHubDataService.Instance.InvokeFeature(CurrentDeviceId, vm.FeatureMethodName, vm.FeatureMethodParameter);

                        if (vm.Timer != null)
                        {
                            vm.Timer.Dispose();
                        }
                    }
                }
                else
                {
                    // Invoke feature action - manipulate Is busy as necessary
                    var response2 = await IoTHubDataService.Instance.InvokeFeature(CurrentDeviceId, vm.FeatureMethodName, vm.FeatureMethodParameter);

                    vm.IsBusy = false;
                }
                // persist the resultant busy state
                FullFeatureCollection[CurrentDeviceSelection][vm.Index].IsBusy = vm.IsBusy;
            }
        }