Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d5b893948 | ||
|
|
4a73099460 | ||
|
|
addc90d856 | ||
|
|
d23be3062a | ||
|
|
740b654be6 | ||
|
|
dcc3cfa44f | ||
|
|
ae7b2f170a | ||
|
|
fad4da3f6b | ||
|
|
76cca9c1c0 | ||
|
|
16f7d80677 | ||
|
|
cf7bb96517 | ||
|
|
f0c94d8d7d |
@@ -1,3 +1,4 @@
|
||||
result
|
||||
vrnp
|
||||
main
|
||||
*.swp
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 4.0 KiB |
@@ -81,3 +81,17 @@ static const uint8_t PROGMEM spinny[][8] = {
|
||||
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,
|
||||
},
|
||||
};
|
||||
|
||||
+104
-82
@@ -1,37 +1,68 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <SPI.h>
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <Adafruit_ST7735.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266HTTPClient.h>
|
||||
#include <SPI.h>
|
||||
#include <WiFiClientSecureBearSSL.h>
|
||||
|
||||
#include "bitmaps.h"
|
||||
#include "wifi_credentials.h"
|
||||
|
||||
// Screen stuff
|
||||
#define SCREEN_WIDTH 128 // OLED display width, in pixels
|
||||
#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
|
||||
#define TFT_CS D8
|
||||
#define TFT_DC D4
|
||||
#define TFT_RESET D3
|
||||
|
||||
// Button stuff
|
||||
#define BUTTON_PIN D6
|
||||
|
||||
// Display stuff
|
||||
#define FRAME_DELAY 80
|
||||
#define COLOR_BG (rgb(0, 0, 0))
|
||||
#define COLOR_DIVIDER (rgb(0xff, 0x60, 0x0d))
|
||||
#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 MAIN_X MARGIN_LEFT
|
||||
#define MAIN_Y 9
|
||||
#define MAIN_WIDTH (WIDTH - 2 * MARGIN_LEFT)
|
||||
#define MAIN_HEIGHT (8 * CY)
|
||||
|
||||
#define CLOCK_X MARGIN_LEFT
|
||||
#define CLOCK_Y 113
|
||||
|
||||
#define STATUS_X 95
|
||||
#define STATUS_Y CLOCK_Y
|
||||
#define STATUS_WIDTH (10 * CX)
|
||||
#define STATUS_HEIGHT CY
|
||||
|
||||
// Peripherals
|
||||
|
||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||
Adafruit_ST7735 display(TFT_CS, TFT_DC, TFT_RESET);
|
||||
|
||||
// App state
|
||||
|
||||
enum {
|
||||
typedef enum {
|
||||
CONNECTING,
|
||||
SHOWING_DEPARTURES
|
||||
} state = CONNECTING;
|
||||
} State;
|
||||
|
||||
State state = CONNECTING;
|
||||
State previous_state = state;
|
||||
int frame = 0;
|
||||
// Use this variable to trigger state enter effects
|
||||
int state_changed = 1;
|
||||
|
||||
int buttonPushed = false;
|
||||
JsonDocument departures;
|
||||
|
||||
@@ -39,38 +70,41 @@ void ICACHE_RAM_ATTR onButtonFalling() {
|
||||
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() {
|
||||
Serial.begin(9600);
|
||||
|
||||
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
|
||||
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
|
||||
Serial.println("SSD1306 allocation failed");
|
||||
for(;;); // Don't proceed, loop forever
|
||||
}
|
||||
Serial.println("SSD1306 initialized");
|
||||
display.clearDisplay();
|
||||
Serial.println("serial init");
|
||||
display.initR();
|
||||
display.setSPISpeed(40000000);
|
||||
display.setRotation(3);
|
||||
Serial.println("display initialized");
|
||||
Serial.printf("display dimensions: %" PRId16 "x%" PRId16 "\n", display.width(), display.height());
|
||||
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(COLOR_TEXT);
|
||||
drawLayout();
|
||||
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
attachInterrupt(BUTTON_PIN, onButtonFalling, FALLING);
|
||||
|
||||
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 drawSpinner(int frame) {
|
||||
display.fillRect(120, 56, spinny_dims[0], spinny_dims[1], SSD1306_BLACK);
|
||||
display.drawBitmap(120, 56, spinny[frame % spinny_dims[2]], spinny_dims[0], spinny_dims[1], SSD1306_WHITE);
|
||||
}
|
||||
#define QUERY_STRING "stop_id=de:08221:1225&platform=A"
|
||||
|
||||
// TODO: Error handling
|
||||
String fetchDepartures() {
|
||||
@@ -78,11 +112,10 @@ String fetchDepartures() {
|
||||
https->setInsecure();
|
||||
|
||||
HTTPClient client;
|
||||
if (!client.begin(*https, "vrnp.beany.club", 443, "/departures?stop_id=de:08221:1225&platform=A")) {
|
||||
display.clearDisplay();
|
||||
if (!client.begin(*https, "vrnp.beany.club", 443, "/departures?" QUERY_STRING)) {
|
||||
display.fillScreen(COLOR_BG);
|
||||
display.setCursor(0, 0);
|
||||
display.print("begin failed");
|
||||
display.display();
|
||||
for (;;);
|
||||
}
|
||||
|
||||
@@ -90,10 +123,9 @@ String fetchDepartures() {
|
||||
client.addHeader("Accept", "application/json");
|
||||
int statusCode = client.GET();
|
||||
if (statusCode != 200) {
|
||||
display.clearDisplay();
|
||||
display.fillScreen(COLOR_BG);
|
||||
display.setCursor(0, 0);
|
||||
display.printf("http non-ok: %d\n", statusCode);
|
||||
display.display();
|
||||
for (;;);
|
||||
}
|
||||
|
||||
@@ -101,20 +133,29 @@ String fetchDepartures() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (state != previous_state) {
|
||||
state_changed = 1;
|
||||
previous_state = state;
|
||||
}
|
||||
logic_loop();
|
||||
frame++;
|
||||
state_changed = 0;
|
||||
}
|
||||
|
||||
void logic_loop() {
|
||||
switch (state) {
|
||||
case CONNECTING: {
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
drawSpinner(frame++);
|
||||
display.display();
|
||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
||||
display.setCursor(STATUS_X, STATUS_Y);
|
||||
display.printf(" connect %c", "|/-\\"[frame % 4]);
|
||||
delay(FRAME_DELAY);
|
||||
return;
|
||||
}
|
||||
|
||||
display.clearDisplay();
|
||||
display.fillRect(0, 56, 128, 8, SSD1306_INVERSE);
|
||||
display.setCursor(0, 56);
|
||||
display.print("requesting departures");
|
||||
display.display();
|
||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
||||
display.setCursor(STATUS_X, STATUS_Y);
|
||||
display.print(" fetching");
|
||||
|
||||
String departuresRaw = fetchDepartures();
|
||||
deserializeJson(departures, departuresRaw);
|
||||
@@ -130,48 +171,29 @@ void loop() {
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
if (buttonPushed) {
|
||||
buttonPushed = false;
|
||||
delay(FRAME_DELAY);
|
||||
return;
|
||||
}
|
||||
*/
|
||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
||||
display.fillRect(MAIN_X, MAIN_Y, MAIN_WIDTH, MAIN_HEIGHT, COLOR_BG);
|
||||
|
||||
// Marquee effect for direction strings longer than 11 characters
|
||||
// Neither particularly efficient nor legible
|
||||
// (This could use some love)
|
||||
|
||||
int availableWidth = 11;
|
||||
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
int line = 0;
|
||||
for (JsonVariant departure : departures["departures"].as<JsonArray>()) {
|
||||
const char *directionStr = departure["direction"].as<const char *>();
|
||||
|
||||
char buf[128] = {0};
|
||||
const char *dirStart;
|
||||
|
||||
if (strlen(directionStr) <= availableWidth || (strlen(directionStr) + 3 + availableWidth) > 127) {
|
||||
dirStart = directionStr;
|
||||
} else {
|
||||
memcpy(buf, directionStr, strlen(directionStr));
|
||||
memcpy(buf + strlen(directionStr), " ", 3);
|
||||
memcpy(buf + strlen(directionStr) + 3, directionStr, availableWidth);
|
||||
buf[strlen(directionStr) + 3 + availableWidth] = '\0';
|
||||
dirStart = buf + frame % (strlen(directionStr) + 3);
|
||||
}
|
||||
|
||||
display.printf("%2s %-11.11s %6s\n",
|
||||
display.setCursor(MAIN_X, MAIN_Y + (CY + 3) * line);
|
||||
display.printf("%2s %-15.15s ",
|
||||
departure["symbol"].as<const char*>(),
|
||||
dirStart,
|
||||
departure["leaving"].as<const char*>()
|
||||
departure["direction"].as<const char *>()
|
||||
);
|
||||
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*>());
|
||||
}
|
||||
display.display();
|
||||
|
||||
frame++;
|
||||
delay(500);
|
||||
line++;
|
||||
}
|
||||
|
||||
delay(25000);
|
||||
state = CONNECTING;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,3 +43,4 @@ def generate_bitmap_from_gif(file: Path, fd):
|
||||
|
||||
if __name__ == "__main__":
|
||||
generate_bitmap_from_gif(Path("./animations/spinny.gif"), sys.stdout)
|
||||
generate_bitmap_from_gif(Path("./animations/tiny_train.gif"), sys.stdout)
|
||||
|
||||
Generated
+34
@@ -1,5 +1,23 @@
|
||||
{
|
||||
"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": {
|
||||
"inputs": {
|
||||
"nixpkgs": [
|
||||
@@ -38,9 +56,25 @@
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"gitignore": "gitignore",
|
||||
"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",
|
||||
|
||||
@@ -3,20 +3,20 @@
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
gitignore = {
|
||||
url = "github:hercules-ci/gitignore.nix";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, gitignore }:
|
||||
outputs = { self, nixpkgs, flake-utils, gitignore }: flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
system = "x86_64-linux";
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
|
||||
vrnp-static = pkgs.buildGoModule {
|
||||
pname = "vrnp";
|
||||
version = "0.0.4";
|
||||
version = "0.0.7";
|
||||
vendorHash = null;
|
||||
|
||||
# For building the package, we use only the files not ignored by Git as inputs.
|
||||
@@ -31,15 +31,15 @@
|
||||
CGO_ENABLED = 0;
|
||||
};
|
||||
in {
|
||||
devShell.${system} = pkgs.mkShellNoCC {
|
||||
devShell = pkgs.mkShell {
|
||||
packages = [
|
||||
pkgs.go
|
||||
(pkgs.python3.withPackages (ps: with ps; [ pillow ]))
|
||||
];
|
||||
};
|
||||
packages.${system} = {
|
||||
packages = {
|
||||
default = vrnp-static;
|
||||
image-meta = pkgs.runCommand "vrnp-version" {} ''
|
||||
image-meta = pkgs.runCommand "image-meta" {} ''
|
||||
mkdir -p $out
|
||||
echo -n ${vrnp-static.version} > $out/version
|
||||
echo -n git.pbrinkmeier.de/paul/vrnp:${vrnp-static.version} > $out/name
|
||||
@@ -55,5 +55,6 @@
|
||||
config.Cmd = [ "${vrnp-static}/bin/vrnp" ];
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package main
|
||||
|
||||
import "cmp"
|
||||
import "encoding/json"
|
||||
import "errors"
|
||||
import "fmt"
|
||||
@@ -8,7 +9,9 @@ import "net/http"
|
||||
import "net/url"
|
||||
import "os"
|
||||
import "slices"
|
||||
import "strings"
|
||||
import "strconv"
|
||||
import "sync/atomic"
|
||||
import "time"
|
||||
|
||||
// JSON unmarshaling types for departure monitor API
|
||||
@@ -43,11 +46,23 @@ type DMDateTime struct {
|
||||
Minute string `json:"minute"`
|
||||
}
|
||||
|
||||
func FetchDepartures(stopId string) (DMResponse, error) {
|
||||
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 DMResponse{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Configure our request
|
||||
@@ -65,6 +80,77 @@ func FetchDepartures(stopId string) (DMResponse, error) {
|
||||
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
|
||||
}
|
||||
|
||||
func FetchDepartures(c EFAClient, stopId string) (DMResponse, error) {
|
||||
req, err := c.BuildRequest(stopId)
|
||||
if err != nil {
|
||||
return DMResponse{}, err
|
||||
}
|
||||
|
||||
// Send the request, wait max 10 seconds
|
||||
client := http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
@@ -95,6 +181,7 @@ type Departure struct {
|
||||
Symbol string `json:"symbol"`
|
||||
Direction string `json:"direction"`
|
||||
Leaving string `json:"leaving"`
|
||||
Countdown int
|
||||
}
|
||||
|
||||
func ParseDepartures(response DMResponse, allowedPlatform *string) (Departures, error) {
|
||||
@@ -117,6 +204,12 @@ func ParseDepartures(response DMResponse, allowedPlatform *string) (Departures,
|
||||
return Departures{}, errors.New("could not parse countdown")
|
||||
}
|
||||
|
||||
matchesPlatform := allowedPlatform == nil || d.Platform == *allowedPlatform
|
||||
isInThePast := countdown < 0
|
||||
if !matchesPlatform || isInThePast {
|
||||
continue
|
||||
}
|
||||
|
||||
if countdown == 0 {
|
||||
leaving = "sofort"
|
||||
}
|
||||
@@ -128,18 +221,19 @@ func ParseDepartures(response DMResponse, allowedPlatform *string) (Departures,
|
||||
leaving = fmt.Sprintf("%02s:%02s", dt.Hour, dt.Minute)
|
||||
}
|
||||
|
||||
if allowedPlatform != nil && d.Platform != *allowedPlatform {
|
||||
continue
|
||||
}
|
||||
|
||||
ds = append(ds, Departure{
|
||||
d.Platform,
|
||||
d.ServingLine.Symbol,
|
||||
direction,
|
||||
leaving,
|
||||
countdown,
|
||||
})
|
||||
}
|
||||
|
||||
slices.SortFunc(ds, func(a Departure, b Departure) int {
|
||||
return cmp.Compare(a.Countdown, b.Countdown)
|
||||
})
|
||||
|
||||
return Departures{ds}, nil
|
||||
}
|
||||
|
||||
@@ -149,6 +243,14 @@ func main() {
|
||||
panic("Required environment variable VRNP_PASSWORD is not set")
|
||||
}
|
||||
|
||||
// Use round-robin to send incoming requests to different servers
|
||||
var efaClient atomic.Uint64
|
||||
efaClients := []EFAClient{
|
||||
BwegtEFAClient{},
|
||||
VRNEFAClient{},
|
||||
KVVEFAClient{},
|
||||
}
|
||||
|
||||
http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) {
|
||||
user, pass, ok := r.BasicAuth()
|
||||
if !(ok && user == "admin" && pass == password) {
|
||||
@@ -170,7 +272,9 @@ func main() {
|
||||
platform = &query["platform"][0]
|
||||
}
|
||||
|
||||
ds, err := FetchDepartures(stopId)
|
||||
c := efaClients[(efaClient.Add(1) - 1) % uint64(len(efaClients))]
|
||||
log.Print(c.GetName())
|
||||
ds, err := FetchDepartures(c, stopId)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user