#!/usr/bin/python
#By Chris Schoener and Nate Davis, Python 2.7

import ConfigParser #uses built in module to read config
from select import select
import struct
import argparse
import sys
from solenoid import *
import time
import socket
from Student import *
from CodeReader import *
from subprocess import PIPE, Popen
from threading  import Thread
from ComponentRESTClient import ComponentRESTClient


def main():
    #input parsing stuff
    try:
        from Queue import Queue, Empty
    except ImportError:
        from queue import Queue, Empty  # python 3.x

    ON_POSIX = 'posix' in sys.builtin_module_names

    def enqueue_output(out, queue):
        for line in iter(out.readline, b''):
            queue.put(line)
        out.close()


    #sets up the box config parser
    config = ConfigParser.RawConfigParser()
    config.read("boxconfig.txt")
    waitSecs = config.getint("general","seconds_lights_on")

    greenLight = light(config.getint("light", "greenPin"))
    redLight = light(config.getint("light", "redPin"))


    containerIDInputHostPath = config.get("container_id_input_host", "ENTRY_POINT")
    containerIDInputHostArgs = config.get("container_id_input_host", "ARGS")

    containerIDInputHostTarget = []
    containerIDInputHostTarget.append(containerIDInputHostPath)
    containerIDInputHostTarget.extend(containerIDInputHostArgs.split(str=";"))


    studentIDInputHostPath = config.get("container_id_input_host", "ENTRY_POINT")
    studentIDInputHostArgs = config.get("container_id_input_host", "ARGS")

    studentIDInputHostTarget = []
    studentIDInputHostTarget.append(studentIDInputHostPath)
    studentIDInputHostTarget.extend(studentIDInputHostArgs.split(str=";"))


    studentIDInputHost = Popen(studentIDInputHostTarget, stdout=PIPE, stderr=PIPE, bufsize=50, close_fds=ON_POSIX)
    containerIDInputHost = Popen(containerIDInputHostTarget, stdout=PIPE, stderr=PIPE, bufsize=50, close_fds=ON_POSIX)


    studentIDInputQueue = Queue()
    studentIDInputQueueWatcher = Thread(target=enqueue_output, args=(studentIDInputHost.stdout, inputQueue))
    studentIDInputQueueWatcher.daemon = True # thread dies with the program
    studentIDInputQueueWatcher.start()


    containerIDInputQueue = Queue()
    containerIDInputQueueWatcher = Thread(target=enqueue_output, args=(containerIDInputHost.stdout, inputQueue))
    containerIDInputQueueWatcher.daemon = True # thread dies with the program
    containerIDInputQueueWatcher.start()

    RESTClient = ComponentRESTClient()

    while True:
        greenLight.off()
        redLight.off()
        containerIDInput = ''
        studentIDInput = ''

        while containerIDInput == '' or studentIDInput == '':
            try:
                if containerIDInput == '':
                    containerIDInput = containerIDInputQueue.get_nowait()
                else:
                    redLight.on()
                if studentIDInput == '':
                    studentIDInput = studentIDInputQueue.get_nowait()
                else:
                    redLight.on()
            except Empty:
                pass

        if RESTClinet.CheckOutBox(idInput):
            print("IDs accepted")
            lock.unlockThenLock()
        else:
            print("IDs rejected")


main()
