-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
120 lines (103 loc) · 3.29 KB
/
main.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
119
120
// utlilty ajax function
//
function ajax(url, verb){
var httpRequest;
// document.getElementById("trigger").addEventListener('click', postRequest(url));
if(verb == 'post'){
postRequest(url);
}
if(verb == 'get'){
getRequest(url);
}
function postRequest(url) {
httpRequest = new XMLHttpRequest();
if (!httpRequest) {
console.error('Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = logResponse;
httpRequest.open('POST', url);
httpRequest.send();
}
function getRequest(url) {
httpRequest = new XMLHttpRequest();
if (!httpRequest) {
console.error('Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = logResponse;
httpRequest.open('GET', url);
httpRequest.send();
}
function logResponse() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var response = httpRequest.responseText;
console.log(response);
var json = JSON.parse(response)
if(json['onoff'] == 0){
document.getElementById("status").textContent = "Ready, trigger off.";
}
if(json['onoff'] == 1) {
document.getElementById("status").textContent = "Ready, trigger on."
}
} else {
document.getElementById("status").textContent = "Offline. " + httpRequest.status;
console.error('There was a problem with the request.', httpRequest.status);
}
}
}
}
// Update UI to match light setup.
// open is bool
function updateUI(open){
// show garage door images
// show some indicator when signal is sending
}
// set the lights to defined colors
// call api
//
function sendPulse(msec){
var token = "?access_token=" + getToken();
var api = "https://us.wio.seeed.io/v1/node/GroveRelayD0/onoff/";
ajax(api+1+token, 'post')
// upon success,
// update UI to say relay is off
document.getElementById("trigger").classList.add('active');
document.getElementById("light").classList.add('active');
document.getElementById("status").textContent = "Sending...";
console.log("relay contact: " + api + 1 + token);
setTimeout(function(){
ajax(api+0+token, 'post');
// upon success, wait $sec
// update UI to say relay is sending
document.getElementById("trigger").classList.remove('active');
document.getElementById("light").classList.remove('active');
document.getElementById("status").textContent = "Sent.";
console.log("relay open: " + api + 0 + token);
}, msec);
}
function checkStatus(){
var token = getToken();
if (token == null){
document.getElementById("status").textContent = "Enter device token"
} else {
var api = "https://us.wio.seeed.io/v1/node/GroveRelayD0/onoff_status/";
ajax(api+"?access_token="+token, 'get');
}
}
// set the auth token in local storage
// allows me to come back and use easily and not post token outside here.
function setToken(){
var token = document.getElementById("token").value;
localStorage.setItem('garageToken', token);
}
// gets the token from local storage, puts it in UI, returns to methods
//
function getToken(){
var token = localStorage.getItem('garageToken', token);
if (token != ''){
document.getElementById("token").value = token;
}
return token;
}