#!/usr/bin/python

import nilmdb
import argparse

parser = argparse.ArgumentParser(description='Run the NILM server')
parser.add_argument('-p', '--port', help='Port number', type=int, default=12380)
parser.add_argument('-y', '--yappi', help='Run with yappi profiler',
                    action='store_true')
args = parser.parse_args()

# Start web app on a custom port
db = nilmdb.NilmDB("db")
server = nilmdb.Server(db, host = "127.0.0.1",
                       port = args.port,
                       embedded = False)


if args.yappi:
    print "Running in yappi"
    try:
        import yappi
        yappi.start()
        server.start(blocking = True)
    finally:
        yappi.stop()
        print "Try: yappi.print_stats(sort_type=yappi.SORTTYPE_TTOT,limit=50)"
        from IPython import embed
        embed()
else:
    server.start(blocking = True)
db.close()
