etat_imprimante.py 3.17 KB
Newer Older
gdetrez's avatar
gdetrez committed
1
2
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
gdetrez's avatar
gdetrez committed
3
4
5
"""
 etat_imprimante.py

6
7
     Rcupre, filtre et formate l'tat actuel de l'imprimante

8
 Copyright (c) 2006, 2007, 2008, 2009 by Cr@ns (http://www.crans.org)
gdetrez's avatar
gdetrez committed
9
10

"""
gdetrez's avatar
gdetrez committed
11
12
13
14

import sys
sys.path.append("/usr/scripts/gestion")

15
16
from hptools import snmp
from config import impression
gdetrez's avatar
gdetrez committed
17
18

class ErreurCommunication(Exception):
gdetrez's avatar
gdetrez committed
19
    """Si une erreur est survenue lors de la communication avec l'imprimante"""
gdetrez's avatar
gdetrez committed
20
21
22
    pass

def etat():
23
    return (globals()["etat_%s" % impression.imprimante])()
24
25

def etat_canon():
26
    _dico = {
27
        'idle(3)\nrunning(2)'     : u'Imprimante fonctionnelle',
28
        'printing(4)\nrunning(2)' : u'Impression en cours',
29
        'other(1)\ndown(5)'       : u"Imprimante hors-service",
30
        'other(1)\nrunning(2)'    : u'Imprimante en veille',
31
        'warmup(5)\nrunning(2)'   : u'Prchauffage',
32
33
        'idle(3)\nwarning(3)'     : u'Imprimante fonctionnelle',
        'printing(4)\nwarning(3)' : u'Impression en cours'
34
      }
35
    try:
36
        raw_msg = msg = []
37
        comm = snmp(host="imprimante.adm.crans.org", version="1", community="public")
38

39
40
        for oid in [".hrPrinterStatus.1", ".hrDeviceStatus.1"]:
                    #".hrPrinterDetectedErrorState.1"
41
42
43
44
            raw_msg.append(comm.get_string(oid))
        msg = _dico['\n'.join(msg)].split('\n')

        if raw_msg[1] == 'warning(3)':
45
            msg += [v.replace('"', '') for v in comm.walk('mib-2.43.18.1.1.8').values()]
46

47
    except Exception,err:
48
        msg.append("[%s]" % unicode(err))
49

50
51
    msg.append(u"L'imprimante laisse des traines sur les pages")

52
    return msg
53
54

def etat_laserjet():
gdetrez's avatar
gdetrez committed
55
    """ Renvoie une liste des differents ecrans actuels du display de l'imprimante """
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    _dico = {
      u"READY": u"Prte",
      u"Prt": u"Prt",
      u"Pr menus, appuy \x1e": u"",
      u"Powersave activ": u"En veille",
      u"Verification": u"Vrification imprimante",
      u"imprimante": u"",
      u"Prchauffage": u"Prchauffage",
      u"Traitement de la": u"Impression en cours",
      u"tche du bac 4": u"",
      u"COMMANDER CARTOUCHE": u"",
      u"COMMANDER KIT NETTOY": u"",
      u"COMMANDER FOURNIT.": u"",
      u"CYAN": u"",
      u"MAGENTA": u"",
      u"JAUNE": u"",
      u"NOIR": u"",
      u'Pr aide appuyer \x1f': u"",
74
      u'13.12.12 BOURRAGE DS':u"Bourrage papier",
75
      u'ACCESSOIRE GAUCHE':u"",
76
      u'13.03.00 BOURRAGE':u"Bourrage papier",
77
78
79
80
81
      u'DS TRAPPE SUP DROITE':u"",
      u'COMM TAMB. NOIR':u"",
      u'PAGES REST.': u"",
    }

gdetrez's avatar
gdetrez committed
82
83
    liste_oid = ["mib-2.43.16.5.1.2.1.1","mib-2.43.16.5.1.2.1.2","mib-2.43.16.5.1.2.1.3","mib-2.43.16.5.1.2.1.4","mib-2.43.16.5.1.2.1.5"]
    try:
84
        comm = snmp(host="laserjet.adm.crans.org",version="1",community="public")
gdetrez's avatar
gdetrez committed
85
86
87
        liste_msg = []
        for oid in liste_oid:
            msg = comm.get_string(oid).decode("ISO-8859-1")
88
89
            for k in _dico.keys():
                if k in msg: msg = _dico[k]
gdetrez's avatar
gdetrez committed
90
91
92
93
            if msg: liste_msg.append(msg)
    except Exception,err:
        raise ErreurCommunication, str(err)
    return liste_msg
94
95
96
97
98
99
100
101

def enregistre(filename="/usr/script/impression/imprimante.etat"):
    l = etat()
    fichier = open(filename,'w')
    for x in l:
        fichier.write(x+"\n")
    fichier.close()