Mel imprimante est débranché ou plus de papier
This commit is contained in:
parent
d1ef64937f
commit
8fd1c0e272
5 changed files with 84 additions and 1 deletions
61
daemon.py
61
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()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue