public TripModel(ObdModel start, ObdModel end) { this.TripStart = start.When; this.TripEnd = end.When; this.StartSoC = start.SoC; this.EndSoC = end.SoC; this.KiloWattHoursUsed = end.TotalKiloWattHoursUsed; this.AverageEnergyEconomy = end.AverageEnergyEconomy; this.Distance = end.TotalDistance; this.Capacity = end.Capacity; this.RawCapacity = end.RawCapacity; this.Ascent = end.TotalAscent; this.Descent = end.TotalDescent; }
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); }
private void DisplayObdData(ObdModel obdModel) { obdModel.GeoCoordinate = this.lastGeoCoordinate; TimeSpan duration; if (this.tracking && App.MainViewModel.MapObdUpdates.Count > 0 && App.MainViewModel.Geocoordinates.Count() > 1 && this.lastGeoCoordinate != null) { // Get the first map update ObdModel firstMapUpdate = App.MainViewModel.MapObdUpdates.First(); // get the last map obd update ObdModel lastMapUpdate = App.MainViewModel.MapObdUpdates.Last(); // trip duration duration = lastMapUpdate.When.Subtract(firstMapUpdate.When).Duration(); // get all points since last check var geoCoordinates = App.MainViewModel.Geocoordinates.Where(x => x.Timestamp > lastMapUpdate.When).Select(x => x.GeoCoordinate).ToList(); // add last map update to the beginning of the list geoCoordinates.Insert(0, lastMapUpdate.GeoCoordinate); // calculate the distance from the last map update to now double distance = 0; double ascent = 0; double descent = 0; GeoCoordinate first, second; for (int i = 0; i < geoCoordinates.Count - 1; i++) { first = geoCoordinates[i]; second = geoCoordinates[i + 1]; distance += first.GetDistanceTo(second); if (second.Altitude > first.Altitude) { ascent += (second.Altitude - first.Altitude); } else if (first.Altitude > second.Altitude) { descent += (first.Altitude - second.Altitude); } } obdModel.Distance = distance / 1609.344; obdModel.TotalDistance = lastMapUpdate.TotalDistance + obdModel.Distance; obdModel.Ascent = ascent; obdModel.TotalAscent = lastMapUpdate.TotalAscent + ascent; obdModel.Descent = descent; obdModel.TotalDescent = lastMapUpdate.TotalDescent + descent; obdModel.KiloWattHoursUsed = lastMapUpdate.AvailableKiloWattHours - obdModel.AvailableKiloWattHours; obdModel.TotalKiloWattHoursUsed = firstMapUpdate.AvailableKiloWattHours - obdModel.AvailableKiloWattHours; } Dispatcher.BeginInvoke(() => { this.SetProgressIndicatorVisibility(false); this.dataPivotItem.DataContext = obdModel; this.track.DataContext = obdModel; if (this.tracking && this.lastGeoCoordinate != null) { if (App.MainViewModel.SettingsViewModel.LiveTileEnabled) { ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault(); if (shellTile != null) { wideBackTileUserControl.DataContext = obdModel; backTileUserControl.DataContext = obdModel; TextBlock backTileDuration = (TextBlock)backTileUserControl.FindName("duration"); TextBlock wideBackTileDuration = (TextBlock)wideBackTileUserControl.FindName("duration"); backTileDuration.Text = string.Format("Trip Duration: {0:h\\:mm\\:ss}", duration); wideBackTileDuration.Text = string.Format("Trip Duration: {0:h\\:mm\\:ss}", duration); ExtendedImage wideBackTileImage = wideBackTileUserControl.ToImage(); ExtendedImage backTileImage = backTileUserControl.ToImage(); using (var storageFile = IsolatedStorageFile.GetUserStoreForApplication()) { if (storageFile.FileExists(BackBackgroundImage)) { storageFile.DeleteFile(BackBackgroundImage); } if (storageFile.FileExists(WideBackBackgroundImage)) { storageFile.DeleteFile(WideBackBackgroundImage); } using (var stream = new IsolatedStorageFileStream(WideBackBackgroundImage, FileMode.Create, FileAccess.Write, storageFile)) { encoder.Encode(wideBackTileImage, stream); } using (var stream = new IsolatedStorageFileStream(BackBackgroundImage, FileMode.Create, FileAccess.Write, storageFile)) { encoder.Encode(backTileImage, stream); } } shellTile.Update( new FlipTileData() { BackContent = string.Empty, WideBackContent = string.Empty, BackBackgroundImage = new Uri("isostore:" + BackBackgroundImage, UriKind.Absolute), WideBackBackgroundImage = new Uri("isostore:" + WideBackBackgroundImage, UriKind.Absolute), Count = (int)(obdModel.SoC * 100.0) }); } } App.MainViewModel.Add(obdModel); } }); }
public void Add(ObdModel newObd) { obdDb.ObdModels.InsertOnSubmit(newObd); obdDb.SubmitChanges(); MapObdUpdates.Add(newObd); }