Skip to content

Commit

Permalink
Move get_tf_printer_host to tinkerforge_util
Browse files Browse the repository at this point in the history
  • Loading branch information
photron committed Jul 8, 2024
1 parent 461101a commit 734131a
Showing 1 changed file with 2 additions and 58 deletions.
60 changes: 2 additions & 58 deletions provisioning/print-esp32-label.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import argparse
import socket
import tinkerforge_util as tfutil # sudo apt install python3-tinkerforge-util

BASE58 = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'

Expand All @@ -18,63 +19,6 @@
COPIES_FORMAT = '^C{0}\r'


def get_tf_printer_host(task):
import re
import os
import sys
import socket
import tkinter.messagebox

path = '~/tf_printer_host.txt'
x = re.compile(r'^([A-Za-z0-9_-]+)\s+([A-Za-z0-9_\.-]+)$')
host = None

try:
with open(os.path.expanduser(path), 'r', encoding='utf-8') as f:
for line in f.readlines():
line = line.strip()

if len(line) == 0 or line.startswith('#'):
continue

m = x.match(line)

if m == None:
message = 'WARNING: Invalid line in {0}: {1}'.format(path, repr(line))

print(message)
tkinter.messagebox.showerror(title=path, message=message)

continue

other_task = m.group(1)
other_host = m.group(2)

if other_task != task:
continue

host = other_host
break
except FileNotFoundError:
pass

if host == None:
message = 'ERROR: Printer host for task {0} not found in {1}'.format(task, path)
else:
try:
with socket.create_connection((host, 9100), timeout=5) as s:
pass

return host
except Exception as e:
message = 'ERROR: Coould not connect to printer at {0} for task {1}: {2}'.format(host, task, e)

print(message)
tkinter.messagebox.showerror(title=path, message=message)

sys.exit(1)


def print_esp32_label(ssid, passphrase, copies, stdout):
# check SSID
if re.match('^(esp32|warp|warp2|warp3|wem)-[{0}]{{3,6}}$'.format(BASE58), ssid) == None:
Expand Down Expand Up @@ -128,7 +72,7 @@ def print_esp32_label(ssid, passphrase, copies, stdout):
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
else:
with socket.create_connection((get_tf_printer_host('esp32'), 9100)) as s:
with socket.create_connection((tfutil.get_tf_printer_host('esp32'), 9100)) as s:
s.send(data)


Expand Down

0 comments on commit 734131a

Please sign in to comment.