summaryrefslogtreecommitdiffstats
path: root/ampel.py
diff options
context:
space:
mode:
authormakefu <github@syntax-fehler.de>2017-12-06 13:52:53 +0100
committermakefu <github@syntax-fehler.de>2017-12-06 13:52:53 +0100
commit6bfcd8a348962f0357372f4e74c286f93e6eec26 (patch)
tree00d97bfb8a716afc118a35f7bb7097456bf46511 /ampel.py
parent8727451b41b057d36e76cecdb543015c0f50b304 (diff)
setup.py: init
Diffstat (limited to 'ampel.py')
-rwxr-xr-xampel.py141
1 files changed, 0 insertions, 141 deletions
diff --git a/ampel.py b/ampel.py
deleted file mode 100755
index 976bdd8..0000000
--- a/ampel.py
+++ /dev/null
@@ -1,141 +0,0 @@
-#! /usr/bin/env python3
-#! nix-shell -i python3 -p python3 python35Packages.docopt python35Packages.paho-mqtt
-""" usage: ampel [options] NUMLEDS
-
- --mqtt-server=HOST path to mqtt server [Default: 192.168.8.11]
- --influx-server=HOST path to influxdb server [Default: 192.168.8.11]
- --no-mqtt disable mqtt connections for debugging
- --no-influx disable sending stats to influxdb
-
-NUMLEDS is the number of leds to output data for (--add-empty does not count in here)
-
-13 22 24 26 28
-|-----------|----|----|----|
-BLUE GR YEL RED BLUE
- EEN LOW
-"""
-from docopt import docopt
-from influxdb import InfluxDBClient
-from fade import calc_chain
-import time
-from time import sleep
-import json
-import sys
-from datetime import datetime, timedelta
-import requests
-import logging as log
-log.basicConfig(level=log.INFO)
-
-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 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__)
- db = "telegraf"
- numleds = int(args['NUMLEDS'])
- mqtt_server = args["--mqtt-server"]
- influx_server = args["--influx-server"]
- no_mqtt = args["--no-mqtt"]
- no_influx = args["--no-influx"]
-
- t = fetch_data()
- 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))
- if not no_influx:
- log.info("preparing influxdb")
- client = InfluxDBClient(host=influx_server, port=8086, database=db)
- msg = {
- "measurement": "seconds_to_bus",
- "tags": {
- "": "thalesplatz"
- },
- "time": "2009-11-10T23:00:00Z",
- "fields": {
- "value": 0.64
- }
- }
-
- sendstat = lambda secs: client.write_points([ {
- "measurement": "seconds_to_bus",
- "tags": { "station": "thalesplatz" },
- "fields": { "value": secs } }])
- else:
- log.info("No influx")
- sendstat = lambda secs: log.info("stat {} -> {} secs".format(datetime.now().replace(microsecond=0).isoformat(),secs))
-
- val = 0.0
- step = 0.01
- oldstate = state = None
- while True:
- ret = []
- now = datetime.now()
-
-
- if now > t:
- t = fetch_data()
-
- delta = t - now
- sendstat(delta.seconds + delta.days*86400)
- deltastr = str(delta).split('.', 2)[0]
-
- if not ( (t - maxtime) < now < t):
- log.info("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:
- state = "red"
- ret = [[255,0,0]] * 4
- elif (t-orange) < now < t:
- state = "orange"
- ret = [[255,128,0]] * 4
- elif (t-yellow) < now < t:
- state = "yellow"
- ret = [[255,255,0]] * 4
- elif (t-green) < now < t:
- state = "green"
- ret = [[0,255,0]] * 4
- else:
- state = "blue"
- ret = [[0,0,255]] * 4
- if oldstate != state:
- oldstate = state
- pub("/leds/nodemcu-switcher/set",json.dumps(ret))
- sleep(1)
-
-if __name__ == "__main__":
- main()