/
receipt-printer.py
executable file
·314 lines (250 loc) · 8.58 KB
/
receipt-printer.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import RPi.GPIO as GPIO
import json
import os
import re
import time
import math
from connpass import Connpass
from datetime import datetime, timedelta
from escpos.constants import FS, ESC
from escpos.printer import Usb
from escpos.exceptions import USBNotFoundError
from json.decoder import JSONDecodeError
from requests.exceptions import ConnectionError
import argparse
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading
image_dir = os.path.dirname(os.path.realpath(__file__))
root_dir = os.path.dirname(image_dir)
def set_sjis_mode(p):
p._raw(FS + b'C\x01') # set SJIS mode
def set_quadruple_size_mode(p):
p._raw(FS + b'W\x01')
def unset_quadruple_size_mode(p):
p._raw(FS + b'W\x00')
def text_sjis(p, text):
p._raw(text.encode('Shift_JIS'))
def print_receipt(p, dt, records):
set_sjis_mode(p)
p.set(align='right', smooth=True)
text_sjis(p, dt.strftime('%Y/%m/%d %H:%M\n'))
p.set(align='center', smooth=True)
p.image(image_dir + '/halake-logo.png')
set_quadruple_size_mode(p)
text_sjis(p, '\n領収書\n\n\n')
p.set(align='center', smooth=True, text_type='U')
text_sjis(p, ' 様\n\n\n')
if len(records) == 0:
text_sjis(p, ' 円\n\n\n')
unset_quadruple_size_mode(p)
text_sjis(p, ' として\n')
else:
unset_quadruple_size_mode(p)
sum = 0
for record in records:
sum += record['price']
p.set(align='left', smooth=True)
text_sjis(p, record['title'] + '\n')
p.set(align='right', smooth=True)
text_sjis(p, '¥' + str(record['price']) + '\n')
set_quadruple_size_mode(p)
tax = math.floor(sum/11)
p.set(align='center', smooth=True, text_type='B', height=2, width=2)
text_sjis(p, '\n合計 ¥' + str(sum) + '\n')
unset_quadruple_size_mode(p)
p.set(align='center', smooth=True)
text_sjis(p, '\n10%標準対象 ¥' + str(sum-tax) + '\n')
text_sjis(p, '内消費税等 ¥' + str(tax) + '\n')
p.set(align='right', smooth=True)
text_sjis(p, '\n\n〒343-0827 埼玉県越谷市レイクタウン8-11-1\n')
text_sjis(p, 'レイクタウンオークラビル4F\n\n')
p.set(align='center', smooth=True)
set_quadruple_size_mode(p)
text_sjis(p, 'ニャンパス株式会社\n')
unset_quadruple_size_mode(p)
p.set(align='right', smooth=True)
text_sjis(p, '\nURL:https://nyampass.com\n')
p.set(align='center', smooth=True)
text_sjis(p, '\n登録番号:T6030001068243\n')
p.cut()
CONNPASS_GROUP_ID = 1382
# Load .secret data
with open(root_dir + '/.secret.json') as f:
secret = json.load(f)
events = []
got_event_at = None
CHECK_EVENT_INTERVAL = timedelta(hours = 1)
def receipt_info():
header = """HaLake Wifi
SSID: halake
パスワード: {password}
今後のイベント情報:
https://halake.connpass.com/
""".format(**secret)
global events
return header + '\n'.join([event['headline'] for event in events])
def print_info(p):
set_sjis_mode(p)
p.set(align='center', smooth=True)
p.image(image_dir + '/halake-logo.png')
p.set(align='left', smooth=True)
text_sjis(p, receipt_info())
p.cut()
def questions_receipt():
return """よろしければアンケートにご協力ください
HaLakeをどのように知りましたか?
[ ] web検索
[ ] 友人からの紹介
[ ] 情報誌
[ ] その他( )
HaLakeをご利用になり、気になったことがありましたら、お知らせください。
気になったこと
(
)
HaLakeまたご利用されたいですか?
[ ] 積極的に利用したい
[ ] たまに利用したい
[ ] 利用するかもしれない
[ ] あまり利用したくない
[ ] もう利用したくない、二度と
ご回答いただいたアンケートは入口付近の回収箱にお入れください。
ご協力ありがとうございました。"""
def print_questions(p):
text_sjis(p, questions_receipt())
p.cut()
def button_process(pin, action):
def process():
i = 0
flag = False
while True:
if GPIO.input(pin):
# print(pin, 'high')
i = 0
flag = False
else:
# print(pin, 'low')
i += 1
if i >= 5:
if not flag:
print(pin, "do action")
action()
flag = True
yield
return process()
def open_printer(vendor_id, product_id):
try:
return Usb(vendor_id, product_id, 0)
except USBNotFoundError:
print("cannot open printer")
return None
BUTTON_TOP_LEFT = 25
BUTTON_TOP_CENTER = 23
BUTTON_TOP_RIGHT = 18
BUTTON_BOTTOM_LEFT = 24
BUTTON_BOTTOM_CENTER = 22
BUTTON_BOTTOM_RIGHT = 17
PRINT_BUTTON = BUTTON_TOP_RIGHT
DAY_BUTTON = BUTTON_TOP_CENTER
TWO_HOURS_BUTTON = BUTTON_TOP_LEFT
RESET_BUTTON = BUTTON_BOTTOM_LEFT
INFO_BUTTON = BUTTON_BOTTOM_CENTER
day_record = {'title': 'コワーキングスペース一日利用', 'price': 1000}
two_hours_record = {'title': 'コワーキングスペース2時間利用', 'price': 500}
vendor_id = 0x04b8
product_id = 0x0202
GPIO.setmode(GPIO.BCM)
GPIO.setup(PRINT_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(DAY_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(TWO_HOURS_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(RESET_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(INFO_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
records = []
def print_action():
global records
p = open_printer(vendor_id, product_id)
if (p != None):
print_receipt(p, datetime.now(), records)
records = []
p.close()
def reset_action():
global records
records = []
def info_action():
p = open_printer(vendor_id, product_id)
if (p != None):
print_info(p)
# print_questions(p)
records = []
p.close()
msg = ''
button_processes = []
button_processes.append(button_process(PRINT_BUTTON, print_action))
button_processes.append(button_process(
DAY_BUTTON, lambda: records.append(day_record)))
button_processes.append(button_process(
TWO_HOURS_BUTTON, lambda: records.append(two_hours_record)))
button_processes.append(button_process(RESET_BUTTON, reset_action))
button_processes.append(button_process(INFO_BUTTON, info_action))
def total_print(request_json):
global msg
if 'items' not in request_json:
if 'info' in request_json:
info_action()
elif request_json == r'{}':
msg = 'Empty receipt'
print_action()
else:
msg = 'Invalid content'
return
else:
for record in request_json['items']:
records.append(record)
msg = 'Print receipt'
print_action()
class MyHandler(BaseHTTPRequestHandler):
global records
global msg
def do_POST(self):
try:
content_len = int(self.headers.get('content-length'))
requestBody = json.loads(self.rfile.read(content_len).decode('utf-8'))
total_print(requestBody)
response = {'status' : 200,
'msg' : msg}
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
responseBody = json.dumps(response)
self.wfile.write(responseBody.encode('utf-8'))
except Exception as e:
print("An error occured")
print("The information of error is as following")
print(type(e))
print(e.args)
print(e)
response = { 'status' : 500,
'msg' : 'An error occured' }
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
responseBody = json.dumps(response)
self.wfile.write(responseBody.encode('utf-8'))
def run(server_class=HTTPServer, handler_class=MyHandler, server_name='', port=8008):
server = server_class((server_name, port), handler_class)
server.serve_forever()
def button():
print('button start')
while True:
for process in button_processes:
next(process)
time.sleep(0.01)
def server():
print('server starts')
run(server_name='', port=8008)
t = threading.Thread(target=button)
t2 = threading.Thread(target=server)
t.start()
t2.start()