10
10
from influxdb_logger import InfluxdbLogger
11
11
from pms7003 import PMS7003 , PMSData
12
12
13
+
13
14
def get_breakpoint (pm25 : float ) -> Tuple [str , str ]:
14
15
"""get colorized breakpoint for the pm25 value"""
15
16
if pm25 < 15.5 :
@@ -25,6 +26,7 @@ def get_breakpoint(pm25: float) -> Tuple[str, str]:
25
26
else :
26
27
return "Hazardous" , Fore .MAGENTA
27
28
29
+
28
30
def print_debug (data : PMSData ) -> None :
29
31
"""print the entire PMSData structure to the console"""
30
32
print (
@@ -36,9 +38,7 @@ def print_debug(data: PMSData) -> None:
36
38
)
37
39
print ("PM 1.0 (CF=1) : %s\t | PM 1.0 : %s" % (data .pm1_0_cf1 , data .pm1_0_atm ))
38
40
print ("PM 2.5 (CF=1) : %s\t | PM 2.5 : %s" % (data .pm2_5_cf1 , data .pm2_5_atm ))
39
- print (
40
- "PM 10.0 (CF=1) : %s\t | PM 10.0 : %s" % (data .pm10_0_cf1 , data .pm10_0_atm )
41
- )
41
+ print ("PM 10.0 (CF=1) : %s\t | PM 10.0 : %s" % (data .pm10_0_cf1 , data .pm10_0_atm ))
42
42
print ("0.3um in 0.1L of air : %s" % (data .count_0_3 ))
43
43
print ("0.5um in 0.1L of air : %s" % (data .count_0_5 ))
44
44
print ("1.0um in 0.1L of air : %s" % (data .count_1_0 ))
@@ -52,6 +52,7 @@ def print_debug(data: PMSData) -> None:
52
52
"============================================================================"
53
53
)
54
54
55
+
55
56
def print_pm (data : PMSData ) -> None :
56
57
"""print PM values to the console"""
57
58
aqi , style = get_breakpoint (data .pm2_5_atm )
@@ -63,9 +64,10 @@ def print_pm(data: PMSData) -> None:
63
64
"AQI" : f"{ style } { aqi } { Style .RESET_ALL } " ,
64
65
}
65
66
66
- pairs = [f"{ k } : { v } " for k ,v in result .items ()]
67
+ pairs = [f"{ k } : { v } " for k , v in result .items ()]
67
68
click .echo (" " .join (pairs ))
68
69
70
+
69
71
@click .command ()
70
72
@click .option (
71
73
"--port" ,
@@ -75,23 +77,36 @@ def print_pm(data: PMSData) -> None:
75
77
show_default = True ,
76
78
)
77
79
@click .option (
78
- "--debug" ,
80
+ "--debug/--no-debug " ,
79
81
default = False ,
80
82
help = "Print debug data from the device" ,
81
83
show_default = True ,
82
84
)
83
- def main (port : str , debug : bool ) -> None :
85
+ @click .option (
86
+ "--log-only/--no-log-only" ,
87
+ default = False ,
88
+ help = "Only log to the influxdb log file; nothing on stdout" ,
89
+ )
90
+ @click .option (
91
+ "--log-path" ,
92
+ default = "measurements.log" ,
93
+ help = "Location where logs are written" ,
94
+ show_default = True ,
95
+ )
96
+ def main (
97
+ port : str , debug : bool , log_only : bool , log_path : str
98
+ ) -> None :
84
99
if not os .access (port , mode = os .R_OK , follow_symlinks = True ):
85
100
click .echo (
86
101
f"{ Fore .RED } cannot access { port } ; check path and permissions" , err = True
87
102
)
88
103
return
89
104
90
- logger = InfluxdbLogger ()
91
- tags = {' type' : ' PMS7003' , 'id' : port }
105
+ logger = InfluxdbLogger (log_path )
106
+ tags = {" type" : " PMS7003" , "id" : port }
92
107
click .echo (
93
108
f"{ Fore .BLUE } "
94
- f"writing influxdb measurement { logger .MEASUREMENT } to { logger .LOG_OUTPUT_FILE } "
109
+ f"writing influxdb measurement { logger .MEASUREMENT } to { logger .path } "
95
110
f"{ Style .RESET_ALL } "
96
111
)
97
112
@@ -103,13 +118,12 @@ def main(port: str, debug: bool) -> None:
103
118
if debug :
104
119
print_verbose (data )
105
120
else :
106
- print_pm (data )
107
121
logger .emit (
108
- fields = {k :v for k ,v in data ._asdict ().items () if k .startswith ('pm' )},
122
+ fields = {k : v for k , v in data ._asdict ().items () if k .startswith ("pm" )},
109
123
tags = tags ,
110
124
)
111
-
112
- time . sleep ( 1 )
125
+ if not log_only :
126
+ print_pm ( data )
113
127
114
128
if __name__ == "__main__" :
115
129
main ()
0 commit comments