From 441e7aee30d9af3023320ff91568809daa46956a Mon Sep 17 00:00:00 2001 From: blockarchitech Date: Mon, 24 Jun 2024 20:49:01 -0700 Subject: [PATCH] Get data from transit api (with the all new flashy API!) --- .gitignore | 3 +- subway.svg | 81 ++++++++++++++++++ watchapps/subway.pdc | Bin 0 -> 234 bytes watchapps/transit/package-lock.json | 13 +++ watchapps/transit/package.json | 11 ++- watchapps/transit/src/c/main.c | 10 ++- watchapps/transit/src/c/transit.c | 11 +-- watchapps/transit/src/c/transit.h | 1 + watchapps/transit/src/pkjs/index.js | 17 ++-- watchapps/transit/src/pkjs/sender.js | 30 +++++-- watchapps/transit/src/pkjs/transit.js | 118 ++++++++++++++++++++++++++ 11 files changed, 269 insertions(+), 26 deletions(-) create mode 100644 subway.svg create mode 100644 watchapps/subway.pdc create mode 100644 watchapps/transit/package-lock.json diff --git a/.gitignore b/.gitignore index fd51909..29c9fa4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ -.lock-waf* \ No newline at end of file +.lock-waf* +node_modules/ \ No newline at end of file diff --git a/subway.svg b/subway.svg new file mode 100644 index 0000000..a43974c --- /dev/null +++ b/subway.svg @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + diff --git a/watchapps/subway.pdc b/watchapps/subway.pdc new file mode 100644 index 0000000000000000000000000000000000000000..7e943bbcedf599391561b18cd9be7deb7aa862d7 GIT binary patch literal 234 zcmY+8y$XQ=6o#MkeMn+3C^C?LW$;r56S3G#W|A@}vn*mZxd^xM-GSl`T!PWU`=O-M z^Pb*wPH$&4=nrpz(Nsm$y`aP)C|eiqXAs~_k_8>wY{_!4zeU+vEN#u+j;09r9TB5O zMd;EWggG%~#f~v~o71zd^=xECYuJVd6Ww^O2O(mVMEYG7X;nTYzDW)jW?Z@JPtSe& S568*> 16) & 0xFF; + var g = (color >> 8) & 0xFF; + var b = color & 0xFF; + r = Math.round(r * 0.8); + g = Math.round(g * 0.8); + b = Math.round(b * 0.8); + return (r << 16) | (g << 8) | b; +} + +function unixToMinutes(unix) { + // Convert Unix time to minutes + var date = new Date(unix * 1000); + var hours = date.getHours(); + var minutes = date.getMinutes(); + return hours * 60 + minutes; +} + +// Function to get data and print it +function data() { + return new Promise(function(resolve, reject) { + var url = root + "/public/nearby_routes?lat=" + lat + "&lon=" + lng + "&max_distance=500"; + var xhr = new XMLHttpRequest(); + xhr.open("GET", url, true); + xhr.setRequestHeader("apiKey", api_key); + xhr.onreadystatechange = function () { + if (xhr.readyState === 4 && xhr.status === 200) { + var data = JSON.parse(xhr.responseText); + console.log(data); + var pebble = []; + // Iterate through the routes and print the required details + for (var i = 0; i < data.routes.length; i++) { + // if departure time is undefined, skip this route + try { + if (data.routes[i].itineraries[0].schedule_items[0].departure_time === undefined) { + continue; + } + } catch (e) { + continue; + } + var route_short_name = data.routes[i].route_short_name ? data.routes[i].route_short_name : "N/A"; + var mode_name = data.routes[i].mode_name; + var headsign = data.routes[i].itineraries[0].merged_headsign; + var departure_time = data.routes[i].itineraries[0].schedule_items[0].departure_time ? data.routes[i].itineraries[0].schedule_items[0].departure_time : 0; + var stop_name = data.routes[i].itineraries[0].closest_stop.stop_name; + var route_color = data.routes[i].route_color; + var route_highlight = darkenColor(hexToRGB(route_color)); + route_highlight = RGBToHex(route_highlight[0], route_highlight[1], route_highlight[2]); + console.log("Route: " + route_short_name + ", Mode: " + mode_name + ", Destination: " + headsign + ", Departure Time: " + departure_time + ", Stop: " + stop_name); + // Convert to data useful for Pebble + + // are we black and white? + if (Pebble.getActiveWatchInfo) { + var watch = Pebble.getActiveWatchInfo(); + if (watch.platform === "aplite" || watch.platform === "diorite") { + route_color = "#AAAAAA"; + route_highlight = "#FFFFFF"; + } + } + + console.log(unixToMinutes(departure_time)); + + pebble.push({ + "name": route_short_name, + "time": unixToMinutes(departure_time), + "destination": headsign, + "color": convertHex(route_color), + "highlight": convertHex(route_highlight) + }); + } + + resolve(pebble); + } else { + console.log("State: " + xhr.readyState) + console.log("Resp. Text: " + xhr.responseText) + console.log("Status: " + xhr.status) + console.log("Error: " + xhr.statusText); + } + }; + + xhr.onerror = function () { + reject(this.statusText); + }; + xhr.send(); + }); +} + +module.exports = { + data: data +}; \ No newline at end of file