Printing working now. The env file for the worker needs to know the printer name.

Then, the address can be used to print the data.
This commit is contained in:
Amritanshu Agrawal 2021-03-17 09:43:19 +05:30
parent ea2bfb1c0c
commit d851714136
10 changed files with 80 additions and 25 deletions

16
.env
View File

@ -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=""

View File

@ -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)

View File

@ -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"

View File

@ -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:

View File

@ -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):

View File

@ -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):

View File

@ -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}",
)
)

View File

@ -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):

View File

@ -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<host>[\w\d.-]+):(?P<port>[\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"

View File

@ -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