# Path: plugins/io_base.py
# class IOBase:
#     def __init__(self, bus):
#         self.bus = bus
#         self.bus.subscribe(self.process_event, thread=False)
#         self.write_queue = Queue(10)
#         self.reader = threading.Thread(target=self.reader_thread)
#         self.reader.daemon = True
#         self.reader.start()
#         self.writer = threading.Thread(target=self.writer_thread)
#         self.writer.daemon = True
#         self.writer.start()
# 
#     def reader_thread(self):
#         raise NotImplementedError()
# 
#     def writer_thread(self):
#         raise NotImplementedError()
# 
#     def convert_data(self, data):
#         return data
# 
#     def process_event(self, ev):
#         try:
#             if ev.name == "leds_enabled":
#                 data = self.convert_data(ev.data)
#                 self.write_queue.put_nowait(data)
#         except queue.Full:
#             #TODO alert somehow without flooding?
#             pass
# 
# Path: plugins/arduino.py
# def getEventForButton(line):
#     if any([k in line for k in goalmap.keys()]):
#         parts = line.split()
#         data = {'source': 'serial', 'team': goalmap[parts[0]]}
# 
#         if len(parts) > 1:
#             data['duration'] = int(parts[1])
# 
#         if len(parts) > 2:
#             data['elapsed'] = int(parts[2])
# 
#         return 'goal_event', data
# 
#     if '_' in line:
#         btn, state = line.split('_')
#         btn = btnmap.get(btn, 'ERROR')
#         state = statemap.get(state, 'ERROR')
#         return 'button_event', {'source': 'serial', 'btn': btn, 'state': state}
# 
#     return None


import time
import os
import logging
from .io_base import IOBase
from .arduino import getEventForButton
        return ("Leds: %s\n" % ', '.join(data))

    def reader_thread(self):
        fifo_file = "/tmp/foos-debug.in"
        try:
            os.mkfifo(fifo_file)
        except:
            pass
        while True:
            f = open(fifo_file, "r")
            if not f:
                logger.error("Error opening fifo file %s", fifo_file)
                time.sleep(5)
                continue

            logger.info("Opened new debugging session")
            while True:
                line = f.readline()
                if not line:
                    break
                line = line.strip()
                ev = getEventForButton(line)
                if ev:
                    self.bus.notify(*ev)

    def writer_thread(self):
        fifo_file = "/tmp/foos-debug.out"
        try:
            os.mkfifo(fifo_file)
        except:

# Path: plugins/io_base.py
# class IOBase:
#     def __init__(self, bus):
#         self.bus = bus
#         self.bus.subscribe(self.process_event, thread=False)
#         self.write_queue = Queue(10)
#         self.reader = threading.Thread(target=self.reader_thread)
#         self.reader.daemon = True
#         self.reader.start()
#         self.writer = threading.Thread(target=self.writer_thread)
#         self.writer.daemon = True
#         self.writer.start()
# 
#     def reader_thread(self):
#         raise NotImplementedError()
# 
#     def writer_thread(self):
#         raise NotImplementedError()
# 
#     def convert_data(self, data):
#         return data
# 
#     def process_event(self, ev):
#         try:
#             if ev.name == "leds_enabled":
#                 data = self.convert_data(ev.data)
#                 self.write_queue.put_nowait(data)
#         except queue.Full:
#             #TODO alert somehow without flooding?
#             pass
# 
# Path: plugins/arduino.py
# def getEventForButton(line):
#     if any([k in line for k in goalmap.keys()]):
#         parts = line.split()
#         data = {'source': 'serial', 'team': goalmap[parts[0]]}
# 
#         if len(parts) > 1:
#             data['duration'] = int(parts[1])
# 
#         if len(parts) > 2:
#             data['elapsed'] = int(parts[2])
# 
#         return 'goal_event', data
# 
#     if '_' in line:
#         btn, state = line.split('_')
#         btn = btnmap.get(btn, 'ERROR')
#         state = statemap.get(state, 'ERROR')
#         return 'button_event', {'source': 'serial', 'btn': btn, 'state': state}
# 
#     return None


# Path: plugins/io_debug.py
import time
import os
import logging
from .io_base import IOBase
from .arduino import getEventForButton

        return ("Leds: %s\n" % ', '.join(data))

    def reader_thread(self):
        fifo_file = "/tmp/foos-debug.in"
        try:
            os.mkfifo(fifo_file)
        except:
            pass
        while True:
            f = open(fifo_file, "r")
            if not f:
                logger.error("Error opening fifo file %s", fifo_file)
                time.sleep(5)
                continue

            logger.info("Opened new debugging session")
            while True:
                line = f.readline()
                if not line:
                    break
                line = line.strip()
                ev = getEventForButton(line)
                if ev:
                    self.bus.notify(*ev)

    def writer_thread(self):
        fifo_file = "/tmp/foos-debug.out"
        try:
            os.mkfifo(fifo_file)
        except:

            pass