-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
118 lines (108 loc) · 3.56 KB
/
index.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
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
getdata()
function water()
{
requestt("https://api.thingspeak.com/update?api_key=SUY4417W22YI4RKQ&field2=12")
console.log('sent water data')
gettime1()
}
function food()
{
requestt("https://api.thingspeak.com/update?api_key=SUY4417W22YI4RKQ&field1=4")
console.log('sent food data')
gettime2()
}
function requestt(url)
{
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true)
xhr.setRequestHeader('Content-Type', 'application/json')
//document.getElementById("loading").innerHTML ='<img class="preloader" src="assets/img/Eclipse-loader-200px.svg" alt="">'
xhr.send()
xhr.onload=function()
{
if(this.status==200 && this.readyState==4)
{
var data = JSON.parse(this.responseText)
getdata()
//document.getElementById("loading").innerHTML = '';
}
else if(this.status==400){
alert('Error in getting items')
}
else if(this.status==401){
// document.getElementById("loading").innerHTML = '';
console.log('Please authenticate user')
}
//getdata()
}
}
function getdata()
{
console.log("request made")
url= "https://api.thingspeak.com/channels/1695002/feeds.json?api_key=BRUGPIARM8C5JE0O&results=2"
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true)
xhr.setRequestHeader('Content-Type', 'application/json')
//document.getElementById("loading").innerHTML ='<img class="preloader" src="assets/img/Eclipse-loader-200px.svg" alt="">'
xhr.send()
xhr.onload=function()
{
if(this.status==200 && this.readyState==4)
{
var data = JSON.parse(this.responseText)
console.log(data)
checkpet(data);
document.getElementById("water-refill").innerHTML = data["feeds"][0]["field2"];
document.getElementById("food-refill").innerHTML = data["feeds"][1]["field1"];
}
else if(this.status==400){
alert('Error in getting items')
}
else if(this.status==401){
// document.getElementById("loading").innerHTML = '';
console.log('Please authenticate user')
}
}
}
function gettime1()
{
var currentdate = new Date();
var datetime =currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":" + currentdate.getSeconds()+ " on "+currentdate.getDay() + "/" + currentdate.getMonth()
+ "/" + currentdate.getFullYear();
+
console.log(datetime)
document.getElementById("water-time").innerHTML = datetime;
}
function gettime2()
{
var currentdate = new Date();
var datetime =currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":" + currentdate.getSeconds()+ " on "+currentdate.getDay() + "/" + currentdate.getMonth()
+ "/" + currentdate.getFullYear();
+
console.log(datetime)
document.getElementById("food-time").innerHTML = datetime;
}
function checkpet(data)
{
var checker=data["feeds"][1]["field3"];
console.log(data["feeds"][1]["field3"])
//checker=1;
if(checker==="1")
{
console.log('pet is near')
document.getElementById("petalert").innerHTML = "Pet is near the feeder";
var pokemon= document.getElementsByClassName('alert');
console.log(pokemon)
pokemon[0].className += ' class_two'
}
else
{
console.log('not near')
//var active = document.querySelector("alert");
var pokemon= document.getElementsByClassName('alert');
pokemon[0].classList.remove("class_two");
document.getElementById("petalert").innerHTML = "Pet is not near the feeder";
}
}