From 7ebbf99b9913de621e2f989fbdebfd066a22c4a4 Mon Sep 17 00:00:00 2001 From: r8btx <60275558+r8btx@users.noreply.github.com> Date: Mon, 20 Nov 2023 02:10:48 -0500 Subject: [PATCH 1/3] service register --- front-end/src/App.js | 2 + front-end/src/utils/serviceRegister.js | 76 ++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 front-end/src/utils/serviceRegister.js diff --git a/front-end/src/App.js b/front-end/src/App.js index 66513a5..a9356bb 100644 --- a/front-end/src/App.js +++ b/front-end/src/App.js @@ -14,6 +14,7 @@ import PrivacyPolicyPage from './components/settings/PrivacyPolicyPage'; import LoadingScreen from './components/LoadingScreen'; import TutorialComponent from './components/TutorialComponent'; import useDarkMode from './hooks/darkMode'; +import { registerService } from './utils/serviceRegister'; import './index.css'; import './css/tutorialComponent.css'; @@ -50,6 +51,7 @@ function App() { window.addEventListener('keydown', devTools); // add button press even listeners for dev tools // if first time is null, set it to true + registerService(); }, []); useEffect(() => { diff --git a/front-end/src/utils/serviceRegister.js b/front-end/src/utils/serviceRegister.js new file mode 100644 index 0000000..268e6bd --- /dev/null +++ b/front-end/src/utils/serviceRegister.js @@ -0,0 +1,76 @@ +import axios from 'axios'; + +const SERVICE_HOME_URL = 'https://passiogo.com/'; +const SERVICE_SUB_URL = 'https://nyu.passiogo.com'; +const acronymId = 1007; +const FALLBACK = { + softwareVer: 110, +}; + +// Get service endpoint version. +async function checkSoftwareVersion() { + try { + const response = await axios.get(`${SERVICE_HOME_URL}goServices.php?goWebVer=1`); + const data = response.data; + + // console.log('Version', data ? data.ver : data); + localStorage.softwareVer = data.ver; + } catch (error) { + console.error('Error fetching software version:', error); + localStorage.softwareVer = FALLBACK.softwareVer; + } +} + +// Generate a pseudo UUID +function generatePseudoUUID() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { + var r = (Math.random() * 16) | 0, + v = c === 'x' ? r : (r & 0x3) | 0x8; + return v.toString(16); + }); +} + +// Send a pseudo token to the service endpoint +function savePseudoTokenToServer() { + let token = localStorage.currentToken || `pseudo${localStorage.softwareVer}_${generatePseudoUUID()}`; + saveTokenToServer(token); +} + +// Send the current token to the service endpoint +function saveTokenToServer(currentToken) { + if (!currentToken) { + currentToken = localStorage.currentToken || ''; + } else { + localStorage.currentToken = currentToken; + } + + const queryParams = new URLSearchParams({ + register: '1', + deviceId: localStorage.deviceId, + token: currentToken, + platform: 'web', + buildNo: localStorage.softwareVer, + oldToken: localStorage.currentToken || '', + }); + + const url = `${SERVICE_SUB_URL}/goServices.php?${queryParams.toString()}`; + + axios + .get(url) + .then((response) => { + const data = response.data; + // if (parseInt(localStorage.deviceId) !== data.deviceId) { + // console.log(`Changed deviceId ${localStorage.deviceId} to ${data.deviceId}`); + // } + localStorage.deviceId = data.deviceId; + localStorage.removeItem('currentToken'); + }) + .catch((error) => { + console.error('Error while saving token to server:', error); + }); +} + +export function registerService() { + localStorage.acronymId = acronymId; + checkSoftwareVersion().then(savePseudoTokenToServer); +} From 6b5d0a7a1d2f78f8b950ae9348b31271ce91232a Mon Sep 17 00:00:00 2001 From: r8btx <60275558+r8btx@users.noreply.github.com> Date: Mon, 20 Nov 2023 03:01:27 -0500 Subject: [PATCH 2/3] get initial map center --- front-end/src/App.js | 2 ++ front-end/src/utils/mapUtility.js | 27 +++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/front-end/src/App.js b/front-end/src/App.js index a9356bb..beea306 100644 --- a/front-end/src/App.js +++ b/front-end/src/App.js @@ -15,6 +15,7 @@ import LoadingScreen from './components/LoadingScreen'; import TutorialComponent from './components/TutorialComponent'; import useDarkMode from './hooks/darkMode'; import { registerService } from './utils/serviceRegister'; +import { getUserPos } from './utils/mapUtility'; import './index.css'; import './css/tutorialComponent.css'; @@ -52,6 +53,7 @@ function App() { // if first time is null, set it to true registerService(); + getUserPos(); }, []); useEffect(() => { diff --git a/front-end/src/utils/mapUtility.js b/front-end/src/utils/mapUtility.js index c6882fb..701ff53 100644 --- a/front-end/src/utils/mapUtility.js +++ b/front-end/src/utils/mapUtility.js @@ -1,3 +1,5 @@ +import axios from 'axios'; + const nycCoordinates = [ [-73.935242, 40.73061], // Manhattan [-73.944158, 40.678178], // Brooklyn @@ -35,7 +37,7 @@ const SIMPLE_MAP = [ ]; const API_BASE = 'https://maps.googleapis.com/maps/api/js'; -//const API_KEY = ''; +const API_KEY = process.env.REACT_APP_MAP_API_KEY; const API_LIBRARIES = ['geometry', 'places']; const CALLBACK_NAME = 'gmapAPICallback'; const POS_DEFAULT = [40.716503, -73.976077]; @@ -54,8 +56,9 @@ export function loadGoogleMapsAPI(callback) { } export function initializeMap(mapRef, setIsMapLoaded, setMap) { + const center = localStorage.center ? localStorage.center.split(',') : POS_DEFAULT; const googleMap = new window.google.maps.Map(mapRef.current, { - center: new window.google.maps.LatLng(...POS_DEFAULT), + center: new window.google.maps.LatLng(...center), zoom: 13, styles: SIMPLE_MAP, options: MAP_OPTIONS, @@ -67,6 +70,26 @@ export function initializeMap(mapRef, setIsMapLoaded, setMap) { }); } +export async function getUserPos() { + const url = new URL('mapGetData.php', localStorage.serviceEndpointSub); + url.search = new URLSearchParams({ + getSystems: '2', + sortMode: '1', + deviceId: '0', + credentials: '1', + acronymId: localStorage.acronymId, + }).toString(); + + try { + const response = await axios.get(url); + const data = response.data; + const pos = data && data.lat && data.lng ? `${data.lat},${data.lng}` : POS_DEFAULT.join(','); + localStorage.center = pos; + } catch (error) { + console.error('Error fetching systems:', error); + } +} + export function getCoordinates() { return nycCoordinates; } From a11c2b280355f838a4e873f126e1b5168c8f32c3 Mon Sep 17 00:00:00 2001 From: r8btx <60275558+r8btx@users.noreply.github.com> Date: Mon, 20 Nov 2023 03:08:40 -0500 Subject: [PATCH 3/3] minor change --- front-end/src/utils/mapUtility.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front-end/src/utils/mapUtility.js b/front-end/src/utils/mapUtility.js index 701ff53..aa3a760 100644 --- a/front-end/src/utils/mapUtility.js +++ b/front-end/src/utils/mapUtility.js @@ -77,7 +77,7 @@ export async function getUserPos() { sortMode: '1', deviceId: '0', credentials: '1', - acronymId: localStorage.acronymId, + acronymId: localStorage.agencyId, }).toString(); try {