import io
import protectedBytesIO
import time
import BaseHTTPServer
from SocketServer import ThreadingMixIn
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import SimpleHTTPServer
import buttonIncrementor

# available camera selection
stream = protectedBytesIO.protectedBytesIO(io.BytesIO())
try:
    import drivePiCamera
    camera = drivePiCamera.piCamera(stream)
except ImportError:
    import driveCvCamera
    camera = driveCvCamera.cvCamera(stream)

PORT = 8060
FPS = 24

axis1 = buttonIncrementor.buttonIncrementor()
axis2 = buttonIncrementor.buttonIncrementor()

try:
    # pi enviroment
    import pigpio
    piController = pigpio.pi()
except ImportError:
    # non-pi enviroment
    # FIXME : implemnt some virtual devices
    pass

class TestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path.endswith('cam.mjpg'):
            print "handling cam.mjpgl.."
            self.send_response(200)
            self.send_header('Content-type', 'multipart/x-mixed-replace; boundary=--jpgboundary')
            self.end_headers()
            while True:
                try:
                    value = stream.getvalue()
                    self.wfile.write("--jpgboundary")
                    self.send_header('Content-type', 'image/jpeg')
                    self.send_header('Content-length', len(value))
                    self.end_headers()
                    self.wfile.write(value)
                except KeyboardInterrupt:
					break
            return
        if self.path.endswith('index.html'):
            return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
            """The test example handler."""
    def do_POST(self):
        """Handle a post request ."""
        # FIXME : put a lock here , revent multi-thread re-entry issue
        length = int(self.headers.getheader('content-length'))
        data_string = self.rfile.read(length)
        if data_string == "up":
            axis1.move(True)
        if data_string == "down":
            axis1.move(False)
        if data_string == "left":
            axis2.move(True)
        if data_string == "right":
            axis2.move(False)
        # piController.set_servo_pulsewidth(16, axis1.currentPosition)
        # piController.set_servo_pulsewidth(20, axis2.currentPosition)
        response_string = 'received:{0},{1}'.format(axis1,axis2)
        print response_string
        self.wfile.write(response_string) #as response


class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
	"""Handle requests in a separate thread."""

def start_server():
    """Start the server."""
    server_address = ("", PORT)
    server = ThreadedHTTPServer(server_address, TestHandler)
    server.serve_forever()

if __name__ == "__main__":
    camera.serve_forever()
    print 'cam'
    start_server()
