|
|
@@ -30,7 +30,7 @@ import nilmrun.trainola |
|
|
|
cherrypy.tools.CORS_allow = cherrypy.Tool('on_start_resource', CORS_allow) |
|
|
|
|
|
|
|
# CherryPy apps |
|
|
|
class NilmRunApp(object): |
|
|
|
class App(object): |
|
|
|
"""Root application for NILM runner""" |
|
|
|
|
|
|
|
def __init__(self): |
|
|
@@ -55,7 +55,49 @@ class NilmRunApp(object): |
|
|
|
def version(self): |
|
|
|
return nilmrun.__version__ |
|
|
|
|
|
|
|
# /trainola |
|
|
|
class AppThread(object): |
|
|
|
|
|
|
|
def __init__(self, manager): |
|
|
|
self.manager = manager |
|
|
|
|
|
|
|
def thread_status(self, pid): |
|
|
|
return { |
|
|
|
"pid": pid, |
|
|
|
"alive": self.manager[pid].alive, |
|
|
|
"name": self.manager[pid].name, |
|
|
|
"start_time": self.manager[pid].start_time, |
|
|
|
"parameters": self.manager[pid].parameters, |
|
|
|
"log": self.manager[pid].log, |
|
|
|
} |
|
|
|
|
|
|
|
# /thread/status |
|
|
|
@cherrypy.expose |
|
|
|
@cherrypy.tools.json_out() |
|
|
|
def status(self, pid, clear = False): |
|
|
|
if pid not in self.manager: |
|
|
|
raise cherrypy.NotFound() |
|
|
|
status = thread_status(pid) |
|
|
|
if clear: |
|
|
|
self.manager[pid].clear_log() |
|
|
|
return status |
|
|
|
|
|
|
|
# /thread/kill |
|
|
|
@cherrypy.expose |
|
|
|
@cherrypy.tools.json_in() |
|
|
|
@cherrypy.tools.json_out() |
|
|
|
@cherrypy.tools.CORS_allow(methods = ["POST"]) |
|
|
|
def kill(self, pid): |
|
|
|
if pid not in self.manager: |
|
|
|
raise CherryPy.NotFound() |
|
|
|
if not self.manager.terminate(pid): |
|
|
|
raise cherrypy.HTTPError("503 Service Unavailable", |
|
|
|
"Failed to stop thread") |
|
|
|
status = thread_status(pid) |
|
|
|
manager.remove(pid) |
|
|
|
|
|
|
|
class AppFilter(object): |
|
|
|
|
|
|
|
# /filter/trainola |
|
|
|
@cherrypy.expose |
|
|
|
@cherrypy.tools.json_in() |
|
|
|
@cherrypy.tools.json_out() |
|
|
@@ -114,7 +156,9 @@ class Server(object): |
|
|
|
cherrypy._cperror._ie_friendly_error_sizes = {} |
|
|
|
|
|
|
|
# Build up the application and mount it |
|
|
|
root = NilmRunApp() |
|
|
|
root = App() |
|
|
|
root.thread = AppThread() |
|
|
|
root.filter = AppFilter() |
|
|
|
cherrypy.tree.apps = {} |
|
|
|
cherrypy.tree.mount(root, basepath, config = { "/" : app_config }) |
|
|
|
|
|
|
|