Moved printing into its own separate project called frank. It also has its own toml and deployement system.

It also works on a raspberry pi
This commit is contained in:
2021-03-23 18:18:33 +05:30
parent 0b30ce258c
commit f4caa19bb2
17 changed files with 184 additions and 67 deletions

16
frank/config.py Normal file
View File

@ -0,0 +1,16 @@
from dotenv import load_dotenv
from pydantic import BaseSettings
class Settings(BaseSettings):
REDIS_HOST: str = "127.0.0.1"
REDIS_PORT: int = 6379
QUEUE_NAME: str = "arq:queue"
class Config:
case_sensitive = True
env_file = ".env"
load_dotenv()
settings = Settings()

38
frank/printing.py Normal file
View File

@ -0,0 +1,38 @@
import re # noqa: F401
import socket # noqa: F401
import sys # noqa: F401
from arq import Retry
async def sent_to_printer(ctx: dict, data: str, address: str, cut_code: str):
address = "/printer"
try:
print("Printing to :", address, "\n", data, "\n")
with open(address, "w") as printer:
printer.write(data)
printer.write("\n")
print(cut_code)
printer.write(cut_code)
except LookupError as e:
print("Lookup error:", e)
raise Retry(defer=ctx["job_try"] * 30)
except FileNotFoundError as e:
print("File not found error:", e)
raise Retry(defer=ctx["job_try"] * 30)
except: # noqa: E722
print("Unexpected error:", sys.exc_info()[0])
# retry the job with increasing back-off
# delays will be 5s, 10s, 15s, 20s
# after max_tries (default 5) the job will permanently fail
raise Retry(defer=ctx["job_try"] * 30)
# GS = "\x1d"
# PAPER_CUT = GS + "V"
# # For the printer ESC/POS Reference
# # https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=87
# # The m codes are in decimal and can be converted using the following table
# # https://www.eso.org/~ndelmott/ascii.html
# # The \x03 in all the following is to feed 3 lines before cut it can be increased or reduced
# FUNCTION_B_FULL_CUT = "\x41\x03"
# FUNCTION_B_PARTIAL_CUT = "\x42\x03"

15
frank/pyproject.toml Normal file
View File

@ -0,0 +1,15 @@
[tool.poetry]
name = "frank"
version = "7.3.0"
description = "Point of Sale for a restaurant"
authors = ["tanshu <git@tanshu.com>"]
[tool.poetry.dependencies]
python = "^3.8"
pydantic = {extras = ["dotenv"], version = "^1.8.1"}
arq = "^0.19.1"
aiohttp = "^3.7.4"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

4
frank/settings.py Normal file
View File

@ -0,0 +1,4 @@
from arq.connections import RedisSettings
from config import settings as sett
settings = RedisSettings(host=sett.REDIS_HOST, port=sett.REDIS_PORT)

5
frank/worker-start.sh Executable file
View File

@ -0,0 +1,5 @@
#! /usr/bin/env bash
set -e
echo arq worker.WorkerSettings
arq worker.WorkerSettings

30
frank/worker.py Normal file
View File

@ -0,0 +1,30 @@
import sys
from aiohttp import ClientSession
from config import settings as sett
from printing import sent_to_printer
from settings import settings
sys.path.extend(["./"])
async def startup(ctx):
ctx["session"] = ClientSession()
print(f"Worker listening for: {sett.QUEUE_NAME}")
# print(f"Worker printing on: {sett.WORKER_PRINTER_ADDRESS}")
async def shutdown(ctx):
await ctx["session"].close()
class WorkerSettings:
"""
Settings for the ARQ worker.
"""
queue_name = f"barker:print:{sett.QUEUE_NAME}"
redis_settings = settings
functions: list = [sent_to_printer]
on_startup = startup
on_shutdown = shutdown