Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 6 additions & 47 deletions src/components/Navigation/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { withStyles, Typography, Button } from '@material-ui/core';
import { Clear } from '@material-ui/icons';
import dayjs from 'dayjs';

import { athena as Athena, devices as Devices } from '@commaai/api';
import { devices as Devices } from '@commaai/api';
import { primeNav, analyticsEvent } from '../../actions';
import { DEFAULT_LOCATION, MAPBOX_STYLE, MAPBOX_TOKEN, networkPositioning, reverseLookup } from '../../utils/geocode';
import { DEFAULT_LOCATION, MAPBOX_STYLE, MAPBOX_TOKEN, reverseLookup } from '../../utils/geocode';
import Colors from '../../colors';
import { PinCarIcon } from '../../icons';
import { timeFromNow } from '../../utils';
Expand Down Expand Up @@ -135,8 +135,6 @@ const initialState = {
hasFocus: false,
carLastLocation: null,
carLastLocationTime: null,
carNetworkLocation: null,
carNetworkLocationAccuracy: null,
geoLocateCoords: null,
searchSelect: null,
searchLooking: false,
Expand Down Expand Up @@ -178,7 +176,6 @@ class Navigation extends Component {
this.itemLngLat = this.itemLngLat.bind(this);
this.viewportChange = this.viewportChange.bind(this);
this.getDeviceLastLocation = this.getDeviceLastLocation.bind(this);
this.getDeviceNetworkLocation = this.getDeviceNetworkLocation.bind(this);
this.getCarLocation = this.getCarLocation.bind(this);
this.carLocationCircle = this.carLocationCircle.bind(this);
this.clearSearchSelect = this.clearSearchSelect.bind(this);
Expand All @@ -193,9 +190,9 @@ class Navigation extends Component {

componentDidUpdate(prevProps, prevState) {
const { dongleId, device } = this.props;
const { geoLocateCoords, search, carLastLocation, carNetworkLocation, searchSelect } = this.state;
const { geoLocateCoords, search, carLastLocation, searchSelect } = this.state;

if ((carLastLocation && !prevState.carLastLocation) || (carNetworkLocation && !prevState.carNetworkLocation)
if ((carLastLocation && !prevState.carLastLocation)
|| (geoLocateCoords && !prevState.geoLocateCoords) || (searchSelect && prevState.searchSelect !== searchSelect)
|| (search && prevState.search !== search)) {
this.flyToMarkers();
Expand All @@ -214,7 +211,7 @@ class Navigation extends Component {

if (!prevState.hasFocus && this.state.hasFocus) {
this.props.dispatch(analyticsEvent('nav_focus', {
has_car_location: Boolean(carLastLocation || carNetworkLocation),
has_car_location: Boolean(carLastLocation),
}));
}

Expand All @@ -239,7 +236,6 @@ class Navigation extends Component {

updateDevice() {
this.getDeviceLastLocation();
this.getDeviceNetworkLocation();
}

async getDeviceLastLocation() {
Expand All @@ -263,46 +259,9 @@ class Navigation extends Component {
}
}

async getDeviceNetworkLocation() {
const { dongleId } = this.props;

const payload = {
method: 'getNetworks',
jsonrpc: '2.0',
id: 0,
};
try {
let resp = await Athena.postJsonRpcPayload(dongleId, payload);
if (!resp.result || Object.keys(resp.result).length === 0 || !this.mounted || dongleId !== this.props.dongleId) {
return;
}
resp = await networkPositioning(resp.result);
if (resp && this.mounted && dongleId === this.props.dongleId) {
this.setState({
carNetworkLocation: [resp.lng, resp.lat],
carNetworkLocationAccuracy: resp.accuracy,
}, this.flyToMarkers);
}
} catch (err) {
if (this.mounted && dongleId === this.props.dongleId
&& (!err.message || err.message.indexOf('{"error": "Device not registered"}') === -1)) {
console.error(err);
Sentry.captureException(err, { fingerprint: 'nav_fetch_network_location' });
}
}
}

getCarLocation() {
const { carLastLocation, carLastLocationTime, carNetworkLocation, carNetworkLocationAccuracy } = this.state;
const { carLastLocation, carLastLocationTime } = this.state;

if (carNetworkLocation && carNetworkLocationAccuracy <= 10000
&& (carNetworkLocationAccuracy <= 100 || !carLastLocation)) {
return {
location: carNetworkLocation,
accuracy: carNetworkLocationAccuracy,
time: Date.now(),
};
}
if (carLastLocation) {
return {
location: carLastLocation,
Expand Down
14 changes: 0 additions & 14 deletions src/utils/geocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const DEFAULT_LOCATION = {

export const MAPBOX_STYLE = 'mapbox://styles/commaai/cjj4yzqk201c52ss60ebmow0w';
export const MAPBOX_TOKEN = 'pk.eyJ1IjoiY29tbWFhaSIsImEiOiJjangyYXV0c20wMGU2NDluMWR4amUydGl5In0.6Vb11S6tdX6Arpj6trRE_g';
const HERE_API_KEY = 'O0atgmTwzKnwYJL2hk5N5qqG2R9y78f5GdHlvr_mtiw';

const geocodingClient = mbxGeocoding({ accessToken: MAPBOX_TOKEN });

Expand Down Expand Up @@ -194,16 +193,3 @@ export async function reverseLookup(coords, navFormat = false) {
return null;
}

export async function networkPositioning(req) {
const resp = await fetch(`https://positioning.hereapi.com/v2/locate?apiKey=${HERE_API_KEY}&fallback=any,singleWifi`, {
method: 'POST',
headers: new Headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify(req),
});
if (!resp.ok) {
console.error(resp);
return null;
}
const json = await resp.json();
return json.location;
}
2 changes: 1 addition & 1 deletion src/utils/geocode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('reverseLookup', () => {
// place: 'White House Lawn',
// });
expect(await reverseLookup([-0.106640, 51.514209], true)).toEqual({
details: 'London, EC4A 2BB, United Kingdom',
details: 'London, EC4A 2BH, United Kingdom',
place: 'Fleet St',
});
expect(await reverseLookup([-2.076843, 51.894799], true)).toEqual({
Expand Down