Commit 697d9f07 authored by root's avatar root

Passage en python3 + code avec imprimante epson

parent 94580105
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys,os,pwd,grp
# Bad namming: change namming
from AdafruitThermal import Adafruit_Thermal as AdafruitThermal
import common
import pika
import json
......@@ -13,6 +12,7 @@ import config
import threading, time, smtplib
from email.mime.text import MIMEText
from config import DEVICE
import codecs
def run():
conn = pika.BlockingConnection(config.PARAMS)
......@@ -20,7 +20,7 @@ def run():
ch.queue_declare(queue=config.QUEUE)
def callback(ch, method, properties, body):
print (" [x] Received %r" % (body,))
dump.print_liste(json.loads(body))
dump.print_liste(json.loads(body.decode('utf8')))
ch.basic_consume(callback, queue=config.QUEUE, no_ack=True)
ch.start_consuming()
......@@ -75,11 +75,11 @@ def checkPaper():
messageCourant, messagePapier = checkPrinter(False, messageCourant, messagePapier)
if __name__ == "__main__":
if '-fg' in sys.argv:
thHasPaper = threading.Thread(target = checkPaper, name = 'checkPaper')
thHasPaper.start()
run()
exit()
#if '-fg' in sys.argv:
#thHasPaper = threading.Thread(target = checkPaper, name = 'checkPaper')
#thHasPaper.start()
#run()
#exit()
# do the UNIX double-fork magic, see Stevens' "Advanced
# Programming in the UNIX Environment" for details (ISBN 0201563177)
try:
......@@ -87,7 +87,7 @@ if __name__ == "__main__":
if pid > 0:
# exit first parent
sys.exit(0)
except OSError, e:
except OSError as e:
print("fork #1 failed: %d (%s)" % (e.errno, e.strerror),file=sys.stderr)
sys.exit(1)
......@@ -101,16 +101,16 @@ if __name__ == "__main__":
pid = os.fork()
if pid > 0:
# exit from second parent, print eventual PID before
#print "Daemon PID %d" % pid
print("Daemon PID %d" % pid)
open(common.PIDFILE, 'w').write("%d" % pid)
sys.exit(0)
except OSError, e:
except OSError as e:
print("fork #2 failed: %d (%s)" % (e.errno, e.strerror),file=sys.stderr)
sys.exit(1)
# start the daemon thread to monitor the printer
thHasPaper = threading.Thread(target = checkPaper, name = 'checkPaper')
thHasPaper.start()
#thHasPaper = threading.Thread(target = checkPaper, name = 'checkPaper')
#thHasPaper.start()
#main loop
run()
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
# Bad namming: change namming
from AdafruitThermal import Adafruit_Thermal as AdafruitThermal
import subprocess, time, Image, socket
from escpos.printer import Usb
import subprocess, time, socket
import os
import json
from config import DEVICE
......@@ -24,16 +23,15 @@ SAMPLE_ACCOUNT = {
'pass': 'omgverysecure',
}
# Load data
crans_logo = Image.open(os.path.join(os.path.dirname(__file__), 'logo_crans.png'))
printer = AdafruitThermal(DEVICE, 19200, timeout=5)
""" Seiko Epson Corp. Receipt Printer (EPSON TM-T88III) """
printer = Usb(0x04b8, 0x0e15, 0)
printer.charcode(code='CA_FRENCH')
def print_carac(text, value):
printer.justify('L')
pad = 384/12 - len(text) - len(value)
printer.println(text + ('.'*pad) + value )
printer.text(text + ('.'*int(pad)) + value + '\n')
def show_entry(entry):
......@@ -42,17 +40,17 @@ def show_entry(entry):
if 'fid' in entry.keys():
show_facture(entry)
return
printer.justify('C')
printer.underlineOn()
printer.text("\n")
printer.set(align='center', text_type="BU")
if 'host' in entry:
title = u'Détails machine'
title = 'Détails machine'
if 'type' in entry:
title += u' ' + entry['type']
printer.println(title.encode(CODING))
title += ' ' + entry['type']
printer.text(title + '\n')
else:
printer.println(u'Compte Crans'.encode(CODING))
printer.underlineOff()
printer.justify('L')
printer.text('Compte Crans \n')
printer.set(align='center', text_type="NORMAL")
login = entry.get('login', None) or entry['host']
print_carac('Login', login)
......@@ -63,7 +61,6 @@ def show_entry(entry):
if 'pass' in entry:
print_carac('Mot de passe', entry['pass'])
printer.feed(1)
def make_fact_elem(element,nb_car,is_last):
......@@ -96,34 +93,34 @@ def show_facture(facture):
#Impression de l'en-tête
printer.justify('L')
printer.println(facture['recuPaiement'].encode(CODING))
printer.text(facture['recuPaiement'].encode(CODING))
printer.justify('C')
printer.println(u'--------------------------------'.encode(CODING))
printer.text(u'--------------------------------'.encode(CODING))
printer.boldOn()
fid = u'Facture n°%d' % facture['fid']
printer.println(fid.encode(CODING))
printer.text(fid.encode(CODING))
printer.boldOff()
printer.println(u'--------------------------------'.encode(CODING))
printer.text(u'--------------------------------'.encode(CODING))
#Impression de l'en-tête de la facture
nom_complet = facture['nom'] + u' ' + facture['prenom']
printer.justify('R')
printer.println((u'Adhérent : ' + nom_complet).encode(CODING))
printer.text((u'Adhérent : ' + nom_complet).encode(CODING))
if facture['chbre'] is u'EXT':
printer.println(u'Externe'.encode(CODING))
printer.text(u'Externe'.encode(CODING))
else:
printer.println((u'Chambre : ' + facture['chbre']).encode(CODING))
printer.text((u'Chambre : ' + facture['chbre']).encode(CODING))
if facture['debutAdhesion']:
printer.println((u'Début adhésion : ' + facture['debutAdhesion']).encode(CODING))
printer.text((u'Début adhésion : ' + facture['debutAdhesion']).encode(CODING))
if facture['finAdhesion']:
printer.println((u'Fin adhésion : ' + facture['finAdhesion']).encode(CODING))
printer.text((u'Fin adhésion : ' + facture['finAdhesion']).encode(CODING))
if facture['finConnexion']:
printer.println((u'Fin connexion : ' + facture['finConnexion']).encode(CODING))
printer.text((u'Fin connexion : ' + facture['finConnexion']).encode(CODING))
printer.justify('L')
printer.println(u'--------------------------------'.encode(CODING))
printer.println(u' Code | Désig. |Qté| P.U '.encode(CODING)) #Code(6c. max.),Désignation(11c. max.),Qté(3c. max),PU(8[+1]c.max)
printer.println(u'------|-----------|---|---------'.encode(CODING))
printer.text(u'--------------------------------'.encode(CODING))
printer.text(u' Code | Désig. |Qté| P.U '.encode(CODING)) #Code(6c. max.),Désignation(11c. max.),Qté(3c. max),PU(8[+1]c.max)
printer.text(u'------|-----------|---|---------'.encode(CODING))
#Impression du corps de la facture + calcul du prix total
total = 0.0
......@@ -135,21 +132,21 @@ def show_facture(facture):
row += make_fact_elem(art['pu']+u'e',9,True)
total += float(art['pu'])
printer.println(row.encode(CODING))
printer.text(row.encode(CODING))
printer.println(u'--------------------------------'.encode(CODING))
printer.text(u'--------------------------------'.encode(CODING))
#On affiche le total
printer.justify('R')
printer.println((u'---------').encode(CODING))
printer.println((u'Total à régler '+ '|' + make_fact_elem(total,8,True) + u'e').encode(CODING))
printer.println((u'---------').encode(CODING))
printer.text((u'---------').encode(CODING))
printer.text((u'Total à régler '+ '|' + make_fact_elem(total,8,True) + u'e').encode(CODING))
printer.text((u'---------').encode(CODING))
printer.justify('L')
printer.println((u'Payé par ' + facture['modePaiement']).encode(CODING))
printer.text((u'Payé par ' + facture['modePaiement']).encode(CODING))
#On met une super phrase sur la TVA
printer.feed(1)
printer.println(u'TVA non applicable(art. 293B du CGI)'.encode(CODING))
printer.text(u'TVA non applicable(art. 293B du CGI)'.encode(CODING))
#Fin de la facture
......@@ -158,19 +155,19 @@ def show_facture(facture):
# Do print
def print_liste(liste):
printer.setDefault() # Restore printer to defaults
printer.printImage(crans_logo, True)
printer.set(align='center')
printer.image("/home/pi/cransticket/logo_crans.png", True)
for entry in liste:
show_entry(entry)
if any('secret' in entry or 'pass' in entry for entry in liste):
printer.println(u'Veuillez conserver ces'.encode(CODING))
printer.println(u'informations en lieu sûr.'.encode(CODING))
printer.println(u' '.encode(CODING))
printer.println(u' '.encode(CODING))
printer.text(' \n')
printer.text(' \n')
printer.text(str('Veuillez conserver ces \n'))
printer.text(str('informations en lieu sûr. \n'))
printer.feed(2)
printer.cut()
if __name__ == '__main__':
if not sys.argv[1:]:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment