mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
add stop and system data updates from api
This commit is contained in:
@@ -5,11 +5,12 @@ import { resolvers } from "./resolvers";
|
||||
import { sharedMemory } from "./sharedMemory";
|
||||
import { loadTestData } from "./testData";
|
||||
import { ServerContext } from "./serverContext";
|
||||
import { startDataUpdater } from "./sharedMemoryUpdater";
|
||||
|
||||
const typeDefs = readFileSync("./schema.graphql", "utf8");
|
||||
|
||||
async function main() {
|
||||
loadTestData(sharedMemory);
|
||||
// loadTestData(sharedMemory);
|
||||
|
||||
const server = new ApolloServer<ServerContext>({
|
||||
typeDefs,
|
||||
@@ -27,6 +28,8 @@ async function main() {
|
||||
},
|
||||
});
|
||||
|
||||
startDataUpdater();
|
||||
|
||||
console.log(`Server ready at: ${url}`);
|
||||
}
|
||||
|
||||
|
||||
96
src/sharedMemoryUpdater.ts
Normal file
96
src/sharedMemoryUpdater.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { sharedMemory } from "./sharedMemory";
|
||||
import { Stop, System } from "./generated/graphql";
|
||||
|
||||
const baseUrl = "https://passiogo.com/mapGetData.php";
|
||||
|
||||
async function updateStopAndOrderedStopDataForSystem(systemToConstruct: System) {
|
||||
const params = {
|
||||
getStops: "2",
|
||||
};
|
||||
|
||||
const formDataJsonObject = {
|
||||
"s0": systemToConstruct.id,
|
||||
"sA": "1",
|
||||
}
|
||||
const formData = new FormData();
|
||||
formData.set("json", JSON.stringify(formDataJsonObject));
|
||||
|
||||
const query = new URLSearchParams(params).toString();
|
||||
const response = await fetch(`${baseUrl}?${query}`, {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
const json = await response.json();
|
||||
|
||||
if (typeof json.stops === "object") {
|
||||
const jsonStops = Object.values(json.stops);
|
||||
|
||||
systemToConstruct.stops = jsonStops.map((jsonStop: any) => {
|
||||
return {
|
||||
id: jsonStop.id,
|
||||
name: jsonStop.name,
|
||||
coordinates: {
|
||||
latitude: jsonStop.latitude,
|
||||
longitude: jsonStop.longitude,
|
||||
},
|
||||
system: systemToConstruct,
|
||||
etas: [],
|
||||
orderedStops: [],
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function updateSystemData() {
|
||||
// Construct the graph from the bottom up, so that
|
||||
// system data is populated with all necessary fields
|
||||
|
||||
const params = {
|
||||
getSystems: "2",
|
||||
};
|
||||
const query = new URLSearchParams(params).toString();
|
||||
const response = await fetch(`${baseUrl}?${query}`);
|
||||
const json = await response.json()
|
||||
|
||||
if (typeof json.all === "object") {
|
||||
sharedMemory.systems = await Promise.all(json.all.map(async (jsonSystem: any) => {
|
||||
const constructedSystem: System = {
|
||||
id: jsonSystem.id,
|
||||
name: jsonSystem.fullname,
|
||||
routes: [],
|
||||
stops: [],
|
||||
shuttles: [],
|
||||
};
|
||||
|
||||
try {
|
||||
await updateStopAndOrderedStopDataForSystem(constructedSystem);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
return constructedSystem;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchDataAndUpdateStore() {
|
||||
try {
|
||||
await updateSystemData();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function runDataUpdateScheduler() {
|
||||
// while (true) {
|
||||
const timeoutInMilliseconds = 10000;
|
||||
await fetchDataAndUpdateStore();
|
||||
await new Promise((resolve) => setTimeout(resolve, timeoutInMilliseconds));
|
||||
// }
|
||||
}
|
||||
|
||||
function startDataUpdater() {
|
||||
runDataUpdateScheduler();
|
||||
}
|
||||
|
||||
export { startDataUpdater };
|
||||
Reference in New Issue
Block a user