-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
183 lines (169 loc) · 3.98 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
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
host_ip = 'http://192.168.1.7/';
function RenderContent()
{
//Add handlers
exp_time = document.getElementById("expose_time");
expose_time.value =90;
exp_btn = document.getElementById("StartTimer")
stop_btn = document.getElementById("StopTimer")
exp_btn.onclick = StartTimer;
stop_btn.onclick = StopTimer;
GetLedStatus();
GetTimerStatus();
}
function GetTimerStatus()
{
fetch(host_ip + "api/GetTimerStatus")
.then((response) => {
return response.text(); })
.then((data) => {
console.log(data);
if(data == "RUNNING")
{
RefreshPageTimer();
}
});
}
function RefreshPageTimer()
{
fetch(host_ip + "api/GetTimerTime")
.then((response) => {
return response.text(); })
.then((data) => {
console.log(data);
StartPageTimer(Math.round(Number(data)/1000));
});
}
function StartTimer()
{
exp_btn = document.getElementById("StartTimer");
exp_btn.disabled = true;
expose_time = document.getElementById("expose_time");
fetch(host_ip + "api/StartTimer",{
mode:'no-cors',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({time:expose_time.value})
})
.then((response) => {
return response.text(); })
.then((data) => {
console.log(data);
});
StartPageTimer(expose_time.value);
GetLedStatus();
}
function StopTimer()
{
fetch(host_ip + "api/StopTimer",{
mode:'no-cors',
method: 'POST'}).then((response) => {
return response.text(); })
.then((data) => {
console.log(data);
});
GetLedStatus();
StopPageTimer();
}
function GetLedStatus()
{
led_status = document.getElementById("led_status");
fetch(host_ip + "api/GetLedStatus").then((response) => {
return response.text(); })
.then((data) => {
console.log(data);
if(data == "ON")
{
led_status.innerHTML = "ON";
led_status.style.color = 'red';
}
else
{
led_status.innerHTML = "OFF";
led_status.style.color = 'green';
}
}).catch((e) => {
console.log('Error: ' + e.message);
console.log(e.response);
});
}
// Page timer
var timer = 0;
var timer_interval;
function StartPageTimer(seconds)
{
timer = seconds;
timer_interval =setInterval(RenderPageTimer,1000);
}
function StopPageTimer()
{
clearInterval(timer_interval);
el = document.getElementById("cdown");
el.innerHTML = "off";
exp_btn = document.getElementById("StartTimer");
exp_btn.disabled = true;
GetLedStatus();
}
function RenderPageTimer()
{
if(timer == 0)
{
OnPageTimerEnd();
StopPageTimer();
return;
}
else
{
el = document.getElementById("cdown");
el.innerHTML = timer;
timer-=1;
}
}
function OnPageTimerEnd()
{
el = document.getElementById("cdown");
el.innerHTML = "off";
window.navigator.vibrate(200);
console.log("Timer up!")
}
// function OnTimerStart()
// {
// const queryString = window.location.search;
// var searchParams = new URLSearchParams(queryString);
// let time_s = Number(searchParams.get('time'))
// if(!isNaN(time_s))
// {
// RenderTimer(Number(searchParams.get('time')))
// }
// }
// function RenderTimer(seconds)
// {
// if(seconds == 0)
// {
// OnTimerEnd();
// return;
// }
// else
// {
// el = document.getElementById("cdown");
// el.innerHTML = seconds;
// setTimeout(RenderTimer, 1000, (seconds-1));
// }
// }
// function CancelTimer()
// {
// }
// function OnTimerEnd()
// {
// el = document.getElementById("cdown");
// el.innerHTML = "off";
// console.log("Timer up!")
// }
// function OnTimerCanceled()
// {
// el = document.getElementById("cdown");
// el.innerHTML = "off";
// console.log("Timer canceled!")
// }