Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions pywebdriver/plugins/cashlogy_cashdrawer_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ def get_status(self, **kwargs):
return self.status

def _check_keep_alive(self):
# Disconnect if the POS has not polled status in KEEPALIVE_TIME_LIMIT seconds.
now = time.time()
if (
self.status.get("status") == "connected"
and (now - self._keepalive_tick) >= KEEPALIVE_TIME_LIMIT
):
app.logger.debug("Cashlogy: disconnected because of inactivity")
self.disconnect()
if (time.time() - self._keepalive_tick) >= KEEPALIVE_INTERVAL:
self._keepalive_tick = time.time()
if (
self.status.get("status") == "connected"
and (time.time() - self._keepalive_tick) >= KEEPALIVE_TIME_LIMIT
):
app.logger.debug("Disconnected because of timeout")
self.disconnect()

def run(self):
while True:
Expand All @@ -75,7 +75,9 @@ def run(self):
timestamp, task, data = self.queue.get(timeout=KEEPALIVE_INTERVAL)
except Empty:
continue
self.process_task(task, timestamp, data)
# Process tasks
if task == "connect":
self.connect(data)
except Exception as e:
traceback.print_exc()
self.set_status("error", str(e))
Expand All @@ -89,6 +91,10 @@ def run(self):
+ "\n"
)
app.logger.error(errmsg)
except (KeyboardInterrupt, SystemExit):
# TODO: Not working as expected..
app.logger.debug("Shutdown signaled. Shutting down connection..")
self.disconnect()

def connect(self, config):
"""Connect to CashlogyConnector and initialize the device.
Expand All @@ -105,15 +111,19 @@ def connect(self, config):
"Configuration error (host: {}, port: {})".format(host, port),
)
return False
# Connect and initialize
try:
self.set_status("connecting")
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(SOCKET_TIMEOUT)
self.socket.connect((host, port))
# Initialize Cashlogy
self.set_status("connecting", "Initializing..")
self.initialize()
self.set_status("connected")
self._device_config = config
self.config = config
# Start thread
self.lockedstart()
return True
except Exception as e:
self.set_status("error", repr(e))
Expand Down Expand Up @@ -148,6 +158,7 @@ def keepalive(self, device_config=None, force=False):
if (device_config and status == "disconnected") or (
force and status not in ["connected", "connecting"]
):
self.lockedstart()
self.push_task("connect", device_config)
return self.get_status()

Expand Down
Loading