KlippyWebsocket: changes to allow for moonraker api key
This commit is contained in:
parent
717e4ee902
commit
37c043b3f9
@ -15,6 +15,8 @@ config is included here: [ks_includes/KlipperScreen.conf](../ks_includes/Klipper
|
||||
# Define the moonraker host/port if different from 127.0.0.1 and 7125
|
||||
moonraker_host: 127.0.0.1
|
||||
moonraker_port: 7125
|
||||
# Moonraker API key if this is not connecting from a trusted client IP
|
||||
moonraker_api_key: False
|
||||
|
||||
# Invert axis in move panel. Default is False. Change to true to invert
|
||||
invert_x: False
|
||||
|
@ -5,20 +5,28 @@ import logging
|
||||
logger = logging.getLogger("KlipperScreen.KlippyRest")
|
||||
|
||||
class KlippyRest:
|
||||
def __init__(self, ip, port=7125):
|
||||
def __init__(self, ip, port=7125, api_key=False):
|
||||
self.ip = ip
|
||||
self.port = port
|
||||
self.api_key = api_key
|
||||
|
||||
def get_server_info(self):
|
||||
return self.send_request("server/info")
|
||||
|
||||
def get_oneshot_token(self):
|
||||
r = self.send_request("access/oneshot_token")
|
||||
if r == False:
|
||||
return False
|
||||
return r['result']
|
||||
|
||||
def get_printer_info(self):
|
||||
return self.send_request("printer/info")
|
||||
|
||||
def send_request(self, method):
|
||||
url = "http://%s:%s/%s" % (self.ip, self.port, method)
|
||||
logger.debug("Sending request to %s" % url)
|
||||
r = requests.get(url)
|
||||
headers = {} if self.api_key == False else {"x-api-key":self.api_key}
|
||||
r = requests.get(url, headers=headers)
|
||||
if r.status_code != 200:
|
||||
return False
|
||||
|
||||
|
@ -46,18 +46,19 @@ class KlippyWebsocket(threading.Thread):
|
||||
|
||||
self._url = "%s:%s" % (host, port)
|
||||
|
||||
def connect (self):
|
||||
#r = requests.get(
|
||||
# "http://%s%s" % (self._url, api['oneshot_token']['url']),
|
||||
# headers={"x-api-key":api_key}
|
||||
#)
|
||||
#if r.status_code != 200:
|
||||
# logger.info("Failed to retrieve oneshot token")
|
||||
# return
|
||||
def initial_connect(self):
|
||||
# Enable a timeout so that way if moonraker is not running, it will attempt to reconnect
|
||||
if self.timeout == None:
|
||||
self.timeout = GLib.timeout_add(500, self.reconnect)
|
||||
|
||||
#token = json.loads(r.content)['result']
|
||||
#self.ws_url = "ws://%s/websocket?token=%s" % (self._url, token)
|
||||
self.ws_url = "ws://%s/websocket" % (self._url)
|
||||
def connect (self):
|
||||
try:
|
||||
token = self._screen.apiclient.get_oneshot_token()
|
||||
except:
|
||||
logger.debug("Unable to get oneshot token")
|
||||
return False
|
||||
logger.debug("Token: %s" % token)
|
||||
self.ws_url = "ws://%s/websocket?token=%s" % (self._url, token)
|
||||
self.ws = websocket.WebSocketApp(self.ws_url,
|
||||
on_message = lambda ws,msg: self.on_message(ws, msg),
|
||||
on_error = lambda ws,msg: self.on_error(ws, msg),
|
||||
@ -155,7 +156,16 @@ class KlippyWebsocket(threading.Thread):
|
||||
|
||||
|
||||
def on_error(self, ws, error):
|
||||
print(error)
|
||||
logger.debug("Websocket error: %s" % error)
|
||||
if error.status_code == 401:
|
||||
# Check for any pending reconnects and remove. No amount of trying will help
|
||||
if self.timeout != None:
|
||||
GLib.source_remove(self.timeout)
|
||||
Gdk.threads_add_idle(
|
||||
GLib.PRIORITY_HIGH_IDLE,
|
||||
self._callback['on_close'],
|
||||
"Unable to authenticate with moonraker.\nCheck the API key"
|
||||
)
|
||||
|
||||
class MoonrakerApi:
|
||||
def __init__ (self, ws):
|
||||
|
@ -94,7 +94,8 @@ class KlipperScreen(Gtk.Window):
|
||||
_ = self.lang.gettext
|
||||
|
||||
self.apiclient = KlippyRest(self._config.get_main_config_option("moonraker_host"),
|
||||
self._config.get_main_config_option("moonraker_port"))
|
||||
self._config.get_main_config_option("moonraker_port"),
|
||||
self._config.get_main_config_option("moonraker_api_key", False))
|
||||
|
||||
Gtk.Window.__init__(self)
|
||||
self.width = self._config.get_main_config().getint("width", Gdk.Screen.get_width(Gdk.Screen.get_default()))
|
||||
@ -119,7 +120,7 @@ class KlipperScreen(Gtk.Window):
|
||||
self._config.get_main_config_option("moonraker_host"),
|
||||
self._config.get_main_config_option("moonraker_port")
|
||||
)
|
||||
self._ws.connect()
|
||||
self._ws.initial_connect()
|
||||
|
||||
# Disable DPMS
|
||||
os.system("/usr/bin/xset -display :0 s off")
|
||||
|
Loading…
x
Reference in New Issue
Block a user