コード例 #1
0
        public async Task<ObdModel> GetObdData(PeerInformationWrapper selectedDevice)
        {
            Thread.Sleep(1000);
            soc = random.Next(soc - 5, soc);

            return new ObdModel()
            {
                SoC = ((double)soc + random.NextDouble()) / 100,
                Capacity = capacity,
                RawCapacity = 66,
                Temperature1 = random.Next(32, 100),
                Temperature2 = random.Next(32, 100),
                Temperature3 = random.Next(32, 100),
                Temperature4 = random.Next(32, 100),
                When = DateTime.Now.ToUniversalTime()
            };
        }
コード例 #2
0
        public async Task<ObdModel> GetObdData(PeerInformationWrapper selectedDevice)
        {
            string errorMessage = null;
            string[] result = null;
            try
            {
                ObdModel model = null;
                result = await this.InitializeAndReadObdStream(selectedDevice.PeerInformation);

                // Charge
                string[] rows = result[(int)ObdGroups.Charge].Split('\r');
                if (rows != null &&
                    rows.Length == 8)
                {
                    string socString = rows[4].Replace(" ", "").Trim().Substring(14);
                    string capString = rows[5].Replace(" ", "").Trim().Substring(8, 6);

                    int socValue = int.Parse(socString, NumberStyles.AllowHexSpecifier);
                    int capValue = int.Parse(capString, NumberStyles.AllowHexSpecifier);

                    model = new ObdModel()
                    {
                        SoC = (double)socValue / 1000000,
                        Capacity = (double)capValue / (totalCapacity * 10000),
                        RawCapacity = (double)capValue / 10000,
                        When = DateTime.Now.ToUniversalTime()
                    };
                }
                else
                {
                    // retry
                    if (result[(int)ObdGroups.Charge].StartsWith("NO"))
                    {
                        errorMessage = "No charge data available";
                    }
                    else
                    {
                        errorMessage = "Communication interrupted";
                    }
                }

                if (errorMessage == null)
                {
                    rows = result[(int)ObdGroups.Temperature].Split('\r');
                    if (rows != null &&
                        rows.Length == 5)
                    {
                        int temp1 = int.Parse(rows[0].Replace(" ", "").Trim().Substring(16, 2), NumberStyles.AllowHexSpecifier);
                        int temp2 = int.Parse(rows[1].Replace(" ", "").Trim().Substring(8, 2), NumberStyles.AllowHexSpecifier);
                        int temp3 = int.Parse(rows[1].Replace(" ", "").Trim().Substring(14, 2), NumberStyles.AllowHexSpecifier);
                        int temp4 = int.Parse(rows[2].Replace(" ", "").Trim().Substring(6, 2), NumberStyles.AllowHexSpecifier);

                        model.Temperature1 = temp1 * 1.8 + 32.0;
                        model.Temperature2 = temp2 * 1.8 + 32.0;
                        model.Temperature3 = temp3 * 1.8 + 32.0;
                        model.Temperature4 = temp4 * 1.8 + 32.0;
                    }
                    else
                    {
                        // retry
                        if (result[(int)ObdGroups.Temperature].StartsWith("NO"))
                        {
                            errorMessage = "No temperature data available";
                        }
                        else
                        {
                            errorMessage = "Communication interrupted";
                        }
                    }
                }

                if (model != null)
                {
                    return model;
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }

            throw new Exception(errorMessage);
        }
コード例 #3
0
ファイル: MainPage.xaml.cs プロジェクト: sirfergy/LEAF-Logger
        private void GetObdData()
        {
            if (App.RunningInEmulator &&
                this.progressIndicatorVisibility)
            {
                return;
            }

            Dispatcher.BeginInvoke(() =>
            {
                this.SetProgressIndicatorVisibility(true);
            });

            ThreadPool.QueueUserWorkItem(async (object state) =>
            {
                // If user changes the device name, null it out so that future calls will re-create the device.
                if (this.device != null &&
                    this.device.DisplayName != App.MainViewModel.SettingsViewModel.BluetoothDeviceName)
                {
                    this.device = null;
                }

                if (this.device == null)
                {
                    try
                    {
                        this.device = await this.service.GetObdDevice(App.MainViewModel.SettingsViewModel.BluetoothDeviceName);
                        if (this.device == null &&
                            this.RunningInForeground())
                        {
                            Dispatcher.BeginInvoke(() =>
                            {
                                MessageBox.Show("No OBD II devices found of name " + App.MainViewModel.SettingsViewModel.BluetoothDeviceName, "Error", MessageBoxButton.OK);
                                this.SetProgressIndicatorVisibility(false);
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        Dispatcher.BeginInvoke(() =>
                        {
                            this.SetProgressIndicatorVisibility(false);
                            if (this.RunningInForeground())
                            {
                                if ((uint)ex.HResult == ObdErrorCodes.ERR_BLUETOOTH_OFF)
                                {
                                    var result = MessageBox.Show("Bluetooth is turned off, would you like to see the current Bluetooth settings?", "Bluetooth Off", MessageBoxButton.OKCancel);
                                    if (result == MessageBoxResult.OK)
                                    {
                                        ConnectionSettingsTask connectionSettingsTask = new ConnectionSettingsTask();
                                        connectionSettingsTask.ConnectionSettingsType = ConnectionSettingsType.Bluetooth;
                                        connectionSettingsTask.Show();
                                    }
                                }
                                else
                                {
                                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
                                }
                            }
                        });
                    }
                }

                if (this.device != null)
                {
                    try
                    {
                        ObdModel data = await this.service.GetObdData(this.device);
                        this.DisplayObdData(data);
                    }
                    catch (Exception ex)
                    {
                        Dispatcher.BeginInvoke(() =>
                        {
                            this.SetProgressIndicatorVisibility(false);
                            if (this.RunningInForeground())
                            {
                                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
                            }
                        });
                    }
                }
            });
        }