-
Notifications
You must be signed in to change notification settings - Fork 22
/
solar-monitor.py
executable file
·132 lines (103 loc) · 3.44 KB
/
solar-monitor.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
#!/usr/bin/env python3
from argparse import ArgumentParser
import configparser
import time
import logging
import sys
from solardevice import SolarDeviceManager, SolarDevice
from datalogger import DataLogger
import duallog
# Read arguments
arg_parser = ArgumentParser(description="Solar Monitor")
arg_parser.add_argument(
'--adapter',
help="Name of Bluetooth adapter. Overrides what is set in .ini")
arg_parser.add_argument(
'-d', '--debug', action='store_true',
help="Enable debug")
arg_parser.add_argument(
'--ini',
help="Path to .ini-file. Defaults to 'solar-monior.ini'")
args = arg_parser.parse_args()
# Read config
config = configparser.ConfigParser()
ini_file = "solar-monitor.ini"
if args.ini:
ini_file = args.ini
try:
config.read(ini_file)
except:
print(f"Unable to read ini-file {ini_file}")
sys.exit(1)
if args.adapter:
config.set('monitor', 'adapter', args.adapter)
if args.debug:
config.set('monitor', 'debug', '1')
# Set up logging
if config.getboolean('monitor', 'debug', fallback=False):
print("Debug enabled")
level = logging.DEBUG
else:
level = logging.INFO
duallog.setup('solar-monitor', minLevel=level, fileLevel=level, rotation='daily', keep=30)
# Set up data logging
# datalogger = None
try:
datalogger = DataLogger(config)
except Exception as e:
logging.error("Unable to set up datalogger")
logging.error(e)
sys.exit(1)
# Set up device manager and adapter
device_manager = SolarDeviceManager(adapter_name=config['monitor']['adapter'])
logging.info("Adapter status - Powered: {}".format(device_manager.is_adapter_powered))
if not device_manager.is_adapter_powered:
logging.info("Powering on the adapter ...")
device_manager.is_adapter_powered = True
logging.info("Powered on")
# Run discovery
device_manager.update_devices()
logging.info("Starting discovery...")
# scan all the advertisements from the services list
device_manager.start_discovery()
discovering = True
wait = 15
found = []
# delay / sleep for 10 ~ 15 sec to complete the scanning
while discovering:
time.sleep(1)
f = len(device_manager.devices())
logging.debug("Found {} BLE-devices so far".format(f))
found.append(f)
if len(found) > 5:
if found[len(found) - 5] == f:
# We did not find any new devices the last 5 seconds
discovering = False
wait = wait - 1
if wait == 0:
discovering = False
device_manager.stop_discovery()
logging.info("Found {} BLE-devices".format(len(device_manager.devices())))
for dev in device_manager.devices():
logging.debug("Processing device {} {}".format(dev.mac_address, dev.alias()))
for section in config.sections():
if config.get(section, "mac", fallback=None) and config.get(section, "type", fallback=None):
mac = config.get(section, "mac").lower()
if dev.mac_address.lower() == mac:
logging.info("Trying to connect to {}...".format(dev.mac_address))
try:
device = SolarDevice(mac_address=dev.mac_address, manager=device_manager, logger_name=section, config=config, datalogger=datalogger)
except Exception as e:
logging.error(e)
continue
device.connect()
logging.info("Terminate with Ctrl+C")
try:
device_manager.run()
except KeyboardInterrupt:
pass
for dev in device_manager.devices():
try:
dev.disconnect()
except:
pass