(options, args) = parser.parse_args()

if options.portname is not None:
    port = options.portname
else:
    print "Usage: python Huxley_UK_Rail_Station_Delays.py -p <port name>"
    print "(ex.: python Huxley_UK_Rail_Station_Delays.py -p /dev/ttyACM0)"
    exit()

url = "https://huxley.apphb.com/delays/{}/to/{}/50/{}?accessToken={}".format(
    crs, filterCrs, trainTime, accessToken)

bt = BlinkyTape(port)

# Some visual indication that it works for headless setups (green tape)
bt.displayColor(0, 100, 0)
sleep(2)
# Tape resets to stored pattern after a couple of seconds of inactivity

while True:
    try:
        print "GET %s" % (url)
        rawHttpResponse = urllib.urlopen(url)
        stationStatus = json.load(rawHttpResponse)

        if not len(stationStatus) or stationStatus is None:
            raise Exception("Error parsing data")

        alert = stationStatus["delays"]  # bool

        print "%s to %s - Trains Delayed by over 5 minutes: %s" % (
示例#2
0
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":
            "district" : (0, 114, 41),
            "dlr" : (0, 175, 173),
            "hammersmith-city" : (215, 153, 175),
            "jubilee" : (106, 114, 120), 
            "london-overground" : (232, 106, 16),
            "metropolitan" : (117, 16, 86),
            "northern" : (0, 0, 0),
            "piccadilly" : (0, 25, 168),
            "victoria" : (0, 160, 226),
            "waterloo-city" : (118, 208, 189),
            "tfl-rail" : (0, 25, 168) }

bt = BlinkyTape(port)

# Some visual indication that it works for headless setups (green tape)
bt.displayColor(0, 100, 0)
sleep(2)
# Tape resets to stored pattern after a couple of seconds of inactivity

while True:
    try:
        print "GET %s" % (url)
        rawHttpResponse = urllib.urlopen(url)
        lines = json.load(rawHttpResponse)

        if not len(lines) or lines is None:
            raise Exception("Error parsing data")

        # Sort the lines
        lines.sort(key = lambda l: l['modeName'], reverse = True)
class BlinkytapeDriver(threading.Thread):
    def __init__(self, port, mailbox):
        super(BlinkytapeDriver, self).__init__()
        self.mailbox = mailbox
        self.color_generator = None
        self.current_color = (0,0,0)

        if port == 'gui':
            # Start GUI process
            gui_out_connection, gui_in_connection = Pipe()
            self.led = GUIBackend(gui_out_connection)
            p = Process(target=run_gui, args=(gui_in_connection,))
            p.start()

        else:
            # TODO: Implement actual LED stuff
            self.led = BlinkyTape(port) 


    def get_color_generator(self, instruction):
        if instruction.command == "color":
            return single_color_generator(instruction.color)
        if instruction.command == "program":
            return frame_generator(instruction.frames)
        if instruction.command == "stream":
            return socket_color_generator()
        
    def step(self):
        """
        The update function of the run loop

        1) Check the mailbox
        2) If instruction in the mailbox, set program_generator to the appropriate generator
        3) Get the next value from the generator
        4) Display the color if it's not None
        5) Sleep for a bit - future enhacement: rate limit?
        
        """
        try:
            # Step 1
            new_instruction = self.mailbox.get_nowait() 
            self.mailbox.task_done()
            # Step 2
            self.color_generator = self.get_color_generator(new_instruction)

        except Empty:
            pass # There's nothing new
        
        # Step 3
        if self.color_generator == None:
            return
        next_color = self.color_generator.next()
        # Step 4
        if next_color is not None: 
            red, green, blue = next_color
            self.led.displayColor(red, green, blue)
            self.current_color = next_color


        # Step 5
        time.sleep(STEP_TIME) 

    def run(self):
        while True:
            self.step()