44import datetime
55from pathlib import Path
66import sys
7- from time import sleep
8-
97import requests
8+ from time import sleep
109
1110from aranet4 import client
1211
12+
1313def parse_args (ctl_args ):
1414 parser = argparse .ArgumentParser ()
15- parser .add_argument ("device_mac" , nargs = "?" , help = "Aranet Bluetooth Address" )
1615 parser .add_argument (
17- "--scan" , action = "store_true" , help = "Scan for Aranet devices"
16+ "device_mac" ,
17+ nargs = "?" ,
18+ help = "Aranet Bluetooth Address"
19+ )
20+ parser .add_argument (
21+ "--scan" ,
22+ action = "store_true" ,
23+ help = "Scan for Aranet devices"
1824 )
25+
1926 current = parser .add_argument_group ("Options for current reading" )
2027 current .add_argument (
21- "-u" , "--url" , metavar = "URL" , help = "Remote url for current value push"
28+ "-u" ,
29+ "--url" ,
30+ metavar = "URL" ,
31+ help = "Remote url for current value push"
2232 )
33+
2334 parser .add_argument (
24- "-r" , "--records" , action = "store_true" , help = "Fetch historical log records"
35+ "-r" ,
36+ "--records" ,
37+ action = "store_true" ,
38+ help = "Fetch historical log records"
2539 )
40+
2641 history = parser .add_argument_group ("Filter History Log Records" )
2742 history .add_argument (
2843 "-s" ,
@@ -39,7 +54,11 @@ def parse_args(ctl_args):
3954 help = "Records range end (UTC time, example: 2019-09-30T14:00:00" ,
4055 )
4156 history .add_argument (
42- "-o" , "--output" , metavar = "FILE" , type = Path , help = "Save records to a file"
57+ "-o" ,
58+ "--output" ,
59+ metavar = "FILE" ,
60+ type = Path ,
61+ help = "Save records to a file"
4362 )
4463 history .add_argument (
4564 "-w" ,
@@ -49,7 +68,11 @@ def parse_args(ctl_args):
4968 help = "Wait until new data point available" ,
5069 )
5170 history .add_argument (
52- "-l" , "--last" , metavar = "COUNT" , type = int , help = "Get <COUNT> last records"
71+ "-l" ,
72+ "--last" ,
73+ metavar = "COUNT" ,
74+ type = int ,
75+ help = "Get <COUNT> last records"
5376 )
5477 history .add_argument (
5578 "--xt" ,
@@ -148,7 +171,9 @@ def print_records(records):
148171 print ("" )
149172 print ("-" * char_repeat )
150173
151- for record_id , line in enumerate (records .value , start = records .filter .begin ):
174+ for record_id , line in enumerate (
175+ records .value , start = records .filter .begin
176+ ):
152177 print (f"{ record_id :>4d} | { line .date .isoformat ()} |" , end = "" )
153178 if records .filter .incl_co2 :
154179 print (f" { line .co2 :>6d} |" , end = "" )
@@ -170,20 +195,22 @@ def print_records(records):
170195 print ("-" * char_repeat )
171196
172197
173- def store_scan_result (advertisement ):
174- global found
198+ def store_scan_result (found , advertisement ):
175199 if not advertisement .device :
176200 return
177201
178202 found [advertisement .device .address ] = advertisement
179203
204+
180205def write_csv (filename , log_data ):
181206 """
182207 Output `client.Record` dataclass to csv file
183208 :param filename: file name
184209 :param log_data: `client.Record` data object
185210 """
186- with open (file = filename , mode = "w" , encoding = "utf-8" , newline = "" ) as csv_file :
211+ with open (
212+ file = filename , mode = "w" , encoding = "utf-8" , newline = ""
213+ ) as csv_file :
187214 fieldnames = ["date" ]
188215 if log_data .filter .incl_co2 :
189216 fieldnames .append ("co2" )
@@ -201,7 +228,9 @@ def write_csv(filename, log_data):
201228 fieldnames .append ("rad_dose_total" )
202229 if log_data .filter .incl_radon_concentration :
203230 fieldnames .append ("radon_concentration" )
204- writer = csv .DictWriter (csv_file , fieldnames = fieldnames , extrasaction = "ignore" )
231+ writer = csv .DictWriter (
232+ csv_file , fieldnames = fieldnames , extrasaction = "ignore"
233+ )
205234
206235 writer .writeheader ()
207236
@@ -234,13 +263,12 @@ def wait_for_new_record(address):
234263
235264
236265def main (argv ):
237- global found
238266 found = {}
239267 args = parse_args (argv )
240268
241269 if args .scan :
242270 print ("Looking for Aranet devices..." )
243- devices = client .find_nearby (store_scan_result )
271+ devices = client .find_nearby (lambda ad : store_scan_result ( found , ad ) )
244272 print (f"Scan finished. Found { len (devices )} " )
245273 print ()
246274 for _ , advertisement in found .items ():
0 commit comments