#!/usr/bin/env python3 # # Copyright (c) 2020 Michael Büsch # # 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 2 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, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # import argparse import asyncio import sys from envclient import EnvsensorsClient, EnvsensorsError async def amain(): p = argparse.ArgumentParser(description="Envsensors GATT client.") p.add_argument("-a", "--address", type=str, help="Bluetooth address of the device.") p.add_argument("-D", "--dump", action="store_true", help="Dump the device attributes to stdout.") args = p.parse_args() actionCount = 0 async def runOne(address): nonlocal actionCount async with EnvsensorsClient(address) as sensor: if args.dump: print(await sensor.dump()) actionCount += 1 try: if args.address: await runOne(args.address) else: found = False async for address in EnvsensorsClient.discover(): await runOne(address) found = True if not found: raise EnvsensorsError("No Envsensors device found.") if not actionCount: raise EnvsensorsError("No action specified.") except EnvsensorsError as e: print(f"ERROR: {e}", file=sys.stderr) return 1 return 0 if __name__ == "__main__": sys.exit(asyncio.run(amain())) # vim: ts=4 sw=4 expandtab