webhooks: Add client_info parameter to "info" webhook

Allow clients to send their version info to klipper and arrange for
that info to be logged.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-08-25 14:49:43 -04:00
parent 2bb6b0f58f
commit b6a25393d3
2 changed files with 32 additions and 14 deletions

View File

@ -39,6 +39,9 @@ class QueueListener(logging.handlers.TimedRotatingFileHandler):
self.bg_queue.put_nowait(None) self.bg_queue.put_nowait(None)
self.bg_thread.join() self.bg_thread.join()
def set_rollover_info(self, name, info): def set_rollover_info(self, name, info):
if info is None:
self.rollover_info.pop(name, None)
return
self.rollover_info[name] = info self.rollover_info[name] = info
def clear_rollover_info(self): def clear_rollover_info(self):
self.rollover_info.clear() self.rollover_info.clear()

View File

@ -61,7 +61,8 @@ class WebRequest:
value = self.params.get(item, default) value = self.params.get(item, default)
if value is Sentinel: if value is Sentinel:
raise WebRequestError("Missing Argument [%s]" % (item,)) raise WebRequestError("Missing Argument [%s]" % (item,))
if types is not None and type(value) not in types: if (types is not None and type(value) not in types
and item in self.params):
raise WebRequestError("Invalid Argument Type [%s]" % (item,)) raise WebRequestError("Invalid Argument Type [%s]" % (item,))
return value return value
@ -168,12 +169,23 @@ class ClientConnection:
self.sock.fileno(), self.process_received) self.sock.fileno(), self.process_received)
self.partial_data = self.send_buffer = "" self.partial_data = self.send_buffer = ""
self.is_sending_data = False self.is_sending_data = False
logging.info( self.set_client_info("?", "New connection")
"webhooks: New connection established")
def set_client_info(self, client_info, state_msg=None):
if state_msg is None:
state_msg = "Client info %s" % (repr(client_info),)
logging.info("webhooks client %s: %s", self.uid, state_msg)
log_id = "webhooks %s" % (self.uid,)
if client_info is None:
self.printer.set_rollover_info(log_id, None, log=False)
return
rollover_msg = "webhooks client %s: %s" % (self.uid, repr(client_info))
self.printer.set_rollover_info(log_id, rollover_msg, log=False)
def close(self): def close(self):
if self.fd_handle is not None: if self.fd_handle is None:
logging.info("webhooks: Client connection closed") return
self.set_client_info(None, "Disconnected")
self.reactor.unregister_fd(self.fd_handle) self.reactor.unregister_fd(self.fd_handle)
self.fd_handle = None self.fd_handle = None
try: try:
@ -276,9 +288,12 @@ class WebHooks:
web_request.send({'endpoints': self._endpoints.keys()}) web_request.send({'endpoints': self._endpoints.keys()})
def _handle_info_request(self, web_request): def _handle_info_request(self, web_request):
client_info = web_request.get_dict('client_info', None)
if client_info is not None:
web_request.get_client_connection().set_client_info(client_info)
state_message, state = self.printer.get_state_message() state_message, state = self.printer.get_state_message()
klipper_path = os.path.normpath(os.path.join( src_path = os.path.dirname(__file__)
os.path.dirname(__file__), "..")) klipper_path = os.path.normpath(os.path.join(src_path, ".."))
response = {'state': state, 'state_message': state_message, response = {'state': state, 'state_message': state_message,
'hostname': socket.gethostname(), 'hostname': socket.gethostname(),
'klipper_path': klipper_path, 'python_path': sys.executable} 'klipper_path': klipper_path, 'python_path': sys.executable}