1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 ampel.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()
|