moonraker: add ability to register remote methods with Klippy

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-10-27 09:10:53 -04:00
parent c6d629659b
commit ba78a82a26
2 changed files with 17 additions and 4 deletions

View File

@ -19,7 +19,8 @@ from authorization import Authorization
# These endpoints are reserved for klippy/server communication only and are # These endpoints are reserved for klippy/server communication only and are
# not exposed via http or the websocket # not exposed via http or the websocket
RESERVED_ENDPOINTS = [ RESERVED_ENDPOINTS = [
"list_endpoints", "gcode/subscribe_output" "list_endpoints", "gcode/subscribe_output",
"register_remote_method"
] ]

View File

@ -78,10 +78,13 @@ class Server:
# they do not return a response to Klippy after execution # they do not return a response to Klippy after execution
self.pending_requests = {} self.pending_requests = {}
self.remote_methods = {} self.remote_methods = {}
self.klippy_reg_methods = []
self.register_remote_method( self.register_remote_method(
'process_gcode_response', self._process_gcode_response) 'process_gcode_response', self._process_gcode_response,
need_klippy_reg=False)
self.register_remote_method( self.register_remote_method(
'process_status_update', self._process_status_update) 'process_status_update', self._process_status_update,
need_klippy_reg=False)
# Plugin initialization # Plugin initialization
self.plugins = {} self.plugins = {}
@ -151,12 +154,15 @@ class Server:
for evt in events: for evt in events:
self.ioloop.spawn_callback(evt, *args) self.ioloop.spawn_callback(evt, *args)
def register_remote_method(self, method_name, cb): def register_remote_method(self, method_name, cb, need_klippy_reg=True):
if method_name in self.remote_methods: if method_name in self.remote_methods:
# XXX - may want to raise an exception here # XXX - may want to raise an exception here
logging.info(f"Remote method ({method_name}) already registered") logging.info(f"Remote method ({method_name}) already registered")
return return
self.remote_methods[method_name] = cb self.remote_methods[method_name] = cb
if need_klippy_reg:
# These methods need to be registered with Klippy
self.klippy_reg_methods.append(method_name)
def get_host_info(self): def get_host_info(self):
hostname = socket.gethostname() hostname = socket.gethostname()
@ -286,6 +292,12 @@ class Server:
await self._verify_klippy_requirements() await self._verify_klippy_requirements()
logging.info("Klippy ready") logging.info("Klippy ready")
self.init_list.append('klippy_ready') self.init_list.append('klippy_ready')
# register methods with klippy
for method in self.klippy_reg_methods:
try:
await self.klippy_apis.register_method(method)
except ServerError:
logging.exception(f"Unable to register method '{method}'")
self.send_event("server:klippy_ready") self.send_event("server:klippy_ready")
elif self.init_attempts % LOG_ATTEMPT_INTERVAL == 0 and \ elif self.init_attempts % LOG_ATTEMPT_INTERVAL == 0 and \
self.init_attempts <= MAX_LOG_ATTEMPTS: self.init_attempts <= MAX_LOG_ATTEMPTS: