forked from adafruit/Python-Thermal-Printer
/
thpr.py
executable file
·517 lines (439 loc) · 20.8 KB
/
thpr.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""[Th]ermal [Pr]inter
Usage:
thpr.py [-s | --no-server] [-v | --verbose] [-d | --no-deleting] [-p | --no-printing] [-r | --run-once]
thpr.py (-h | --help)
thpr.py --version
Options:
-h --help Show this help
--version Show version
-s --no-server Do not start Web server
-v --verbose Enable debug logging
-d --no-deleting Do not delete mails after printing
-p --no-printing Do not print anything
-r --run-once Run only one check, then exit
Central server to combine several functions around the
Thermal Printer.
1) Server to receive tasks by IfThisThanThat (ifttt.com), requires
a internet connection and publicly available domain.
2) Controller over the Thermal Printer hardware (together with
Adafruit_Thermal) and one additional button and one LED
3) Scheduler to execute regular prints, e.g. for updates for important
mails, RSS-feeds, tweets (controlled by a config file)
Credits:
Original version based on
* http://mattrichardson.com/Raspberry-Pi-Flask/,
* main.py from Adafruit ThermalPrinter on GitHub,
* MailReceiver, http://stackoverflow.com/questions/8307809/save-email-attachment-python3-pop3-ssl-gmail, https://gist.github.com/vwillcox/5090214, https://gist.github.com/baali/2633554
"""
from __future__ import print_function
from flask import Flask, render_template, request
import configparser
import subprocess
import time
from PIL import Image, ImageFilter
import socket
from AdafruitThermal import *
import RPi.GPIO as GPIO
import imaplib
import email
from email.header import decode_header
import os
from threading import Timer
import uuid
import shutil
import numpy as np
from docopt import docopt
import logging
logger = logging.getLogger(__name__)
lastId = '1' # State information passed to/from interval script
webapp = Flask(__name__)
@webapp.route("/")
def main():
# For each pin, read the pin state and store it in the pins dictionary:
#for pin in pins:
# pins[pin]['state'] = GPIO.input(pin)
# Put the pin dictionary into the template data dictionary:
#templateData = {'pins': pins}
# Pass the template data into the template main.html and
# return it to the user
return render_template('main.html')#, **templateData)
@webapp.route("/<secret_key>/<action>/<param>")
def action(secret_key, action, param):
"""Executed when someone requests a URL with secret_key, action and param"""
if secret_key == config['api']['secret_key']:
# key is correct, so continue
if action == 'print':
pass
print('Here I would do something')
#thermo_print(param)
# Along with the pin dictionary, put the message into the template data dictionary:
templateData = {
'action' : action,
'param' : param
}
return render_template('main.html', **templateData)
class Scheduler(object):
def __init__(self, sleep_time, function):
self.sleep_time = sleep_time
self.function = function
self._t = None
def start(self):
if self._t is None:
self._t = Timer(self.sleep_time, self._run)
self._t.start()
else:
raise Exception("this timer is already running")
def _run(self):
self.function()
self._t = Timer(self.sleep_time, self._run)
self._t.start()
def stop(self):
if self._t is not None:
self._t.cancel()
self._t = None
class MyThermalPrinter:
"""Thermal Printer class with added button and LED"""
# Poll initial button state and time
def __init__(self, *args, **kwargs):
self.button_pin = int(kwargs.pop('button_pin'))
LED_PIN = int(kwargs.pop('led_pin'))
self.hold_time = int(kwargs.pop('hold_time'))
self.available = True
self.tap_time = 0.01 # Debounce time for button taps
self.next_interval = 0.0 # Time of next recurring operation
self.daily_flag = False # Set after daily trigger occurs
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(self.button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
self.tp = AdafruitThermal(*args, **kwargs)
#self.available = False
# Enable LED and button (w/pull-up on latter)
self.prev_button_state = GPIO.input(self.button_pin)
self.prev_time = time.time()
self.tap_enable = False
self.hold_enable = False
def check_network(self):
# Processor load is heavy at startup; wait a moment to avoid
# stalling during greeting.
if self.available:
time.sleep(0)
# Show IP address (if network is available)
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 0))
if self.available:
GPIO.output(LED_PIN, GPIO.HIGH)
#self.tp.print_line('My IP address is ' + s.getsockname()[0])
logger.debug('My IP address is ' + s.getsockname()[0])
#self.tp.feed(3)
GPIO.output(LED_PIN, GPIO.LOW)
except:
if self.available:
GPIO.output(LED_PIN, GPIO.HIGH)
self.tp.bold_on()
self.tp.print_line('Network is unreachable.')
logger.debug('Network is unreachable.')
self.tp.bold_off()
self.tp.print('Connect display and keyboard\n' + \
'for network troubleshooting.')
self.tp.feed(3)
GPIO.output(LED_PIN, GPIO.LOW)
exit(0)
def greeting(self):
GPIO.output(LED_PIN, GPIO.HIGH)
# Print greeting image
if self.available:
self.tp.print_image(Image.open('gfx/hello.png'), True)
self.tp.feed(3)
GPIO.output(LED_PIN, GPIO.LOW)
def query_button(self):
# Poll current button state and time
button_state = GPIO.input(self.button_pin)
t = time.time()
# Has button state changed?
if button_state != self.prev_button_state:
if button_state:
GPIO.output(LED_PIN, GPIO.HIGH)
else:
GPIO.output(LED_PIN, GPIO.LOW)
#print('Button changed')
self.prev_button_state = button_state # Yes, save new state/time
self.prev_time = t
else: # Button state unchanged
if (t - self.prev_time) >= self.hold_time: # Button held more than 'holdTime'?
# Yes it has. Is the hold action as-yet untriggered?
if self.hold_enable == True: # Yep!
print('Hold action to be exec')
self.hold() # Perform hold action (usu. shutdown)
self.hold_enable = False # 1 shot...don't repeat hold action
self.tap_enable = False # Don't do tap action on release
elif (t - self.prev_time) >= self.tap_time: # Not holdTime. tapTime elapsed?
#print('tap')
# Yes. Debounced press or release...
if button_state == True: # Button released?
#print('button_state', button_state)
if self.tap_enable == True: # Ignore if prior hold()
self.tap() # Tap triggered (button released)
print('tap action executed')
self.hold_enable = False
else: # Button pressed
self.tap_enable = True # Enable tap and hold actions
self.hold_enable = True
#print('tap_enable', self.tap_enable)
# LED blinks while idle, for a brief interval every 2 seconds.
# Pin 18 is PWM-capable and a "sleep throb" would be nice, but
# the PWM-related library is a hassle for average users to install
# right now. Might return to this later when it's more accessible.
if ((int(t) & 1) == 0) and ((t - int(t)) < 0.05):
GPIO.output(LED_PIN, GPIO.HIGH)
else:
GPIO.output(LED_PIN, GPIO.LOW)
# Once per day (currently set for 6:30am local time, or when script
# is first run, if after 6:30am), run forecast and sudoku scripts.
loc_time = time.localtime()
if loc_time.tm_hour == 6 and loc_time.tm_min == 30:
if self.daily_flag == False:
self.daily()
self.daily_flag = True
else:
self.daily_flag = False # Reset daily trigger
def tap(self):
"""Called when button is briefly tapped. Invokes time/temperature script."""
GPIO.output(LED_PIN, GPIO.HIGH) # LED on while working
subprocess.call(["python", "timetemp.py"])
GPIO.output(LED_PIN, GPIO.LOW)
def hold(self):
"""Called when button is held down. Prints image, invokes shutdown process."""
GPIO.output(LED_PIN, GPIO.HIGH)
self.tp.print_line('Switching off...')
self.tp.feed(3)
for i in range(10):
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(LED_PIN, GPIO.LOW)
subprocess.call("sync")
subprocess.call(["shutdown", "-h", "now"])
GPIO.output(LED_PIN, GPIO.LOW)
def interval(self):
"""Called at periodic intervals (30 seconds by default).
Invokes twitter script.
"""
GPIO.output(LED_PIN, GPIO.HIGH)
p = subprocess.Popen(["python", "twitter.py", str(lastId)],
stdout=subprocess.PIPE)
GPIO.output(LED_PIN, GPIO.LOW)
return p.communicate()[0] # Script pipes back lastId, returned to main
def daily(self):
""" Called once per day (6:30am by default).
Invokes weather forecast and sudoku-gfx scripts.
"""
GPIO.output(LED_PIN, GPIO.HIGH)
subprocess.call(["python", "forecast.py"])
subprocess.call(["python", "sudoku-gfx.py"])
GPIO.output(LED_PIN, GPIO.LOW)
class MailReceiver(object):
def __init__(self, config, printer, no_deleting=False):
self.base_dir = os.path.join('/tmp', 'thpr')
try:
shutil.rmtree(self.base_dir)
except:
pass
#os.makedirs(self.base_dir)
self.user = config['EMail']['user']
self.password = config['EMail']['password']
self.printer = printer
self.no_deleting = no_deleting
self.valid_senders = [x[1].lower() for x in config.items('Valid senders')]
self.valid_recipients = [x[1].lower() for x in config.items('Valid recipients')]
self.printable_extensions = ['.jpg', '.jpeg', '.png']
def check_mail(self):
# Icloud mail
# Benutzername: Dies ist in der Regel der Namensteil Ihrer
# iCloud-E-Mail-Adresse (beispielsweise emilyparker, nicht
# emilyparker@icloud.com). Wenn Ihr E-Mail-Client bei Verwendung
# des Namensteils Ihrer iCloud-E-Mail-Adresse keine Verbindung zu
# iCloud herstellen kann, probieren Sie, die vollständige Adresse
# zu verwenden.
# Kennwort: Ihr iCloud-Kennwort (app specific!!!)
imap_session = imaplib.IMAP4_SSL('imap.mail.me.com', 993)
imap_session.login(self.user, self.password)
imap_session.select('inbox')
typ, data = imap_session.search(None, 'ALL')
if typ != 'OK':
print('Error searching Inbox.')
raise
# Iterating over all emails
relevant_message = False
message_with_image = False
to_be_deleted = []
for msgId in data[0].split():
typ, message_parts = imap_session.fetch(msgId, '(RFC822)')
if typ != 'OK':
print('Error fetching mail.')
raise
email_body = message_parts[0][1]
mail = email.message_from_string(email_body)
subject = decode_header(mail['subject'])[0]
logger.debug('Output from decode header: {0}, encoding {1}'.format(subject[0], subject[1]))
subject = subject[0]#.decode(subject[1])
sender = mail['from']
recipient = mail['to']
mail_text = None
#print('Mail found: subject "{0}", From: {1}, To: {2}'.format(subject, sender, recipient))
if sender != None and recipient != None:
logger.debug('Decoded subject: {0}'.format(subject))
found_valid_recipient = False
for val in self.valid_recipients:
if recipient.lower().find(val) != -1:
found_valid_recipient = True
found_valid_sender = False
if sender:
for val in self.valid_senders:
if sender.lower().find(val) != -1:
found_valid_sender = True
if found_valid_recipient and found_valid_sender:
print('Valid mail found: subject "{0}", From: {1}, To: {2}'.format(subject, sender, recipient))
if mail.is_multipart():
mail_text = None
file_name = None
for num, part in enumerate(mail.walk()):
logger.debug('---'*10)
logger.debug('Part No. {0}, Content type: :::{1}:::'.format(num, part.get_content_type()))
logger.debug('Part as string')
logger.debug(part.as_string()[:100])
logger.debug('Part payload')
logger.debug(part.get_payload()[:100])
if not mail_text and part.get_content_type() == 'text/plain':
mail_text = part.get_payload(decode=True).strip(' \n\r')
logger.debug('MAILTEXT (Multipart; length: {1}): {0}'.format(mail_text[:100], len(mail_text)))
if not file_name:
file_name = part.get_filename()
if file_name:
logger.debug('Filename: {0}'.format(file_name))
fn, file_extension = os.path.splitext(file_name)
if file_extension.lower() in self.printable_extensions:
message_with_image = True
savedir = os.path.join(self.base_dir, uuid.uuid1().hex)
os.makedirs(savedir)
fp = open(os.path.join(savedir, file_name), 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
else:
file_name = mail.get_filename()
if file_name:
logger.debug('Filename: {0}'.format(file_name))
fn, file_extension = os.path.splitext(file_name)
if file_extension.lower() in self.printable_extensions:
message_with_image = True
savedir = os.path.join(self.base_dir, uuid.uuid1().hex)
os.makedirs(savedir)
fp = open(os.path.join(savedir, file_name), 'wb')
fp.write(mail.get_payload(decode=True))
fp.close()
else:
mail_text = mail.get_payload(decode=True).strip(' \n\r')
logger.debug('MAILTEXT (Singlepart; length: {1}): {0}'.format(mail_text[:100], len(mail_text)))
if not message_with_image:
GPIO.output(LED_PIN, GPIO.HIGH)
self.printer.justify('C')
self.printer.print_line(20*'-')
self.printer.bold_on()
self.printer.print_line(subject)
self.printer.bold_off()
if mail_text:
self.printer.print(mail_text)
self.printer.print_line(20*'-')
self.printer.justify('L')
GPIO.output(LED_PIN, GPIO.LOW)
else:
GPIO.output(LED_PIN, GPIO.HIGH)
self.printer.justify('C')
self.printer.print_line(20*'-')
self.printer.bold_on()
self.printer.print_line(subject)
self.printer.bold_off()
if mail_text != None and mail_text != '':
self.printer.print_line(mail_text)
logger.debug('Directory ({0}) and name ({1})'.format(savedir, file_name))
my_image = Image.open(os.path.join(savedir, file_name))
new_width = 384
percentage = new_width/float(my_image.size[0])
new_height = my_image.size[1] * percentage
#print('New width {0}, height {1}'.format(int(new_width+0.5), int(new_height+0.5)))
gray = my_image.resize([int(new_width+0.5), int(new_height+0.5)]).convert('L')
gray_array = np.asarray(gray)
#bw = (gray_array > gray_array.mean())*255
bw = gray.point(lambda x: 0 if x<np.median(gray_array)*1.1 else 255, '1').convert('1')
bw.save(os.path.join('/tmp', 'convert_' + file_name + '.png'))
# self.printer.print_image(bw, True)
self.printer.print_line(20*'-')
self.printer.justify('L')
GPIO.output(LED_PIN, GPIO.LOW)
to_be_deleted.append(msgId)
if to_be_deleted and not self.no_deleting:
logger.debug('IDs to be moved to trash: {0}'.format(to_be_deleted))
boxes = imap_session.list()
#for box in boxes[1]:
# print(box)
for id in to_be_deleted:
result = imap_session.copy(id, 'Deleted Messages')
print(result)
if result[0] == 'OK':
imap_session.store(id, '+FLAGS', '\\Deleted')
imap_session.expunge()
imap_session.close()
imap_session.logout()
if __name__ == "__main__":
# Use Broadcom pin numbers (not Raspberry Pi pin numbers) for GPIO
arguments = docopt(__doc__, version='thpr.py 0.7')
ch = logging.StreamHandler()
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
#print(arguments)
if arguments['--verbose']:
logger.setLevel(logging.DEBUG)
ch.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
ch.setLevel(logging.INFO)
# add the handlers to the logger
logger.addHandler(ch)
GPIO.setmode(GPIO.BCM)
config = configparser.ConfigParser()
config.read('thpr.conf')
LED_PIN = int(config['Printer']['ledPin'])
BUTTON_PIN = config['Printer']['buttonPin']
HOLD_TIME = config['Printer']['timeout_for_hold'] # Duration for button hold (shutdown)
# Initialization of printer
PRINTER = MyThermalPrinter(config['Printer']['serial_port'],
9600, timeout=5,
button_pin = BUTTON_PIN,
led_pin = LED_PIN,
hold_time = HOLD_TIME,
no_printing = arguments['--no-printing'])
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(2.5)
GPIO.output(LED_PIN, GPIO.LOW)
if not PRINTER.available:
print('No printer available.')
PRINTER.check_network()
#PRINTER.greeting()
MR = MailReceiver(config, PRINTER.tp, no_deleting = arguments['--no-deleting'])
#MR.check_mail()
mail_schedule = Scheduler(60, MR.check_mail)
todo_schedule = Scheduler(0.05, PRINTER.query_button)
if not arguments['--run-once']:
#mail_schedule.start()
todo_schedule.start()
if not arguments['--no-server']:
logger.debug('Starting server')
webapp.run(host='0.0.0.0', port=80, debug=True, use_reloader=False)
elif not arguments['--run-once']:
logger.debug('Starting endless loop')
while True:
pass
if not arguments['--run-once']:
todo_schedule.stop()
#mail_schedule.stop()