summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormakefu <github@syntax-fehler.de>2017-12-06 11:15:38 +0100
committermakefu <github@syntax-fehler.de>2017-12-06 11:15:38 +0100
commit16b1e7e0198181bddd92032c0d00782853402db5 (patch)
tree440963dc916ec7109190fd96cef33a5bf5875896
parent193765d8c89c76e54109ea343217ba273f2a9f9d (diff)
ampel: now gets times via vvs efa
-rwxr-xr-xampel.py96
-rw-r--r--times.json33
-rw-r--r--vvs_efa/tests/__init__.py39
3 files changed, 63 insertions, 105 deletions
diff --git a/ampel.py b/ampel.py
index ccbb603..b74814a 100755
--- a/ampel.py
+++ b/ampel.py
@@ -1,12 +1,11 @@
#! /usr/bin/env python3
#! nix-shell -i python3 -p python3 python35Packages.docopt python35Packages.paho-mqtt
-""" usage: ampel [options] NUMLEDS TIMEFILE
+""" usage: ampel [options] NUMLEDS
--mqtt-server=HOST path to mqtt server [Default: 192.168.8.11]
+ --no-mqtt disable mqtt connections for debugging
NUMLEDS is the number of leds to output data for (--add-empty does not count in here)
-TIMEFILE refers to a json file which contains all the dates where the bus
-drives
13 22 24 26 28
|-----------|----|----|----|
@@ -20,63 +19,94 @@ from time import sleep
import json
import sys
from datetime import datetime, timedelta
+import requests
+import logging as log
+log.basicConfig(level=log.DEBUG)
import paho.mqtt.client as mqtt
+thalesplatz = "5003035"
+ditzingen = "5007000"
+
red = timedelta(minutes=2)
orange = timedelta(minutes=3)
yellow = timedelta(minutes=4)
green = timedelta(minutes=6)
maxtime = timedelta(minutes=15)
-def next_date(now,times):
- # now = datetime.now()
- for time in times: #times are sorted
- h,m = [ int(i) for i in time.split(":") ]
- t = datetime.now().replace(hour=h,minute=m,second=0,microsecond=0)
- if (t - maxtime) < now < t:
- return t
- raise ValueError("Unable to find date for today")
+
+
+def fetch_data():
+ from datetime import datetime as dt
+ from vvs_efa import VVS_EFA
+ efa = VVS_EFA.EFA()
+ now = dt.now()
+ conns = efa.get_next_connections(
+ "Ditzingen Thalesplatz",
+ "Hauptbahnhof (tief)",
+ now,
+ True)
+ for dep in conns:
+ if now < dep.time_of_departure:
+ return dep.time_of_departure
def main():
args = docopt(__doc__)
numleds = int(args['NUMLEDS'])
- times = json.load(open(args['TIMEFILE']))
mqtt_server = args["--mqtt-server"]
- last = []
- mq = mqtt.Client()
- mq.connect(mqtt_server,1883,60)
- mq.loop_start()
+ no_mqtt = args["--no-mqtt"]
+ t = fetch_data()
+ log.error(t)
+ if not no_mqtt:
+ log.info("Starting MQTT")
+ mq = mqtt.Client()
+ mq.connect(mqtt_server,1883,60)
+ mq.loop_start()
+ pub = lambda topic,data: mq.publish(topic,data)
+ else:
+ pub = lambda topic,data: log.info("topic: {}\ndata: {}".format(topic,data))
+
val = 0.0
step = 0.01
+ oldstate = state = None
while True:
- try:
- now = datetime.now()
- t = next_date(now,times)
- sys.stdout.write("{}:{} -> {}:{} ".format(now.hour,now.minute,t.hour,t.minute))
- ret = []
+ ret = []
+ now = datetime.now()
+
+
+ if now > t:
+ t = fetch_data()
+
+ delta = t - now
+ deltastr = str(delta).split('.', 2)[0]
+
+ if not ( (t - maxtime) < now < t):
+ log.error("Bus too far away - {}".format(deltastr))
+ ret = calc_chain(3,val)
+ ret.insert(0,[0,0,0])
+ pub("/leds/nodemcu-switcher/set",json.dumps(ret))
+ val += step % 1
+ else:
+ log.info("{}:{:02d} -> {}:{:02d} delta: {}".format(
+ now.hour,now.minute,t.hour,t.minute,deltastr))
if (t-red) < now < t:
- print("Red alert")
+ state = "red"
ret = [[255,0,0]] * 4
elif (t-orange) < now < t:
- print("orange")
+ state = "orange"
ret = [[255,128,0]] * 4
elif (t-yellow) < now < t:
- print("yellow")
+ state = "yellow"
ret = [[255,255,0]] * 4
elif (t-green) < now < t:
- print("green")
+ state = "green"
ret = [[0,255,0]] * 4
else:
- print("blue")
+ state = "blue"
ret = [[0,0,255]] * 4
- mq.publish("/leds/nodemcu-switcher/set",json.dumps(ret))
- except ValueError:
- print("No bus for now, you will have to walk!")
- ret = calc_chain(3,val)
- ret.insert(0,[0,0,0])
- mq.publish("/leds/nodemcu-switcher/set",json.dumps(ret))
- val += step % 1
+ if oldstate != state:
+ oldstate = state
+ pub("/leds/nodemcu-switcher/set",json.dumps(ret))
sleep(1)
if __name__ == "__main__":
diff --git a/times.json b/times.json
deleted file mode 100644
index bb3ffce..0000000
--- a/times.json
+++ /dev/null
@@ -1,33 +0,0 @@
-[
-
- "13:10",
- "13:25",
- "13:40",
-
- "15:13",
- "15:28",
- "15:43",
- "15:58",
-
- "16:13",
- "16:28",
- "16:43",
- "16:58",
-
- "17:13",
- "17:28",
- "17:43",
- "17:58",
-
- "18:10",
- "18:25",
- "18:40",
- "18:55",
-
- "19:10",
- "19:25",
- "19:40",
- "19:55",
-
- "20:10"
-]
diff --git a/vvs_efa/tests/__init__.py b/vvs_efa/tests/__init__.py
deleted file mode 100644
index bbb1bcf..0000000
--- a/vvs_efa/tests/__init__.py
+++ /dev/null
@@ -1,39 +0,0 @@
-# coding: utf-8
-
-import unittest
-import datetime as dt
-from vvs_efa import VVS_EFA
-
-vvs_efa = VVS_EFA.VVS_EFA()
-
-class TestConvertNameToId(unittest.TestCase):
-
- def test_stadtbibliothek_name(self):
- self.assertEqual(vvs_efa.convertNameToId("Stadtbibliothek"), "5006116")
-
- def test_hauptbahnhof_name(self):
- self.assertEqual(vvs_efa.convertNameToId("Hauptbahnhof"), "5006118")
-
- def test_name_with_umlaut(self):
- self.assertEqual(vvs_efa.convertNameToId("Möhringen"), "5006169")
-
- def test_name_with_scharfs(self):
- self.assertEqual(vvs_efa.convertNameToId("Vaihinger Straße"), "5000170")
-
- def test_stadtbibliothek_name_mobile(self):
- self.assertEqual(vvs_efa.convertNameToId("Stadtbibliothek", True), "5006116")
-
- def test_hauptbahnhof_name_mobile(self):
- self.assertEqual(vvs_efa.convertNameToId("Hauptbahnhof", True), "5006118")
-
- def test_empty_name(self):
- self.assertEqual(vvs_efa.convertNameToId(""), None)
-
-class TestGetNextConnections(unittest.TestCase):
-
- def test_invalid_origin(self):
- with self.assertRaises(TypeError):
- vvs_efa.getNextConnections("", "Feuersee", dt.datetime(2015, 7, 13, 7, 20), True)
-
-if __name__ == '__main__':
- unittest.main()