Commit 228c9a91 authored by Nikita Yurishev's avatar Nikita Yurishev

added influxWriter

parent 79b153f3
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
import os
import logging as log
import hid
import time
import sys
import socket #для hostname
import socket
from send_data_to_influxdb import InfluxWriter
CO2_USB_MFG = 'Holtek'
CO2_USB_PRD = 'USB-zyTemp'
......@@ -46,6 +46,8 @@ class ZyTemp():
self._magic_word = [((w << 4) & 0xFF) | (w >> 4) for w in bytearray(_CO2MON_MAGIC_WORD)]
self._magic_table = _CO2MON_MAGIC_TABLE
self._magic_table_int = list_to_longint(_CO2MON_MAGIC_TABLE)
self.influx_writer = InfluxWriter() # Создаем объект для работы с InfluxDB
if decrypt:
self.h.send_feature_report(self._magic_table)
......@@ -59,9 +61,9 @@ class ZyTemp():
values[key] = value
if all(value is not None for value in values.values()):
#hostname
hostname = socket.gethostname()
print(f"{hostname}: {values}")
sensor_id = socket.gethostname()
print(f"{sensor_id}: {values}")
self.influx_writer.write_data(sensor_id, values) # Записываем данные в InfluxDB
sys.exit(0)
def run_once(self, decrypt=False):
......
# -*- coding: utf-8 -*-
from influxdb import InfluxDBClient
import time
class InfluxWriter:
def __init__(self):
self.INFLUXDB_URL = "http://localhost:8086"
self.INFLUXDB_DB = "co2"
self.INFLUXDB_USER = "your_username"
self.INFLUXDB_PASSWORD = "your_password"
# Подключение к InfluxDB
self.client = InfluxDBClient(
host='localhost',
port=8086,
username=self.INFLUXDB_USER,
password=self.INFLUXDB_PASSWORD,
database=self.INFLUXDB_DB
)
def write_data(self, sensor_id, values):
json_body = [
{
"measurement": "sensor_data",
"tags": {
"sensor_id": sensor_id
},
"time": int(time.time() * 1000), # Время в миллисекундах
"fields": values
}
]
self.client.write_points(json_body, database=self.INFLUXDB_DB)
def __del__(self):
self.client.close()
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