File: aranetctl.py

package info (click to toggle)
python-aranet4 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: python: 1,845; makefile: 5
file content (300 lines) | stat: -rw-r--r-- 9,504 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import argparse
import csv
from dataclasses import asdict
import datetime
from pathlib import Path
import sys
from time import sleep

import requests

from aranet4 import client

def parse_args(ctl_args):
    parser = argparse.ArgumentParser()
    parser.add_argument("device_mac", nargs="?", help="Aranet Bluetooth Address")
    parser.add_argument(
        "--scan", action="store_true", help="Scan for Aranet devices"
    )
    current = parser.add_argument_group("Options for current reading")
    current.add_argument(
        "-u", "--url", metavar="URL", help="Remote url for current value push"
    )
    parser.add_argument(
        "-r", "--records", action="store_true", help="Fetch historical log records"
    )
    history = parser.add_argument_group("Filter History Log Records")
    history.add_argument(
        "-s",
        "--start",
        metavar="DATE",
        type=datetime.datetime.fromisoformat,
        help="Records range start (UTC time, example: 2019-09-29T14:00:00",
    )
    history.add_argument(
        "-e",
        "--end",
        metavar="DATE",
        type=datetime.datetime.fromisoformat,
        help="Records range end (UTC time, example: 2019-09-30T14:00:00",
    )
    history.add_argument(
        "-o", "--output", metavar="FILE", type=Path, help="Save records to a file"
    )
    history.add_argument(
        "-w",
        "--wait",
        action="store_true",
        default=False,
        help="Wait until new data point available",
    )
    history.add_argument(
        "-l", "--last", metavar="COUNT", type=int, help="Get <COUNT> last records"
    )
    history.add_argument(
        "--xt",
        dest="temp",
        default=True,
        action="store_false",
        help="Don't get temperature records",
    )
    history.add_argument(
        "--xh",
        dest="humi",
        default=True,
        action="store_false",
        help="Don't get humidity records",
    )
    history.add_argument(
        "--xp",
        dest="pres",
        default=True,
        action="store_false",
        help="Don't get pressure records",
    )
    history.add_argument(
        "--xc",
        dest="co2",
        default=True,
        action="store_false",
        help="Don't get co2 records",
    )
    settings = parser.add_argument_group("Change device settings")
    settings.add_argument(
        "--set-interval",
        dest="set_interval",
        metavar="MINUTES",
        type=int,
        help="Change update interval"
    )
    settings.add_argument(
        "--set-integrations",
        dest="set_integrations",
        type=str,
        choices=["on", "off"],
        help="Toggle Smart Home Integrations"
    )
    settings.add_argument(
        "--set-range",
        dest="set_btrange",
        type=str,
        choices=["normal", "extended"],
        help="Change bluetooth range"
    )

    return parser.parse_args(ctl_args)


def print_records(records):
    """Format log records to be printed to screen"""
    char_repeat = 34
    if records.filter.incl_co2:
        char_repeat += 9
    if records.filter.incl_temperature:
        char_repeat += 7
    if records.filter.incl_humidity:
        char_repeat += 8
    if records.filter.incl_pressure:
        char_repeat += 11
    if records.filter.incl_rad_dose_rate:
        char_repeat += 11
    if records.filter.incl_rad_dose:
        char_repeat += 11
    if records.filter.incl_rad_dose_total:
        char_repeat += 12
    if records.filter.incl_radon_concentration:
        char_repeat += 8
    print("-" * char_repeat)
    print(f"{'Device Name':<15}: {records.name:>20}")
    print(f"{'Device Version':<15}: {records.version:>20}")
    print("-" * char_repeat)
    print(f"{'id': ^4} | {'date': ^25} |", end="")
    if records.filter.incl_co2:
        print(f" {'co2':^6} |", end="")
    if records.filter.incl_temperature:
        print(" temp |", end="")
    if records.filter.incl_humidity:
        print(" humid |", end="")
    if records.filter.incl_pressure:
        print(" pressure |", end="")
    if records.filter.incl_rad_dose:
        print(" rad_dose |", end="")
    if records.filter.incl_rad_dose_rate:
        print(" rad_rate |", end="")
    if records.filter.incl_rad_dose_total:
        print(" rad_total |", end="")
    if records.filter.incl_radon_concentration:
        print(f" {'radon':^5} |", end="")
    print("")
    print("-" * char_repeat)

    for record_id, line in enumerate(records.value, start=records.filter.begin):
        print(f"{record_id:>4d} | {line.date.isoformat()} |", end="")
        if records.filter.incl_co2:
            print(f" {line.co2:>6d} |", end="")
        if records.filter.incl_temperature:
            print(f" {line.temperature:>4.1f} |", end="")
        if records.filter.incl_humidity:
            print(f" {line.humidity:>5.1f} |", end="")
        if records.filter.incl_pressure:
            print(f" {line.pressure:>8.1f} |", end="")
        if records.filter.incl_rad_dose:
            print(f" {line.rad_dose/1000:>8.3f} |", end="")
        if records.filter.incl_rad_dose_rate:
            print(f" {line.rad_dose_rate/1000:>8.3f} |", end="")
        if records.filter.incl_rad_dose_total:
            print(f" {line.rad_dose_total/1000000:>9.4f} |", end="")
        if records.filter.incl_radon_concentration:
            print(f" {line.radon_concentration:>5d} |", end="")
        print("")
    print("-" * char_repeat)


