You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.0 KiB

import sys
import boto3
import arrow
import json
import decimal
import configparser
from mijia.mijia.mijia_poller import MijiaPoller, \
MI_HUMIDITY, MI_TEMPERATURE, MI_BATTERY
from miflora.miflora.miflora_poller import MiFloraPoller, \
MI_CONDUCTIVITY, MI_MOISTURE, MI_LIGHT
config = configparser.ConfigParser()
try:
config.read('my.ini')
region_name = config['dynamodb']['region_name']
table = config['dynamodb']['table']
except:
print('Config file read failed')
sys.exit(1)
dynamodb = boto3.resource('dynamodb', region_name=region_name)
table = dynamodb.Table(table)
def flowercare(mac_addr, poller):
temperature = poller.parameter_value(MI_TEMPERATURE)
battery = poller.parameter_value(MI_BATTERY)
light = poller.parameter_value(MI_LIGHT)
moisture = poller.parameter_value(MI_MOISTURE)
conductivity = poller.parameter_value(MI_CONDUCTIVITY)
firmware = poller.firmware_version()
print('Flower care {}'.format(mac_addr))
print("Temperature: {}°C".format(temperature))
print("Moisture: {}%".format(moisture))
print("Light: {} lux".format(light))
print("Fertility: {} uS/cm".format(conductivity))
print("Battery: {}%".format(battery))
response = table.put_item(
Item={
'device': mac_addr,
'timestamp': arrow.get().naive.isoformat(),
'data': {
'temperature': decimal.Decimal('{}'.format(temperature)),
'battery': decimal.Decimal('{}'.format(battery)),
'light': decimal.Decimal('{}'.format(light)),
'moisture': decimal.Decimal('{}'.format(moisture)),
'conductivity': decimal.Decimal('{}'.format(conductivity)),
},
'info': {
'sensor': 'Xiaomi Mi Flora',
'firmware': '{}'.format(poller.firmware_version()),
},
}
)
return response
def mijia(mac_addr, poller):
temperature = poller.parameter_value(MI_TEMPERATURE)
battery = poller.parameter_value(MI_BATTERY)
humidity = poller.parameter_value(MI_HUMIDITY)
firmware = poller.firmware_version()
print('Mijia {}'.format(mac_addr))
print("Temperature: {}°C".format(temperature))
print("Humidity: {}%".format(humidity))
print("Battery: {}%".format(battery))
response = table.put_item(
Item={
'device': mac_addr,
'timestamp': arrow.get().naive.isoformat(),
'data': {
'temperature': decimal.Decimal('{}'.format(temperature)),
'battery': decimal.Decimal('{}'.format(battery)),
'humidity': decimal.Decimal('{}'.format(humidity)),
},
'info': {
'sensor': 'Mijia',
'firmware': '{}'.format(firmware),
},
}
)
return response
def save_data(mac_addr, device, poller):
if device == 'flora':
return flowercare(mac_addr, poller)
elif device == 'mijia':
return mijia(mac_addr, poller)
else:
return False