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:
Arksine 2020-11-17 12:21:16 -05:00
parent 346fce177f
commit de34fee72a
1 changed files with 17 additions and 3 deletions

View File

@ -13,21 +13,35 @@ class Machine:
self.server.register_endpoint(
"/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):
ep = web_request.get_endpoint()
if ep == "/machine/shutdown":
cmd = "sudo shutdown now"
await self.shutdown_machine()
elif ep == "/machine/reboot":
cmd = "sudo shutdown -r now"
await self.reboot_machine()
else:
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')
scmd = shell_command.build_shell_command(cmd, None)
try:
await scmd.run(timeout=2., verbose=False)
except Exception:
logging.exception(f"Error running cmd '{cmd}'")
return "ok"
def load_plugin(config):
return Machine(config)