예제 #1
0
        public LandingPageViewModel()
        {
            _progressDialogService = Locator.Current.GetService <IProgressDialogService>();
            _btDiscovery           = Locator.Current.GetService <IBtDiscovery>();
            _btClient = Locator.Current.GetService <IBtClient>();

            _btClient.ReceivedData += _btClient_ReceivedData;

            Operations = new ObservableCollection <string>();

            // Subscribe to the update of paired devices property
            this.WhenAnyValue(vm => vm._btDiscovery.PairedDevices)
            .Subscribe((pairedDevices) =>
            {
                PairedDevices = pairedDevices;
            });

            // Display a progress dialog when the bluetooth discovery takes place
            this.WhenAnyValue(vm => vm._btDiscovery.IsBusy)
            .DistinctUntilChanged()
            .Subscribe((isBusy) =>
            {
                if (isBusy)
                {
                    _progressDialogService.Show("Please wait...");
                }
                else
                {
                    _progressDialogService.Hide();
                }
            });

            // The refresh command
            Refresh = ReactiveCommand.Create(() =>
            {
                _btDiscovery.Refresh();
            });

            // Handle the selection of a device from the list
            this.WhenAnyValue(vm => vm.SelectedDevice)
            .Subscribe((device) =>
            {
                if (device == null)
                {
                    return;
                }

                if (!device.HasRequiredServiceID)
                {
                    Message = "Device not supported or service not running";
                    return;
                }

                if (_btClient.Connect(device.Address))
                {
                    // Fetch supported operations from the server
                    _btClient.SendData("getop");
                }
                else
                {
                    Message = "Unable to connect to device";
                    return;
                }
            });

            // Execute an operation
            this.Execute = ReactiveCommand.Create <string>((operation) =>
            {
                _btClient.SendData(operation);
            });

            Refresh.Execute().Subscribe();
        }
