-
Notifications
You must be signed in to change notification settings - Fork 1
/
javascript.h
226 lines (121 loc) · 3.95 KB
/
javascript.h
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
/* Javascript functions go here
*/
// New Idea
void javascriptClockItem(WiFiClient client) {
client.println("<div>");
client.println("<p1 id='live_clock'></p>");
client.println("</div>");
}
/*
void write_xml(int inputData){
if(xmlDocument.Parse(live_clock)!= XML_SUCCESS){
Serial.println("Error parsing");
return;
};
}
void script_read_xml(){
XMLNode * root = xmlDocument.FirstChild();
XMLElement * element = root->FirstChildElement("element");
int val;
element->QueryIntText(&val);
Serial.println(val);
}
*/
// Client side clock. Advantages - less load on microcontroller. No refresh of page required.
//disadvantages - no true sync between webpage clock and actual clock time.
// Needs alot more work to work smoothly through t = 0
// This whole thing is scuffed
void clockData_toJson(WiFiClient client, int h, int m, int s) {
client.println("<script>");
// Set up variables
client.print("var hours =");
client.print(h);
client.println(";");
client.print("var mins =");
client.print(m);
client.println(";");
client.print("var sec =");
client.print(s - 1);
client.println(";");
// Update the count down every 1 second
client.println("var x = setInterval(function() {");
if (simpleClock.runCountdown) {
client.println("sec = (sec - 1);");
// if minuites and seconds = zero, subtract 1 from hours
client.println("if (mins <= 0 && sec <= 0){");
client.println("hours = hours - 1;");
client.println("if (hours > 1){");
client.println("mins = 60;");
client.println("}");
// if seconds is zero subtract 1 from minuites
client.println("if (sec <= 0){");
client.println("sec = 60;");
client.println("mins = mins -1;");
client.println("}");
client.println("}");
}
client.print("document.getElementById(\"live_clock\").innerHTML =");
if (simpleClock.tminus){
client.print("hours");
client.print(" + \":\" + ");
client.print("mins");
client.print(" + \":\" + ");
client.print("sec");
} else {
client.print("T > 0 || Test In Progress || ");
}
client.println(";");
client.println("}, 1000);");
client.println("</script>");
}
// This updater requires data to be saved in an XML document called "liveClock" with the data laid out like
// {"live_clock": T- 00:20:10}
// This may be possible with tinyxm12.h library
/*
void javascriptXMLUpdater(){
<script>
requestData(); // get intial data straight away
// request data updates every 5000 milliseconds
setInterval(requestData, 1000);
function requestData() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'liveClock');
xhr.onload = function() {
if (xhr.status === 200) {
if (xhr.responseText) { // if the returned data is not null, update the values
var data = JSON.parse(xhr.responseText);
document.getElementById("live_clock").innerText = data.live_clock;
} else { // a problem occurred
document.getElementById("live_clock").innerText = "?";
}
} else {
console.log('Request failed. Returned status of ' + xhr.status);
document.getElementById("live_clock").innerText = "00:00:00";
}
};
xhr.send();
}
</script>
}
*/
// Old didnt work stuff
// Live Clock - Broken atm
/*
void javaScript_timeHead(WiFiClient client) {
// This Code goes in Head //
client.println("<script>");
client.println("function startTime() {");
client.println("var time = simpleClock.countdown_clock_string");
client.println(" document.getElementById('webTime').innerHTML =");
client.println(" time;");
client.println(" var t = setTimeout(startTime, 500);");
client.println("}");
client.println("</script>");
// </head>
}
void javascript_timeBody(WiFiClient client) {
// Code for body //
client.println("<body onload=\"startTime()\">");
client.println("<div id=\"webTime\"></div>");
}
*/