From f8b7684d60171a19fe7cba337ea1207031a40580 Mon Sep 17 00:00:00 2001 From: syllebra Date: Fri, 8 Dec 2023 14:56:31 -1000 Subject: [PATCH] Main menu options to shutdown/reboot a windows server from ui --- __init__.py | 32 +++++++++++++++++++++- reboot/reboot.js | 61 ++++++++++++++++++++++++++++++++++++++++++ web_to_copy/bilbox.css | 36 +++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 reboot/reboot.js diff --git a/__init__.py b/__init__.py index 269c885..b282730 100644 --- a/__init__.py +++ b/__init__.py @@ -15,6 +15,7 @@ NODE_DISPLAY_NAME_MAPPINGS = { __all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] + # ensure .js and web copy print("### Copying: BilboX web...") @@ -24,4 +25,33 @@ import folder_paths bx_path = os.path.dirname(__file__) comfy_path = os.path.dirname(folder_paths.__file__) dest_path = os.path.join(comfy_path, "web", "extensions") -shutil.copytree(os.path.join(bx_path,"web_to_copy"), os.path.join(dest_path,"bilbox"), dirs_exist_ok=True) # Fin \ No newline at end of file +shutil.copytree(os.path.join(bx_path,"web_to_copy"), os.path.join(dest_path,"bilbox"), dirs_exist_ok=True) # Fin + + + +# Expand Server api + +import server +from aiohttp import web +import aiohttp +import json +import zipfile +import urllib.request + +@server.PromptServer.instance.routes.get("/bilbox/reboot") +async def fetch_customnode_mappings(request): + type = request.rel_url.query["mode"] + 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') + +WEB_DIRECTORY = "reboot" \ No newline at end of file diff --git a/reboot/reboot.js b/reboot/reboot.js new file mode 100644 index 0000000..7b0516e --- /dev/null +++ b/reboot/reboot.js @@ -0,0 +1,61 @@ +import { app } from "../../scripts/app.js"; +import { api } from "../../scripts/api.js"; +app.registerExtension({ + name: "Comfy.BilboXReboot", + init() { + }, + async setup() { + + const menu = document.querySelector(".comfy-menu"); + const separator = document.createElement("hr"); + + separator.style.margin = "20px 0"; + separator.style.width = "100%"; + menu.append(separator); + + const rebootButton = document.createElement("button"); + rebootButton.textContent = "Server..."; + rebootButton.onclick = () => { + + if (document.getElementById("bxRebootContextMenu") .style.display == "block"){ + document.getElementById("bxRebootContextMenu").style.display = "none" ; + }else{ + var menu = document.getElementById("bxRebootContextMenu") + menu.style.display = 'block'; + } + } + menu.append(rebootButton); + + + menu.insertAdjacentHTML('beforeend',' \ + '); + + 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..."); + } + }); + } + + menu.querySelector(".lock").onclick = () => { apiReboot("lock");} + menu.querySelector(".logout").onclick = () => { apiReboot("logout");} + menu.querySelector(".reboot").onclick = () => { apiReboot("reboot");} + menu.querySelector(".shutdown").onclick = () => { apiReboot("shutdown");} + + rebootButton.style.background = "linear-gradient(90deg, #442222 0%, #222222 100%)"; + //rebootButton.style.color = "black"; + }, +}); \ No newline at end of file diff --git a/web_to_copy/bilbox.css b/web_to_copy/bilbox.css index 0a5124c..4cba6f5 100644 --- a/web_to_copy/bilbox.css +++ b/web_to_copy/bilbox.css @@ -301,4 +301,40 @@ textarea:read-only::selection { text-transform: capitalize; font-style: italic; +} + +.menu { + list-style: none; + display: flex; + flex-direction: column; + background-color: var(--comfy-input-bg); + border-radius: 10px; + box-shadow: 0 10px 20px rgb(64 64 64 / 5%); + padding: 10px 0; +} +.menu > li > a { +font: inherit; +border: 0; +padding: 10px 30px 10px 15px; +display: flex; +align-items: center; +position: relative; +text-decoration: unset; +color: var(--border-color); +font-weight: 500; +transition: 0.5s linear; +-webkit-transition: 0.5s linear; +-moz-transition: 0.5s linear; +-ms-transition: 0.5s linear; +-o-transition: 0.5s linear; +} +.menu > li > a:hover { +background:#161616; +color: #97a9c4; +} +.menu > li > a > i { +padding-right: 10px; +} +.menu > li.shutdown > a:hover { +color: red; } \ No newline at end of file