Made database file configurable
This commit is contained in:
		
							parent
							
								
									b0ae19db23
								
							
						
					
					
						commit
						901b3ca960
					
				
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							|  | @ -2,4 +2,5 @@ venv | |||
| .idea | ||||
| /test/clients.json | ||||
| *.pyc | ||||
| *.egg-info | ||||
| *.egg-info | ||||
| *.json | ||||
							
								
								
									
										9
									
								
								torch-sub.conf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								torch-sub.conf
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,9 @@ | |||
| ############################################################################## | ||||
| #  Sample torch-sub.conf file | ||||
| ############################################################################## | ||||
| 
 | ||||
| [mqtt] | ||||
| Topic=torch/+/wake              #Required | ||||
| 
 | ||||
| [database] | ||||
| Filename=torch_clients.json     #Optional; Default: clients.json | ||||
|  | @ -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, | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user