def store_scan_result(advertisement):
    global found
    if not advertisement.device:
        return

    found[advertisement.device.address] = advertisement

def write_csv(filename, log_data):
    """
    Output `client.Record` dataclass to csv file
    :param filename: file name
    :param log_data: `client.Record` data object
    """
    with open(file=filename, mode="w", encoding="utf-8", newline="") as csv_file:
        fieldnames = ["date"]
        if log_data.filter.incl_co2:
            fieldnames.append("co2")
        if log_data.filter.incl_temperature:
            fieldnames.append("temperature")
        if log_data.filter.incl_humidity:
            fieldnames.append("humidity")
        if log_data.filter.incl_pressure:
            fieldnames.append("pressure")
        if log_data.filter.incl_rad_dose:
            fieldnames.append("rad_dose")
        if log_data.filter.incl_rad_dose_rate:
            fieldnames.append("rad_dose_rate")
        if log_data.filter.incl_rad_dose_total:
            fieldnames.append("rad_dose_total")
        if log_data.filter.incl_radon_concentration:
            fieldnames.append("radon_concentration")
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames, extrasaction="ignore")

        writer.writeheader()

        for line in log_data.value:
            writer.writerow(asdict(line))


def post_data(url, current):
    # get current measurement minute
    now = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0)
    delta_ago = datetime.timedelta(seconds=current.ago)
    t = now - delta_ago
    t = t.replace(second=0)  # epoch, floored to minutes
    data = current.toDict()
    data["time"] = t.timestamp()
    r = requests.post(
        url=url,
        data=data,
        timeout=300
    )
    print(f"Pushing data: {r.text}")


def wait_for_new_record(address):
    current_vals = client.get_current_readings(address)
    wait_time = current_vals.interval - current_vals.ago
    for secs in range(wait_time, 0, -1):
        sleep(1)
        print(f"Next data point in {secs}...", end="\r")


def main(argv):
    global found
    found = {}
    args = parse_args(argv)

    if args.scan:
        print("Looking for Aranet devices...")
        devices = client.find_nearby(store_scan_result)
        print(f"Scan finished. Found {len(devices)}")
        print()
        for _, advertisement in found.items():
            if advertisement.readings:
                print(advertisement.readings.toString(advertisement))
            else:
                print("=======================================")
                print(f"  Name:     {advertisement.device.name}")
                print(f"  Address:  {advertisement.device.address}")
                print(f"  RSSI:     {advertisement.rssi} dBm")
                print()
            print()

        return

    if not args.device_mac:
        print("Device address not specified")
        return

    if args.records:
        if args.wait:
            wait_for_new_record(args.device_mac)
        records = client.get_all_records(args.device_mac, vars(args), True)
        print_records(records)
        if args.output:
            write_csv(args.output, records)
    else:
        settings = {}

        if args.set_interval:
            settings["interval"] = args.set_interval

        if args.set_integrations:
            settings["integrations"] = args.set_integrations

        if args.set_btrange:
            settings["range"] = args.set_btrange

        if settings:
            result = client.set_settings(args.device_mac, settings, True)
            for k in result:
                val = settings[k]
                ret = "SUCCESS" if result[k] else "FAILED"
                print(f"Set {k} to \"{val}\": {ret}")
        else:
            current = client.get_current_readings(args.device_mac)
            print(current.toString())
            if args.url:
                post_data(args.url, current)


def entry_point():
    main(argv=sys.argv[1:])


if __name__ == "__main__":
    entry_point()