From 7550b6ac56d00266f119b182b6c70d79979ef33a Mon Sep 17 00:00:00 2001
From: Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>
Date: Mon, 15 Aug 2022 18:01:49 +0200
Subject: [PATCH] Parsers: CD_speed and CD_next_stop

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

diff --git a/Parsers.cs b/Parsers.cs
index fb514e7..df8dd30 100644
--- a/Parsers.cs
+++ b/Parsers.cs
@@ -319,3 +319,70 @@ class ParserICENextStop: Parser
 	}
 	#pragma warning restore 8602
 }
+
+[ParserName("CD_speed")]
+class ParserCDSpeed: 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();
+			return new Block[]{new Block($"{json["speed"].GetValue<double>()} km/h", Color: Color.White)};
+		}
+		catch(Exception e)
+		{
+			Console.Error.WriteLine(e);
+			return new Block[]{new Block("Speed PE", Color: Color.Red)};
+		}
+	}
+	#pragma warning restore 8602
+}
+
+[ParserName("CD_next_stop")]
+class ParserCDNextStop: Parser
+{
+	#pragma warning disable 8602
+	public void Init(ModuleParent _bar, Module _module, ConfigSection section)
+	{
+	}
+	public IEnumerable<Block> Parse(string data)
+	{
+		try
+		{
+			string[] lines = data.Split("\n");
+			JsonObject delayJson = JsonObject.Parse(lines[1]).AsObject();
+			int delay_min = delayJson.AsObject()["delay"]?.GetValue<int>() ?? 0;
+			JsonObject json = JsonObject.Parse(lines[0]).AsObject();
+			JsonObject nextStop = null;
+			foreach(var s in json["connexionTimes"].AsArray())
+			{
+				string arrivalTime = s.AsObject()["timeArrival"]?.GetValue<string>();
+				if(arrivalTime != null)
+				{
+					var arrival = DateTime.Parse(arrivalTime);
+					if(arrival.AddMinutes(delay_min) >= DateTime.Now)
+					{
+						nextStop = s.AsObject();
+						break;
+					}
+				}
+			}
+			string nextStopName = nextStop["station"].AsObject()["name"].GetValue<string>();
+			DateTime nextStopArrival = DateTime.Parse(nextStop.AsObject()["timeArrival"]?.GetValue<string>()).AddMinutes(delay_min);
+			long arrivalIn = (long)(nextStopArrival - DateTime.Now).TotalSeconds;
+			string delayStr = delay_min==0?"":$" ({TimeShow.WithSign(delay_min * 60)})";
+			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("CD PE", Color: Color.Red)};
+		}
+	}
+	#pragma warning restore 8602
+}
diff --git a/sample.conf b/sample.conf
index e97ea91..994fbe3 100644
--- a/sample.conf
+++ b/sample.conf
@@ -417,3 +417,27 @@ error_handler =
 period = 20
 show_old = 60
 max_old = 300
+
+# Parser: CD_speed and CD_next_stop
+# -----------------------------------
+# Show information about the speed or next stop and delay for České dráhy trains.
+# Example usage:
+[CD_speed]
+_type=http
+url = http://cdwifi.cz/portal/api/vehicle/realtime
+period = 5
+show_old = 20
+max_old = 120
+error_handler =
+no_data_handler =
+_exec = chromium http://cdwifi.cz
+
+[CD_next_stop]
+_type=http_multi
+urls = http://cdwifi.cz/portal/api/timetable/connexion/current?locale=cs_CZ
+       http://cdwifi.cz/portal/api/vehicle/realtime
+error_handler =
+period = 20
+show_old = 60
+max_old = 300
+_exec = chromium http://cdwifi.cz
-- 
GitLab