From a51ac1e29eacbfcf7ff13d0d56528100f9cc28f1 Mon Sep 17 00:00:00 2001 From: makefu Date: Thu, 11 May 2017 15:39:06 +0200 Subject: init --- ampel.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 ampel.py (limited to 'ampel.py') diff --git a/ampel.py b/ampel.py new file mode 100755 index 0000000..5b1391f --- /dev/null +++ b/ampel.py @@ -0,0 +1,83 @@ +#! /usr/bin/env nix-shell +#! nix-shell -i python3 -p python3 python35Packages.docopt python35Packages.paho-mqtt +""" usage: ampel [options] NUMLEDS TIMEFILE + + --mqtt-server=HOST path to mqtt server [Default: 192.168.8.11] + +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 +|-----------|----|----|----| +BLUE GR YEL RED BLUE + EEN LOW +""" +from docopt import docopt +from fade import calc_chain +import time +from time import sleep +import json +import sys +from datetime import datetime, timedelta + +import paho.mqtt.client as mqtt + +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 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() + val = 0.0 + step = 0.01 + while True: + try: + now = datetime.now() + t = next_date(now,times) + sys.stdout.write("{}:{} -> {}:{} ".format(now.hour,now.minute,t.hour,t.minute)) + ret = [] + if (t-red) < now < t: + print("Red alert") + ret = [[255,0,0]] * 4 + elif (t-orange) < now < t: + print("orange") + ret = [[255,128,0]] * 4 + elif (t-yellow) < now < t: + print("yellow") + ret = [[255,255,0]] * 4 + elif (t-green) < now < t: + print("green") + ret = [[0,255,0]] * 4 + else: + print("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 + sleep(1) + +if __name__ == "__main__": + main() -- cgit v1.2.3