-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpower_monitor.py
120 lines (103 loc) · 4.45 KB
/
power_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
#!/usr/bin/python3
#
# Author: sascha_lammers@gmx.de
#
import logging
import sys
import os
import socket
import argparse
import threading
from pathlib import Path
from Config import (JsonWriter, YamlWriter)
from PowerMonitor.MainApp import MainApp
from PowerMonitor import AppConfig
from PowerMonitor.Config import Config
from SDL_Pi_INA3221 import INA3211_CONFIG
if 'win' in sys.platform:
home_dir = os.environ.get('APPDATA')
else:
home_dir = os.path.expanduser('~')
config_dir = os.path.realpath(os.path.join(home_dir, '.power_monitor'))
parser = argparse.ArgumentParser(description='Power Monitor')
parser.add_argument('-C', '--config-dir', help='location of config.json and energy.json', type=str, default=config_dir)
parser.add_argument('--display', help='override DISPLAY variable', type=str, default=None)
parser.add_argument('--headless', help='start without GUI', action='store_true', default=None)
parser.add_argument('--fullscreen', help='start in fullscreen mode', action='store_true', default=None)
parser.add_argument('--daemon', help='run as daemon', action='store_true', default=None)
parser.add_argument('--fast-mode', help='enable fast mode', action='store_true', default=None)
parser.add_argument('--verbose', help='enable debug output', action='store_true', default=None)
parser.add_argument('--check', help='check configuration', action='store_true', default=None)
parser.add_argument('--print', help='check and display configuration', choices=['json', 'yaml', 'raw'], default=None)
parser.add_argument('--section', help='config section to display', type=str, default=None)
parser.add_argument('--key', help='config key(s) to display', action="append", nargs='*', default=None)
parser.add_argument('--debug', help='enable debug mode', action='store_true', default=None)
parser.add_argument('--ignore-warnings', help='number of warnings to ignore and continue', type=int, default=0)
args = parser.parse_args()
if args.debug:
args.verbose = True
if args.print:
args.check = True
AppConfig.Mqtt.device_name = socket.gethostname()
AppConfig.Influxdb.tags_host = socket.gethostname()
AppConfig.config_dir = args.config_dir
try:
config = Config(args.config_dir)
root_object = config.load(config.get_filename('config.json'))
except Exception as e:
if args.debug:
raise e
parser.error('failed to load configuration: %s' % e)
setattr(sys.modules[AppConfig.App.__module__], AppConfig.App.__class__.__qualname__, root_object)
AppConfig = root_object
AppConfig.channels = dict(zip(range(0, 3), list(AppConfig.channels)))
setattr(AppConfig, 'get_filename', config.get_filename)
AppConfig.ignore_warnings = args.ignore_warnings
if args.verbose!=None:
AppConfig.verbose = args.verbose
if args.headless!=None:
AppConfig.headless = args.headless
if args.daemon!=None:
AppConfig.daemon = args.daemon
if args.display!=None:
AppConfig.gui.display = args.display
if args.fullscreen!=None:
AppConfig.gui.fullscreen = args.fullscreen
if args.fast_mode:
AppConfig.ignore_warnings += 1
AppConfig.ina3221.averaging_mode = INA3211_CONFIG.AVERAGING_MODE.x1
AppConfig.ina3221.vshunt_conversion_time = INA3211_CONFIG.VSHUNT_CONVERSION_TIME.time_140_us
AppConfig.ina3221.vshunt_conversion_time = INA3211_CONFIG.VBUS_CONVERSION_TIME.time_140_us
print('Fast mode enabled')
AppConfig._debug = args.debug
default_handler = logging.StreamHandler(stream=sys.stdout)
default_handler.setLevel(AppConfig.verbose and logging.DEBUG or logging.INFO)
logger = logging.getLogger('power_monitor')
logger.setLevel(logging.DEBUG)
try:
import colorlog
default_handler.setFormatter(colorlog.ColoredFormatter('%(log_color)s%(levelname)s:%(message)s'))
# default_handler.setFormatter(colorlog.ColoredFormatter('%(log_color)s%(levelname)s:%(name)s:%(message)s'))
default_handler.setFormatter(formatter)
except:
pass
logger.addHandler(default_handler)
if AppConfig.headless!=True:
if AppConfig.gui.display=='$DISPLAY':
AppConfig.gui.display = os.environ.get('DISPLAY')
if not AppConfig.gui.display:
logger.error('DISPLAY not set, forcing headless mode')
AppConfig.headless = True
else:
os.environ['DISPLAY'] = AppConfig.gui.display
if args.check:
if args.print==None:
print('OK')
else:
config.print_config(args.print, (args.section, args.key and args.key[0] or None))
sys.exit(0)
app = MainApp(logger, AppConfig)
if AppConfig.daemon:
app.daemonize(True)
else:
app.daemonize(False)