-
Notifications
You must be signed in to change notification settings - Fork 2
/
labelwriter.py
executable file
·236 lines (190 loc) · 6.16 KB
/
labelwriter.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python
# Module to interface a label writer that accepts Fingerprint on TCP port 9100
# 9100 also known as "RAW" print protocol
# Tested on OS X, Python 3.7, Intermec PC43D
# Template system uses ~{ } delimiter
# NS9405:2014 Standard is provided as Fingerprint code in NS9405.fp
# Other templates can be added later
# PCX images must be uploaded to printer prior to usage in templates
import socket
import sys
import time
from datetime import datetime, date, time
import os
from string import Template
from pathlib import Path
import re
BASE_DIR = Path(__file__).resolve().parent
GLOBAL_TEMPLATES_DIR = BASE_DIR.joinpath('templates')
crlf = "\r\n"
msg_ok = "\r\nOk\r\n"
def bencmsg(msg):
message = msg+crlf
return message.encode()
def zuludatetimenow():
t = datetime.utcnow()
s = t.strftime('%Y-%m-%d %H:%M:%S.%f')
return s[0:-7]+'Z'
def zuluproddatenow():
t = datetime.utcnow()
s = t.strftime('%y%m%d')
return s
def weightflat(weight):
weightflat = str(weight).replace(',','').replace('.','').zfill(6)
return weightflat
class TemplateClone(Template):
delimiter = '~'
pattern = r'''
\~(?:
(?P<escaped>\~) | # Escape sequence of two delimiters
\b\B(?P<named>) | # disable named
{(?P<braced>[_a-z][_a-z0-9]*)} | # delimiter and a braced identifier
(?P<invalid>) # Other ill-formed delimiter exprs
)
'''
class TemplatesMixin:
TEMPLATES_DIR = GLOBAL_TEMPLATES_DIR
def _read_template(self, template_path):
with open(os.path.join(self.TEMPLATES_DIR, template_path)) as template:
return template.read()
def render(self, template_path, **kwargs):
return TemplateClone(
self._read_template(template_path)
).substitute(**kwargs)
class LabelGenerator(TemplatesMixin):
def generate(self, **kwargs):
print('Label class ')
labelbody = self.render('NS9405.fp', **kwargs)
return labelbody
class MySocket:
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def connect(self, host, port):
self.sock.connect((host, port))
def mysend(self, msg):
totalsent = 0
MSGLEN = len(msg)
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError("socket connection broken")
totalsent = totalsent + sent
def myreceive(self):
chunks = []
bytes_recd = 0
MSGLEN = 100 # Arbitrary max message length
while bytes_recd < MSGLEN:
chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048))
print("bytes recvd:", len(chunk), chunk)
if chunk == b'':
raise RuntimeError("socket connection broken")
chunks.append(chunk)
bytes_recd = bytes_recd + len(chunk)
if chunk == msg_ok.encode():
print("Got OK, returning full message")
return b''.join(chunks)
return b''.join(chunks)
class Labelwriter:
def __init__(self, ip, port):
self._ip = ip
self._port = port
self._socket = MySocket()
self._connected = False
while not self._connected:
try:
self._socket.connect(self._ip, self._port)
self._connected = True
print("connected labelwriter successfully")
except ConnectionRefusedError:
print("unable to connect labelwriter, retrying does not work. fail, restart program")
time.sleep(1)
raise
def print_label(self, **kwarg):
kwarg_addons = {
'proddate':zuluproddatenow(), # Generated
'productiondatetime':zuludatetimenow(), # Generated
'weightflat':weightflat(kwarg['weight']) # Generated
}
label_data = {**kwarg, **kwarg_addons}
generator = LabelGenerator()
labelbody = generator.generate(**label_data)
#print('printing')
#print(labelbody)
self.send_template(labelbody)
self.print_template()
def send_single_command(self, command):
print('cmd: '+command)
self._socket.mysend(bencmsg(command))
def beep(self):
self.send_single_command("SOUND 850,10 : SOUND 950,10 ")
def formfeed(self):
self.send_single_command("FORMFEED")
def send_template(self, template):
self.send_single_command(template)
def print_template(self):
command = """
LAYOUT RUN "tmp:LABEL1"
PF
PRINT KEY OFF
KILL "tmp:LABEL1"
LAYOUT RUN ""
"""
self.send_single_command(command)
def kill_template(self, labelid = "LABEL1"):
print("killing template " +labelid)
self.send_single_command("KILL \"tmp:"+labelid+"\"")
# Create and store the layout in tmp memory
def msgSetupLabelTemplate():
retval = """
INPUT ON
LAYOUT INPUT "tmp:LABEL1"
PP 100,250
FT "Univers",9
PT "My first label"
PP 200,250
FT "Univers",12
PT VAR1$
PP 300,250
FT "Univers",18
PT VAR2$
LAYOUT END
INPUT OFF
"""
return retval
# Run the layout from tmp memory
def msgRunLabelTemplate(arg1, arg2):
retval = """
INPUT OFF
FORMAT INPUT "#","@","&"
INPUT ON
LAYOUT RUN "tmp:LABEL1"
#{0}&{1}&@
PF
LAYOUT RUN ""
INPUT OFF
""".format(arg1,arg2)
return retval
def main():
print("running labelwriter module standalone")
mylabelwriter = Labelwriter('192.168.1.3', 9100)
mylabelwriter.beep()
kwarg_partial = {
'friendlyname':'Common Periwinkle',
'scientificname':'LITTORINA LITTOREA',
'productinthirdlanguage':'Produit',
'gtin':'7072773000092',
'processingmethod':'N/A',
'weight':'5,00',
'grade':'N/A',
'customer':'Acustomer',
'batchno':'000001',
'catchdate':'2019-09-17',
'pcskg':'100-140 #/kg'
}
# Uncomment to print an actual label
# mylabelwriter.print_label(**kwarg_partial)
if __name__ == '__main__':
main()