From 9d35fb5002dd7b84c274026978a98bc9061f5387 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sat, 4 Jan 2025 19:44:13 +1300 Subject: [PATCH] Add config --- config.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 config.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..ab6d54b --- /dev/null +++ b/config.py @@ -0,0 +1,49 @@ +import json +import os + +default_config = { + 'color': (255, 0, 0), + 'brightness': 1.0, + 'color_format': 'RGB', + 'servers': [('192.168.0.201', 80)] +} + +config_file = 'config.json' + +class ConfigHandler: + def load_config(self): + """Load color, brightness, format, and server settings from config file.""" + if os.path.exists(config_file): + with open(config_file, 'r') as f: + return json.load(f) + else: + return default_config + + def save_config(self, color, brightness, color_format, servers): + """Save color, brightness, format, and server settings to config file.""" + config = { + 'color': color, + 'brightness': brightness, + 'color_format': color_format, + 'servers': servers + } + with open(config_file, 'w') as f: + json.dump(config, f) + print("Configuration saved.") + + def add_server(self, ip, port): + """Add a new server IP and port.""" + config = self.load_config() + config['servers'].append((ip, port)) + self.save_config(config['color'], config['brightness'], config['color_format'], config['servers']) + print(f"Server {ip}:{port} added.") + + def remove_server(self, ip, port): + """Remove an existing server IP and port.""" + config = self.load_config() + if (ip, port) in config['servers']: + config['servers'].remove((ip, port)) + self.save_config(config['color'], config['brightness'], config['color_format'], config['servers']) + print(f"Server {ip}:{port} removed.") + else: + print(f"Server {ip}:{port} not found.")