machine: register "reboot_machine" and "shutdown_machine" remote methods
This allows Klippy to command moonraker to reboot or shutdown the host machine. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
346fce177f
commit
de34fee72a
|
@ -13,21 +13,35 @@ class Machine:
|
||||||
self.server.register_endpoint(
|
self.server.register_endpoint(
|
||||||
"/machine/shutdown", ['POST'], self._handle_machine_request)
|
"/machine/shutdown", ['POST'], self._handle_machine_request)
|
||||||
|
|
||||||
|
# Register remote methods
|
||||||
|
self.server.register_remote_method(
|
||||||
|
"shutdown_machine", self.shutdown_machine)
|
||||||
|
self.server.register_remote_method(
|
||||||
|
"reboot_machine", self.reboot_machine)
|
||||||
|
|
||||||
async def _handle_machine_request(self, web_request):
|
async def _handle_machine_request(self, web_request):
|
||||||
ep = web_request.get_endpoint()
|
ep = web_request.get_endpoint()
|
||||||
if ep == "/machine/shutdown":
|
if ep == "/machine/shutdown":
|
||||||
cmd = "sudo shutdown now"
|
await self.shutdown_machine()
|
||||||
elif ep == "/machine/reboot":
|
elif ep == "/machine/reboot":
|
||||||
cmd = "sudo shutdown -r now"
|
await self.reboot_machine()
|
||||||
else:
|
else:
|
||||||
raise self.server.error("Unsupported machine request")
|
raise self.server.error("Unsupported machine request")
|
||||||
|
return "ok"
|
||||||
|
|
||||||
|
async def shutdown_machine(self):
|
||||||
|
await self._execute_cmd("sudo shutdown now")
|
||||||
|
|
||||||
|
async def reboot_machine(self):
|
||||||
|
await self._execute_cmd("sudo shutdown -r now")
|
||||||
|
|
||||||
|
async def _execute_cmd(self, cmd):
|
||||||
shell_command = self.server.lookup_plugin('shell_command')
|
shell_command = self.server.lookup_plugin('shell_command')
|
||||||
scmd = shell_command.build_shell_command(cmd, None)
|
scmd = shell_command.build_shell_command(cmd, None)
|
||||||
try:
|
try:
|
||||||
await scmd.run(timeout=2., verbose=False)
|
await scmd.run(timeout=2., verbose=False)
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception(f"Error running cmd '{cmd}'")
|
logging.exception(f"Error running cmd '{cmd}'")
|
||||||
return "ok"
|
|
||||||
|
|
||||||
def load_plugin(config):
|
def load_plugin(config):
|
||||||
return Machine(config)
|
return Machine(config)
|
||||||
|
|
Loading…
Reference in New Issue