diff --git a/README.md b/README.md index 5db4846..45ea834 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ The 'Configure LoRaWAN Gateway' section allows you to set up to ten Dragino LPS8  +The gateway configuration will automatically be saved to a file named 'gateway_config.json'. This file gets loaded each time the application runs. If you would like to save multiple configurations, copy the 'gateway_config.json' to a new file for later use. + ## Analysis Mode Analyze LoRa traffic received at 433, 868, or 915 MHz with ‘Analysis Mode’. Click the desired frequency to get started. Once connected to your LoRa receiver, traffic will automatically be streamed to the web page for analysis. To disconnect a receiver, click the 'Disconnect Serial Port' button. diff --git a/app.py b/app.py index e2d83ac..61974fc 100644 --- a/app.py +++ b/app.py @@ -14,6 +14,9 @@ from io import StringIO, BytesIO import csv import serial.tools.list_ports from ipaddress import ip_address, AddressValueError +import os +import json + app = Flask(__name__) socketio = SocketIO(app) @@ -41,6 +44,7 @@ frequency = lambda port: {'port1': 433, 'port2': 868,'port3': 915}.get(port, Non surveydata = {} parsed_entries = set() used_ports = set() +CONFIG_FILE = 'gateway_config.json' def read_serial_data(port, ser, buffer): """ @@ -728,25 +732,63 @@ def set_gateways(): global gateway_ips data = request.form - # Create an ordered dictionary of gateways + # Update gateway IPs ordered_gateways = {} for i in range(1, 11): key = f'gateway{i}' input_ip = data.get(key, '').strip() - ordered_gateways[key] = input_ip + ordered_gateways[key] = input_ip if input_ip else '' - # Update the global gateway_ips with ordered data gateway_ips.update(ordered_gateways) for gateway, ip_address in gateway_ips.items(): print(f"Gateway {gateway} has IP address: {ip_address}") - - return jsonify({"message": "Gateway IPs updated successfully"}), 200 + + # Save configuration + if save_gateway_config(): + return jsonify({"message": "Gateway IPs updated and saved successfully"}), 200 + else: + return jsonify({"message": "Gateway IPs updated but failed to save configuration"}), 200 @app.route('/get_gateways', methods=['GET']) def get_gateways(): + """ + Retrieves the current IP addresses of the gateways. + + This function handles the '/get_gateways' URL route and returns a JSON + response containing the IP addresses of the configured gateways. + + Returns: + Response: A JSON response with the current gateway IPs, where each key is + a gateway identifier and the value is the corresponding IP address. + """ return jsonify(gateway_ips) +def save_gateway_config(): + """ + Saves the current gateway configuration to a JSON file. + """ + try: + with open(CONFIG_FILE, 'w') as f: + json.dump(gateway_ips, f) + return True + except Exception as e: + print(f"Error saving gateway configuration: {e}") + return False + +def load_gateway_config(): + """ + Loads the gateway configuration from a JSON file. + """ + global gateway_ips + try: + if os.path.exists(CONFIG_FILE): + with open(CONFIG_FILE, 'r') as f: + loaded_config = json.load(f) + gateway_ips.update(loaded_config) + except Exception as e: + print(f"Error loading gateway configuration: {e}") + @app.route('/downloadPackets', methods=['GET']) def downloadPackets(): """ @@ -772,5 +814,6 @@ def downloadPackets(): if __name__ == '__main__': + load_gateway_config() Timer(30, parse_and_store_data).start() socketio.run(app, debug=True) diff --git a/doc/img/config.png b/doc/img/config.png index a6800b2..b3aeab8 100644 Binary files a/doc/img/config.png and b/doc/img/config.png differ diff --git a/doc/img/gateways.png b/doc/img/gateways.png index 0e2c332..5dba31f 100644 Binary files a/doc/img/gateways.png and b/doc/img/gateways.png differ diff --git a/templates/index.html b/templates/index.html index d712c8e..1c3c837 100644 --- a/templates/index.html +++ b/templates/index.html @@ -695,11 +695,11 @@
Empty values will be ignored, and the Gateway IP address will remain unchanged
+Empty values will erase that particular gateway IP address