summaryrefslogtreecommitdiffstats
path: root/ampel.py
diff options
context:
space:
mode:
Diffstat (limited to 'ampel.py')
-rwxr-xr-xampel.py96
1 files changed, 63 insertions, 33 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__":