diff --git a/.env b/.env index f99e825..ac4e149 100644 --- a/.env +++ b/.env @@ -1,18 +1,18 @@ HOST=0.0.0.0 -PORT=6543 -LOG_LEVEL=WARN +PORT=9995 +LOG_LEVEL=INFO DEBUG=true -SQLALCHEMY_DATABASE_URI= +SQLALCHEMY_DATABASE_URI=postgresql://postgres:123456@localhost:5432/petty MODULE_NAME=barker.main PROJECT_NAME=barker -REDIS_HOST= +REDIS_HOST=localhost REDIS_PORT=6379 # openssl rand -hex 32 -SECRET_KEY= +SECRET_KEY=611f9393e58f85521d16d9497bfd847ced70f2b99e12a9c6af40b7bd05cc7b1d # openssl rand -hex 5 -MIDDLEWARE_SECRET_KEY= +MIDDLEWARE_SECRET_KEY=c4afceca1a ALGORITHM=HS256 JWT_TOKEN_EXPIRE_MINUTES=30 @@ -21,3 +21,7 @@ TIMEZONE_OFFSET_MINUTES=330 ALEMBIC_LOG_LEVEL=INFO ALEMBIC_SQLALCHEMY_LOG_LEVEL=WARN + +WORKER_REDIS_HOST= +WORKER_REDIS_PORT=6379 +WORKER_QUEUE_NAME="" diff --git a/barker/barker/core/arq.py b/barker/barker/core/arq.py index 93c21c6..6d60e32 100644 --- a/barker/barker/core/arq.py +++ b/barker/barker/core/arq.py @@ -2,4 +2,4 @@ from arq.connections import RedisSettings from barker.core.config import settings as sett -settings = RedisSettings(host=sett.REDIS_HOST, port=sett.REDIS_PORT) +settings = RedisSettings(host=sett.WORKER_REDIS_HOST, port=sett.WORKER_REDIS_PORT) diff --git a/barker/barker/core/config.py b/barker/barker/core/config.py index 944f665..65c19cd 100644 --- a/barker/barker/core/config.py +++ b/barker/barker/core/config.py @@ -24,6 +24,10 @@ class Settings(BaseSettings): ALEMBIC_LOG_LEVEL: str = "INFO" ALEMBIC_SQLALCHEMY_LOG_LEVEL: str = "WARN" + WORKER_REDIS_HOST: str = "127.0.0.1" + WORKER_REDIS_PORT: int = 6379 + WORKER_QUEUE_NAME: str = "arq:queue" + class Config: case_sensitive = True env_file = ".env" diff --git a/barker/barker/printing/bill.py b/barker/barker/printing/bill.py index 3a9a11d..d69ebf4 100644 --- a/barker/barker/printing/bill.py +++ b/barker/barker/printing/bill.py @@ -51,7 +51,11 @@ def print_bill(voucher_id: uuid.UUID, db: Session): get_tax_item(i.tax.id, i.tax.name, i.tax_amount, tax) data = design_bill(voucher, items_dict.values(), tax.values(), db) redis: ArqRedis = asyncio.run(create_pool(redis_settings)) - asyncio.run(redis.enqueue_job("sent_to_printer", data, printer.address, printer.cut_code)) + asyncio.run( + redis.enqueue_job( + "sent_to_printer", data, printer.address, "\x1dV\x41\x03", _queue_name=f"barker:print:{printer.name}" + ) + ) def get_tax_item(id_: uuid.UUID, tax_name: str, amount: Decimal, tax_dict: {}) -> None: diff --git a/barker/barker/printing/cashier_report.py b/barker/barker/printing/cashier_report.py index 42b66f9..85feec6 100644 --- a/barker/barker/printing/cashier_report.py +++ b/barker/barker/printing/cashier_report.py @@ -28,7 +28,11 @@ def print_cashier_report(report: CashierReport, device_id: uuid.UUID, db: Sessio ) redis: ArqRedis = asyncio.run(create_pool(redis_settings)) - asyncio.run(redis.enqueue_job("sent_to_printer", data, printer.address, printer.cut_code)) + asyncio.run( + redis.enqueue_job( + "sent_to_printer", data, printer.address, "\x1dV\x41\x03", _queue_name=f"barker:print:{printer.name}" + ) + ) def design_cashier_report(report: CashierReport): diff --git a/barker/barker/printing/discount_report.py b/barker/barker/printing/discount_report.py index 50d6432..826acf2 100644 --- a/barker/barker/printing/discount_report.py +++ b/barker/barker/printing/discount_report.py @@ -26,7 +26,11 @@ def print_discount_report(report: DiscountReport, device_id: uuid.UUID, db: Sess ) redis: ArqRedis = asyncio.run(create_pool(redis_settings)) - asyncio.run(redis.enqueue_job("sent_to_printer", data, printer.address, printer.cut_code)) + asyncio.run( + redis.enqueue_job( + "sent_to_printer", data, printer.address, "\x1dV\x41\x03", _queue_name=f"barker:print:{printer.name}" + ) + ) def design_discount_report(report: DiscountReport): diff --git a/barker/barker/printing/kot.py b/barker/barker/printing/kot.py index 98c4b27..bc3e672 100644 --- a/barker/barker/printing/kot.py +++ b/barker/barker/printing/kot.py @@ -113,4 +113,12 @@ def print_kot(voucher_id: uuid.UUID, db: Session): printer, items = value for c in range(int(copies)): data = design_kot(voucher, kot, items, c, db) - asyncio.run(redis.enqueue_job("sent_to_printer", data, printer.address, printer.cut_code)) + asyncio.run( + redis.enqueue_job( + "sent_to_printer", + data, + printer.address, + "\x1dV\x41\x03", + _queue_name=f"barker:print:{printer.name}", + ) + ) diff --git a/barker/barker/printing/sale_report.py b/barker/barker/printing/sale_report.py index 11f4e93..27485ea 100644 --- a/barker/barker/printing/sale_report.py +++ b/barker/barker/printing/sale_report.py @@ -28,7 +28,11 @@ def print_sale_report(report: SaleReport, device_id: uuid.UUID, db: Session): ) redis: ArqRedis = asyncio.run(create_pool(redis_settings)) - asyncio.run(redis.enqueue_job("sent_to_printer", data, printer.address, printer.cut_code)) + asyncio.run( + redis.enqueue_job( + "sent_to_printer", data, printer.address, "\x1dV\x41\x03", _queue_name=f"barker:print:{printer.name}" + ) + ) def design_sale_report(report: SaleReport): diff --git a/barker/barker/tasks/printing.py b/barker/barker/tasks/printing.py index 2cfa183..96e7380 100644 --- a/barker/barker/tasks/printing.py +++ b/barker/barker/tasks/printing.py @@ -4,16 +4,24 @@ import sys # noqa: F401 async def sent_to_printer(ctx: dict, data: str, address: str, cut_code: str): - print(data, address, cut_code) - # try: - # regex = re.compile(r'pdl://(?P[\w\d.-]+):(?P[\d]+)') - # match = regex.match(address) - # s = socket.socket() - # s.connect((match.group("host"), int(match.group("port")))) - # s.send(bytearray(data + cut_code, "ascii")) - # except LookupError as e: - # print("Lookup error:", e) - # except: - # print("Unexpected error:", sys.exc_info()[0]) - # finally: - # s.close() + print(ctx, data, address, cut_code) + try: + 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) + except: # noqa: E722 + print("Unexpected error:", sys.exc_info()[0]) + + # 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" diff --git a/barker/barker/worker.py b/barker/barker/worker.py index dd8df1c..0f178da 100644 --- a/barker/barker/worker.py +++ b/barker/barker/worker.py @@ -1,5 +1,7 @@ import sys +from aiohttp import ClientSession +from barker.core.config import settings as sett from barker.tasks.printing import sent_to_printer from .core.arq import settings @@ -8,13 +10,26 @@ from .core.arq import settings sys.path.extend(["./"]) +async def startup(ctx): + ctx["session"] = ClientSession() + print(f"Worker listening for: {sett.WORKER_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:printer:{sett.WORKER_QUEUE_NAME}" redis_settings = settings functions: list = [sent_to_printer] + on_startup = startup + on_shutdown = shutdown # command to run arq barker.worker.WorkerSettings