1
0
Fork 0
mirror of https://github.com/Luzifer/mqtt2influx.git synced 2024-12-20 09:31:23 +00:00

Move vault connection to single place

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-07-11 19:48:24 +02:00
parent 4fb30ba2d8
commit 8730975202
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 16 additions and 14 deletions

View file

@ -1,15 +1,13 @@
from influxdb import InfluxDBClient
import vault
class Influx():
def __init__(self, database):
cfg = vault.read_data('secret/mqtt2influx/influxdb')
def __init__(self, host, port, user, password, database):
self.client = InfluxDBClient(host, port,
user, password,
database)
self.database = database
self.client = InfluxDBClient(cfg['host'], cfg['port'],
cfg['user'], cfg['pass'], self.database)
def submit(self, body):
"""

20
main.py
View file

@ -14,8 +14,14 @@ class MQTT2InfluxDB():
with open(config_path) as cfg_file:
self.config = yaml.safe_load(cfg_file)
self.influx = Influx(self.obj_get(
self.config, 'influx_db', 'mqtt2influxdb'))
influx_config = vault.read_data('secret/mqtt2influx/influxdb')
self.mqtt_config = vault.read_data('secret/mqtt2influx/mqtt')
self.influx = Influx(
influx_config['host'], influx_config['port'],
influx_config['user'], influx_config['pass'],
self.obj_get(self.config, 'influx_db', 'mqtt2influxdb'),
)
def obj_get(self, obj, key, default=None):
if key in obj:
@ -69,19 +75,17 @@ class MQTT2InfluxDB():
client.on_connect = self.on_connect
client.on_message = self.on_message
mqtt_config = vault.read_data('secret/mqtt2influx/mqtt')
client.username_pw_set(
self.obj_get(os.environ, 'MQTT_USER', mqtt_config['user']),
self.obj_get(os.environ, 'MQTT_PASS', mqtt_config['pass']),
self.obj_get(os.environ, 'MQTT_USER', self.mqtt_config['user']),
self.obj_get(os.environ, 'MQTT_PASS', self.mqtt_config['pass']),
)
logging.debug('Connecting to MQTT broker...')
client.connect(
self.obj_get(os.environ, 'MQTT_HOST', mqtt_config['host']),
self.obj_get(os.environ, 'MQTT_HOST', self.mqtt_config['host']),
self.obj_get(os.environ, 'MQTT_PORT',
self.obj_get(mqtt_config, 'port', 1883)),
self.obj_get(self.mqtt_config, 'port', 1883)),
keepalive=10,
)