moonraker: Connect to Klippy as a client
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
e1addb16fd
commit
0775f3d0c6
|
@ -83,7 +83,5 @@ def get_configuration(server, cmd_line_args):
|
||||||
if server_cfg.get('enable_debug_logging', True):
|
if server_cfg.get('enable_debug_logging', True):
|
||||||
logging.getLogger().setLevel(logging.DEBUG)
|
logging.getLogger().setLevel(logging.DEBUG)
|
||||||
|
|
||||||
config['cmd_args'] = {
|
config['cmd_args'] = {'logfile': cmd_line_args.logfile}
|
||||||
'logfile': cmd_line_args.logfile,
|
|
||||||
'socketfile': cmd_line_args.socketfile}
|
|
||||||
return ConfigHelper(server, config, 'server')
|
return ConfigHelper(server, config, 'server')
|
||||||
|
|
|
@ -11,9 +11,6 @@ import time
|
||||||
import socket
|
import socket
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
import errno
|
|
||||||
import tornado
|
|
||||||
import tornado.netutil
|
|
||||||
import confighelper
|
import confighelper
|
||||||
from tornado import gen, iostream
|
from tornado import gen, iostream
|
||||||
from tornado.ioloop import IOLoop, PeriodicCallback
|
from tornado.ioloop import IOLoop, PeriodicCallback
|
||||||
|
@ -42,12 +39,8 @@ class Server:
|
||||||
self.events = {}
|
self.events = {}
|
||||||
|
|
||||||
# Klippy Connection Handling
|
# Klippy Connection Handling
|
||||||
socketfile = config['cmd_args'].get('socketfile', "/tmp/moonraker")
|
self.klippy_address = config.get(
|
||||||
socketfile = os.path.normpath(os.path.expanduser(socketfile))
|
'klippy_uds_address', "/tmp/klippy_uds")
|
||||||
self.klippy_server_sock = tornado.netutil.bind_unix_socket(
|
|
||||||
socketfile, backlog=1)
|
|
||||||
self.remove_server_sock = tornado.netutil.add_accept_handler(
|
|
||||||
self.klippy_server_sock, self._handle_klippy_connection)
|
|
||||||
self.klippy_iostream = None
|
self.klippy_iostream = None
|
||||||
self.is_klippy_ready = False
|
self.is_klippy_ready = False
|
||||||
self.moonraker_available = False
|
self.moonraker_available = False
|
||||||
|
@ -85,6 +78,7 @@ class Server:
|
||||||
(self.host, self.port))
|
(self.host, self.port))
|
||||||
self.moonraker_app.listen(self.host, self.port)
|
self.moonraker_app.listen(self.host, self.port)
|
||||||
self.server_running = True
|
self.server_running = True
|
||||||
|
self.ioloop.spawn_callback(self._connect_klippy)
|
||||||
|
|
||||||
# ***** Plugin Management *****
|
# ***** Plugin Management *****
|
||||||
def _load_plugins(self, config):
|
def _load_plugins(self, config):
|
||||||
|
@ -147,13 +141,22 @@ class Server:
|
||||||
self.remote_methods[method_name] = cb
|
self.remote_methods[method_name] = cb
|
||||||
|
|
||||||
# ***** Klippy Connection *****
|
# ***** Klippy Connection *****
|
||||||
def _handle_klippy_connection(self, conn, addr):
|
async def _connect_klippy(self):
|
||||||
if self.klippy_iostream is not None and \
|
ksock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
not self.klippy_iostream.closed():
|
kstream = iostream.IOStream(ksock)
|
||||||
logging.info("New Connection received while Klippy Connected")
|
try:
|
||||||
self.klippy_iostream.close()
|
await kstream.connect(self.klippy_address)
|
||||||
|
except iostream.StreamClosedError:
|
||||||
|
# Klippy Socket Server not available
|
||||||
|
self.ioloop.call_later(1., self._connect_klippy)
|
||||||
|
return
|
||||||
|
await gen.sleep(0.5)
|
||||||
|
if kstream.closed():
|
||||||
|
# Klippy Connection was rejected
|
||||||
|
self.ioloop.call_later(1., self._connect_klippy)
|
||||||
|
return
|
||||||
logging.info("Klippy Connection Established")
|
logging.info("Klippy Connection Established")
|
||||||
self.klippy_iostream = iostream.IOStream(conn)
|
self.klippy_iostream = kstream
|
||||||
self.klippy_iostream.set_close_callback(
|
self.klippy_iostream.set_close_callback(
|
||||||
self._handle_stream_closed)
|
self._handle_stream_closed)
|
||||||
self.ioloop.spawn_callback(
|
self.ioloop.spawn_callback(
|
||||||
|
@ -178,23 +181,28 @@ class Server:
|
||||||
if cb is not None:
|
if cb is not None:
|
||||||
cb(**params)
|
cb(**params)
|
||||||
else:
|
else:
|
||||||
logging.info("Unknown command received %s" % cmd.decode())
|
logging.info("Unknown command received %s" % data.decode())
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception(
|
logging.exception(
|
||||||
"Error processing Klippy Host Response: %s"
|
"Error processing Klippy Host Response: %s"
|
||||||
% (cmd.decode()))
|
% (data.decode()))
|
||||||
|
|
||||||
def _handle_stream_closed(self):
|
def _handle_stream_closed(self):
|
||||||
self.is_klippy_ready = False
|
self.is_klippy_ready = False
|
||||||
self.moonraker_available = False
|
self.moonraker_available = False
|
||||||
|
self.klippy_iostream = None
|
||||||
self.init_cb.stop()
|
self.init_cb.stop()
|
||||||
for request in self.pending_requests.values():
|
for request in self.pending_requests.values():
|
||||||
request.notify(ServerError("Klippy Disconnected", 503))
|
request.notify(ServerError("Klippy Disconnected", 503))
|
||||||
self.pending_requests = {}
|
self.pending_requests = {}
|
||||||
logging.info("Klippy Connection Removed")
|
logging.info("Klippy Connection Removed")
|
||||||
self.send_event("server:klippy_state_changed", "disconnect")
|
self.send_event("server:klippy_state_changed", "disconnect")
|
||||||
|
self.ioloop.call_later(1., self._connect_klippy)
|
||||||
|
|
||||||
async def send_klippy_request(self, request):
|
async def send_klippy_request(self, request):
|
||||||
|
if self.klippy_iostream is None:
|
||||||
|
request.notify(ServerError("Klippy Host not connected", 503))
|
||||||
|
return
|
||||||
data = json.dumps(request.to_dict()).encode() + b"\x03"
|
data = json.dumps(request.to_dict()).encode() + b"\x03"
|
||||||
try:
|
try:
|
||||||
await self.klippy_iostream.write(data)
|
await self.klippy_iostream.write(data)
|
||||||
|
@ -356,9 +364,6 @@ def main():
|
||||||
"-c", "--configfile", default="~/moonraker.conf",
|
"-c", "--configfile", default="~/moonraker.conf",
|
||||||
metavar='<configfile>',
|
metavar='<configfile>',
|
||||||
help="Location of moonraker configuration file")
|
help="Location of moonraker configuration file")
|
||||||
parser.add_argument(
|
|
||||||
"-s", "--socketfile", default="/tmp/moonraker", metavar='<socketfile>',
|
|
||||||
help="file name and location for the Unix Domain Socket")
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-l", "--logfile", default="/tmp/moonraker.log", metavar='<logfile>',
|
"-l", "--logfile", default="/tmp/moonraker.log", metavar='<logfile>',
|
||||||
help="log file name and location")
|
help="log file name and location")
|
||||||
|
|
Loading…
Reference in New Issue