summaryrefslogtreecommitdiffstats
path: root/client/envsensors
blob: 6f45cdd9545fa0b13d153075fb2a3b6a71434091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
#
# Copyright (c) 2020 Michael Büsch <m@bues.ch>
#
# 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
bues.ch cgit interface