1
0
Fork 0
air/main.py

51 lines
1.7 KiB
Python
Raw Normal View History

2017-10-22 17:43:45 +09:00
from miio.airpurifier import AirPurifier, OperationMode, LedBrightness
2017-10-22 18:33:48 +09:00
from miio.discovery import create_device
2017-09-24 23:28:38 +09:00
from collections import namedtuple
from datetime import datetime
import time
from influxdb import InfluxDBClient
import json
from systemd import daemon
2017-10-22 18:33:48 +09:00
import socket
2017-09-24 23:28:38 +09:00
class Message(namedtuple('Message', ('measurement', 'time', 'tags', 'fields'))):
__slots__ = ()
class Atmosphere(namedtuple('Atmosphere', ('purifier_activated', 'purifier_fan_rpm', 'temperature', 'aqi', 'humidity'))):
__slots__ = ()
def __new__(cls, status):
return super(Atmosphere, cls).__new__(cls,
status.power == 'on',
int(status.motor_speed),
float(status.temperature),
int(status.aqi),
status.humidity / 100.0
)
def getStat(dev):
while True:
time.sleep(1)
yield time.time(), Atmosphere(dev.status())
if __name__ == '__main__':
try:
metric = InfluxDBClient('db', database='core',
use_udp=True, udp_port=8089)
2017-10-22 18:33:48 +09:00
air_addr=socket.gethostbyname("air")
#dev = create_device(air_addr,AirPurifier)
dev = AirPurifier(air_addr,'6e1e05b87d9f7cd10a3f8f43616896fa')
2017-09-24 23:28:38 +09:00
tags = {
'source': 'mi_air_2',
'position': 'center_room',
}
daemon.notify('READY=1')
for at, info in getStat(dev):
message = Message('air', int(at*1000000000), tags, info._asdict())
metric.write_points([message._asdict()])
finally:
daemon.notify('STOPPING=1')