Files
test/sf_base/commons/common.py

95 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
import time, datetime
import hashlib
from odoo import models
import socket
class Common(models.Model):
_name = 'sf.sync.common'
_description = u'公用类'
def get_headers(self, token, secret_key):
'''
获取requests中的heardes参数
'''
timestamp = int(time.time())
check_str = '%s%s%s' % (token, timestamp, secret_key)
check_sf_str = hashlib.sha1(check_str.encode('utf-8')).hexdigest()
headers = {'TOKEN': token,
'TIMESTAMP': str(timestamp),
'checkstr': check_sf_str}
return headers
def get_add_time(self, parse_time):
"""
把时间增加8小时
:return:
"""
dt = datetime.datetime.strptime(parse_time, "%Y-%m-%d %H:%M:%S")
d = dt + datetime.timedelta(hours=8)
nTime = d.strftime("%Y-%m-%d %H:%M:%S")
return nTime
class PrintingUtils(models.AbstractModel):
_name = 'printing.utils'
_description = 'Utility class for printing functionalities'
def generate_zpl_code(self, code):
# 实现生成ZPL代码的逻辑
# 初始化ZPL代码字符串
zpl_code = "^XA\n"
zpl_code += "^CW1,E:SIMSUN.TTF^FS\n"
zpl_code += "^CI28\n"
# 设置二维码位置
zpl_code += "^FO50,50\n" # 调整二维码位置,使其与资产编号在同一行
zpl_code += f"^BQN,2,6^FDLM,B0093{code}^FS\n"
# 设置资产编号文本位置
zpl_code += "^FO300,60\n" # 资产编号文本的位置,与二维码在同一行
zpl_code += "^A1N,45,45^FD编码名称: ^FS\n"
# 设置{code}文本位置
# 假设{code}文本需要位于资产编号和二维码下方,中间位置
# 设置{code}文本位置并启用自动换行
zpl_code += "^FO300,120\n" # {code}文本的起始位置
zpl_code += "^FB400,4,0,L,0\n" # 定义一个宽度为500点的文本框最多4行左对齐
zpl_code += f"^A1N,40,40^FD{code}^FS\n"
# 在{code}文本框周围绘制线框
# 假设线框的外部尺寸为宽度500点高度200点
# zpl_code += "^FO300,110^GB500,200,2^FS\n" # 绘制线框边框粗细为2点
zpl_code += "^PQ1,0,1,Y\n"
zpl_code += "^XZ\n"
return zpl_code
def send_to_printer(self, host, port, zpl_code):
# 实现发送ZPL代码到打印机的逻辑
# 将ZPL代码转换为字节串
print('zpl_code', zpl_code)
zpl_bytes = zpl_code.encode('utf-8')
print(zpl_bytes)
# 创建socket对象
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mysocket.connect((host, port)) # 连接到打印机
mysocket.send(zpl_bytes) # 发送ZPL代码
print("ZPL code sent to printer successfully.")
except Exception as e:
print(f"Error with the connection: {e}")
finally:
mysocket.close() # 关闭连接
def print_qr_code(self, lot_name, host, port):
# 实现打印二维码的逻辑
# 这里需要传入 lot_name 参数,因为我们不能直接访问 self.lot_id.name
zpl_code = self.generate_zpl_code(lot_name)
# 发送ZPL代码到打印机
# host = "192.168.50.110" # 可以作为参数传入,或者在此配置
# port = 9100 # 可以作为参数传入,或者在此配置
self.send_to_printer(host, port, zpl_code)