-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.html
112 lines (98 loc) · 3.23 KB
/
settings.html
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Settings</title>
<style>
body {
background-color: #333;
color: white;
font-family: Arial, sans-serif;
padding: 20px;
}
label, input {
margin-bottom: 10px;
}
.disabled {
background-color: #555;
}
.styled-button {
background-color: #2196F3;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 10px 0;
cursor: pointer;
border-radius: 12px;
width: 100%; /* Full-width button */
}
.styled-button:hover {
background-color: #1e88e5; /* Darker color on hover */
}
</style>
</head>
<body>
<h1>Settings</h1>
<form id="settingsForm">
<label for="ssid">Wi-Fi SSID:</label><br>
<input type="text" id="ssid" name="ssid"><br>
<label for="wifi_password">Wi-Fi Password:</label><br>
<input type="password" id="wifi_password" name="wifi_password"><br><br>
<label for="enable_mqtt">Enable MQTT:</label>
<input type="checkbox" id="enable_mqtt" name="mqtt_enabled" onclick="toggleMQTTFields()"><br><br>
<fieldset id="mqtt_fields" disabled>
<label for="mqtt_server">MQTT Server IP:</label><br>
<input type="text" id="mqtt_server" name="mqtt_server"><br>
<label for="mqtt_username">MQTT Username:</label><br>
<input type="text" id="mqtt_username" name="mqtt_username"><br>
<label for="mqtt_password">MQTT Password:</label><br>
<input type="password" id="mqtt_password" name="mqtt_password"><br><br>
</fieldset>
<button type="button" class="styled-button" onclick="saveSettings()">Save Settings</button>
</form>
<script>
function toggleMQTTFields() {
const mqttEnabled = document.getElementById('enable_mqtt').checked;
const mqttFields = document.getElementById('mqtt_fields');
mqttFields.disabled = !mqttEnabled;
}
function saveSettings() {
const formData = new FormData(document.getElementById('settingsForm'));
const jsonData = {};
formData.forEach((value, key) => {
jsonData[key] = value;
});
// Handle the checkbox for MQTT explicitly
jsonData['mqtt_enabled'] = document.getElementById('enable_mqtt').checked ? 1 : 0;
fetch('/save-settings', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
})
.then(response => {
if (response.ok) {
alert('Settings saved successfully! The device will restart now.');
fetch('/restart', { method: 'POST' })
.then(restartResponse => {
if (restartResponse.ok) {
console.log('Device restarting...');
} else {
alert('Failed to restart device.');
}
})
.catch(error => console.error('Error restarting device:', error));
} else {
alert('Failed to save settings.');
}
})
.catch(error => console.error('Error saving settings:', error));
}
</script>
</body>
</html>