/* This script provides a PYNQ manager */ var PynqManager = (function(){ "use strict"; // Constructor var PynqManager = function(clients){ if (!(this instanceof PynqManager)){ return new PynqManager(clients); } this.clients = clients; } var _p = PynqManager.prototype; // Status check for clients _p.getStatus = function(){ for (var key in this.clients){ (function(_key, _client){ var statusObj = $(".client-status-row[data-cname='"+_key+"']").find(".client-status-value").eq(0) $.ajax({ type: "POST", url: "/status", dataType: "json", data: { "client": _client } }).done(function(res){ var answer = res["status"]; var message = ""; if (answer) { message = answer; }else{ message = "Illegal response: " + res; } statusObj.text(message); }).fail(function(){ statusObj.text("Connection error"); }); })(key, this.clients[key]); } } _p.sendQuestion = function(qname, after=null){ $("#solving-question-name").text(qname); $("#solving-question-status").text('Processing'); $.ajax({ type: "POST", dataType: "json", url: "/start", data: { "qname": qname } }).done(function(res){ var answer = res; $("#solving-question-status").text(answer["status"]); if (after !== null){ after(); } }); } _p.sendStop = function(){ for (var key in this.clients){ (function(_key, _client){ var statusObj = $(".client-status-row[data-cname='"+_key+"']").find(".client-status-value").eq(0) $.ajax({ type: "POST", url: "/stop", dataType: "json", data: { "client": _client } }).done(function(res){ var answer = res["status"]; var message = ""; if (answer) { message = answer; }else{ message = "Illegal response: " + res; } statusObj.text(message); }).fail(function(){ statusObj.text("Connection error"); }); })(key, this.clients[key]); } } return PynqManager; })(); $(function(){ var pynqClients = {} var pm = null; var last_req = 0; var refresh_question_table = function(){ $.ajax({ type: "GET", dataType: "html", url: "/get_question_table" }).done(function(d){ $("#question-table-wrapper").find("#question-table").remove(); $("#question-table-wrapper").html(d); $(".question-row td").click(function(){ var $tr = $(this).parent("tr.question-row"); $(".question-row").removeClass("q-selected"); $tr.addClass("q-selected"); var qname = $tr.data("qname"); // show_question_status(qname); location.hash = "#" + qname; return false; }); var hash = location.hash.replace("#", ""); if(hash != ""){ $(".question-row[data-qname='" + hash + "']").addClass("q-selected"); } }); } var show_client_table = function(){ $.ajax({ type: "GET", dataType: "html", url: "/get_client_table" }).done(function(d){ $("#client-control-pane").html(""); $("#client-control-pane").html(d); var button_action_with_ajax = function($obj, url){ $obj.prop("disabled", "disabled"); $.ajax({ type: "GET", url: url, dataType: "json", }).done((data)=>{ $obj.prop("disabled", false); alert(data['message']); }); }; $("#adccli-login-button").click(function(){ button_action_with_ajax($(this), "/adccli-login"); }); $("#adccli-logout-button").click(function(){ button_action_with_ajax($(this), "/adccli-logout"); }); $("#adccli-get-all-q").click(function(){ button_action_with_ajax($(this), "/adccli-get-all-q"); }); $.ajax({ type: "GET", url: "/adccli-whoami", dataType: "json", }).done((data)=>{ if(data['status']) $("#adccli-status").text("ログイン中"); else $("#adccli-status").text("ログアウト"); }); if(pm) pm.getStatus(); }); } var show_question_status = function(qname){ $.ajax({ type: "GET", dataType: "html", url: "/get_question_status", data: {qname: qname} }).done(function(d){ $("#client-control-pane").html(""); $("#client-control-pane").html(d); $("#client-control-pane").find(".start-button").eq(0).click(function(){ var qname = $(this).data("qname"); pm.sendQuestion(qname); }); $("#client-control-pane").find(".stop-button").eq(0).click(function(){ pm.sendStop(); }); $("#client-control-pane").find(".save-button").eq(0).click(function(){ var qname = $(this).data("qname"); $.ajax({ type: "POST", dataType: "json", url: "/save", data: {qname: qname} }).done((data) => { alert(data['status']); location.reload(); }); }); $("#client-control-pane").find(".submit-button").eq(0).click(function(){ var qname = $(this).data("qname"); $.ajax({ type: "POST", dataType: "json", url: "/adccli-put-a", data: {qname: qname} }).done((data) => { alert(data['message']); }); }); $(".answer-detail-row td").click(function(){ var json_name = $(this).parent("tr").data("json"); var qname = $(this).parent("tr").data("qname"); var viewer_url = "/board-viewer#" + qname + "," + json_name window.open(viewer_url, "_blank"); }) }); } $(window).on('hashchange', function(){ var hash = location.hash.replace("#", ""); if(hash == ""){ show_client_table(); }else{ show_question_status(hash); } }).trigger('hashchange'); var ws = new WebSocket("ws://" + location.host + "/ws"); ws.onmessage = function(e){ var recv = JSON.parse(e.data.replace(/\n/g, "\\n")); console.log(recv); var hash = location.hash.replace("#", ""); if(hash == recv['qname']){ show_question_status(hash); } refresh_question_table(); }; refresh_question_table(); $.ajax({ type: "GET", dataType: "json", url: "/get_clients" }).done(function(d){ pynqClients = d; pm = PynqManager(pynqClients); if(location.hash.replace("#", "") == ""){ pm.getStatus(); } }); });