Skip to content

Commit

Permalink
Web UI works perfectly for loading, unloading, and reloading modules.…
Browse files Browse the repository at this point in the history
… Server now terminates when it's module unloads.
  • Loading branch information
computer-whisperer committed Dec 23, 2014
1 parent 936f695 commit 03c8d64
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion yeti/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def stop(self):
This is used to stop module operation. It cancels all running coroutines and calls the `module_deinit`
hook to stop everything
"""
self.call_hook("deinit")
for task in self.tasks:
task.cancel()
self.event_loop = None
self.call_hook("deinit")
self.logger.info("Finished module deinit.")

def add_task(self, coroutine):
Expand Down
5 changes: 5 additions & 0 deletions yeti/preloaded_modules/webui-resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ <h2 class="sub-header">Modules</h2>
<tbody></tbody>
</table>
</div>
<button id="load_button" class="btn btn-success,">Load Module</button> <input id="module_target" name="target"/>
<div id="module_info">
<h3 class="sub-header">My Module</h3>
<
</div>
</div>
</div>
</div>
Expand Down
56 changes: 49 additions & 7 deletions yeti/preloaded_modules/webui-resources/script.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,64 @@
//$("#modtable").bootgrid()


function onData(data){
$("#modtable tbody tr").remove()
html = "";
for(var i = 0; i < data.modules.length; i++){
html += '<tr><td>' + data.modules[i].subsystem + '</td><td>'
+ data.modules[i].filename + '</td><td>'
+ data.modules[i].filename + '</td><td>'
+ data.modules[i].status + '</td></tr>'
unload_command = 'command_unloadmod("' + data.modules[i].subsystem + '")'
reload_command = 'command_reloadmod("' + data.modules[i].subsystem + '")'
mod_select = 'watch_module("' + data.modules[i].subsystem + '")'
commands = "<button class='btn btn-warning' onClick='" + unload_command + "'>unload</button> <button class='btn btn-danger' onClick='" + reload_command +"'>reload</button>"
status_tag = "<h4 style='display: inline;'><span class='label label-default'>" + data.modules[i].status + "</span></h4>"
if(data.modules[i].status == "Loaded"){
status_tag = "<h4 style='display: inline;'><span class='label label-success'>" + data.modules[i].status + "</span></h4>"
}
html += '<tr onClick=' + mod_select + '><td>' + data.modules[i].subsystem + '</td><td>'
+ data.modules[i].filename + '</td><td>'
+ status_tag + '</td><td>'
+ commands + '</td></tr>'
}

$("#modtable tbody").html(html)
}

function watch_module(modname){
var watchingmod = modname
getData()
}

function load_handler(){
send_command("load", $("#module_target")[0].value)
}

function command_unloadmod(modname){
send_command("unload", modname)
}

function command_reloadmod(modname){
send_command("reload", modname)
}

function send_command(command, target){
data = { command: command, target: target }
$.post("/api/command", data)
setTimeout(getData, 200);
}

function getDataLoop() {
$.getJSON("/json", onData);
getData()
//setTimeout(getDataLoop, 1000);
}

function getData(){$.getJSON("/api/json", onData)}

$(document).ready(function(){
setTimeout(getDataLoop, 1000);
$("#modtable").bootgrid()
$("#load_button").click(load_handler)
$('#module_target').keypress(function (e) {
if (e.which == 13) {
load_handler();
return false;
}
});
setTimeout(getDataLoop, 100);
})
32 changes: 29 additions & 3 deletions yeti/preloaded_modules/webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,42 @@ def data_handler(self, request):
mod_data["subsystem"] = modname
if hasattr(mod_object, "loader"):
mod_data["filename"] = mod_object.loader.module_path
mod_data["status"] = "Loaded"
data_structure["fallback"] = mod_object.loader.fallback_list
data_structure["modules"].append(mod_data)
text = json.dumps(data_structure, allow_nan=False)
return web.Response(body=text.encode("utf-8"))

@asyncio.coroutine
def command_handler(self, request):
commands = {"load": self.load_command, "unload": self.unload_command, "reload": self.reload_command}
data = yield from request.post()
commands[data["command"]](data["target"])

#print(request.match_info['target'])
return web.Response(body=":)".encode("utf-8"))

def load_command(self, target):
loader = yeti.ModuleLoader()
loader.set_context(self.context)
loader.load(target)

def unload_command(self, target):
self.context.unload_module(target)

def reload_command(self, target):
self.context.loaded_modules[target].call_hook("reload")

@asyncio.coroutine
def init_server(self):
app = web.Application()
app.router.add_route("GET", "/json", self.data_handler)
app.router.add_route("GET", "/api/json", self.data_handler)
app.router.add_route("POST", "/api/command", self.command_handler)
app.router.add_static("/", self.file_root)
srv = yield from self.event_loop.create_server(app.make_handler(),
self.srv = yield from self.event_loop.create_server(app.make_handler(),
'127.0.0.1', 8080)

print("Yeti WebUI started at http://127.0.0.1:8080")
return srv

def module_deinit(self):
self.srv.close()

0 comments on commit 03c8d64

Please sign in to comment.