Refactor printer integration to use new WebSocket parameters and add error handl…

…ing for server responses
1 parent ce2320cd
...@@ -520,8 +520,9 @@ class BaseApp(): ...@@ -520,8 +520,9 @@ class BaseApp():
self.reg_nip = 0 self.reg_nip = 0
self.single_device = "false" self.single_device = "false"
self.is_pylpr = False self.is_pylpr = False
self.ws_print_url = "" self.wsp_url = ""
self.ws_print_id = "" self.wsp_client_id = ""
self.wsp_client_key = ""
def get_route_file(self, filename="routes.csv"): def get_route_file(self, filename="routes.csv"):
fullpath = os.path.join(self.base_dir, 'scripts', 'data', filename) fullpath = os.path.join(self.base_dir, 'scripts', 'data', filename)
...@@ -567,8 +568,9 @@ class BaseApp(): ...@@ -567,8 +568,9 @@ class BaseApp():
self.login_tpl = get_params("login_tpl", "", settings=settings) self.login_tpl = get_params("login_tpl", "", settings=settings)
self.login_captcha = int(get_params( self.login_captcha = int(get_params(
"login_captcha", 0, settings=settings)) "login_captcha", 0, settings=settings))
self.ws_print_url = settings.get("ws_print_url", "") self.wsp_url = settings.get("wsp_url", "")
self.ws_print_id = settings.get("ws_print_id", "") self.wsp_client_id = settings.get("wsp_client_id", "")
self.wsp_client_key = settings.get("wsp_client_key", "")
def add_menu(self, config, route_menus, parent=None, paket="opensipkd.base.views", def add_menu(self, config, route_menus, parent=None, paket="opensipkd.base.views",
template_path="views/templates/"): template_path="views/templates/"):
......
...@@ -97,7 +97,7 @@ class AddSchema(colander.Schema): ...@@ -97,7 +97,7 @@ class AddSchema(colander.Schema):
epson_values = [ epson_values = [
("0", "Other"), ("0", "Other"),
("1", "Epson Type"),] ("1", "Epson Type"),]
if BASE_CLASS.ws_print_url: if BASE_CLASS.wsp_url:
epson_values.append(("2", "Web Socket")) epson_values.append(("2", "Web Socket"))
schema["is_epson"].widget.values = epson_values schema["is_epson"].widget.values = epson_values
...@@ -220,7 +220,7 @@ class Views(BaseView): ...@@ -220,7 +220,7 @@ class Views(BaseView):
async def print_text(user_id=None, print_id=None, text=None, filename=None, async def print_text(user_id=None, print_id=None, text=None, filename=None,
ws_url=BASE_CLASS.ws_print_url): ws_url=BASE_CLASS.wsp_url):
if not user_id and not print_id: if not user_id and not print_id:
log.error("User ID or Print ID must be provided.") log.error("User ID or Print ID must be provided.")
raise Exception("User ID or Print ID must be provided.") raise Exception("User ID or Print ID must be provided.")
...@@ -236,8 +236,8 @@ async def print_text(user_id=None, print_id=None, text=None, filename=None, ...@@ -236,8 +236,8 @@ async def print_text(user_id=None, print_id=None, text=None, filename=None,
timeout = printer.timeout if printer else 10 # Timeout in seconds timeout = printer.timeout if printer else 10 # Timeout in seconds
if is_ws: if is_ws:
printer_name = printer.nama if printer else "unknown" printer_name = printer.nama if printer else "unknown"
printer_name = BASE_CLASS.ws_print_id+'_'+ printer_name printer_name = BASE_CLASS.wsp_client_id+'_'+ printer_name
ws_url = ws_url or BASE_CLASS.ws_print_url ws_url = ws_url or BASE_CLASS.wsp_url
if not ws_url: if not ws_url:
log.error("WebSocket URL is not configured.") log.error("WebSocket URL is not configured.")
raise Exception("WebSocket URL is not configured.") raise Exception("WebSocket URL is not configured.")
...@@ -246,10 +246,29 @@ async def print_text(user_id=None, print_id=None, text=None, filename=None, ...@@ -246,10 +246,29 @@ async def print_text(user_id=None, print_id=None, text=None, filename=None,
async with websockets.connect(ws_url) as websocket: async with websockets.connect(ws_url) as websocket:
# Send user on connect # Send user on connect
log.info(f"Connected to server at {ws_url} as {printer_name}") log.info(f"Connected to server at {ws_url} as {printer_name}")
data = {"login": BASE_CLASS.ws_print_id} # data = {"action": "print",
# "printer": self.printer,
# "message": message
# }
data = {
"action": "login",
"client_id": BASE_CLASS.wsp_client_id,
"api_key": BASE_CLASS.wsp_client_key,
"printer": BASE_CLASS.wsp_client_id
}
await websocket.send(json.dumps(data)) await websocket.send(json.dumps(data))
text = await websocket.recv() text = await websocket.recv()
log.info(f"Received from server: {text}") log.info(f"Received from server: {text}")
try:
resp = json.loads(text)
except json.JSONDecodeError as e:
log.error(f"Failed to decode server response: {e}")
raise Exception(f"Invalid response from server: {text}") from e
if resp.get("status") != True:
log.error(
f"Login failed: {resp.get('message', 'No message provided')}")
raise Exception(f"Login failed: {resp.get('message', 'No message provided')}")
# Wait for server to process login # Wait for server to process login
if filename and os.path.isfile(filename): if filename and os.path.isfile(filename):
......
...@@ -38,6 +38,8 @@ dependencies = [ ...@@ -38,6 +38,8 @@ dependencies = [
'wheezy.captcha', 'wheezy.captcha',
'ziggurat-foundations', 'ziggurat-foundations',
'zope.sqlalchemy', 'zope.sqlalchemy',
'websockets',
"asyncio",
] ]
requires-python = '>= 3.8' requires-python = '>= 3.8'
authors = [ authors = [
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!