pep8 et constante en majuscule
This commit is contained in:
parent
0702cc0801
commit
2dc277e8a4
24
setup.cfg
Normal file
24
setup.cfg
Normal file
|
@ -0,0 +1,24 @@
|
|||
# - https://timothycrosley.github.io/isort/
|
||||
# - https://github.com/timothycrosley/isort#configuring-isort
|
||||
# - https://github.com/timothycrosley/isort/wiki/isort-Settings
|
||||
|
||||
[isort]
|
||||
combine_as_imports = True
|
||||
ensure_newline_before_comments = True
|
||||
force_grid_wrap = 0
|
||||
include_trailing_comma = True
|
||||
known_first_party = itou
|
||||
lines_after_imports = 2
|
||||
line_length = 119
|
||||
multi_line_output = 3
|
||||
use_parentheses = True
|
||||
|
||||
# - https://www.flake8rules.com
|
||||
# - http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html#changing-the-ignore-list
|
||||
|
||||
[flake8]
|
||||
# E203: Whitespace before ':', used to please Black in `yield items[i : i + n]`
|
||||
# E266: Too many leading '#' for block comment
|
||||
# W503: Line break occurred before a binary operator
|
||||
ignore = E203, E266, W503
|
||||
max-line-length = 119
|
55
teleinfo.py
55
teleinfo.py
|
@ -22,16 +22,17 @@
|
|||
# }
|
||||
|
||||
|
||||
import serial
|
||||
import logging
|
||||
import time
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
import serial
|
||||
from influxdb import InfluxDBClient
|
||||
|
||||
|
||||
# clés téléinfo
|
||||
int_measure_keys = ['BASE', 'IMAX', 'HCHC', 'IINST', 'PAPP', 'ISOUSC', 'ADCO', 'HCHP']
|
||||
no_checksum = ['MOTDETAT']
|
||||
INT_MESURE_KEYS = ['BASE', 'IMAX', 'HCHC', 'IINST', 'PAPP', 'ISOUSC', 'ADCO', 'HCHP']
|
||||
|
||||
# création du logguer
|
||||
logging.basicConfig(filename='/var/log/teleinfo/releve.log', level=logging.INFO, format='%(asctime)s %(message)s')
|
||||
|
@ -39,17 +40,17 @@ logging.info("Teleinfo starting..")
|
|||
|
||||
# connexion a la base de données InfluxDB
|
||||
client = InfluxDBClient('localhost', 8086)
|
||||
db = "teleinfo"
|
||||
DB_NAME = "teleinfo"
|
||||
connected = False
|
||||
while not connected:
|
||||
try:
|
||||
logging.info("Database %s exists?" % db)
|
||||
if not {'name': db} in client.get_list_database():
|
||||
logging.info("Database %s creation.." % db)
|
||||
client.create_database(db)
|
||||
logging.info("Database %s created!" % db)
|
||||
client.switch_database(db)
|
||||
logging.info("Connected to %s!" % db)
|
||||
logging.info("Database %s exists?" % DB_NAME)
|
||||
if not {'name': DB_NAME} in client.get_list_database():
|
||||
logging.info("Database %s creation.." % DB_NAME)
|
||||
client.create_database(DB_NAME)
|
||||
logging.info("Database %s created!" % DB_NAME)
|
||||
client.switch_database(DB_NAME)
|
||||
logging.info("Connected to %s!" % DB_NAME)
|
||||
except requests.exceptions.ConnectionError:
|
||||
logging.info('InfluxDB is not reachable. Waiting 5 seconds to retry.')
|
||||
time.sleep(5)
|
||||
|
@ -61,17 +62,17 @@ def add_measures(measures, time_measure):
|
|||
points = []
|
||||
for measure, value in measures.items():
|
||||
point = {
|
||||
"measurement": measure,
|
||||
"tags": {
|
||||
# identification de la sonde et du compteur
|
||||
"host": "raspberry",
|
||||
"region": "linky"
|
||||
},
|
||||
"time": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
"fields": {
|
||||
"value": value
|
||||
}
|
||||
}
|
||||
"measurement": measure,
|
||||
"tags": {
|
||||
# identification de la sonde et du compteur
|
||||
"host": "raspberry",
|
||||
"region": "linky"
|
||||
},
|
||||
"time": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
"fields": {
|
||||
"value": value
|
||||
}
|
||||
}
|
||||
points.append(point)
|
||||
|
||||
client.write_points(points)
|
||||
|
@ -106,14 +107,16 @@ def main():
|
|||
logging.debug(line)
|
||||
|
||||
try:
|
||||
[key, val, *_] = line_str.split(" ") # separation sur espace /!\ attention le caractere de controle 0x32 est un espace aussi
|
||||
# separation sur espace /!\ attention le caractere de controle 0x32 est un espace aussi
|
||||
[key, val, *_] = line_str.split(" ")
|
||||
|
||||
# supprimer les retours charriot et saut de ligne puis selectionne le caractere de controle en partant de la fin
|
||||
# supprimer les retours charriot et saut de ligne puis selectionne le caractere
|
||||
# de controle en partant de la fin
|
||||
checksum = (line_str.replace('\x03\x02', ''))[-3:-2]
|
||||
|
||||
if verif_checksum(f"{key} {val}", checksum):
|
||||
# creation du champ pour la trame en cours avec cast des valeurs de mesure en "integer"
|
||||
trame[key] = int(val) if key in int_measure_keys else val
|
||||
trame[key] = int(val) if key in INT_MESURE_KEYS else val
|
||||
|
||||
if b'\x03' in line: # si caractère de fin dans la ligne, on insère la trame dans influx
|
||||
del trame['ADCO'] # adresse du compteur : confidentiel!
|
||||
|
|
Loading…
Reference in a new issue