diff --git a/__init__.py b/__init__.py index 784e2ac..73f3953 100644 --- a/__init__.py +++ b/__init__.py @@ -23,18 +23,21 @@ from aiohttp import web @server.PromptServer.instance.routes.get("/bilbox/reboot") async def fetch_customnode_mappings(request): type = request.rel_url.query["mode"] - print("BilboX:",type) + print("BilboX:", type) json_obj = {"server_op": type} - - if(type == "shutdown"): - os.system("shutdown /s") # Shutdown - if(type == "reboot"): - os.system("shutdown /r") # Restart - if(type == "logout"): - os.system("shutdown /l") # logout - if(type == "lock"): - os.system("rundll32.exe user32.dll,LockWorkStation") # logout - return web.json_response(json_obj, content_type='application/json') + + if type == "shutdown": + os.system("shutdown /s") # Shutdown + if type == "hibernate": + os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0") # hibernate + if type == "reboot": + os.system("shutdown /r") # Restart + if type == "logout": + os.system("shutdown /l") # logout + if type == "lock": + os.system("rundll32.exe user32.dll,LockWorkStation") # logout + return web.json_response(json_obj, content_type="application/json") + WEB_DIRECTORY = "./web" __all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"] diff --git a/web/reboot.js b/web/reboot.js index f831b66..a0b1bbc 100644 --- a/web/reboot.js +++ b/web/reboot.js @@ -1,111 +1,127 @@ import { app } from "../../scripts/app.js"; import { api } from "../../scripts/api.js"; + +function apiReboot(type) { + api + .fetchApi("/bilbox/reboot?mode=" + type) + .then((response) => response.json()) + .then(async (data) => { + try { + console.log(data["server_op"]); + } catch (exception) { + console.log("Problem when sending " + type + " to server..."); + } + }); +} + app.registerExtension({ - name: "Comfy.BilboXReboot", - init() { - }, - async setup() { + name: "Comfy.BilboXReboot", + commands: [ + { + id: "reboot_lock", + label: "Lock Session", + function: () => { + apiReboot("lock"); + }, + }, + { + id: "reboot_logout", + label: "Logout", + function: () => { + apiReboot("logout"); + }, + }, + { + id: "reboot_reboot", + label: "Reboot", + function: () => { + apiReboot("reboot"); + }, + }, + { + id: "reboot_hibernate", + label: "Hibernate", + function: () => { + apiReboot("hibernate"); + }, + }, + { + id: "reboot_shutdown", + label: "Shutdown", + function: () => { + apiReboot("shutdown"); + }, + }, + ], + menuCommands: [ + // Create a nested menu structure + { + path: ["Server..."], + commands: ["reboot_lock", "reboot_logout", "reboot_reboot", "reboot_hibernate", "reboot_shutdown"], + }, + ], + init() {}, + async setup() { + function toggle_menu(el) { + if (el.style.display == "block") { + el.style.display = "none"; + } else { + el.style.display = "block"; + } + } - function toggle_menu(el) { - if ( el.style.display == "block"){ - el.style.display = "none" ; - }else{ - el.style.display = 'block'; - } - } - - function apiReboot(type) { - api.fetchApi('/bilbox/reboot?mode='+type) - .then(response => response.json()) - .then(async data => { - try { - console.log(data["server_op"]); - } - catch (exception) { - console.log("Problem when sending "+type+" to server..."); - } - }); - } - - function create_menu(el, id, isnew=false) { - - let cl = "bx-reboot-context-menu"; - if(isnew) - cl = "bx-reboot-cm-new " + cl; - el.insertAdjacentHTML('beforeend',' \ - ' + ); - el.querySelector(".lock").onclick = () => { apiReboot("lock");} - el.querySelector(".logout").onclick = () => { apiReboot("logout");} - el.querySelector(".reboot").onclick = () => { apiReboot("reboot");} - el.querySelector(".shutdown").onclick = () => { apiReboot("shutdown");} - } + el.querySelector(".lock").onclick = () => { + apiReboot("lock"); + }; + el.querySelector(".logout").onclick = () => { + apiReboot("logout"); + }; + el.querySelector(".reboot").onclick = () => { + apiReboot("reboot"); + }; + el.querySelector(".hibernate").onclick = () => { + apiReboot("hibernate"); + }; + el.querySelector(".shutdown").onclick = () => { + apiReboot("shutdown"); + }; + } - // Old Interface - const menu = document.querySelector(".comfy-menu"); - if(menu) - { - const rebootButton = document.createElement("button"); - rebootButton.id = "bxRebootButtonOld"; - rebootButton.textContent = "Server..."; - rebootButton.onclick = () => { toggle_menu(document.getElementById("bxRebootContextMenuOld")) } - rebootButton.style.background = "linear-gradient(90deg, #442222 0%, #222222 100%)"; - - menu.append(rebootButton); + // Old Interface + const menu = document.querySelector(".comfy-menu"); + if (menu) { + const rebootButton = document.createElement("button"); + rebootButton.id = "bxRebootButtonOld"; + rebootButton.textContent = "Server..."; + rebootButton.onclick = () => { + toggle_menu(document.getElementById("bxRebootContextMenuOld")); + }; + rebootButton.style.background = "linear-gradient(90deg, #442222 0%, #222222 100%)"; - create_menu(menu, "bxRebootContextMenuOld") - } - - // New interface - function add_button_new_interface() { - const side_tb = document.querySelector(".side-tool-bar-end"); - const params_button = document.querySelector(".pi-cog"); - if(side_tb && params_button) - { - const shutButton = params_button.parentElement.cloneNode(true); - shutButton.id = "bxRebootButtonNew"; - let shuticon = shutButton.querySelector(".pi-cog"); - shuticon.classList.remove("pi-cog"); - shuticon.classList.add("pi-power-off"); - side_tb.append(shutButton); + menu.append(rebootButton); - shutButton.onclick = () => { toggle_menu(document.getElementById("bxRebootContextMenuNew")) } - - create_menu(document.querySelector(".side-tool-bar-container"), "bxRebootContextMenuNew", true) - } - else - setTimeout(add_button_new_interface, 1000); - } - add_button_new_interface(); - - const setting = app.ui.settings.addSetting({ - id: "BilboX.DistantShutdownMenu", - name: "Show distant server shutdown menu", - defaultValue: false, - type: "boolean", - onChange: async (v) => { - let b_old = document.getElementById("bxRebootButtonOld"); - let m_old = document.getElementById("bxRebootContextMenuOld"); - if(b_old) - b_old.style.display = v ? "block":"none"; - if(!v && m_old) - m_old.style.display = "none"; - - let b_new = document.getElementById("bxRebootButtonNew"); - let m_new = document.getElementById("bxRebootContextMenuNew"); - if(b_new) - b_new.style.display = v ? "block":"none"; - if(!v && m_new) - m_new.style.display = "none"; - } - }); - - }, -}); \ No newline at end of file + create_menu(menu, "bxRebootContextMenuOld"); + } + }, +});