Commit 94580105 authored by Gabriel Detraz's avatar Gabriel Detraz

Merge branch 'master' into 'master'

Envoie de mail automatique, état de l'imprimante

L'imprimante envoie un mail quand elle est débranchée ou si elle n'a plus de papier.

See merge request !3
parents dc22d01b 4fed7be7
#!/usr/bin/env python
#-*- coding: utf-8 -*-
PIDFILE = '/var/run/daemon.pid'
PIDFILE = '/var/run/cransticket.pid'
USER='crans_ticket'
GROUP='adm'
......@@ -3,11 +3,16 @@
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
import dump
import config
import threading, time, smtplib
from email.mime.text import MIMEText
from config import DEVICE
def run():
conn = pika.BlockingConnection(config.PARAMS)
......@@ -22,9 +27,57 @@ def run():
conn.close()
# fork en arrière plan + pidfile
def checkPaper():
printer = AdafruitThermal(DEVICE, 19200, timeout=5)
messageCourant = False
messagePapier = False
def sendMel(msgfile, etat):
fp = open(msgfile, 'rb')
msg = MIMEText(fp.read())
fp.close()
me = 'oison@crans.org'
dest = 'respbats@crans.org'
msg['Subject'] = '''Etat de l'imprimante thermique : %s''' % etat
msg['From'] = me
msg['To'] = dest
s = smtplib.SMTP('smtp.crans.org', 25)
s.sendmail(me, [dest], msg.as_string())
s.quit()
def checkPrinter(dejaVu, messageCourant, messagePapier):
try:
papier = printer.hasPaper()
if messageCourant:
sendMel('melSecteur', 'alimentation')
messageCourant = False
else:
if not (messagePapier or papier):
sendMel('melPasPapier', 'papier')
messagePapier = True
if messagePapier and papier:
sendMel('melPapier', 'papier')
messagePapier = False
except TypeError:
if not messageCourant:
if dejaVu:
sendMel('melSurBatterie', 'alimentation')
messageCourant = True
else:
time.sleep(600)
messageCourant, messagePapier = checkPrinter(True, messageCourant, messagePapier)
return messageCourant, messagePapier
while True:
time.sleep(60)
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()
# do the UNIX double-fork magic, see Stevens' "Advanced
......@@ -55,6 +108,9 @@ if __name__ == "__main__":
print("fork #2 failed: %d (%s)" % (e.errno, e.strerror),file=sys.stderr)
sys.exit(1)
# start the daemon main loop
# start the daemon thread to monitor the printer
thHasPaper = threading.Thread(target = checkPaper, name = 'checkPaper')
thHasPaper.start()
#main loop
run()
Retour du papier !
Le sain papier est de retour, loue soit il !
--
Oison
Help !!!
Gentils cableurs, je n'ai plus de papier.
--
Oison
Le courant est revenue !
Merci de m'avoir branche.
--
Oison
Help !!!
Gentils cableurs derriere le bar, rebranchez moi, please !
--
Oison
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