From 52cc78431eb76bd9020e8ff1900bec347bcc59fc Mon Sep 17 00:00:00 2001
From: Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>
Date: Tue, 16 Aug 2022 13:57:12 +0200
Subject: [PATCH] Module: OBB speed and next_stop

---
 Parsers.cs  | 44 ++++++++++++++++++++++++++++++++++++++++++++
 sample.conf | 23 +++++++++++++++++++++++
 2 files changed, 67 insertions(+)

diff --git a/Parsers.cs b/Parsers.cs
index df8dd30..ab0e249 100644
--- a/Parsers.cs
+++ b/Parsers.cs
@@ -386,3 +386,47 @@ class ParserCDNextStop: Parser
 	}
 	#pragma warning restore 8602
 }
+
+[ParserName("OBB_speed")]
+class ParserOBBSpeed: Parser
+{
+	#pragma warning disable 8602
+	public void Init(ModuleParent _bar, Module _module, ConfigSection section)
+	{
+	}
+	public IEnumerable<Block> Parse(string data)
+	{
+		return new Block[]{new Block($"{data} km/h", Color: Color.White)};
+	}
+	#pragma warning restore 8602
+}
+
+[ParserName("OBB_next_stop")]
+class ParserOBBNextStop: Parser
+{
+	#pragma warning disable 8602
+	public void Init(ModuleParent _bar, Module _module, ConfigSection section)
+	{
+	}
+	public IEnumerable<Block> Parse(string data)
+	{
+		try
+		{
+			JsonObject json = JsonObject.Parse(data).AsObject();
+			JsonObject nextStop = json["currentStation"].AsObject();
+			string nextStopName = nextStop["name"].AsObject()["all"].GetValue<string>();
+			DateTime nextStopArrival = DateTime.Parse(nextStop.AsObject()["arrivalForecast"]?.GetValue<string>());
+			DateTime nextStopArrivalScheudled = DateTime.Parse(nextStop.AsObject()["arrivalSchedule"]?.GetValue<string>());
+			long arrivalIn = (long)(nextStopArrival - DateTime.Now).TotalSeconds;
+			int delay_s = (int)(nextStopArrival - nextStopArrivalScheudled).TotalSeconds;
+			string delayStr = delay_s==0?"":$" ({TimeShow.WithSign(delay_s)})";
+			return new Block[]{new Block($"{nextStopName} in {TimeShow.Show(arrivalIn)}"+delayStr, Color: Color.White)};
+		}
+		catch(Exception e)
+		{
+			Console.Error.WriteLine(e);
+			return new Block[]{new Block("OBB PE", Color: Color.Red)};
+		}
+	}
+	#pragma warning restore 8602
+}
diff --git a/sample.conf b/sample.conf
index 994fbe3..49e1368 100644
--- a/sample.conf
+++ b/sample.conf
@@ -441,3 +441,26 @@ period = 20
 show_old = 60
 max_old = 300
 _exec = chromium http://cdwifi.cz
+
+# Parser: OBB_speed and OBB_next_stop
+# -----------------------------------
+# Show information about the speed or next stop and delay for Österreichische Bundesbahnen trains.
+# Example usage:
+[OBB_speed]
+_type=http
+url = https://railnet.oebb.at/api/speed
+period = 5
+show_old = 20
+max_old = 120
+error_handler =
+no_data_handler =
+_exec = chromium http://railnet.oebb.at/
+
+[OBB_next_stop]
+_type=http
+url = https://railnet.oebb.at/assets/modules/fis/combined.json
+error_handler = 
+period = 20
+show_old = 60
+max_old = 300
+_exec = chromium http://railnet.oebb.at/
-- 
GitLab