-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAcControlUnit.js
69 lines (62 loc) · 2.18 KB
/
AcControlUnit.js
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
const weatherApiKey = "8bfa3cba8138475a9db210137232804";
let cityName = "";
const tempSpan = document.getElementById('temperature');
const humiditySpan = document.getElementById('humidity');
const locationSpan = document.getElementById('location');
const timeSpan = document.getElementById('time');
const daySpan = document.getElementById('dayOfWeek');
let AC = 1;
let cond_temp = 0;
let cond_humidity = 0;
const acdiv = document.getElementsByClassName('ac-blows-air')[0];
function SetCityName(e){
cityName = e.target.value;
locationSpan.innerText = cityName;
console.log(cityName);
}
function SetTime(e){
timeInput = e.target.value;
timeSpan.innerText = timeInput;
}
function SetDayOfWeek(e){
dayInput = e.target.value;
daySpan.innerText = dayInput;
}
function handleClick(){
let url =`http://api.weatherapi.com/v1/current.json?key=${weatherApiKey} &q=${cityName}&aqi=no`;
fetch(url)
.then(res=> res.json())
.then(data => {
cond_temp = data.current.temp_c;
cond_humidity = data.current.humidity;
tempSpan.innerText = data.current.temp_f + "F";
humiditySpan.innerText = data.current.humidity + "%";
switchAC();
return data;
})
.catch(error => {
console.error(error);
});
}
function switchAC(){
if(timeInput<17 && timeInput>=8 && cond_temp >= 78){
AC = 1;
}else if(cond_humidity>85 && cond_temp >=78 && timeInput>=8 && timeInput<17){
AC = 1;
}else if(cond_humidity>85 && cond_temp >=78 && dayInput.toLowerCase()=="sunday" || dayInput.toLowerCase()=="saturday"){
AC = 1;
}else if(dayInput.toLowerCase()=="sunday" || dayInput.toLowerCase()=="saturday" && cond_humidity >= 85){
AC = 1;
}else{
AC = 0;
}
if(!AC){
acdiv.classList.add("acoff");
document.getElementById("acStatus").innerText = "AC Turned off: Saving Power";
console.log(document.getElementById("acStatus"));
}else{
acdiv.classList.remove("acoff");
console.log(document.getElementById("acStatus"));
document.getElementById("acStatus").innerText = "AC Turned on";
}
}