Commit 489ac07d authored by Nikita Yurishev's avatar Nikita Yurishev

added new connection logic

parent 228c9a91
[InfluxDB]
url = http://localhost:8086
database = co2
username = admin
password = 6wp9fCi0zSjTDISXFWg4
\ No newline at end of file
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import configparser
from influxdb import InfluxDBClient from influxdb import InfluxDBClient
import time import time
class InfluxWriter: class InfluxWriter:
def __init__(self): def __init__(self):
self.INFLUXDB_URL = "http://localhost:8086" # Чтение конфигурации из файла config.ini
self.INFLUXDB_DB = "co2" config = configparser.ConfigParser()
self.INFLUXDB_USER = "your_username" config.read('config.ini')
self.INFLUXDB_PASSWORD = "your_password"
self.INFLUXDB_URL = config.get('influxdb', 'url')
self.INFLUXDB_DB = config.get('influxdb', 'database')
self.INFLUXDB_USER = config.get('influxdb', 'username')
self.INFLUXDB_PASSWORD = config.get('influxdb', 'password')
# Подключение к InfluxDB # Подключение к InfluxDB
self.client = InfluxDBClient( self.client = InfluxDBClient(
host='localhost', host=self.INFLUXDB_URL.split(':')[1].strip('/'),
port=8086, port=int(self.INFLUXDB_URL.split(':')[2]),
username=self.INFLUXDB_USER, username=self.INFLUXDB_USER,
password=self.INFLUXDB_PASSWORD, password=self.INFLUXDB_PASSWORD,
database=self.INFLUXDB_DB database=self.INFLUXDB_DB
...@@ -25,11 +30,11 @@ class InfluxWriter: ...@@ -25,11 +30,11 @@ class InfluxWriter:
"tags": { "tags": {
"sensor_id": sensor_id "sensor_id": sensor_id
}, },
"time": int(time.time() * 1000), # Время в миллисекундах "time": int(time.time() * 1000),
"fields": values "fields": values
} }
] ]
self.client.write_points(json_body, database=self.INFLUXDB_DB) self.client.write_points(json_body, database=self.INFLUXDB_DB)
def __del__(self): def __del__(self):
self.client.close() self.client.close()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment