public ConnectionPageViewModel( INavigationService navigationService, IDriverRepository driverRepository, IEventHubRepository eventHubRepository, IOBDDevice iOBDDevice) : base(navigationService) { this.DriverRepository = driverRepository; this.OBDDevice = iOBDDevice; this.EventHubRepository = eventHubRepository; Title = "Connection Page"; ConnectCommand = new DelegateCommand(Connect); SimulateConnectionCommand = new DelegateCommand(SimulateConnection); ClearCommand = new DelegateCommand(Clear); CurrentTrip = new Trip { Id = Guid.NewGuid().ToString(), Points = new ObservableCollection <TripPoint>() }; fuelConsumptionRate = 0; //FuelConsumptionUnits = Settings.MetricUnits ? "Liters" : "Gallons"; FuelConsumptionUnits = "Gallons"; DistanceUnits = "Kilometers"; ElapsedTime = "0s"; Distance = "0.0"; FuelConsumption = "N/A"; EngineLoad = "N/A"; }
private async Task <bool> ConnectToObdDeviceWithConfirmation() { var isConnected = false; var progress = Acr.UserDialogs.UserDialogs.Instance.Loading("Connecting to OBD Device...", maskType: Acr.UserDialogs.MaskType.Clear); if (obdDevice == null) { obdDevice = ServiceLocator.Instance.Resolve <IOBDDevice>(); } try { isConnected = await Task.Run(async() => await obdDevice.Initialize()).WithTimeout(5000); } catch (Exception ex) { Logger.Instance.WriteLine(ex.ToString()); } finally { progress.Dispose(); } if (!isConnected) { var result = await Acr.UserDialogs.UserDialogs.Instance.ConfirmAsync( "Unable to connect to an OBD device, please ensure one is configured. Would you like to attempt to connect to an OBD device again?", "OBD Connection", "Retry", "Use Simulator"); if (result) { //Attempt to connect to OBD device again isConnected = await ConnectToObdDeviceWithConfirmation(); } else { //Use the OBD simulator try { isConnected = await Task.Run(async() => await obdDevice.Initialize(true)).WithTimeout(5000); IsObdDeviceSimulated = obdDevice.IsSimulated; } catch (Exception ex) { Logger.Instance.WriteLine(ex.ToString()); } } } return(isConnected); }
//Init must be called each time to connect and reconnect to the OBD device public async Task Initialize(IStoreManager storeManager) { //Ensure that initialization is only performed once if (!isInitialized) { isInitialized = true; this.storeManager = storeManager; //Get platform specific implementation IOBDDevice obdDevice = ServiceLocator.Instance.Resolve<IOBDDevice>(); //Start listening for connectivity change event so that we know if connection is restablished\dropped when pushing data to the IOT Hub CrossConnectivity.Current.ConnectivityChanged += Current_ConnectivityChanged; //Provision the device with the IOT Hub var connectionStr = await DeviceProvisionHandler.GetHandler().ProvisionDevice(); iotHub.Initialize(connectionStr); //Check right away if there is any trip data left in the buffer that needs to be sent to the IOT Hub - run this thread in the background SendBufferedDataToIOTHub(); } }
//Init must be called each time to connect and reconnect to the OBD device public async Task Initialize(IStoreManager storeManager) { //Ensure that initialization is only performed once if (!isInitialized) { isInitialized = true; this.storeManager = storeManager; //Get platform specific implementation IOBDDevice obdDevice = ServiceLocator.Instance.Resolve <IOBDDevice>(); //Start listening for connectivity change event so that we know if connection is restablished\dropped when pushing data to the IOT Hub CrossConnectivity.Current.ConnectivityChanged += Current_ConnectivityChanged; //Provision the device with the IOT Hub var connectionStr = await DeviceProvisionHandler.GetHandler().ProvisionDevice(); iotHub.Initialize(connectionStr); //Check right away if there is any trip data left in the buffer that needs to be sent to the IOT Hub - run this thread in the background SendBufferedDataToIOTHub(); } }
private async Task<bool> ConnectToObdDeviceWithConfirmation() { var isConnected = false; var progress = Acr.UserDialogs.UserDialogs.Instance.Loading("Connecting to OBD Device...", maskType: Acr.UserDialogs.MaskType.Clear); if (obdDevice == null) { obdDevice = ServiceLocator.Instance.Resolve<IOBDDevice>(); } try { isConnected = await Task.Run(async () => await obdDevice.Initialize()).WithTimeout(5000); } catch (Exception ex) { Logger.Instance.Report(ex); } finally { progress.Dispose(); } if (!isConnected) { var result = await Acr.UserDialogs.UserDialogs.Instance.ConfirmAsync( "Unable to connect to an OBD device, please ensure one is configured. Would you like to attempt to connect to an OBD device again?", "OBD Connection", "Retry", "Use Simulator"); if (result) { //Attempt to connect to OBD device again isConnected = await ConnectToObdDeviceWithConfirmation(); } else { //Use the OBD simulator try { isConnected = await Task.Run(async () => await obdDevice.Initialize(true)).WithTimeout(5000); IsObdDeviceSimulated = obdDevice.IsSimulated; } catch (Exception ex) { Logger.Instance.Report(ex); } } } return isConnected; }