-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_label.py
More file actions
102 lines (68 loc) · 2.93 KB
/
main_label.py
File metadata and controls
102 lines (68 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify
import socket
from datetime import datetime
import json
from dotenv import load_dotenv
import os
# Load environment variables from .env
load_dotenv()
PRINTER_IP = os.getenv("PRINTER_IP")
# API
app = Flask(__name__)
def send_to_printer(printer_ip, port, data):
current_datetime = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
try:
with socket.create_connection((printer_ip, port), timeout=5) as printer:
printer.sendall(data.encode())
return True
except Exception as e:
print(f"{current_datetime} Error sending data to printer: {e}")
return False
def generate_full_template(gifts, contact_id):
number_of_stickers_in_set = 3
template = f'SIZE 26 mm,18 mm\n' \
f'GAP 3 mm,0 mm\n' \
f'DIRECTION 1\n'
# Generate commands for each set
for i in range(len(gifts)):
template += f'CLS\n' \
f'SOUND 5,200\n' \
f'PUTBMP 0,0,"sticker.bmp",1,100\n' \
f'TEXT 50,44,"3",0,2,2,0,"{gifts[i]["gift_number"]}"\n' \
f'TEXT 5,105,"2",0,1,1,0,"{contact_id} / {gifts[i]["gift_type"]}"\n' \
f'PRINT {number_of_stickers_in_set}\n'
return template
# API_1 Endpoint:/label/print-sets
# Full documentation available in API_DOC.md
@app.route('/print-sets', methods=['POST'])
def print_sticker_sets():
current_datetime = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
# Receive data from the request
request_data = request.json
printer_port = request_data.get("printer")
contact_id = request_data.get("contact_id")
gifts = request_data.get("gifts")
# Create a print job
stickers_content = generate_full_template(gifts, contact_id)
# Send to the printer
if send_to_printer(PRINTER_IP, printer_port, stickers_content):
status = {"code": 200,
"message": "Success: All stickers are printed."}
with open("label_logs.log", "a", encoding="utf-8") as log_file:
log_file.write(f"\n{current_datetime} API print_sets\n")
log_file.write(f"Request_content: {json.dumps(request_data)}\n")
log_file.write(f"Stickers_content: {json.dumps(stickers_content)}\n")
log_file.write(f"Status: {json.dumps(status)}\n")
return jsonify(status), 200
else:
status = {"code": 500,
"message": "Error: Failed to send to printer!"}
with open("label_logs.log", "a", encoding="utf-8") as log_file:
log_file.write(f"\n{current_datetime} API print_sets\n")
log_file.write(f"Request_content: {json.dumps(request_data)}\n")
log_file.write(f"Stickers_content: {json.dumps(stickers_content)}\n")
log_file.write(f"Status: {json.dumps(status)}\n")
return jsonify(status), 500
if __name__ == '__main__':
app.run(debug=True)