Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Commit

Permalink
javascript optimizations, error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Katsute committed Nov 29, 2022
1 parent bb38e9b commit b779253
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.katsute</groupId>
<artifactId>mta-information-site</artifactId>
<version>3.28.0</version>
<version>3.30.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
1 change: 1 addition & 0 deletions run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -jar mta-information-site.jar
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -jar mta-information-site.jar
8 changes: 5 additions & 3 deletions site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<option value="ko" name="Korean">Korean (한국어)</option>
<option value="kri" name="Krio">Krio</option>
<option value="ku" name="Kurdish">Kurdish</option>
<option value="ckb" name="Kurdish (Sorani)">Kurdish (Sorani) (Central Kurdish)</option>
<option value="ckb" name="Kurdish (Sorani)">Kurdish (Sorani)</option>
<option value="ky" name="Kyrgyz">Kyrgyz</option>
<option value="lo" name="Lao">Lao</option>
<option value="la" name="Latin">Latin</option>
Expand All @@ -107,7 +107,7 @@
<option value="mt" name="Maltese">Maltese</option>
<option value="mi" name="Maori">Maori (Māori)</option>
<option value="mr" name="Marathi">Marathi (मराठी)</option>
<option value="mni-Mtei" name="Meiteilon">Meiteilon (mni (Meitei Mayek))</option>
<option value="mni-Mtei" name="Meiteilon">Meiteilon (Meitei Mayek)</option>
<option value="lus" name="Mizo">Mizo</option>
<option value="mn" name="Mongolian">Mongolian</option>
<option value="my" name="Myanmar">Myanmar (Burmese)</option>
Expand All @@ -132,7 +132,7 @@
<option value="st" name="Sesotho">Sesotho (Southern Sotho)</option>
<option value="sn" name="Shona">Shona</option>
<option value="sd" name="Sindhi">Sindhi</option>
<option value="si" name="Sinhala (Sinhalese)">Sinhala (Sinhalese) (Sinhala)</option>
<option value="si" name="Sinhala (Sinhalese)">Sinhala (Sinhalese)</option>
<option value="sk" name="Slovak">Slovak (slovenčina)</option>
<option value="sl" name="Slovenian">Slovenian (slovenščina)</option>
<option value="so" name="Somali">Somali</option>
Expand Down Expand Up @@ -163,5 +163,7 @@
<option value="zu" name="Zulu">Zulu</option>
</select>
</main>
<script src="mta.js"></script>
<script defer src="index.js"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions site/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

const redir = async () => {
const here = await getLocation();

// todo

window.location.href =
"/tracking" +
`?type=${type}` +
`&route=${route}` +
`&direction=${direction}` +
`&latitude=${here[0]}` +
`&longitude=${here[1]}` +
`&lang=${lang}`;
}
2 changes: 1 addition & 1 deletion site/live.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>MTA Information Site - Bus</title>
<title>MTA Information Site</title>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand Down
25 changes: 9 additions & 16 deletions site/mta.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,19 @@
const LAT = 40.74058128194693;
const LON = -73.98325077286562;

const __testMTA = async () => {
const bus = await getBusByCoord("M1", 0, LAT, LON);
const subway = await getSubwayByCoord(6, 0, LAT, LON);
console.log(bus);
console.log(await getBusByID(bus.vehicle.id));
console.log(subway);
console.log(await getSubwayByID(subway.vehicle.id));
}

/**
* @returns [latitude, longitude]
*/
const getLocation = async () => new Promise((res, rej) => {
if(!navigator.geolocation){
console.error("geolocation not supported");
rej("geolocation not supported");
res([LAT, LON]);
}else{
navigator.geolocation.getCurrentPosition(
(success) => {
res([success.coords.latitude, success.coords.longitude]);
},
(success) => res([success.coords.latitude, success.coords.longitude]),
(err) => {
console.error(err);
rej(err);
res([LAT, LON]);
}
);
}
Expand All @@ -61,6 +50,8 @@ const getLocation = async () => new Promise((res, rej) => {
* @returns bus
*/
const getBusByCoord = async (route, dir, lat, lon, lang) => {
+dir === 0 || +dir === 1 || console.warn(`Invalid direction ${dir}, expected 0 or 1`);

return await get("/request", {
"type": "bus",
"route": route,
Expand Down Expand Up @@ -90,9 +81,11 @@ const getBusByID = async (id, lang) => {
* @param {*} lat latitude
* @param {*} lon longitude
* @param {*} lang ISO 2-letter language code
* @returns bus
* @returns subway
*/
const getSubwayByCoord = async (route, dir, lat, lon, lang) => {
+dir === 1 || +dir === 3 || console.warn(`Invalid direction ${dir}, expected 1 or 3`);

return await get("/request", {
"type": "subway",
"route": route,
Expand All @@ -106,7 +99,7 @@ const getSubwayByCoord = async (route, dir, lat, lon, lang) => {
/**
* @param {*} id vehicle id
* @param {*} lang ISO 2-letter language code
* @returns bus
* @returns subway
*/
const getSubwayByID = async (id, lang) => {
return await get("/request", {
Expand Down
6 changes: 4 additions & 2 deletions site/tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ const main = document.querySelector("main");
}else return console.error(`Unknown type: ${params.type}`);

generate(data);

const m = params.type == "bus" ? getBusByID : getSubwayByID;

setInterval(() => {
main.innerHTML = "";

(params.type == "bus" ? getBusByID : getSubwayByID)(data.vehicle.id, params.lang).then(generate);
m(data.vehicle.id, params.lang).then(generate);
}, 60 * 1000);
})();

Expand Down
31 changes: 18 additions & 13 deletions src/main/java/dev/katsute/mis/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import dev.katsute.simplehttpserver.handler.file.FileOptions;

import java.io.*;
import java.net.BindException;
import java.net.URL;
import java.nio.file.Files;
import java.util.HashMap;
Expand Down Expand Up @@ -94,22 +95,26 @@ public static void main(String[] args) throws Throwable {
System.out.println("Initializing server");
// initialize server
{
final SimpleHttpServer server = SimpleHttpServer.create(8080);
server.setExecutor(Executors.newCachedThreadPool());
try{
final SimpleHttpServer server = SimpleHttpServer.create(8080);
server.setExecutor(Executors.newCachedThreadPool());

final FileHandler handler = new MimeFileHandler();
final FileOptions options = new FileOptions.Builder()
.setLoadingOption(FileOptions.FileLoadingOption.LIVE)
.setWalk(true)
.build();
handler.addFile(new File("site/index.html"), "/", options);
handler.addDirectory(new File("site"), "/", options);
server.createContext("/", handler);
server.createContext("/request", new RequestHandler(mta));
final FileHandler handler = new MimeFileHandler();
final FileOptions options = new FileOptions.Builder()
.setLoadingOption(FileOptions.FileLoadingOption.LIVE)
.setWalk(true)
.build();
handler.addFile(new File("site/index.html"), "/", options);
handler.addDirectory(new File("site"), "/", options);
server.createContext("/", handler);
server.createContext("/request", new RequestHandler(mta));

server.start();
server.start();

System.out.println("Server started at localhost:8080");
System.out.println("Server started at localhost:8080");
}catch(final BindException e){
System.out.println("Failed to start server, server is already running or port is occupied");
}
}
}

Expand Down

0 comments on commit b779253

Please sign in to comment.