#!/usr/bin/env python3
#
# BTZen - library to asynchronously access Bluetooth devices.
#
# Copyright (C) 2015-2022 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 typing as tp
import uvloop
from datetime import datetime

import btzen

#
# sensor definitions
#
SENSORS = [
    ('pressure', btzen.pressure),
    ('temperature', btzen.temperature),
    ('humidity', btzen.humidity),
    ('light', btzen.light_rgb),
    ('button', btzen.button),
]

async def read_data(mac: str, interface: str, interval: float) -> None:
    # initialize all Thingy52 sensors
    devices = [ctor(mac, make=btzen.Make.THINGY52) for _, ctor in SENSORS] # type: ignore
    devices = [btzen.set_interval(d, interval) for d in devices]

    battery = btzen.battery_level(mac)
    devices.append(battery)

    async with btzen.connect(devices, interface=interface) as session:
        items = zip(SENSORS, devices)
        tasks = [read_sensor(name, dev) for (name, _), dev in items]
        tasks.append(read_sensor('battery', battery))

        await asyncio.gather(session, *tasks)

async def read_sensor(name: str, sensor: btzen.DeviceTrigger[btzen.Service, tp.Any]) -> None:
    async for value in btzen.read_all(sensor):
        if isinstance(value, btzen.Button):
            print_data(name, '{}'.format(str(value)))
        elif isinstance(value, (float, int)):
            print_data(name, '{:.1f}'.format(value))
        else:
            print_data(name, '{}'.format(value))

def print_data(name: str, value: tp.Any) -> None:
    print('{} {}: {}'.format(datetime.now(), name, value))

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(
    '-t', '--interval', default=1.0, type=float,
    help='Sensor data read interval'
)
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)

# install uvloop for demonstration purposes
uvloop.install()

task = read_data(args.device, args.interface, args.interval)
asyncio.run(task)

# vim: sw=4:et:ai
