#!/usr/bin/env python3
#
# BTZen - library to asynchronously access Bluetooth devices.
#
# Copyright (C) 2015-2020 by Artur Wroblewski <wrobell@riseup.net>
#
# 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 asyncio
import logging
import uvloop
from contextlib import asynccontextmanager
from datetime import datetime

import btzen
import btzen.sensortag

@asynccontextmanager
async def read_data(dev):
    while True:
        try:
            value = await dev.read()
        except asyncio.CancelledError:
            print('{}: cancelled'.format(dev))
        else:
            yield value
            break

async def read_sensor(name, sensor):
    while True:
        async with read_data(sensor) as value:
            print_data(name, '{:.1f}'.format(value))

        await asyncio.sleep(-loop.time() % 1)

async def read_accelerometer(name, sensor):
    sensor.set_interval(0.1)
    while True:
        async with read_data(sensor) as values:
            values = ', '.join('{:.4f}'.format(v) for v in values)
            print_data(name, values)

async def read_button(name, button):
    while True:
        async with read_data(button) as value:
            print_data('button', value)

async def battery_level(battery):
    while True:
        async with read_data(battery) as value:
            print_data('battery level', value)

        await asyncio.sleep(60)

def print_data(name, value):
    print('{} {}: {}'.format(datetime.now(), name, value))


#
# sensor definitions
#
SENSORS = [
    ('pressure', btzen.sensortag.Pressure, False, read_sensor),
    ('temperature', btzen.sensortag.Temperature, False, read_sensor),
    ('humidity', btzen.sensortag.Humidity, False, read_sensor),
    ('light', btzen.sensortag.Light, False, read_sensor),
    # notifying readers
    ('button', btzen.sensortag.Button, True, read_button),
    ('accelerometer', btzen.sensortag.Accelerometer, True, read_accelerometer),
]

parser = argparse.ArgumentParser()
parser.add_argument(
    '--verbose', default=False, action='store_true',
    help='show debug log'
)
parser.add_argument(
    '-i', '--interface', default='hci0',
    help='Host controller interface (HCI)'
)
parser.add_argument('device', help='MAC address of device')
args = parser.parse_args()

level = logging.DEBUG if args.verbose else logging.INFO
logging.basicConfig(level=level)

# get the loop first
uvloop.install()
loop = asyncio.get_event_loop()

# initialize all Sensor Tag sensors
items = [
    (name, reader, cls(args.device, notifying=notifying))
    for name, cls, notifying, reader in SENSORS
]
sensors =  [sensor for _, _, sensor in items]

# create tasks reading the sensor data
tasks = [reader(name, sensor) for name, reader, sensor in items]

battery = btzen.BatteryLevel(args.device, notifying=True)
tasks.append(battery_level(battery))

# create connection manager
manager = btzen.ConnectionManager(interface=args.interface)
manager.add(battery, *sensors)

try:
    loop.run_until_complete(asyncio.gather(manager, *tasks))
finally:
    # connection manager closes all sensors
    manager.close()

# vim: sw=4:et:ai
