#!/usr/bin/env python3
#
# BTZen - Bluetooh Smart sensor reading library.
#
# Copyright (C) 2015 by Artur Wroblewski <wrobell@pld-linux.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import argparse
import logging
import time

logging.basicConfig(level=logging.DEBUG)

import btzen

parser = argparse.ArgumentParser()
parser.add_argument('device', help='MAC address of device')
args = parser.parse_args()

print('connecting to {}...'.format(args.device))
dev = btzen.connect(args.device)
print('connected to {}'.format(dev.Name))

readers = {}
try:
    readers['pressure'] = btzen.Pressure(dev)
except ValueError as ex:
    print(ex)

try:
    readers['temperature'] = btzen.Temperature(dev)
except ValueError as ex:
    print(ex)
try:
    readers['humidity'] = btzen.Humidity(dev)
except ValueError as ex:
    print(ex)
try:
    readers['light'] = btzen.Light(dev)
except ValueError as ex:
    print(ex)

print('sensors initialized')

data = sorted([(n, r) for n, r in readers.items() if r])
while True:
    for n, r in data:
        print('{}: {:.1f}'.format(n, r.read()))
    print()
    time.sleep(1)

# vim: sw=4:et:ai
