-
Notifications
You must be signed in to change notification settings - Fork 0
/
releverTeleinfo.py
executable file
·100 lines (88 loc) · 2.66 KB
/
releverTeleinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
import time
import datetime
from lib import teleinfo
import sys
import MySQLdb
FORMAT = '%(asctime)-15s:%(levelname)s:%(message)s'
logging.basicConfig(format=FORMAT,level=logging.DEBUG)
def insertTrameTeleinfo(time, trame):
conn = MySQLdb.connect(host= "localhost",
db="test")
x = conn.cursor()
try:
x.execute("""INSERT INTO RELEVE_CONSOMMATION VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",
(time.strftime('%Y-%m-%d %H:%M:%S'),
trame[teleinfo.ADCO],
trame[teleinfo.OPTARIF],
trame[teleinfo.ISOUSC],
trame[teleinfo.PAPP],
trame[teleinfo.IINST],
trame[teleinfo.IMAX],
trame[teleinfo.PTEC],
trame[teleinfo.BASE],
trame[teleinfo.HCHC],
trame[teleinfo.HCHP],
trame[teleinfo.HHPHC],
trame[teleinfo.BBRHCJB],
trame[teleinfo.BBRHPJB],
trame[teleinfo.BBRHCJW],
trame[teleinfo.BBRHPJW],
trame[teleinfo.BBRHCJR],
trame[teleinfo.BBRHPJR],
trame[teleinfo.DEMAIN]
))
conn.commit()
except Exception as e:
logging.error("Impossible d'insérer la trame de teleinfo : %s", e)
conn.rollback()
return False
finally:
conn.close()
return True
def readTrameAndInsert(port):
ftdiContext = teleinfo.ouvrirPort(port)
try:
nbMaxTentatives = 3
startTime = datetime.datetime.now()
logging.debug("Recuperation d'une trame de teleinfo, port %s", hex(port));
nbTentatives = 0
trameObj = None
while trameObj is None and nbTentatives < nbMaxTentatives:
trame = teleinfo.getTrame(ftdiContext)
trameObj = teleinfo.trameToObj(trame)
nbTentatives += 1
#afficherTrame(trame)
if trameObj != None:
logging.debug("Trame récupérée en %s tentative(s)", nbTentatives)
#print json.dumps(trameObj)
if insertTrameTeleinfo(startTime, trameObj):
logging.debug("Trame de teleinfo insérée en base de données")
else:
logging.warning("Trame de teleinfo non insérée en base de données")
else:
logging.error("Trame non récupérée : %s tentatives en échec", nbMaxTentatives)
finally:
logging.info("Fermeture du port %s", hex(port));
teleinfo.fermerPort(ftdiContext)
ports=[]
argsPorts = sys.argv;
programName=argsPorts[0]
del argsPorts[0] # Supprimer le nom du programme
if len(argsPorts) != 0:
for i in range(len(argsPorts)):
if int(argsPorts[i]) == 2:
ports.append(teleinfo.PORT_2)
else:
ports.append(teleinfo.PORT_1)
logging.info("Port numero %s : %s", argsPorts[i], hex(ports[i]))
while True:
for i in range(len(ports)):
readTrameAndInsert(ports[i])
#wait x seconds and repeat
time.sleep(5)
else:
logging.error("Syntaxe incorrecte, il faut préciser les ports à relever. Ex. : %s 1 2 ", programName)
exit(0)