Compare commits
No commits in common. "main" and "ci/init" have entirely different histories.
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,3 @@
|
|||||||
result
|
result
|
||||||
vrnp
|
vrnp
|
||||||
main
|
|
||||||
*.swp
|
*.swp
|
||||||
|
15
README.md
15
README.md
@ -2,21 +2,12 @@
|
|||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
### Server
|
|
||||||
|
|
||||||
- [x] Use timeout for fetching departures
|
- [x] Use timeout for fetching departures
|
||||||
- [x] Add basic auth
|
- [x] Add basic auth
|
||||||
- [x] Create Nix package
|
- [x] Create Nix package
|
||||||
- [x] Create container
|
- [x] Create container
|
||||||
|
- [x] Write ESP8266 client
|
||||||
|
- [ ] Make port configurable
|
||||||
- [x] Transfer using JSON
|
- [x] Transfer using JSON
|
||||||
- [x] Correctly implement basic auth
|
- [x] Correctly implement basic auth
|
||||||
- [ ] Make port configurable
|
- [ ] Use unidecode to replace non-ascii stuff in the backend
|
||||||
- [ ] Use unidecode to replace non-ascii stuff
|
|
||||||
- [ ] Add query parameter for selecting EFAClient
|
|
||||||
- [ ] Return current time for displaying clock
|
|
||||||
|
|
||||||
### Client
|
|
||||||
|
|
||||||
- [x] Write ESP8266 client
|
|
||||||
- [x] Add code for handling button presses
|
|
||||||
- [ ] Add clock
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 4.0 KiB |
@ -81,17 +81,3 @@ static const uint8_t PROGMEM spinny[][8] = {
|
|||||||
0x00,
|
0x00,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
static const uint16_t PROGMEM tiny_train_dims[] = {24, 9, 1};
|
|
||||||
static const uint8_t PROGMEM tiny_train[][27] = {
|
|
||||||
{
|
|
||||||
0x00, 0x04, 0x00,
|
|
||||||
0x00, 0x02, 0x00,
|
|
||||||
0x01, 0xff, 0xfe,
|
|
||||||
0x00, 0xa4, 0x94,
|
|
||||||
0x01, 0x24, 0x92,
|
|
||||||
0x01, 0x24, 0x92,
|
|
||||||
0x03, 0xfc, 0xff,
|
|
||||||
0x03, 0xff, 0xff,
|
|
||||||
0x01, 0xb0, 0x36,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
@ -1,300 +1,39 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <Adafruit_GFX.h>
|
|
||||||
#include <Adafruit_ST7735.h>
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266HTTPClient.h>
|
|
||||||
#include <memory>
|
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <Adafruit_GFX.h>
|
||||||
|
#include <Adafruit_SSD1306.h>
|
||||||
|
#include <ESP8266HTTPClient.h>
|
||||||
#include <WiFiClientSecureBearSSL.h>
|
#include <WiFiClientSecureBearSSL.h>
|
||||||
|
|
||||||
#include "bitmaps.h"
|
#include "bitmaps.h"
|
||||||
#include "wifi_credentials.h"
|
#include "wifi_credentials.h"
|
||||||
|
|
||||||
#define TFT_CS D8
|
// Screen stuff
|
||||||
#define TFT_DC D4
|
#define SCREEN_WIDTH 128 // OLED display width, in pixels
|
||||||
#define TFT_RESET D3
|
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
|
||||||
|
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
|
||||||
|
#define SCREEN_ADDRESS 0x3C
|
||||||
|
|
||||||
// Button stuff
|
// Button stuff
|
||||||
#define BUTTON_PIN D6
|
#define BUTTON_PIN D6
|
||||||
|
|
||||||
// Display stuff
|
|
||||||
#define FRAME_DELAY 80
|
#define FRAME_DELAY 80
|
||||||
#define COLOR_BG (rgb(0, 0, 0))
|
|
||||||
#define COLOR_DIVIDER (rgb(0xff, 0x60, 0x0d))
|
|
||||||
#define COLOR_TOP (rgb(0xa0, 0xa0, 0xa0))
|
|
||||||
#define COLOR_TEXT (rgb(0xff, 0x60, 0x0d))
|
|
||||||
|
|
||||||
// Layout
|
|
||||||
#define WIDTH 160
|
|
||||||
#define HEIGHT 128
|
|
||||||
|
|
||||||
#define CX 6
|
|
||||||
#define CY 8
|
|
||||||
|
|
||||||
#define MARGIN_LEFT 5
|
|
||||||
#define MARGIN_RIGHT 5
|
|
||||||
|
|
||||||
#define TOP_X MARGIN_LEFT
|
|
||||||
#define TOP_Y 4
|
|
||||||
#define TOP_WIDTH (WIDTH - 2 * MARGIN_LEFT)
|
|
||||||
#define TOP_HEIGHT CY
|
|
||||||
|
|
||||||
#define MAIN_X MARGIN_LEFT
|
|
||||||
#define MAIN_Y 19
|
|
||||||
#define MAIN_WIDTH (WIDTH - 2 * MARGIN_LEFT)
|
|
||||||
#define MAIN_HEIGHT (8 * CY)
|
|
||||||
|
|
||||||
#define CLOCK_X MARGIN_LEFT
|
|
||||||
#define CLOCK_Y 113
|
|
||||||
#define CLOCK_WIDTH (5 * CX)
|
|
||||||
#define CLOCK_HEIGHT CY
|
|
||||||
|
|
||||||
#define STATUS_X 83
|
|
||||||
#define STATUS_Y CLOCK_Y
|
|
||||||
#define STATUS_WIDTH (12 * CX)
|
|
||||||
#define STATUS_HEIGHT CY
|
|
||||||
|
|
||||||
struct Timetable {
|
|
||||||
const char *label;
|
|
||||||
const char *path;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Selectable timetables
|
|
||||||
Timetable timetables[] = {
|
|
||||||
{"Johanneskirche Bstg. 1", "/departures?stop_id=de:08311:30104&platform=1"},
|
|
||||||
{"Johanneskirche Bstg. 2", "/departures?stop_id=de:08311:30104&platform=2"},
|
|
||||||
{"Blumenthalstr Bstg. A", "/departures?stop_id=de:08221:1225&platform=A"},
|
|
||||||
{"Blumenthalstr Bstg. B", "/departures?stop_id=de:08221:1225&platform=B"}
|
|
||||||
};
|
|
||||||
size_t selectedTimetable = 0;
|
|
||||||
|
|
||||||
// Function definitions
|
|
||||||
void drawLayout();
|
|
||||||
void clearTop();
|
|
||||||
void clearStatus();
|
|
||||||
void clearMain();
|
|
||||||
void clearClock();
|
|
||||||
uint16_t rgb(uint16_t, uint16_t, uint16_t);
|
|
||||||
String fetchDepartures(String);
|
|
||||||
|
|
||||||
// Peripherals
|
// Peripherals
|
||||||
|
|
||||||
Adafruit_ST7735 display(TFT_CS, TFT_DC, TFT_RESET);
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||||
|
|
||||||
// App state interface
|
// App state
|
||||||
|
|
||||||
// init
|
|
||||||
// │
|
|
||||||
// │
|
|
||||||
// │
|
|
||||||
// │ ┌───┐
|
|
||||||
// ┌──▼──────▼┐ │
|
|
||||||
// │Connecting│ │!connected
|
|
||||||
// └──┬───▲──┬┘ │
|
|
||||||
// │ │ └───┘
|
|
||||||
// │ │
|
|
||||||
// connected│ │!connected
|
|
||||||
// │ │
|
|
||||||
// │ │
|
|
||||||
// │ │ ┌───┐
|
|
||||||
// ┌──▼───┴─┐ timeout (3s) ┌────────▼┐ │
|
|
||||||
// │Fetching◄──────────────────┤Selecting│ │
|
|
||||||
// └──┬───▲─┘ └────▲───┬┘ │button pressed
|
|
||||||
// │ │ │ └───┘
|
|
||||||
// got response│ │timeout (25s) │
|
|
||||||
// │ │ │
|
|
||||||
// │ │ │
|
|
||||||
// │ │ │button pressed
|
|
||||||
// ┌────▼───┴─────────┐ │
|
|
||||||
// │Showing Departures│───────────────┘
|
|
||||||
// └──────────────────┘
|
|
||||||
|
|
||||||
class State {
|
|
||||||
public:
|
|
||||||
// When the state is entered
|
|
||||||
virtual void enter();
|
|
||||||
// Called in the business loop
|
|
||||||
virtual void tick();
|
|
||||||
// Called when the button is pushed
|
|
||||||
virtual void button();
|
|
||||||
virtual ~State() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
class StateConnecting : public State {
|
|
||||||
public:
|
|
||||||
void enter() override;
|
|
||||||
void tick() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
class StateFetching : public State {
|
|
||||||
public:
|
|
||||||
void enter() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
class StateShowingDepartures : public State {
|
|
||||||
private:
|
|
||||||
// Todo: discard JsonDocument asap, parse into better format
|
|
||||||
JsonDocument departures;
|
|
||||||
unsigned long entered;
|
|
||||||
public:
|
|
||||||
StateShowingDepartures(String&);
|
|
||||||
void enter() override;
|
|
||||||
void tick() override;
|
|
||||||
void button() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
class StateSelecting : public State {
|
|
||||||
private:
|
|
||||||
unsigned long entered;
|
|
||||||
public:
|
|
||||||
void enter() override;
|
|
||||||
void tick() override;
|
|
||||||
void button() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Empty default implementations
|
|
||||||
|
|
||||||
void State::enter() {}
|
|
||||||
void State::tick() {}
|
|
||||||
void State::button() {}
|
|
||||||
|
|
||||||
// App state implementation
|
|
||||||
|
|
||||||
uint64_t currentTick = 0;
|
|
||||||
// Initially true so that enter() of the initial state is called.
|
|
||||||
bool stateChanged = true;
|
|
||||||
std::unique_ptr<State> state = std::make_unique<StateConnecting>();
|
|
||||||
|
|
||||||
template <typename StateType, typename... Args>
|
|
||||||
void setState(Args&&... args) {
|
|
||||||
state = std::make_unique<StateType>(std::forward<Args>(args)...);
|
|
||||||
stateChanged = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void StateConnecting::enter() {
|
|
||||||
Serial.println("Entering StateConnecting");
|
|
||||||
|
|
||||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
|
||||||
display.setCursor(STATUS_X, STATUS_Y);
|
|
||||||
display.setTextColor(COLOR_TEXT);
|
|
||||||
display.printf("connecting");
|
|
||||||
}
|
|
||||||
|
|
||||||
void StateConnecting::tick() {
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
display.fillRect(STATUS_X + 11 * CX, STATUS_Y, CX, CY, COLOR_BG);
|
|
||||||
display.setCursor(STATUS_X + 11 * CX, STATUS_Y);
|
|
||||||
display.setTextColor(COLOR_TEXT);
|
|
||||||
display.print("|/-\\"[currentTick % 4]);
|
|
||||||
delay(125);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setState<StateFetching>();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetching is a bit of an ugly duckling; it performs all its logic in enter().
|
|
||||||
// This is because the request it does is synchronous.
|
|
||||||
// Sadly this means no animation is possible right now.
|
|
||||||
// At least the fetch call still does yield() under the hood :)
|
|
||||||
void StateFetching::enter() {
|
|
||||||
Serial.println("Entering StateFetching");
|
|
||||||
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
setState<StateConnecting>();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
|
||||||
display.setTextColor(COLOR_TEXT);
|
|
||||||
display.setCursor(STATUS_X, STATUS_Y);
|
|
||||||
display.print(" fetching");
|
|
||||||
|
|
||||||
String departuresRaw = fetchDepartures(timetables[selectedTimetable].path);
|
|
||||||
|
|
||||||
setState<StateShowingDepartures>(departuresRaw);
|
|
||||||
};
|
|
||||||
|
|
||||||
StateShowingDepartures::StateShowingDepartures(String &departuresRaw) {
|
|
||||||
deserializeJson(departures, departuresRaw);
|
|
||||||
}
|
|
||||||
|
|
||||||
void StateShowingDepartures::enter() {
|
|
||||||
Serial.println("Entering StateShowingDepartures");
|
|
||||||
entered = millis();
|
|
||||||
|
|
||||||
clearStatus();
|
|
||||||
clearMain();
|
|
||||||
|
|
||||||
// draw timetable
|
|
||||||
display.setTextColor(COLOR_TEXT);
|
|
||||||
int line = 0;
|
|
||||||
for (JsonVariant departure : departures["departures"].as<JsonArray>()) {
|
|
||||||
if (line > 8) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
char symbol[3] = {0};
|
|
||||||
char direction[16] = {0};
|
|
||||||
|
|
||||||
utf8ToCP437German(departure["symbol"].as<const char *>(), symbol, std::size(symbol));
|
|
||||||
utf8ToCP437German(departure["direction"].as<const char *>(), direction, std::size(direction));
|
|
||||||
|
|
||||||
display.setCursor(MAIN_X, MAIN_Y + (CY + 3) * line);
|
|
||||||
display.printf("%2s %-15.15s ", symbol, direction);
|
|
||||||
if (departure["leaving"].as<String>().equals("sofort")) {
|
|
||||||
int16_t x = display.getCursorX();
|
|
||||||
int16_t y = display.getCursorY();
|
|
||||||
display.drawBitmap(x + 6 * CX - tiny_train_dims[0], y, tiny_train[0], tiny_train_dims[0], tiny_train_dims[1], COLOR_TEXT);
|
|
||||||
} else {
|
|
||||||
display.printf("%6s", departure["leaving"].as<const char*>());
|
|
||||||
}
|
|
||||||
|
|
||||||
line++;
|
|
||||||
}
|
|
||||||
|
|
||||||
clearClock();
|
|
||||||
display.setCursor(CLOCK_X, CLOCK_Y);
|
|
||||||
display.printf("%5s", departures["serverTime"].as<const char *>());
|
|
||||||
}
|
|
||||||
|
|
||||||
void StateShowingDepartures::tick() {
|
|
||||||
unsigned long elapsed = millis() - entered;
|
|
||||||
if (elapsed > 25000) {
|
|
||||||
setState<StateFetching>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void StateShowingDepartures::button() {
|
|
||||||
setState<StateSelecting>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void StateSelecting::enter() {
|
|
||||||
Serial.println("Entering StateSelecting");
|
|
||||||
entered = millis();
|
|
||||||
|
|
||||||
clearMain();
|
|
||||||
clearStatus();
|
|
||||||
}
|
|
||||||
|
|
||||||
void StateSelecting::tick() {
|
|
||||||
unsigned long elapsed = millis() - entered;
|
|
||||||
if (elapsed > 3000) {
|
|
||||||
setState<StateFetching>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void StateSelecting::button() {
|
|
||||||
entered = millis();
|
|
||||||
selectedTimetable = (selectedTimetable + 1) % (sizeof timetables / sizeof timetables[0]);
|
|
||||||
clearTop();
|
|
||||||
display.setCursor(TOP_X, TOP_Y);
|
|
||||||
display.setTextColor(COLOR_TOP);
|
|
||||||
display.print(timetables[selectedTimetable].label);
|
|
||||||
}
|
|
||||||
|
|
||||||
// button ISR setup
|
|
||||||
|
|
||||||
|
enum {
|
||||||
|
CONNECTING,
|
||||||
|
SHOWING_DEPARTURES
|
||||||
|
} state = CONNECTING;
|
||||||
|
int frame = 0;
|
||||||
int buttonPushed = false;
|
int buttonPushed = false;
|
||||||
|
JsonDocument departures;
|
||||||
|
|
||||||
void ICACHE_RAM_ATTR onButtonFalling() {
|
void ICACHE_RAM_ATTR onButtonFalling() {
|
||||||
buttonPushed = true;
|
buttonPushed = true;
|
||||||
@ -303,83 +42,47 @@ void ICACHE_RAM_ATTR onButtonFalling() {
|
|||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
Serial.println("serial init");
|
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
|
||||||
display.initR();
|
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
|
||||||
display.setSPISpeed(40000000);
|
Serial.println("SSD1306 allocation failed");
|
||||||
display.cp437(true);
|
for(;;); // Don't proceed, loop forever
|
||||||
display.setRotation(3);
|
}
|
||||||
Serial.println("display initialized");
|
Serial.println("SSD1306 initialized");
|
||||||
Serial.printf("display dimensions: %" PRId16 "x%" PRId16 "\n", display.width(), display.height());
|
display.clearDisplay();
|
||||||
|
|
||||||
drawLayout();
|
|
||||||
display.setCursor(TOP_X, TOP_Y);
|
|
||||||
display.setTextColor(COLOR_TOP);
|
|
||||||
display.print(timetables[selectedTimetable].label);
|
|
||||||
|
|
||||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||||
|
|
||||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||||
attachInterrupt(BUTTON_PIN, onButtonFalling, FALLING);
|
attachInterrupt(BUTTON_PIN, onButtonFalling, FALLING);
|
||||||
|
|
||||||
state = std::make_unique<StateConnecting>();
|
display.setTextColor(SSD1306_INVERSE);
|
||||||
|
display.setTextSize(1);
|
||||||
|
display.setCursor(0, 0);
|
||||||
|
display.printf("Connecting to\n%s\n", WIFI_SSID);
|
||||||
|
display.display();
|
||||||
|
/*
|
||||||
|
display.print(" 5 Mannheim sofort\n"
|
||||||
|
"26 Kirchheim 9 min\n"
|
||||||
|
" 5 Weinheim 20:13");
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void drawSpinner(int frame) {
|
||||||
if (stateChanged) {
|
display.fillRect(120, 56, spinny_dims[0], spinny_dims[1], SSD1306_BLACK);
|
||||||
stateChanged = false;
|
display.drawBitmap(120, 56, spinny[frame % spinny_dims[2]], spinny_dims[0], spinny_dims[1], SSD1306_WHITE);
|
||||||
// Note: enter() may call setState(). In that case we want to end up right back here.
|
|
||||||
state->enter();
|
|
||||||
} else if (buttonPushed) {
|
|
||||||
// "Handle" jitter
|
|
||||||
delay(100);
|
|
||||||
buttonPushed = false;
|
|
||||||
state->button();
|
|
||||||
} else {
|
|
||||||
state->tick();
|
|
||||||
currentTick++;
|
|
||||||
}
|
|
||||||
// Let background system do its thing
|
|
||||||
yield();
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t rgb(uint16_t r, uint16_t g, uint16_t b) {
|
|
||||||
// Color layout: RRRRRGGGGGGBBBBB (5, 6, 5)
|
|
||||||
return ((b >> 3) & 0b11111) << 11 | ((g >> 2) & 0b111111) << 5 | ((r >> 3) & 0b11111);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearTop() {
|
|
||||||
display.fillRect(TOP_X, TOP_Y, TOP_WIDTH, TOP_HEIGHT, COLOR_BG);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearMain() {
|
|
||||||
display.fillRect(MAIN_X, MAIN_Y, MAIN_WIDTH, MAIN_HEIGHT, COLOR_BG);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearStatus() {
|
|
||||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearClock() {
|
|
||||||
display.fillRect(CLOCK_X, CLOCK_Y, CLOCK_WIDTH, CLOCK_HEIGHT, COLOR_BG);
|
|
||||||
}
|
|
||||||
|
|
||||||
void drawLayout() {
|
|
||||||
display.fillScreen(COLOR_BG);
|
|
||||||
display.drawFastHLine(5, 15, 150, COLOR_DIVIDER);
|
|
||||||
display.drawFastHLine(5, 109, 150, COLOR_DIVIDER);
|
|
||||||
display.drawFastHLine(5, 123, 150, COLOR_DIVIDER);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Error handling
|
// TODO: Error handling
|
||||||
String fetchDepartures(String path) {
|
String fetchDepartures() {
|
||||||
std::unique_ptr<BearSSL::WiFiClientSecure> https(new BearSSL::WiFiClientSecure);
|
std::unique_ptr<BearSSL::WiFiClientSecure> https(new BearSSL::WiFiClientSecure);
|
||||||
https->setInsecure();
|
https->setInsecure();
|
||||||
|
|
||||||
HTTPClient client;
|
HTTPClient client;
|
||||||
if (!client.begin(*https, "vrnp.beany.club", 443, path)) {
|
if (!client.begin(*https, "vrnp.beany.club", 443, "/departures?stop_id=de:08221:1225&platform=A")) {
|
||||||
display.fillScreen(COLOR_BG);
|
display.clearDisplay();
|
||||||
display.setCursor(0, 0);
|
display.setCursor(0, 0);
|
||||||
display.print("begin failed");
|
display.print("begin failed");
|
||||||
|
display.display();
|
||||||
for (;;);
|
for (;;);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -387,78 +90,88 @@ String fetchDepartures(String path) {
|
|||||||
client.addHeader("Accept", "application/json");
|
client.addHeader("Accept", "application/json");
|
||||||
int statusCode = client.GET();
|
int statusCode = client.GET();
|
||||||
if (statusCode != 200) {
|
if (statusCode != 200) {
|
||||||
display.fillScreen(COLOR_BG);
|
display.clearDisplay();
|
||||||
display.setCursor(0, 0);
|
display.setCursor(0, 0);
|
||||||
display.printf("http non-ok: %d\n", statusCode);
|
display.printf("http non-ok: %d\n", statusCode);
|
||||||
|
display.display();
|
||||||
for (;;);
|
for (;;);
|
||||||
}
|
}
|
||||||
|
|
||||||
return client.getString();
|
return client.getString();
|
||||||
}
|
}
|
||||||
|
|
||||||
char mapUtf8CodepointToCP437German(uint32_t codepoint) {
|
void loop() {
|
||||||
switch (codepoint) {
|
switch (state) {
|
||||||
case 0x00e4: return 0x84; // ä
|
case CONNECTING: {
|
||||||
case 0x00c4: return 0x8e; // Ä
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
case 0x00f6: return 0x94; // ö
|
drawSpinner(frame++);
|
||||||
case 0x00d6: return 0x99; // Ö
|
display.display();
|
||||||
case 0x00fc: return 0x81; // ü
|
delay(FRAME_DELAY);
|
||||||
case 0x00dc: return 0x9a; // Ü
|
return;
|
||||||
case 0x00df: return 0xe1; // ß
|
|
||||||
default: return '?';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
display.clearDisplay();
|
||||||
|
display.fillRect(0, 56, 128, 8, SSD1306_INVERSE);
|
||||||
|
display.setCursor(0, 56);
|
||||||
|
display.print("requesting departures");
|
||||||
|
display.display();
|
||||||
|
|
||||||
|
String departuresRaw = fetchDepartures();
|
||||||
|
deserializeJson(departures, departuresRaw);
|
||||||
|
|
||||||
|
state = SHOWING_DEPARTURES;
|
||||||
|
frame = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SHOWING_DEPARTURES:
|
||||||
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
|
state = CONNECTING;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Maps german umlauts and sharp s from utf8 to cp437 encoding.
|
if (buttonPushed) {
|
||||||
Other utf8 characters and malformed utf8 are replaced by '?'.
|
buttonPushed = false;
|
||||||
The resulting string is truncated to cp437StrSize.
|
delay(FRAME_DELAY);
|
||||||
|
return;
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
void utf8ToCP437German(const char *utf8Str, char *cp437Str, size_t cp437StrSize) {
|
|
||||||
size_t utf8StrLength = strlen(utf8Str);
|
|
||||||
size_t utf8StrIndex = 0;
|
|
||||||
size_t cp437StrIndex = 0;
|
|
||||||
|
|
||||||
char c;
|
// Marquee effect for direction strings longer than 11 characters
|
||||||
|
// Neither particularly efficient nor legible
|
||||||
|
// (This could use some love)
|
||||||
|
|
||||||
// One char at the end of the cp437Str is reserved to the terminating null
|
int availableWidth = 11;
|
||||||
while (utf8StrIndex < utf8StrLength && cp437StrIndex + 1 < cp437StrSize) {
|
|
||||||
uint8_t cu0 = utf8Str[utf8StrIndex];
|
|
||||||
|
|
||||||
if (cu0 < 0x80) {
|
display.clearDisplay();
|
||||||
// ASCII maps to ASCII
|
display.setCursor(0, 0);
|
||||||
c = cu0;
|
for (JsonVariant departure : departures["departures"].as<JsonArray>()) {
|
||||||
utf8StrIndex++;
|
const char *directionStr = departure["direction"].as<const char *>();
|
||||||
} else if ((cu0 & 0b11100000) == 0b11000000) {
|
|
||||||
// codepoints encoded as two code units may contain german special characters
|
char buf[128] = {0};
|
||||||
if (utf8StrIndex + 1 >= utf8StrLength) {
|
const char *dirStart;
|
||||||
// if there's no 10xxxxxxx byte after this one, we've reached the end (malformed)
|
|
||||||
c = '?';
|
if (strlen(directionStr) <= availableWidth || (strlen(directionStr) + 3 + availableWidth) > 127) {
|
||||||
break;
|
dirStart = directionStr;
|
||||||
} else {
|
} else {
|
||||||
// otherwise decode the codepoint and map it to cp437
|
memcpy(buf, directionStr, strlen(directionStr));
|
||||||
uint8_t cu1 = utf8Str[utf8StrIndex + 1];
|
memcpy(buf + strlen(directionStr), " ", 3);
|
||||||
uint32_t cp = (cu0 & 0x1f << 6) | cu1 & 0x3f;
|
memcpy(buf + strlen(directionStr) + 3, directionStr, availableWidth);
|
||||||
c = mapUtf8CodepointToCP437German(cp);
|
buf[strlen(directionStr) + 3 + availableWidth] = '\0';
|
||||||
utf8StrIndex += 2;
|
dirStart = buf + frame % (strlen(directionStr) + 3);
|
||||||
}
|
|
||||||
// for 3 and 4-code unit codepoints just put a ? and skip all their code units
|
|
||||||
// here we dont care whether the string is malformed
|
|
||||||
} else if ((cu0 & 0b11110000) == 0b11100000) {
|
|
||||||
c = '?';
|
|
||||||
utf8StrIndex += 3;
|
|
||||||
} else if ((cu0 & 0b11111000) == 0b11110000) {
|
|
||||||
c = '?';
|
|
||||||
utf8StrIndex += 4;
|
|
||||||
} else {
|
|
||||||
// catch all for malformed utf8
|
|
||||||
c = '?';
|
|
||||||
utf8StrIndex++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cp437Str[cp437StrIndex] = c;
|
display.printf("%2s %-11.11s %6s\n",
|
||||||
cp437StrIndex++;
|
departure["symbol"].as<const char*>(),
|
||||||
|
dirStart,
|
||||||
|
departure["leaving"].as<const char*>()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
display.display();
|
||||||
|
|
||||||
cp437Str[cp437StrIndex] = '\0';
|
frame++;
|
||||||
|
delay(500);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
@ -43,4 +43,3 @@ def generate_bitmap_from_gif(file: Path, fd):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
generate_bitmap_from_gif(Path("./animations/spinny.gif"), sys.stdout)
|
generate_bitmap_from_gif(Path("./animations/spinny.gif"), sys.stdout)
|
||||||
generate_bitmap_from_gif(Path("./animations/tiny_train.gif"), sys.stdout)
|
|
||||||
|
143
efa_client.go
143
efa_client.go
@ -1,143 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "net/http"
|
|
||||||
import "net/url"
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
type EFAClient interface {
|
|
||||||
GetName() string
|
|
||||||
BuildRequest(string) (*http.Request, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
var allEfaClients []EFAClient = []EFAClient{
|
|
||||||
BwegtEFAClient{},
|
|
||||||
VRNEFAClient{},
|
|
||||||
KVVEFAClient{},
|
|
||||||
VAGEFAClient{},
|
|
||||||
}
|
|
||||||
|
|
||||||
type VRNEFAClient struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c VRNEFAClient) GetName() string {
|
|
||||||
return "VRN"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c VRNEFAClient) BuildRequest(stopId string) (*http.Request, error) {
|
|
||||||
// Create request object
|
|
||||||
req, err := http.NewRequest("GET", "https://www.vrn.de/mngvrn/XML_DM_REQUEST", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configure our request
|
|
||||||
query := url.Values{}
|
|
||||||
query.Set("coordOutputFormat", "EPSG:4326")
|
|
||||||
query.Set("depType", "stopEvents")
|
|
||||||
query.Set("includeCompleteStopSeq", "0")
|
|
||||||
query.Set("limit", "10")
|
|
||||||
query.Set("locationServerActive", "0")
|
|
||||||
query.Set("mode", "direct")
|
|
||||||
query.Set("name_dm", stopId)
|
|
||||||
query.Set("outputFormat", "json")
|
|
||||||
query.Set("type_dm", "stop")
|
|
||||||
query.Set("useOnlyStops", "1")
|
|
||||||
query.Set("useRealtime", "1")
|
|
||||||
req.URL.RawQuery = query.Encode()
|
|
||||||
|
|
||||||
return req, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type KVVEFAClient struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c KVVEFAClient) GetName() string {
|
|
||||||
return "KVV"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c KVVEFAClient) BuildRequest(stopId string) (*http.Request, error) {
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("action", "XSLT_DM_REQUEST")
|
|
||||||
form.Set("name_dm", stopId)
|
|
||||||
form.Set("type_dm", "stop")
|
|
||||||
form.Set("useRealtime", "1")
|
|
||||||
form.Set("limit", "10")
|
|
||||||
form.Set("mode", "direct")
|
|
||||||
form.Set("useRealtime", "1")
|
|
||||||
form.Set("outputFormat", "json")
|
|
||||||
body := strings.NewReader(form.Encode())
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "https://www.kvv.de/tunnelEfaDirect.php", body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("User-Agent", "coolio/1.0")
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
|
|
||||||
return req, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type BwegtEFAClient struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c BwegtEFAClient) GetName() string {
|
|
||||||
return "bwegt"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c BwegtEFAClient) BuildRequest(stopId string) (*http.Request, error) {
|
|
||||||
// Create request object
|
|
||||||
req, err := http.NewRequest("GET", "https://www.bwegt.de/bwegt-efa/XML_DM_REQUEST", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configure our request
|
|
||||||
query := url.Values{}
|
|
||||||
query.Set("coordOutputFormat", "EPSG:4326")
|
|
||||||
query.Set("depType", "stopEvents")
|
|
||||||
query.Set("includeCompleteStopSeq", "0")
|
|
||||||
query.Set("limit", "10")
|
|
||||||
query.Set("locationServerActive", "0")
|
|
||||||
query.Set("mode", "direct")
|
|
||||||
query.Set("name_dm", stopId)
|
|
||||||
query.Set("outputFormat", "json")
|
|
||||||
query.Set("type_dm", "stop")
|
|
||||||
query.Set("useOnlyStops", "1")
|
|
||||||
query.Set("useRealtime", "1")
|
|
||||||
req.URL.RawQuery = query.Encode()
|
|
||||||
|
|
||||||
return req, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type VAGEFAClient struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c VAGEFAClient) GetName() string {
|
|
||||||
return "VAG"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c VAGEFAClient) BuildRequest(stopId string) (*http.Request, error) {
|
|
||||||
// Create request object
|
|
||||||
req, err := http.NewRequest("GET", "https://efa.vagfr.de/vagfr3/XSLT_DM_REQUEST", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configure our request
|
|
||||||
query := url.Values{}
|
|
||||||
query.Set("coordOutputFormat", "EPSG:4326")
|
|
||||||
query.Set("depType", "stopEvents")
|
|
||||||
query.Set("includeCompleteStopSeq", "0")
|
|
||||||
query.Set("limit", "10")
|
|
||||||
query.Set("locationServerActive", "0")
|
|
||||||
query.Set("mode", "direct")
|
|
||||||
query.Set("name_dm", stopId)
|
|
||||||
query.Set("outputFormat", "json")
|
|
||||||
query.Set("type_dm", "stop")
|
|
||||||
query.Set("useOnlyStops", "1")
|
|
||||||
query.Set("useRealtime", "1")
|
|
||||||
req.URL.RawQuery = query.Encode()
|
|
||||||
|
|
||||||
return req, nil
|
|
||||||
}
|
|
34
flake.lock
generated
34
flake.lock
generated
@ -1,23 +1,5 @@
|
|||||||
{
|
{
|
||||||
"nodes": {
|
"nodes": {
|
||||||
"flake-utils": {
|
|
||||||
"inputs": {
|
|
||||||
"systems": "systems"
|
|
||||||
},
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1731533236,
|
|
||||||
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
|
||||||
"owner": "numtide",
|
|
||||||
"repo": "flake-utils",
|
|
||||||
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "numtide",
|
|
||||||
"repo": "flake-utils",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"gitignore": {
|
"gitignore": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"nixpkgs": [
|
"nixpkgs": [
|
||||||
@ -56,25 +38,9 @@
|
|||||||
},
|
},
|
||||||
"root": {
|
"root": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"flake-utils": "flake-utils",
|
|
||||||
"gitignore": "gitignore",
|
"gitignore": "gitignore",
|
||||||
"nixpkgs": "nixpkgs"
|
"nixpkgs": "nixpkgs"
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"systems": {
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1681028828,
|
|
||||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
|
||||||
"owner": "nix-systems",
|
|
||||||
"repo": "default",
|
|
||||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "nix-systems",
|
|
||||||
"repo": "default",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"root": "root",
|
"root": "root",
|
||||||
|
17
flake.nix
17
flake.nix
@ -3,20 +3,20 @@
|
|||||||
|
|
||||||
inputs = {
|
inputs = {
|
||||||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
|
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
|
||||||
flake-utils.url = "github:numtide/flake-utils";
|
|
||||||
gitignore = {
|
gitignore = {
|
||||||
url = "github:hercules-ci/gitignore.nix";
|
url = "github:hercules-ci/gitignore.nix";
|
||||||
inputs.nixpkgs.follows = "nixpkgs";
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = { self, nixpkgs, flake-utils, gitignore }: flake-utils.lib.eachDefaultSystem (system:
|
outputs = { self, nixpkgs, gitignore }:
|
||||||
let
|
let
|
||||||
pkgs = import nixpkgs { inherit system; };
|
system = "x86_64-linux";
|
||||||
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
|
||||||
vrnp-static = pkgs.buildGoModule {
|
vrnp-static = pkgs.buildGoModule {
|
||||||
pname = "vrnp";
|
pname = "vrnp";
|
||||||
version = "0.0.10";
|
version = "0.0.4";
|
||||||
vendorHash = null;
|
vendorHash = null;
|
||||||
|
|
||||||
# For building the package, we use only the files not ignored by Git as inputs.
|
# For building the package, we use only the files not ignored by Git as inputs.
|
||||||
@ -31,15 +31,15 @@
|
|||||||
CGO_ENABLED = 0;
|
CGO_ENABLED = 0;
|
||||||
};
|
};
|
||||||
in {
|
in {
|
||||||
devShell = pkgs.mkShell {
|
devShell.${system} = pkgs.mkShellNoCC {
|
||||||
packages = [
|
packages = [
|
||||||
pkgs.go
|
pkgs.go
|
||||||
(pkgs.python3.withPackages (ps: with ps; [ pillow ]))
|
(pkgs.python3.withPackages (ps: with ps; [ pillow ]))
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
packages = {
|
packages.${system} = {
|
||||||
default = vrnp-static;
|
default = vrnp-static;
|
||||||
image-meta = pkgs.runCommand "image-meta" {} ''
|
image-meta = pkgs.runCommand "vrnp-version" {} ''
|
||||||
mkdir -p $out
|
mkdir -p $out
|
||||||
echo -n ${vrnp-static.version} > $out/version
|
echo -n ${vrnp-static.version} > $out/version
|
||||||
echo -n git.pbrinkmeier.de/paul/vrnp:${vrnp-static.version} > $out/name
|
echo -n git.pbrinkmeier.de/paul/vrnp:${vrnp-static.version} > $out/name
|
||||||
@ -55,6 +55,5 @@
|
|||||||
config.Cmd = [ "${vrnp-static}/bin/vrnp" ];
|
config.Cmd = [ "${vrnp-static}/bin/vrnp" ];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
65
main.go
65
main.go
@ -1,22 +1,19 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "cmp"
|
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
import "errors"
|
import "errors"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "log"
|
import "log"
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
import "net/url"
|
||||||
import "os"
|
import "os"
|
||||||
import "slices"
|
import "slices"
|
||||||
import "strings"
|
|
||||||
import "strconv"
|
import "strconv"
|
||||||
import "sync/atomic"
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// JSON unmarshaling types for departure monitor API
|
// JSON unmarshaling types for departure monitor API
|
||||||
|
|
||||||
type DMResponse struct {
|
type DMResponse struct {
|
||||||
DateTime DMDateTime `json:"dateTime"`
|
|
||||||
DepartureList []DMDeparture `json:"departureList"`
|
DepartureList []DMDeparture `json:"departureList"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,12 +43,28 @@ type DMDateTime struct {
|
|||||||
Minute string `json:"minute"`
|
Minute string `json:"minute"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchDepartures(c EFAClient, stopId string) (DMResponse, error) {
|
func FetchDepartures(stopId string) (DMResponse, error) {
|
||||||
req, err := c.BuildRequest(stopId)
|
// Create request object
|
||||||
|
req, err := http.NewRequest("GET", "https://www.vrn.de/mngvrn/XML_DM_REQUEST", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return DMResponse{}, err
|
return DMResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Configure our request
|
||||||
|
query := url.Values{}
|
||||||
|
query.Set("coordOutputFormat", "EPSG:4326")
|
||||||
|
query.Set("depType", "stopEvents")
|
||||||
|
query.Set("includeCompleteStopSeq", "0")
|
||||||
|
query.Set("limit", "10")
|
||||||
|
query.Set("locationServerActive", "0")
|
||||||
|
query.Set("mode", "direct")
|
||||||
|
query.Set("name_dm", stopId)
|
||||||
|
query.Set("outputFormat", "json")
|
||||||
|
query.Set("type_dm", "stop")
|
||||||
|
query.Set("useOnlyStops", "1")
|
||||||
|
query.Set("useRealtime", "1")
|
||||||
|
req.URL.RawQuery = query.Encode()
|
||||||
|
|
||||||
// Send the request, wait max 10 seconds
|
// Send the request, wait max 10 seconds
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
Timeout: 10 * time.Second,
|
Timeout: 10 * time.Second,
|
||||||
@ -75,7 +88,6 @@ func FetchDepartures(c EFAClient, stopId string) (DMResponse, error) {
|
|||||||
|
|
||||||
type Departures struct {
|
type Departures struct {
|
||||||
Departures []Departure `json:"departures"`
|
Departures []Departure `json:"departures"`
|
||||||
ServerTime string `json:"serverTime"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Departure struct {
|
type Departure struct {
|
||||||
@ -83,7 +95,6 @@ type Departure struct {
|
|||||||
Symbol string `json:"symbol"`
|
Symbol string `json:"symbol"`
|
||||||
Direction string `json:"direction"`
|
Direction string `json:"direction"`
|
||||||
Leaving string `json:"leaving"`
|
Leaving string `json:"leaving"`
|
||||||
Countdown int `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseDepartures(response DMResponse, allowedPlatform *string) (Departures, error) {
|
func ParseDepartures(response DMResponse, allowedPlatform *string) (Departures, error) {
|
||||||
@ -106,12 +117,6 @@ func ParseDepartures(response DMResponse, allowedPlatform *string) (Departures,
|
|||||||
return Departures{}, errors.New("could not parse countdown")
|
return Departures{}, errors.New("could not parse countdown")
|
||||||
}
|
}
|
||||||
|
|
||||||
matchesPlatform := allowedPlatform == nil || d.Platform == *allowedPlatform
|
|
||||||
isInThePast := countdown < 0
|
|
||||||
if !matchesPlatform || isInThePast {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if countdown == 0 {
|
if countdown == 0 {
|
||||||
leaving = "sofort"
|
leaving = "sofort"
|
||||||
}
|
}
|
||||||
@ -123,42 +128,25 @@ func ParseDepartures(response DMResponse, allowedPlatform *string) (Departures,
|
|||||||
leaving = fmt.Sprintf("%02s:%02s", dt.Hour, dt.Minute)
|
leaving = fmt.Sprintf("%02s:%02s", dt.Hour, dt.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if allowedPlatform != nil && d.Platform != *allowedPlatform {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
ds = append(ds, Departure{
|
ds = append(ds, Departure{
|
||||||
d.Platform,
|
d.Platform,
|
||||||
d.ServingLine.Symbol,
|
d.ServingLine.Symbol,
|
||||||
direction,
|
direction,
|
||||||
leaving,
|
leaving,
|
||||||
countdown,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
slices.SortFunc(ds, func(a Departure, b Departure) int {
|
return Departures{ds}, nil
|
||||||
return cmp.Compare(a.Countdown, b.Countdown)
|
|
||||||
})
|
|
||||||
|
|
||||||
dt := response.DateTime
|
|
||||||
|
|
||||||
return Departures{
|
|
||||||
ds,
|
|
||||||
fmt.Sprintf("%02s:%02s", dt.Hour, dt.Minute),
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
password := os.Getenv("VRNP_PASSWORD")
|
password := os.Getenv("VRNP_PASSWORD")
|
||||||
if len(password) == 0 {
|
if len(password) == 0 {
|
||||||
log.Fatal("Required environment variable VRNP_PASSWORD is not set")
|
panic("Required environment variable VRNP_PASSWORD is not set")
|
||||||
}
|
|
||||||
|
|
||||||
clientAllowlist := strings.Split(os.Getenv("VRNP_CLIENT_ALLOWLIST"), ",")
|
|
||||||
|
|
||||||
// Use round-robin to send incoming requests to different servers
|
|
||||||
var efaClient atomic.Uint64
|
|
||||||
var efaClients []EFAClient
|
|
||||||
for _, efaClient := range allEfaClients {
|
|
||||||
if len(clientAllowlist) != 0 && slices.Contains(clientAllowlist, efaClient.GetName()) {
|
|
||||||
efaClients = append(efaClients, efaClient)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -182,8 +170,7 @@ func main() {
|
|||||||
platform = &query["platform"][0]
|
platform = &query["platform"][0]
|
||||||
}
|
}
|
||||||
|
|
||||||
c := efaClients[(efaClient.Add(1) - 1) % uint64(len(efaClients))]
|
ds, err := FetchDepartures(stopId)
|
||||||
ds, err := FetchDepartures(c, stopId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user