-
Notifications
You must be signed in to change notification settings - Fork 17
/
Aurora.py
75 lines (59 loc) · 2.19 KB
/
Aurora.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
74
75
"""
Aurora Alert for Blinky Tape
To run on default (Raspberry Pi) USB serial port: python Aurora.py &
This script works by turning off the tape when there is no Aurora.
You need to save one of the images to the tape with pattern paint first.
(C) 2015 James Singleton (https://unop.uk)
MIT Licensed
"""
import optparse
from blinkytape import BlinkyTape
from time import sleep
from xml.etree import ElementTree
import sys
if sys.version_info > (3, 0):
import urllib.request as requestlib
else:
import urllib2 as requestlib
# Default Blinky Tape port on Raspberry Pi is /dev/ttyACM0
parser = optparse.OptionParser()
parser.add_option("-p", "--port", dest="portname",
help="serial port (ex: /dev/ttyACM0)", default="/dev/ttyACM0")
(options, args) = parser.parse_args()
if options.portname is not None:
port = options.portname
else:
print("Usage: python Aurora.py -p <port name>")
print("(ex.: python Aurora.py -p /dev/ttyACM0)")
exit()
# Documentation: http://aurorawatch.lancs.ac.uk/api_info/
# Code and spec: https://github.com/stevemarple/AuroraWatchNet
url = 'http://aurorawatch.lancs.ac.uk/api/0.1/status.xml'
bt = BlinkyTape(port)
request = requestlib.Request(url)
request.add_header('User-Agent', 'BlinkyTape Aurora Alert unop.uk')
opener = requestlib.build_opener()
# Some visual indication that it works, for headless setups (green tape)
bt.displayColor(0, 100, 0)
sleep(2)
while True:
try:
print("GET %s" % (url))
rawXml = opener.open(request).read()
tree = ElementTree.fromstring(rawXml)
if not len(tree) or tree is None:
raise Exception("Error loading data")
currentStateName = tree.find('current').find('state').get('name')
print(currentStateName)
if currentStateName != "red":
for x in range(300):
bt.displayColor(0, 0, 0)
sleep(1)
else:
# Tape resets to stored pattern after a few seconds of inactivity
sleep(300) # Wait 5min
except:
# Blue indicates an error
bt.displayColor(0, 0, 100)
sleep(300) # Wait 5min
pass