47 lines
1008 B
Python
47 lines
1008 B
Python
import json
|
|
|
|
from escpos import printer
|
|
|
|
|
|
class Item:
|
|
def __init__(self, text, icon):
|
|
self.text = text
|
|
self.icon = icon
|
|
|
|
def print(self, conn):
|
|
conn.text(f"{self.text}\n")
|
|
# TODO: conn.image(self.icon)
|
|
conn.cut()
|
|
|
|
|
|
def connect(conn_cfg):
|
|
if "file" in conn_cfg:
|
|
return printer.File(conn_cfg["file"])
|
|
elif "vendor" in conn_cfg and "product" in conn_cfg:
|
|
return printer.Usb(conn_cfg["vendor"], conn_cfg["product"])
|
|
elif "host" in conn_cfg and "port" in conn_cfg:
|
|
return printer.Network(conn_cfg["host"], conn_cfg["port"])
|
|
else:
|
|
return printer.Dummy()
|
|
|
|
|
|
def load_items(items_cfg):
|
|
return [Item(c['text'], c['img']) for c in items_cfg]
|
|
|
|
|
|
def main():
|
|
with open("config.json") as fh:
|
|
cfg = json.load(fh)
|
|
|
|
items = load_items(cfg['items'])
|
|
conn = connect(cfg["connection"])
|
|
|
|
for i in items:
|
|
i.print(conn)
|
|
|
|
print(conn.output.decode('UTF-8'))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|