summaryrefslogtreecommitdiffstats
path: root/ampel/muell.py
diff options
context:
space:
mode:
Diffstat (limited to 'ampel/muell.py')
-rwxr-xr-xampel/muell.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/ampel/muell.py b/ampel/muell.py
new file mode 100755
index 0000000..3b84f02
--- /dev/null
+++ b/ampel/muell.py
@@ -0,0 +1,62 @@
+#! /usr/bin/env nix-shell
+#! nix-shell -i python3 -p python3 python35Packages.docopt python35Packages.requests2
+""" usage: ampel [options] TIMEFILE
+
+ --esp=HOST esp8266 ip [Default: 192.168.1.23]
+ --sleepval=SEC seconds to sleep [Default: 900]
+
+TIMEFILE refers to a json file which contains all the dates where the bus
+drives
+
+"""
+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 requests
+
+# time before the day
+timebefore = timedelta(hours=24)
+
+colors = {
+ "gelber_sack": [ 255,255,0 ],
+ "orchis": [ 45, 255, 0 ],
+ "papiermuell": [ 0,0,255 ],
+ "restmuell": [ 0, 255, 15 ],
+ "kehrwoche": [ 226, 96, 255 ],
+ "fallback": [ 66, 220, 244]
+}
+
+def next_date(now,db):
+ # now = datetime.now()
+ for e in db: #times are sorted
+ name = list(e.keys())[0]
+ times = e[name]
+ for time in times:
+ y,m,d = [ int(i) for i in time.split("-") ]
+ t = datetime.now().replace(year=y,month=m,day=d,
+ hour=9,minute=0,second=0,microsecond=0)
+ if (t - timebefore) < now < t:
+ return name
+ return "fallback"
+
+def to_payload(arr):
+ return {"r":arr[0],"g":arr[1],"b":arr[2]}
+
+def main():
+ args = docopt(__doc__)
+ times = json.load(open(args['TIMEFILE']))
+ sleepval = float(args["--sleepval"])
+ esp = args["--esp"]
+ while True:
+ now = datetime.now()
+ t = next_date(now,times)
+ print(t)
+ requests.get("http://{}/color".format(esp),params=to_payload(colors[t]))
+ sleep(sleepval)
+
+if __name__ == "__main__":
+ main()