import argparse
import base64
from datetime import datetime
import time
import os
import sys
import select
import shutil

import numpy as np
import socketio
import eventlet
import eventlet.wsgi
from PIL import Image
from flask import Flask
from io import BytesIO

from collections import defaultdict

doWithoutKeras = False # skip using Keras, initially

if not doWithoutKeras:
    from keras.models import load_model
    import h5py
    from keras import __version__ as keras_version

sio = socketio.Server()
app = Flask(__name__)
model = None
prev_image_array = None

# **************** Joystick stuff ****************

doDescriptiveLabels = True # display descriptive names instead of just magic numbers
doPruneEmptyStatus = True # if True, remove 0 values from the button/axis status dictionary

def processJoystickEvent(buffer):
    # get the event type, and either the button or axis, depending on the event type
    items = np.frombuffer(buffer, dtype=np.uint8, count=2, offset=6)
    event = items[0]
    buttonOrAxis = items[1]

    # get the value of the button or joystick axis
    value = np.frombuffer(buffer, dtype=np.int16, count=1, offset=4)[0]

    # get the time in milliseconds (since when?) of the event
    time = np.frombuffer(buffer, dtype=np.uint32, count=1, offset=0)[0]

    return ( int(event), int(buttonOrAxis), int(value), int(time) )

def descriptiveJoystickLabels():
    """ descriptive versions of event and device numbers """