46 lines
1.0 KiB
Python
46 lines
1.0 KiB
Python
import os
|
|
import sys
|
|
import json
|
|
from data_class import Data
|
|
from devices import Device
|
|
|
|
DEBUG = True if os.getenv('DEBUG') == 'true' else False
|
|
|
|
|
|
def log_to_json(devices):
|
|
workdir, filename = os.path.split(os.path.abspath(__file__))
|
|
|
|
for device in devices:
|
|
dev, data_obj, from_config = device
|
|
data_obj: Data
|
|
from_config: Device
|
|
file_name = f'{workdir}/data/{str(data_obj.mac).replace(":", "-")}.json'
|
|
print(file_name) if DEBUG else {}
|
|
|
|
try:
|
|
with open(file_name, 'r') as file: data = json.load(file)
|
|
except:
|
|
with open(file_name, 'w') as file: file.write("[]")
|
|
data = []
|
|
|
|
data.append({
|
|
"timestamp": data_obj.timestamp,
|
|
"temperature": data_obj.temperature,
|
|
"humidity": data_obj.humidity,
|
|
"battery_percent": data_obj.battery_percent,
|
|
"battery_volt": data_obj.battery_volt,
|
|
"name": from_config.name,
|
|
"room": from_config.room
|
|
})
|
|
|
|
with open(file_name, 'w') as file: file.write(json.dumps(data, indent=2))
|
|
|
|
|
|
def log_to_mongodb(data):
|
|
pass
|
|
|
|
|
|
def log_to_mqtt(data):
|
|
pass
|
|
|