"""mems.instrument.fake_camera

Implementation of a simulated camera device.
"""

# created 2001/09/24, AMK

__revision__ = "$Id: fake_camera.py,v 1.5 2002/04/18 20:53:30 akuchlin Exp $"

import sys, time

from PIL import Image
from mems.instrument import protocols
from mems.instrument.server import util
from mems.instrument.server.configuration import get_configuration
import random

# Global variables holding the image size
x_size, y_size = 320, 200

def do_snapshot ():
    path = random.choice([
        "/home/akuchlin/src/matisse/matisse2/java/images/apples.jpg",
        "/home/akuchlin/src/matisse/matisse2/java/images/moraccans.jpg",
        "/home/akuchlin/src/matisse/matisse2/java/images/lablouseroumaine.jpg",
        "/home/akuchlin/src/matisse/matisse2/java/images/icarus1.jpg"])

    image = Image.open(path)
    image = image.resize( (x_size, y_size) )

    # XXX use a generated temp. filename!
    path = '/tmp/new-image.jpg'
    image.save(path)
    util.send_message("network_server",
                       "image path=%s hires=no" % path)

def do_hires ():
    util.send_message("network_server",
                       "image path=/home/akuchlin/src/matisse/matisse2/java/images/beastsofthesea.jpg hires=yes")

def do_size (x, y):
    # Save the desired size
    global x_size, y_size
    x_size, y_size = x,y


def main ():
    config = get_configuration()
    # Take an initial image
    do_snapshot()
    
    while 1:
        if util.has_input_waiting(sys.stdin):
            line = sys.stdin.readline()
            cmd, args = protocols.parse_message_line(line)
            if config.get_debug():
                print '%s: Received command %s %r' % (__name__, cmd, args)
                
            if cmd == 'quit':
                if config.get_debug():
                    print '%s: exiting' % __name__
                break
            elif cmd == "snapshot":
                do_snapshot(**args)
            elif cmd == "hires":
                do_hires(**args)
            elif cmd == "size":
                do_size(**args)
            else:
                raise RuntimeError, ("Unknown command %r args=%r" % (cmd, args))

    return 

