private async void connectionManager_MessageReceived(string message)
        {
            string[] messagesArray = message.Split(';');
            foreach (var msg in messagesArray)
            {
                string[] messageArray = msg.Split(':');
                switch (messageArray[0])
                {
                case "LIGHT":
                    stateManager.LightOn = messageArray[1] == "ON" ? true : false;
                    Dispatcher.BeginInvoke(delegate()
                    {
                        lightButton.Background = stateManager.LightOn ? (App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush) : new SolidColorBrush(Colors.Transparent);
                    });
                    break;

                case "DISPLAY":
                    stateManager.DisplayOn = messageArray[1] == "ON" ? true : false;
                    Dispatcher.BeginInvoke(delegate()
                    {
                        displayButton.Background = stateManager.DisplayOn ? (App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush) : new SolidColorBrush(Colors.Transparent);
                    });
                    break;

                case "TIME":
                    Int32 unixTimestamp = (Int32)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                    await connectionManager.SendCommand("TIME:" + unixTimestamp.ToString());

                    break;
                }
            }
        }
示例#2
0
        /// <summary>
        /// Method to get current coordinate asynchronously so that the UI thread is not blocked. Updates MyCoordinate.
        /// Using Location API requires ID_CAP_LOCATION capability to be included in the Application manifest file.
        /// </summary>
        private async void GetCurrentCoordinate()
        {
            ShowProgressIndicator(AppResources.GettingLocationProgressText);
            if (App.Geolocator == null)
            {
                // Use the app's global Geolocator variable
                App.Geolocator = new Geolocator();
                App.Geolocator.DesiredAccuracy = PositionAccuracy.High;
            }

            try
            {
                Geoposition currentPosition = await App.Geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));

                _accuracy = currentPosition.Coordinate.Accuracy;
                string latitude       = currentPosition.Coordinate.Latitude.ToString("0.000000");
                string longitude      = currentPosition.Coordinate.Longitude.ToString("0.000000");
                string accuracy       = currentPosition.Coordinate.Accuracy.ToString();
                string speed          = currentPosition.Coordinate.Speed.ToString() == "NaN" ? "0" : (currentPosition.Coordinate.Speed).ToString();
                string direction      = currentPosition.Coordinate.Heading.ToString() == "NaN" ? "0" : currentPosition.Coordinate.Heading.ToString();
                string locationMethod = currentPosition.Coordinate.PositionSource.ToString();
                Dispatcher.BeginInvoke(async() =>
                {
                    MyCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);
                    DrawMapMarkers();
                    MyMap.SetView(MyCoordinate, 12, MapAnimationKind.Parabolic);
                    sessionID               = Guid.NewGuid().ToString();
                    Location location       = new Location();
                    location.Latitude       = Double.Parse(latitude);
                    location.Longitude      = Double.Parse(longitude);
                    location.Accuracy       = int.Parse(accuracy);
                    location.Speed          = float.Parse(speed);
                    location.Direction      = int.Parse(direction);
                    location.LocationMethod = locationMethod;
                    location.GpsTime        = DateTimeOffset.Now.UtcDateTime;
                    if (ConnectionHelper.isConnected)
                    {
                        ICollection <Threat> threats = await ConnectionHelper.locationHubProxy.Invoke <ICollection <Threat> >("SendLocation", location, userInfo.PhoneNumber);
                        if (threats.Count != 0)
                        {
                            string threatString = "This area is potential to threats. This area is prone to the following threats.\n";
                            foreach (Threat threat in threats)
                            {
                                threatString += threat.Type + " ";
                            }
                            Dispatcher.BeginInvoke(() =>
                            {
                                MessageBox.Show(threatString, "Alert!", MessageBoxButton.OK);
                            });

                            SpeechSynthesizer speech = new SpeechSynthesizer();
                            await speech.SpeakTextAsync(threatString);
                            BluetoothConnectionManager connectionManager = new BluetoothConnectionManager();
                            await connectionManager.Connect();
                            await connectionManager.SendCommand(threatString);
                        }
                    }
                });
            }
            catch (Exception)
            {
                // Couldn't get current location - location might be disabled in settings
                MessageBox.Show(AppResources.LocationDisabledMessageBoxText, AppResources.ApplicationTitle, MessageBoxButton.OK);
            }
            HideProgressIndicator();
        }