-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
230 lines (209 loc) · 7.44 KB
/
index.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Smart Home Climate Control</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
}
input {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
button {
padding: 10px 15px;
background-color: #007BFF;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.response {
margin-top: 20px;
}
.indicator {
padding: 10px;
color: white;
font-weight: bold;
}
.ac-on {
background-color: #28a745; /* Green */
}
.ac-off {
background-color: #dc3545; /* Red */
}
</style>
</head>
<body>
<h1>Smart Home Climate Control</h1>
<div class="form-group">
<label for="current-temperature">Current Temperature (°C):</label>
<input type="text" id="current-temperature" disabled>
</div>
<div class="form-group">
<label for="current-fan-speed">Current Fan Speed:</label>
<input type="text" id="current-fan-speed" disabled>
</div>
<div class="form-group">
<label>Adjust Temperature (°C):</label>
<button onclick="adjustTemperature(1)">Increase</button>
<button onclick="adjustTemperature(-1)">Decrease</button>
</div>
<div class="form-group">
<label>Set Fan Speed:</label>
<button onclick="setFanSpeed('low')">Low</button>
<button onclick="setFanSpeed('medium')">Medium</button>
<button onclick="setFanSpeed('high')">High</button>
</div>
<div class="form-group">
<label>AC Status:</label>
<div id="ac-status" class="indicator ac-off">AC Off</div>
</div>
<div class="form-group">
<button onclick="toggleAC()">Toggle AC</button>
</div>
<div class="response" id="response"></div>
<script>
const apiUrl = 'http://127.0.0.1:5000';
let currentACStatus = 'Off'; // Keep track of the current AC status
async function getTemperature() {
try {
const response = await fetch(`${apiUrl}/temperature`);
const data = await response.json();
document.getElementById('current-temperature').value = data.current_temperature;
// Automatically control the AC based on the temperature
if (data.current_temperature > 25 && currentACStatus === 'Off') {
await toggleAC(true);
} else if (data.current_temperature < 20 && currentACStatus === 'On') {
await toggleAC(false);
}
} catch (error) {
console.error('Error fetching temperature:', error);
}
}
async function getFanSpeed() {
try {
const response = await fetch(`${apiUrl}/fan_speed`);
const data = await response.json();
document.getElementById('current-fan-speed').value = data.fan_speed;
} catch (error) {
console.error('Error fetching fan speed:', error);
}
}
async function getACStatus() {
try {
const response = await fetch(`${apiUrl}/ac_status`);
const data = await response.json();
currentACStatus = data.ac_status; // Update the local status
const acStatusElement = document.getElementById('ac-status');
if (currentACStatus === 'On') {
acStatusElement.textContent = 'AC On';
acStatusElement.classList.remove('ac-off');
acStatusElement.classList.add('ac-on');
} else {
acStatusElement.textContent = 'AC Off';
acStatusElement.classList.remove('ac-on');
acStatusElement.classList.add('ac-off');
}
} catch (error) {
console.error('Error fetching AC status:', error);
}
}
async function adjustTemperature(amount) {
try {
let currentTemperature = parseFloat(document.getElementById('current-temperature').value);
const newTemperature = currentTemperature + amount;
// Restrict the temperature range to 16°C - 32°C
if (newTemperature < 16) {
document.getElementById('response').innerText = 'Temperature cannot go below 16°C.';
return;
}
if (newTemperature > 32) {
document.getElementById('response').innerText = 'Temperature cannot go above 32°C.';
return;
}
const response = await fetch(`${apiUrl}/update_temperature`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
temperature: newTemperature
})
});
const data = await response.json();
document.getElementById('response').innerText = data.message;
getTemperature();
} catch (error) {
console.error('Error adjusting temperature:', error);
}
}
async function setFanSpeed(speed) {
try {
const response = await fetch(`${apiUrl}/set_fan_speed`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
fan_speed: speed
})
});
const data = await response.json();
document.getElementById('response').innerText = data.message;
getFanSpeed();
} catch (error) {
console.error('Error setting fan speed:', error);
}
}
async function toggleAC(turnOn = null) {
try {
const response = await fetch(`${apiUrl}/toggle_ac`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ turn_on: turnOn })
});
const data = await response.json();
currentACStatus = data.ac_status; // Update the local status
document.getElementById('response').innerText = `AC Status: ${data.ac_status}`;
getACStatus();
} catch (error) {
console.error('Error toggling AC:', error);
}
}
function startPolling() {
getTemperature();
getFanSpeed();
getACStatus();
setInterval(() => {
getTemperature();
getFanSpeed();
getACStatus();
}, 2000); // Poll every 2 seconds
}
document.addEventListener('DOMContentLoaded', () => {
startPolling();
});
</script>
</body>
</html>