-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_config.py
78 lines (67 loc) · 2.81 KB
/
sync_config.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
import requests
import json
import time
import sys
import logging
# Configuration
GCP_HOST = "34.66.128.13"
GCP_PORT = 5000
LOCAL_PATH = "/home/polyppi/raspberrypi-firmware/config.json"
LOG_FILE = "/var/log/sync_config.log"
# Configure logging
logging.basicConfig(filename=LOG_FILE, level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
logger = logging.getLogger()
def send_discord_message(webhook_url, message):
data = {"content": message}
response = requests.post(webhook_url, json=data)
if response.status_code != 204:
logger.error(f"Failed to send message to Discord: {response.status_code}, {response.text}")
else:
logger.info(f"Successfully sent message to Discord: {message}")
def load_local_config():
try:
with open(LOCAL_PATH, 'r') as f:
return json.load(f)
except FileNotFoundError:
logger.error(f'Configuration file not found: {LOCAL_PATH}')
return {"MAX_VOLUME": 100, "DENSITY": "medium"} # Default values
def sync_config(webhook_url):
url = f'http://{GCP_HOST}:{GCP_PORT}/get_config'
try:
logger.info(f"Fetching config from {url}")
response = requests.get(url, timeout=10) # Set a timeout
if response.status_code == 200:
config = response.json()
logger.info(f"Received config: {config}")
local_config = load_local_config()
logger.info(f"Local config: {local_config}")
config_updated = False
# Check for MAX_VOLUME changes
if config.get('MAX_VOLUME') != local_config.get('MAX_VOLUME'):
local_config['MAX_VOLUME'] = config['MAX_VOLUME']
config_updated = True
# send_discord_message(webhook_url, "Volume Change Received by Pi!")
# Check for DENSITY changes
if config.get('DENSITY') != local_config.get('DENSITY'):
local_config['DENSITY'] = config['DENSITY']
config_updated = True
# send_discord_message(webhook_url, "Density Change Received by Pi!")
# Update the local config if any changes were detected
if config_updated:
with open(LOCAL_PATH, 'w') as f:
json.dump(local_config, f, indent=4)
logger.info(f"Config updated: {local_config}")
else:
logger.info("No changes in configuration")
else:
logger.error(f"Failed to fetch config: {response.status_code}")
except Exception as e:
logger.error(f"Error: {e}")
if __name__ == '__main__':
if len(sys.argv) != 2:
logger.error("Usage: sync_config.py <discord_webhook_url>")
sys.exit(1)
webhook_url = sys.argv[1]
while True:
sync_config(webhook_url)
time.sleep(20) # Check for updates every 20 seconds