-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
213 lines (167 loc) · 5.9 KB
/
main.py
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
# from miflora.miflora_poller import MiFloraPoller
# from btlewrap.bluepy import BluepyBackend
# import configparser
# config = configparser.ConfigParser()
# config.read('config.ini')
# print(config.sections())
# for i in config['Connector']:
# print(i)
# poller = MiFloraPoller(config['Connector'][i], BluepyBackend)
# print(poller)
# poller.battery_level()
# for r in poller.fetch_history():
# print(f'device_time: {r.device_time}')
# print(f'conducitivty: {r.conductivity}')
# print(f'light: {r.light}')
# print(f'moisture: {r.moisture}')
# print(f'temperature: {r.temperature}')
# print(f'wall_time: {r.wall_time}')
#!/usr/bin/env python3
"""Demo file showing how to use the miflora library."""
import argparse
import logging
import re
import sys
from btlewrap import BluepyBackend, GatttoolBackend, PygattBackend, available_backends
from btlewrap.bluepy import BluepyBackend
from miflora import miflora_scanner
from miflora.miflora_poller import (
MI_BATTERY,
MI_CONDUCTIVITY,
MI_LIGHT,
MI_MOISTURE,
MI_TEMPERATURE,
MiFloraPoller,
)
def valid_miflora_mac(
mac, pat=re.compile(r"C4:7C:8D:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}")
):
"""Check for valid mac adresses."""
if not pat.match(mac.upper()):
raise argparse.ArgumentTypeError(
f'The MAC address "{mac}" seems to be in the wrong format'
)
return mac
def poll(args):
"""Poll data from the sensor."""
backend = _get_backend(args)
poller = MiFloraPoller(args.mac, backend)
print("Getting data from Mi Flora")
print(f"FW: {poller.firmware_version()}")
print(f"Name: {poller.name()}")
print("Temperature: {}".format(poller.parameter_value(MI_TEMPERATURE)))
print("Moisture: {}".format(poller.parameter_value(MI_MOISTURE)))
print("Light: {}".format(poller.parameter_value(MI_LIGHT)))
print("Conductivity: {}".format(poller.parameter_value(MI_CONDUCTIVITY)))
print("Battery: {}".format(poller.parameter_value(MI_BATTERY)))
def scan(args):
"""Scan for sensors."""
backend = _get_backend(args)
print("Scanning for 10 seconds...")
devices = miflora_scanner.scan(backend, 10)
print("Found {} devices:".format(len(devices)))
for device in devices:
print(f" {device}")
def _get_backend(args):
"""Extract the backend class from the command line arguments."""
if args.backend == "gatttool":
backend = GatttoolBackend
elif args.backend == "bluepy":
backend = BluepyBackend
elif args.backend == "pygatt":
backend = PygattBackend
else:
raise Exception(f"unknown backend: {args.backend}")
return backend
def list_backends(_):
"""List all available backends."""
backends = [b.__name__ for b in available_backends()]
print("\n".join(backends))
def history(args):
"""Read the history from the sensor."""
backend = _get_backend(args)
print("Getting history from sensor...")
poller = MiFloraPoller(args.mac, backend)
history_list = poller.fetch_history()
print("History returned {} entries.".format(len(history_list)))
for entry in history_list:
print(f"History from {entry.wall_time}")
print(f" Temperature: {entry.temperature}")
print(f" Moisture: {entry.moisture}")
print(f" Light: {entry.light}")
print(f" Conductivity: {entry.conductivity}")
def clear_history(args):
"""Clear the sensor history."""
backend = _get_backend(args)
print("Deleting sensor history data...")
poller = MiFloraPoller(args.mac, backend)
poller.clear_history()
def main():
"""Main function.
Mostly parsing the command line arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--backend", choices=["gatttool", "bluepy", "pygatt"], default="bluepy"
)
parser.add_argument("-v", "--verbose", action="store_const", const=True)
subparsers = parser.add_subparsers(help="sub-command help")
parser_poll = subparsers.add_parser("poll", help="poll data from a sensor")
parser_poll.add_argument("mac", type=valid_miflora_mac)
parser_poll.set_defaults(func=poll)
parser_scan = subparsers.add_parser("scan", help="scan for devices")
parser_scan.set_defaults(func=scan)
parser_scan = subparsers.add_parser("backends", help="list the available backends")
parser_scan.set_defaults(func=list_backends)
parser_history = subparsers.add_parser("history", help="get device history")
parser_history.add_argument("mac", type=valid_miflora_mac)
parser_history.set_defaults(func=history)
parser_history = subparsers.add_parser("clear-history", help="clear device history")
parser_history.add_argument("mac", type=valid_miflora_mac)
parser_history.set_defaults(func=clear_history)
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
if not hasattr(args, "func"):
parser.print_help()
sys.exit(0)
args.func(args)
if __name__ == "__main__":
main()
client = InfluxDBClient(host='localhost', port=8086)
json_body = [
{
"measurement": "brushEvents",
"tags": {
"user": "Carol",
"brushId": "6c89f539-71c6-490d-a28d-6c5d84c0ee2f"
},
"time": "2018-03-28T8:01:00Z",
"fields": {
"duration": 127
}
},
{
"measurement": "brushEvents",
"tags": {
"user": "Carol",
"brushId": "6c89f539-71c6-490d-a28d-6c5d84c0ee2f"
},
"time": "2018-03-29T8:04:00Z",
"fields": {
"duration": 132
}
},
{
"measurement": "brushEvents",
"tags": {
"user": "Carol",
"brushId": "6c89f539-71c6-490d-a28d-6c5d84c0ee2f"
},
"time": "2018-03-30T8:02:00Z",
"fields": {
"duration": 129
}
}
]
client.write_points(json_body)