Made database file configurable

This commit is contained in:
B.J. Dweck 2020-10-16 15:36:34 +02:00
parent b0ae19db23
commit 901b3ca960
3 changed files with 22 additions and 5 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@ venv
.idea
/test/clients.json
*.pyc
*.egg-info
*.egg-info
*.json

9
torch-sub.conf Normal file
View File

@ -0,0 +1,9 @@
##############################################################################
# Sample torch-sub.conf file
##############################################################################
[mqtt]
Topic=torch/+/wake #Required
[database]
Filename=torch_clients.json #Optional; Default: clients.json

View File

@ -15,10 +15,6 @@ database_lock = threading.Lock()
def update_client_record(client, userdata, message):
database_lock.acquire()
if not os.path.exists(database_filename):
with open(database_filename, 'w') as database_blank:
json.dump({}, database_blank)
with open(database_filename, 'r') as infile:
database = json.load(infile)
@ -47,6 +43,7 @@ class Config:
self.broker_port = None
self.topic = None
self.tls = None
self.database_file = database_filename
parser = self.do_cli_argument_parsing()
(config_path, config_filename) = self.get_config_path(parser.parse_args())
print("Using torch configuration path: " + config_path)
@ -98,10 +95,20 @@ class Config:
'certfile': cert_file,
'keyfile': key_file
}
if config.has_section('database'):
self.database_file = config['database'].get('Filename', fallback=database_filename)
def main():
config = Config()
global database_filename
database_filename = config.database_file
if not os.path.exists(database_filename):
with open(database_filename, 'w') as database_blank:
json.dump({}, database_blank)
subscribe(config.broker_hostname,
config.broker_port,
config.topic,