From d1ef64937f2f92bf4db0474d1f8725a085e3aab1 Mon Sep 17 00:00:00 2001 From: Fardale Date: Thu, 3 Dec 2015 12:29:40 +0100 Subject: [PATCH 1/3] Nom explicite pour le fichier dans /var/run --- common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common.py b/common.py index cc7818f..1fbe19f 100644 --- a/common.py +++ b/common.py @@ -1,6 +1,6 @@ #!/usr/bin/env python #-*- coding: utf-8 -*- -PIDFILE = '/var/run/daemon.pid' +PIDFILE = '/var/run/cransticket.pid' USER='crans_ticket' GROUP='adm' From 8fd1c0e272bcabf4e6106722b6a6d8b01d67879c Mon Sep 17 00:00:00 2001 From: Fardale Date: Sat, 5 Dec 2015 18:09:16 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Mel=20imprimante=20est=20d=C3=A9branch?= =?UTF-8?q?=C3=A9=20ou=20plus=20de=20papier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daemon.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++- melPapier | 6 +++++ melPasPapier | 6 +++++ melSecteur | 6 +++++ melSurBatterie | 6 +++++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 melPapier create mode 100644 melPasPapier create mode 100644 melSecteur create mode 100644 melSurBatterie diff --git a/daemon.py b/daemon.py index a0ec420..16ac04b 100755 --- a/daemon.py +++ b/daemon.py @@ -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,60 @@ 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') + # Create a text/plain message + msg = MIMEText(fp.read()) + fp.close() + + # me == the sender's email address + # you == the recipient's email address + msg['Subject'] = '''Etat de l'imprimante thermique : %s''' % etat + msg['From'] = 'oison@crans.org' + msg['To'] = 'arrighi@crans.org' + + # Send the message via our own SMTP server, but don't include the + # envelope header. + s = smtplib.SMTP('smtp.crans.org', 25) + s.sendmail('oison@crans.org', ['arrighi@crans.org'], 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 +111,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 + thHasPaper = threading.Thread(target = checkPaper, name = 'checkPaper') + thHasPaper.start() + #main loop run() diff --git a/melPapier b/melPapier new file mode 100644 index 0000000..6c33989 --- /dev/null +++ b/melPapier @@ -0,0 +1,6 @@ +Retour du papier ! + +Le sain papier est de retour, loue soit il ! + +-- +Oison diff --git a/melPasPapier b/melPasPapier new file mode 100644 index 0000000..e932273 --- /dev/null +++ b/melPasPapier @@ -0,0 +1,6 @@ +Help !!! + +Gentils cableurs, je n'ai plus de papier. + +-- +Oison diff --git a/melSecteur b/melSecteur new file mode 100644 index 0000000..56da2ed --- /dev/null +++ b/melSecteur @@ -0,0 +1,6 @@ +Le courant est revenue ! + +Merci de m'avoir branche. + +-- +Oison diff --git a/melSurBatterie b/melSurBatterie new file mode 100644 index 0000000..130f709 --- /dev/null +++ b/melSurBatterie @@ -0,0 +1,6 @@ +Help !!! + +Gentils cableurs derriere le bar, rebranchez moi, please ! + +-- +Oison From 4fed7be7663f47639a22959fb0292332df4d0bd4 Mon Sep 17 00:00:00 2001 From: Fardale Date: Sat, 5 Dec 2015 18:16:02 +0100 Subject: [PATCH 3/3] Correction mel de contact --- daemon.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/daemon.py b/daemon.py index 16ac04b..e0f9875 100755 --- a/daemon.py +++ b/daemon.py @@ -34,20 +34,17 @@ def checkPaper(): def sendMel(msgfile, etat): fp = open(msgfile, 'rb') - # Create a text/plain message msg = MIMEText(fp.read()) fp.close() - # me == the sender's email address - # you == the recipient's email address + me = 'oison@crans.org' + dest = 'respbats@crans.org' msg['Subject'] = '''Etat de l'imprimante thermique : %s''' % etat - msg['From'] = 'oison@crans.org' - msg['To'] = 'arrighi@crans.org' + msg['From'] = me + msg['To'] = dest - # Send the message via our own SMTP server, but don't include the - # envelope header. s = smtplib.SMTP('smtp.crans.org', 25) - s.sendmail('oison@crans.org', ['arrighi@crans.org'], msg.as_string()) + s.sendmail(me, [dest], msg.as_string()) s.quit() def checkPrinter(dejaVu, messageCourant, messagePapier): @@ -111,7 +108,7 @@ if __name__ == "__main__": print("fork #2 failed: %d (%s)" % (e.errno, e.strerror),file=sys.stderr) sys.exit(1) - # start the daemon thread + # start the daemon thread to monitor the printer thHasPaper = threading.Thread(target = checkPaper, name = 'checkPaper') thHasPaper.start() #main loop