예제 #2
0
        public LandingPageViewModel(INavigation navigation, String userJson)
        {
            Message         = "";
            this.Navigation = navigation;
            userJsonString  = userJson;
            //logged = JsonConvert.DeserializeObject<User>(userJson);
            _progressDialogService = Locator.Current.GetService <IProgressDialogService>();
            _btDiscovery           = Locator.Current.GetService <IBtDiscovery>();
            _btClient = Locator.Current.GetService <IBtClient>();

            _btClient.ReceivedData += _btClient_ReceivedData;
            isLocationReady         = true;

            Command audio = new Command(async() => await AudioCheck());

            audio.Execute(null);

            // Command gps = new Command(async () => await UpdateVelocity());
            // gps.Execute(null);


            // Operations = new ObservableCollection<string>(); //commented from original code

            //GET SPEED ON A TIMER. MOVE THIS CODE FURTHER DOWN LATER ON.

            /*
             * Device.StartTimer(TimeSpan.FromSeconds(1), () =>
             * {
             *  //get location speed
             *  Command gps = new Command(async () => await UpdateVelocity());
             *  gps.Execute(null);
             *  return true; //repeat cycle
             * });*/

            Position position = new Position(36.9628066, -122.0194722);
            MapSpan  mapSpan  = new MapSpan(position, 0.01, 0.01);

            Map = new Xamarin.Forms.Maps.Map(mapSpan);

            //Set webview

            /*
             * webvw = new Android.Webkit.WebView(Context);
             * webvw.SetWebViewClient(new GeoWebViewClient());
             * webvw.SetWebChromeClient(new GeoWebChromeClient());
             * webvw.Settings.JavaScriptCanOpenWindowsAutomatically = true;
             * webvw.Settings.DisplayZoomControls = true;
             * webvw.Settings.JavaScriptEnabled = true;
             * webvw.Settings.SetGeolocationEnabled(true);
             * webvw.LoadUrl("https://www.google.com/maps");*/

            // Subscribe to the update of paired devices property
            this.WhenAnyValue(vm => vm._btDiscovery.PairedDevices)
            .Subscribe((pairedDevices) =>
            {
                PairedDevices = pairedDevices;

                /*
                 * //traverse list of paired devices looking for the one with the Raspberry Pi UUID
                 * foreach (BtDeviceInfo dev in PairedDevices)
                 * {
                 *  String devName = dev.Name;
                 *  bool hasId = dev.HasRequiredServiceID;
                 *  //if (dev != null && dev.HasRequiredServiceID)
                 *  if (dev != null && hasId)
                 *  {
                 *
                 *      if (_btClient.Connect(dev.Address))
                 *      {
                 *          // Fetch supported operations from the server
                 *          // _btClient.SendData("getop"); //commented this from original code
                 *          // Ping the device to ensure good communcation
                 *          _btClient.SendData("ping");
                 *          return;
                 *      }
                 *      else
                 *      {
                 *          Message = "Unable to connect to device";
                 *          return;
                 *      }
                 *  }
                 * }*/
            });



            // Display a progress dialog when the bluetooth discovery takes place
            this.WhenAnyValue(vm => vm._btDiscovery.IsBusy)
            .DistinctUntilChanged()
            .Subscribe((isBusy) =>
            {
                if (isBusy)
                {
                    _progressDialogService.Show("Please wait...");
                }
                else
                {
                    //traverse list of paired devices looking for the one with the Raspberry Pi UUID
                    foreach (BtDeviceInfo dev in PairedDevices)
                    {
                        String devName = dev.Name;
                        bool hasId     = dev.HasRequiredServiceID;
                        //if (dev != null && dev.HasRequiredServiceID)
                        if (dev != null && hasId)
                        {
                            if (_btClient.Connect(dev.Address))
                            {
                                // Fetch supported operations from the server
                                // _btClient.SendData("getop"); //commented this from original code
                                // Ping the device to ensure good communcation
                                _btClient.SendData("ping");
                                _progressDialogService.Hide();
                                if (Message.Equals("Java.IO.IOException"))
                                {
                                    Message = "Connection Failed! Please check Raspberry Pi is turned on and try again.";
                                }
                                return;
                            }
                            else
                            {
                                Message = "Unable to connect to device";
                                _progressDialogService.Hide();
                                return;
                            }
                        }
                    }
                    _progressDialogService.Hide();
                }
            });

            // The refresh command
            Refresh = ReactiveCommand.Create(() =>
            {
                _btDiscovery.Refresh();
            });

            //commented from original code

            /*
             * // Handle the selection of a device from the list
             * this.WhenAnyValue(vm => vm.SelectedDevice)
             *  .Subscribe((device) =>
             *  {
             *      if (device == null)
             *      {
             *          return;
             *      }
             *
             *      if(!device.HasRequiredServiceID)
             *      {
             *          Message = "Device not supported or service not running";
             *          return;
             *      }
             *
             *      if(_btClient.Connect(device.Address))
             *      {
             *          // Fetch supported operations from the server
             *          // _btClient.SendData("getop"); //commented this from original code
             *          // Ping the device to ensure good communcation
             *          _btClient.SendData("ping");
             *      }
             *      else
             *      {
             *          Message = "Unable to connect to device";
             *          return;
             *      }
             *  });*/

            //commented from original code

            /*
             * // Execute an operation
             * this.Execute = ReactiveCommand.Create<string>((operation) =>
             * {
             *  _btClient.SendData(operation);
             * });*/


            // Execute GoAlert Operation
            this.GoRearAlert = ReactiveCommand.Create(() =>
            {
                Command navigate = new Command(async() => await GotoAlertPage(true, userJsonString));
                navigate.Execute(null);
            });

            this.GoFrontAlert = ReactiveCommand.Create(() =>
            {
                Command navigate = new Command(async() => await GotoAlertPage(false, userJsonString));
                navigate.Execute(null);
            });

            // Execute Shutdown Operation
            this.Shutdown = ReactiveCommand.Create(() =>
            {
                _btClient.SendData("shutdown");
            });

            //Execute GoContactListPage Operation
            this.SeeContacts = ReactiveCommand.Create(() =>
            {
                Command navigate = new Command(async() => await GotoContactListPage(userJsonString));
                navigate.Execute(null);
            });


            // Execute SendSpeed Operation
            this.SendSpeed = ReactiveCommand.Create <string>((_speed) =>
            {
                _btClient.SendData("speed:" + _speed); //Use speed heading so that the Raspberry knows it is sending speed
            });

            Refresh.Execute().Subscribe();
        }