Ejemplo n.º 1
0
        public static void HandleException(Exception ex)
        {
            AppCenterHelper.Error("AsyncErrorHandler HandleException", ex);
#if DEBUG
            Debug.WriteLine(ex);
#endif
        }
        public async Task GetDeviceTwin()
        {
            var twin = await _registryManager.GetTwinAsync(DeviceId);

            AppCenterHelper.Track(nameof(GetDeviceTwin));

            // Update DeviceTwin
            _deviceTwin.ApplianceConnected = twin.ConnectionState == DeviceConnectionState.Connected;

            try
            {
                var        reportedPropertyForRelayState = twin.Properties.Reported["RelayState"];
                RelayState relayState = JsonConvert.DeserializeObject <RelayState>(reportedPropertyForRelayState.ToJson());
                _deviceTwin.LightsGarden = relayState.LightsGarden;
                _deviceTwin.AlarmStrobe  = relayState.AlarmStrobe;
                _deviceTwin.AlarmSiren   = relayState.AlarmSiren;
            }
            catch (Exception ex)
            {
                AppCenterHelper.Error("Error updating RelayState", ex);
            }

            try
            {
                var reportedPropertyOccupantState = twin.Properties.Reported[nameof(_deviceTwin.Occupant)];
                var occupant = JsonConvert.DeserializeObject <OccupantState>(reportedPropertyOccupantState.ToJson());
                _deviceTwin.Occupant = occupant;
            }
            catch (Exception ex)
            {
                AppCenterHelper.Error("Error updating Occupant", ex);
            }

            try
            {
                var reportedPropertyArmedState = twin.Properties.Reported[nameof(ArmedState)];
                var armedState = JsonConvert.DeserializeObject <ArmedState>(reportedPropertyArmedState.ToJson());
                _deviceTwin.ArmedState = armedState;
            }
            catch (Exception ex)
            {
                AppCenterHelper.Error("Error updating ArmedState", ex);
            }

            try
            {
                _deviceTwin.GarageDoorOpen = (bool)twin.Properties.Reported["GarageDoorOpen"];
            }
            catch (Exception ex)
            {
                AppCenterHelper.Error("Error updating GarageDoorOpen", ex);
            }
        }
        private void MonitorSecureBeacons()
        {
            _secureBeaconManager.AuthorizationStatusChanged += (object sender, SecureBeaconManagerAuthorizationStatusChangedEventArgs e) =>
            {
                StartMonitorinBeacons();
            };

            _secureBeaconManager.EnteredRegion += (object sender, SecureBeaconManagerEnteredRegionEventArgs e) =>
            {
                switch (e.Region.Identifier)
                {
                case nameof(MySecureBeacon):
                    // Do stuff
                    break;
                }

                AppCenterHelper.Track($"SecureBeacon EnteredRegion: {e.Region.Identifier}");
            };

            _secureBeaconManager.ExitedRegion += (object sender, SecureBeaconManagerExitedRegionEventArgs e) =>
            {
                switch (e.Region.Identifier)
                {
                case nameof(MySecureBeacon):
                    // Do stuff
                    break;
                }

                AppCenterHelper.Track($"SecureBeacon ExitedRegion: {e.Region.Identifier}");
            };

            _secureBeaconManager.DeterminedState += (object sender, SecureBeaconManagerDeterminedStateEventArgs e) =>
            {
                switch (e.Region.Identifier)
                {
                case nameof(MySecureBeacon):
                    // Do stuff
                    break;
                }

                AppCenterHelper.Track($"SecureBeacon DeterminedState: {e.Region.Identifier}");
            };

            try
            {
                StartMonitorinBeacons();
            }
            catch (Exception ex)
            {
                AppCenterHelper.Error(nameof(MonitorSecureBeacons), ex);
            }
        }
        public void MonitorHomeGeolocation()
        {
            LocationService.Instance.AuthorizationChanged += (s, e) =>
            {
                if (e.Status == CLAuthorizationStatus.AuthorizedAlways)
                {
                    LocationService.Instance.StartMonitoring(HomeRegion);
                }
            };

            // https://medium.com/@mizutori/tracking-highly-accurate-location-in-ios-vol-3-7cd827a84e4d
            LocationService.Instance.RequestAlwaysAuthorization();
            LocationService.Instance.AllowsBackgroundLocationUpdates    = true;
            LocationService.Instance.PausesLocationUpdatesAutomatically = false;
            LocationService.Instance.DesiredAccuracy = CLLocation.AccurracyBestForNavigation;
            LocationService.Instance.DistanceFilter  = CLLocationDistance.FilterNone;

            LocationService.Instance.RegionEntered += async(object sender, CLRegionEventArgs e) => {
                if (e.Region is CLCircularRegion && e.Region.Identifier == "Microsoft Melbourne")
                {
                    _settings.AtHome = true;

                    AppCenterHelper.Track("Microsoft Melbourne Entered");
                }
            };
            LocationService.Instance.RegionLeft += async(object sender, CLRegionEventArgs e) => {
                if (e.Region is CLCircularRegion && e.Region.Identifier == "Microsoft Melbourne")
                {
                    _settings.AtHome = false;

                    AppCenterHelper.Track("Microsoft Melbourne Left");
                }
            };
            LocationService.Instance.DidDetermineState += (object sender, CLRegionStateDeterminedEventArgs e) => {
                if (e.Region is CLCircularRegion && e.Region.Identifier == "Microsoft Melbourne")
                {
                    _settings.AtHome = e.State == CLRegionState.Inside;

                    AppCenterHelper.Track("Microsoft Melbourne Determine State");
                }
            };

            try
            {
                LocationService.Instance.StartMonitoring(HomeRegion);
            }
            catch (Exception ex)
            {
                AppCenterHelper.Error(nameof(MonitorHomeGeolocation), ex);
            }
        }
Ejemplo n.º 5
0
        private async Task <bool> InvokeMethodWithResponseState(string method, bool currentState)
        {
            AppCenterHelper.Track($"SecurityPageModel - {method}");

            var response = await ResilientCall.ExecuteWithRetry(
                () => _azureIoTHub.InvokeMethod(method));

            if (response.Outcome == OutcomeType.Successful && response.Result.Status == 200)
            {
                var stateResponse = JsonConvert.DeserializeObject <StateResponse>(response.Result.ResponsePayload);
                return(stateResponse.State);
            }

            // Error
            ShowError();
            AppCenterHelper.Error(method, response.FinalException);

            return(currentState);
        }
 public void Failed(MonitoringV2Manager manager, NSError error)
 {
     AppCenterHelper.Error("MonitoringV2Manager failed");
 }