Compare commits
15 Commits
feature/mu
...
main
Author | SHA1 | Date | |
---|---|---|---|
892012bc1c | |||
dc4180320b | |||
6e53521b84 | |||
4964c62212 | |||
b753d9f8ea | |||
a563cc45b5 | |||
1c1aad5846 | |||
f8fe64b1b3 | |||
72734c7faa | |||
5deb037fe5 | |||
6e78235127 | |||
5d5b893948 | |||
4a73099460 | |||
addc90d856 | |||
d23be3062a |
15
README.md
15
README.md
@ -2,12 +2,21 @@
|
|||||||
|
|
||||||
## 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
|
||||||
- [ ] Use unidecode to replace non-ascii stuff in the backend
|
- [ ] Make port configurable
|
||||||
|
- [ ] 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
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <Adafruit_ST7735.h>
|
#include <Adafruit_ST7735.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266HTTPClient.h>
|
#include <ESP8266HTTPClient.h>
|
||||||
|
#include <memory>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <WiFiClientSecureBearSSL.h>
|
#include <WiFiClientSecureBearSSL.h>
|
||||||
|
|
||||||
@ -20,10 +21,10 @@
|
|||||||
#define FRAME_DELAY 80
|
#define FRAME_DELAY 80
|
||||||
#define COLOR_BG (rgb(0, 0, 0))
|
#define COLOR_BG (rgb(0, 0, 0))
|
||||||
#define COLOR_DIVIDER (rgb(0xff, 0x60, 0x0d))
|
#define COLOR_DIVIDER (rgb(0xff, 0x60, 0x0d))
|
||||||
|
#define COLOR_TOP (rgb(0xa0, 0xa0, 0xa0))
|
||||||
#define COLOR_TEXT (rgb(0xff, 0x60, 0x0d))
|
#define COLOR_TEXT (rgb(0xff, 0x60, 0x0d))
|
||||||
|
|
||||||
// Layout
|
// Layout
|
||||||
|
|
||||||
#define WIDTH 160
|
#define WIDTH 160
|
||||||
#define HEIGHT 128
|
#define HEIGHT 128
|
||||||
|
|
||||||
@ -33,86 +34,349 @@
|
|||||||
#define MARGIN_LEFT 5
|
#define MARGIN_LEFT 5
|
||||||
#define MARGIN_RIGHT 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_X MARGIN_LEFT
|
||||||
#define MAIN_Y 9
|
#define MAIN_Y 19
|
||||||
#define MAIN_WIDTH (WIDTH - 2 * MARGIN_LEFT)
|
#define MAIN_WIDTH (WIDTH - 2 * MARGIN_LEFT)
|
||||||
#define MAIN_HEIGHT (8 * CY)
|
#define MAIN_HEIGHT (8 * CY)
|
||||||
|
|
||||||
#define CLOCK_X MARGIN_LEFT
|
#define CLOCK_X MARGIN_LEFT
|
||||||
#define CLOCK_Y 113
|
#define CLOCK_Y 113
|
||||||
|
#define CLOCK_WIDTH (5 * CX)
|
||||||
|
#define CLOCK_HEIGHT CY
|
||||||
|
|
||||||
#define STATUS_X 95
|
#define STATUS_X 83
|
||||||
#define STATUS_Y CLOCK_Y
|
#define STATUS_Y CLOCK_Y
|
||||||
#define STATUS_WIDTH (10 * CX)
|
#define STATUS_WIDTH (12 * CX)
|
||||||
#define STATUS_HEIGHT CY
|
#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_ST7735 display(TFT_CS, TFT_DC, TFT_RESET);
|
||||||
|
|
||||||
// App state
|
// App state interface
|
||||||
|
|
||||||
typedef enum {
|
// init
|
||||||
CONNECTING,
|
// │
|
||||||
SHOWING_DEPARTURES
|
// │
|
||||||
} State;
|
// │
|
||||||
|
// │ ┌───┐
|
||||||
|
// ┌──▼──────▼┐ │
|
||||||
|
// │Connecting│ │!connected
|
||||||
|
// └──┬───▲──┬┘ │
|
||||||
|
// │ │ └───┘
|
||||||
|
// │ │
|
||||||
|
// connected│ │!connected
|
||||||
|
// │ │
|
||||||
|
// │ │
|
||||||
|
// │ │ ┌───┐
|
||||||
|
// ┌──▼───┴─┐ timeout (3s) ┌────────▼┐ │
|
||||||
|
// │Fetching◄──────────────────┤Selecting│ │
|
||||||
|
// └──┬───▲─┘ └────▲───┬┘ │button pressed
|
||||||
|
// │ │ │ └───┘
|
||||||
|
// got response│ │timeout (25s) │
|
||||||
|
// │ │ │
|
||||||
|
// │ │ │
|
||||||
|
// │ │ │button pressed
|
||||||
|
// ┌────▼───┴─────────┐ │
|
||||||
|
// │Showing Departures│───────────────┘
|
||||||
|
// └──────────────────┘
|
||||||
|
|
||||||
State state = CONNECTING;
|
class State {
|
||||||
State previous_state = state;
|
public:
|
||||||
int frame = 0;
|
// When the state is entered
|
||||||
// Use this variable to trigger state enter effects
|
virtual void enter();
|
||||||
int state_changed = 1;
|
// 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
|
||||||
|
|
||||||
int buttonPushed = false;
|
int buttonPushed = false;
|
||||||
JsonDocument departures;
|
|
||||||
|
|
||||||
void ICACHE_RAM_ATTR onButtonFalling() {
|
void ICACHE_RAM_ATTR onButtonFalling() {
|
||||||
buttonPushed = true;
|
buttonPushed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 drawLayout() {
|
|
||||||
display.fillScreen(COLOR_BG);
|
|
||||||
display.drawFastHLine(5, 4, 150, COLOR_DIVIDER);
|
|
||||||
display.drawFastHLine(5, 109, 150, COLOR_DIVIDER);
|
|
||||||
display.drawFastHLine(5, 123, 150, COLOR_DIVIDER);
|
|
||||||
display.setCursor(CLOCK_X, CLOCK_Y);
|
|
||||||
display.print("13:12");
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
Serial.println("serial init");
|
Serial.println("serial init");
|
||||||
display.initR();
|
display.initR();
|
||||||
display.setSPISpeed(40000000);
|
display.setSPISpeed(40000000);
|
||||||
|
display.cp437(true);
|
||||||
display.setRotation(3);
|
display.setRotation(3);
|
||||||
Serial.println("display initialized");
|
Serial.println("display initialized");
|
||||||
Serial.printf("display dimensions: %" PRId16 "x%" PRId16 "\n", display.width(), display.height());
|
Serial.printf("display dimensions: %" PRId16 "x%" PRId16 "\n", display.width(), display.height());
|
||||||
|
|
||||||
display.setTextSize(1);
|
|
||||||
display.setTextColor(COLOR_TEXT);
|
|
||||||
drawLayout();
|
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>();
|
||||||
}
|
}
|
||||||
|
|
||||||
#define QUERY_STRING "stop_id=de:08221:1225&platform=A"
|
void loop() {
|
||||||
|
if (stateChanged) {
|
||||||
|
stateChanged = false;
|
||||||
|
// 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 fetchDepartures(String path) {
|
||||||
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, "/departures?" QUERY_STRING)) {
|
if (!client.begin(*https, "vrnp.beany.club", 443, path)) {
|
||||||
display.fillScreen(COLOR_BG);
|
display.fillScreen(COLOR_BG);
|
||||||
display.setCursor(0, 0);
|
display.setCursor(0, 0);
|
||||||
display.print("begin failed");
|
display.print("begin failed");
|
||||||
@ -132,63 +396,69 @@ String fetchDepartures() {
|
|||||||
return client.getString();
|
return client.getString();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
char mapUtf8CodepointToCP437German(uint32_t codepoint) {
|
||||||
if (state != previous_state) {
|
switch (codepoint) {
|
||||||
state_changed = 1;
|
case 0x00e4: return 0x84; // ä
|
||||||
previous_state = state;
|
case 0x00c4: return 0x8e; // Ä
|
||||||
|
case 0x00f6: return 0x94; // ö
|
||||||
|
case 0x00d6: return 0x99; // Ö
|
||||||
|
case 0x00fc: return 0x81; // ü
|
||||||
|
case 0x00dc: return 0x9a; // Ü
|
||||||
|
case 0x00df: return 0xe1; // ß
|
||||||
|
default: return '?';
|
||||||
}
|
}
|
||||||
logic_loop();
|
|
||||||
frame++;
|
|
||||||
state_changed = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void logic_loop() {
|
/*
|
||||||
switch (state) {
|
Maps german umlauts and sharp s from utf8 to cp437 encoding.
|
||||||
case CONNECTING: {
|
Other utf8 characters and malformed utf8 are replaced by '?'.
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
The resulting string is truncated to cp437StrSize.
|
||||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
*/
|
||||||
display.setCursor(STATUS_X, STATUS_Y);
|
void utf8ToCP437German(const char *utf8Str, char *cp437Str, size_t cp437StrSize) {
|
||||||
display.printf(" connect %c", "|/-\\"[frame % 4]);
|
size_t utf8StrLength = strlen(utf8Str);
|
||||||
delay(FRAME_DELAY);
|
size_t utf8StrIndex = 0;
|
||||||
return;
|
size_t cp437StrIndex = 0;
|
||||||
|
|
||||||
|
char c;
|
||||||
|
|
||||||
|
// One char at the end of the cp437Str is reserved to the terminating null
|
||||||
|
while (utf8StrIndex < utf8StrLength && cp437StrIndex + 1 < cp437StrSize) {
|
||||||
|
uint8_t cu0 = utf8Str[utf8StrIndex];
|
||||||
|
|
||||||
|
if (cu0 < 0x80) {
|
||||||
|
// ASCII maps to ASCII
|
||||||
|
c = cu0;
|
||||||
|
utf8StrIndex++;
|
||||||
|
} else if ((cu0 & 0b11100000) == 0b11000000) {
|
||||||
|
// codepoints encoded as two code units may contain german special characters
|
||||||
|
if (utf8StrIndex + 1 >= utf8StrLength) {
|
||||||
|
// if there's no 10xxxxxxx byte after this one, we've reached the end (malformed)
|
||||||
|
c = '?';
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
// otherwise decode the codepoint and map it to cp437
|
||||||
|
uint8_t cu1 = utf8Str[utf8StrIndex + 1];
|
||||||
|
uint32_t cp = (cu0 & 0x1f << 6) | cu1 & 0x3f;
|
||||||
|
c = mapUtf8CodepointToCP437German(cp);
|
||||||
|
utf8StrIndex += 2;
|
||||||
}
|
}
|
||||||
|
// for 3 and 4-code unit codepoints just put a ? and skip all their code units
|
||||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
// here we dont care whether the string is malformed
|
||||||
display.setCursor(STATUS_X, STATUS_Y);
|
} else if ((cu0 & 0b11110000) == 0b11100000) {
|
||||||
display.print(" fetching");
|
c = '?';
|
||||||
|
utf8StrIndex += 3;
|
||||||
String departuresRaw = fetchDepartures();
|
} else if ((cu0 & 0b11111000) == 0b11110000) {
|
||||||
deserializeJson(departures, departuresRaw);
|
c = '?';
|
||||||
|
utf8StrIndex += 4;
|
||||||
state = SHOWING_DEPARTURES;
|
} else {
|
||||||
frame = 0;
|
// catch all for malformed utf8
|
||||||
return;
|
c = '?';
|
||||||
|
utf8StrIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
case SHOWING_DEPARTURES:
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
state = CONNECTING;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
cp437Str[cp437StrIndex] = c;
|
||||||
display.fillRect(MAIN_X, MAIN_Y, MAIN_WIDTH, MAIN_HEIGHT, COLOR_BG);
|
cp437StrIndex++;
|
||||||
|
|
||||||
int line = 0;
|
|
||||||
for (JsonVariant departure : departures["departures"].as<JsonArray>()) {
|
|
||||||
const char *directionStr = departure["direction"].as<const char *>();
|
|
||||||
|
|
||||||
display.setCursor(MAIN_X, MAIN_Y + (CY + 3) * line);
|
|
||||||
display.printf("%2s %-15.15s %6s",
|
|
||||||
departure["symbol"].as<const char*>(),
|
|
||||||
directionStr,
|
|
||||||
departure["leaving"].as<const char*>()
|
|
||||||
);
|
|
||||||
line++;
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(25000);
|
|
||||||
state = CONNECTING;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
cp437Str[cp437StrIndex] = '\0';
|
||||||
|
}
|
143
efa_client.go
Normal file
143
efa_client.go
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
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
|
||||||
|
}
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
vrnp-static = pkgs.buildGoModule {
|
vrnp-static = pkgs.buildGoModule {
|
||||||
pname = "vrnp";
|
pname = "vrnp";
|
||||||
version = "0.0.5";
|
version = "0.0.10";
|
||||||
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.
|
||||||
|
128
main.go
128
main.go
@ -1,11 +1,11 @@
|
|||||||
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 "strings"
|
||||||
@ -16,6 +16,7 @@ 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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,104 +46,6 @@ type DMDateTime struct {
|
|||||||
Minute string `json:"minute"`
|
Minute string `json:"minute"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EFAClient interface {
|
|
||||||
GetName() string
|
|
||||||
BuildRequest(string) (*http.Request, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
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("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
|
|
||||||
}
|
|
||||||
|
|
||||||
func FetchDepartures(c EFAClient, stopId string) (DMResponse, error) {
|
func FetchDepartures(c EFAClient, stopId string) (DMResponse, error) {
|
||||||
req, err := c.BuildRequest(stopId)
|
req, err := c.BuildRequest(stopId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -172,6 +75,7 @@ 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 {
|
||||||
@ -179,6 +83,7 @@ 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) {
|
||||||
@ -223,24 +128,37 @@ func ParseDepartures(response DMResponse, allowedPlatform *string) (Departures,
|
|||||||
d.ServingLine.Symbol,
|
d.ServingLine.Symbol,
|
||||||
direction,
|
direction,
|
||||||
leaving,
|
leaving,
|
||||||
|
countdown,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return Departures{ds}, nil
|
slices.SortFunc(ds, func(a Departure, b Departure) int {
|
||||||
|
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 {
|
||||||
panic("Required environment variable VRNP_PASSWORD is not set")
|
log.Fatal("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
|
// Use round-robin to send incoming requests to different servers
|
||||||
var efaClient atomic.Uint64
|
var efaClient atomic.Uint64
|
||||||
efaClients := []EFAClient{
|
var efaClients []EFAClient
|
||||||
BwegtEFAClient{},
|
for _, efaClient := range allEfaClients {
|
||||||
VRNEFAClient{},
|
if len(clientAllowlist) != 0 && slices.Contains(clientAllowlist, efaClient.GetName()) {
|
||||||
KVVEFAClient{},
|
efaClients = append(efaClients, efaClient)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user