This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trafficpi.py
executable file
·74 lines (61 loc) · 2.73 KB
/
trafficpi.py
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
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python
import DftTraffic
import argparse
import requests
def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
# Locations: 10501 - M3 to M275 along the M27 Eastbound
# 10710 - M275 to M3 along the M27 Westbound
# TODO: Add support for combined routes, where the journey is split in the source data
# TODO: Change the defaults to be based on percentage of clear journey time or expected journey time
if __name__ == "__main__":
# Parse Parameters
parser = argparse.ArgumentParser(description='Traffic indicator using LedBorg - Green, Amber or Red based on journey time.')
parser.add_argument('--location', default="10710", help='specify the location code to check default: 10710 (M275 to M3)')
parser.add_argument('--warn', default=1500, help='Specify the minimum journey time to warn on default: 25')
parser.add_argument('--alert', default=1800, help='Specify the minimum journey time to alert on default: 30')
parser.add_argument('--notify', default='console', help='How to notify - mqtt, piglow or console')
parser.add_argument('--api', default=False, help='Use API at this URL')
args = parser.parse_args()
if args.location:
selected_location = args.location
# Get the feed over http
if args.api:
times = requests.get("%s/route/%s" % (args.api,args.location)).json()
else:
journeys = DftTraffic.DftTraffic()
times = journeys.journey_times(args.location)
# Current travel time in minutes
travelTime = times['travelTime']
if travelTime < 0:
travelTime = times['normallyExpectedTravelTime']
if travelTime < args.warn:
# If it's lower than this let's glow green
colour = "00ff00"
elif travelTime < args.alert:
# Between amber and red - must be amber
colour = "996600"
else:
# Glow red
colour = "ff0000"
if args.notify == 'mqtt':
import paho.mqtt.client as paho
# Connect to mqtt server
mqttc = paho.Client()
mqttc.connect("localhost", 1883, 60)
mqttc.publish("traffic/%s" % args.location, travelTime, qos=0, retain=True)
mqttc.publish("traffic", travelTime, qos=0, retain=True)
mqttc.publish("light/rgb", colour, qos=0, retain=True)
elif args.notify == 'piglow':
from PyGlow import PyGlow
(r, g, b) = hex_to_rgb(colour)
PyGlow().led(1, r)
PyGlow().led(4, g)
PyGlow().led(5, b)
else:
print "expected: %s" % times['normallyExpectedTravelTime']
print "current: %s" % times['travelTime']
print "delay: %f" % times['delay']
print "colour: %s" % colour