-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtml.go
375 lines (340 loc) · 9.56 KB
/
html.go
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"encoding/json"
"os"
"strconv"
"strings"
orderedmap "github.com/wk8/go-ordered-map/v2"
)
var Disco_HTML string = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sensor Discovery</title>
</head>
<body>
<h1>Sensor discovery mode</h1>
<h1>Status: {{.}}</h1>
<form method='POST'>
<label for='Disco'> </label>
<select name='Disco' id='Disco'>
<option value='Enable'>Enable</option>
<option value='Disable'>Disable</option>
</select>
<input type='submit' Value='Submit'>
</form>
<br />
<a href="index.html"><button>Back to dashboard</button></a>
</body>
</html>
`
var Logic_HTML_1 string = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Logic Editor</title>
<style>
.hidden-option {
display: none;
}
table {
border: 1px solid black;
border-collapse: collapse;
width: 25%;
}
td, th {
border: 1px solid black;
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<h1>Logic Editor</h1>
<form method='POST'>
<label>Power Controller:</label><br />
<input type='text' name='POWERC'><br /><br />
<label for='SENSOR'>IF SENSOR </label>
<select name='SENSOR' id='SENSOR' onchange='update()'>
`
var Logic_HTML_2 string = `
<label for='STATUS'> </label>
<select name='STATUS' id='STATUS'>
<option value='on'>ON</option>
<option value='off'>OFF</option>
</select>
<input type="checkbox" id="THEN" name="THEN" value="TRUE">
<label for="checkbox">THEN</label>
`
var Logic_HTML_END = `
<br /><input type='submit' name='submit' value='Add'><br />
</form>
<br /><a href='index.html'><button>Back to dashboard</button></a>
<script>
function update() {
var reading_drop = document.getElementById("READING");
var sensor_drop = document.getElementById("SENSOR");
var selected = sensor_drop.value;
for (var i = 0; i < reading_drop.options.length; i++) {
var option = reading_drop.options[i];
if (option.getAttribute('data-group') == selected) {
option.style.display = 'block';
} else {
option.style.display = 'none';
}
}
}
window.onload = update;
function validateInput(input) {
let numericValue = input.value.replace(/[^0-9]/g, '');
numericValue = Math.min(15000, Math.max(0, numericValue));
input.value = numericValue;
const placeholderText = "Enter numbers (0-15000)";
input.placeholder = input.value.length === 0 ? placeholderText : "";
}
</script>
</body>
</html>
`
var Sched_HTML = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Schedule On/Off Event</title>
<style>
body {
font-family: Arial, sans-serif;
}
label {
margin-bottom: 10px;
}
select {
width: 200px;
padding: 5px;
font-size: 16px;
}
.hidden-option {
display: none;
}
table {
border: 1px solid black;
border-collapse: collapse;
width: 25%;
}
td, th {
border: 1px solid black;
padding: 8px;
text-align: left;
}
.time-picker input {
padding: 8px;
font-size: 16px;
width: 50px;
display: inline-block;
margin-right: 10px;
}
</style>
</head>
<body>
<h2>Schedule Event</h2>
<form method='POST'>
<label>Power Controller:</label>
<input type='text' name='POWERC'>
<label>Zone:</label>
<input type='text' name='ZONE'><br /><br />
<label for="dayOfWeek">Day of the Week:</label>
<select id="dayOfWeek" name="dayOfWeek">
<option value="sunday">Sunday</option>
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
<option value="friday">Friday</option>
<option value="saturday">Saturday</option>
</select>
<br><br>
<div class="time-picker">
<label for="hours">Hours (24):</label>
<input type="text" id="hours" name="hours" placeholder="HH" maxlength="2">
<label for="minutes">Minutes:</label>
<input type="text" id="minutes" name="minutes" placeholder="MM" maxlength="2">
</div>
`
var Sched_HTML_2 = `
<label for='STATUS'> </label>
<select name='STATUS' id='STATUS'>
<option value='on'>ON</option>
<option value='off'>OFF</option>
</select>
<br><br>
<input type='submit' name='submit' value='Add'>
</form>
`
var Sched_HTML_END = `
<br /><a href='index.html'><button>Back to dashboard</button></a>
</body>
</html>
`
func Build_Logic() string {
var s_builder strings.Builder
s_builder.WriteString(Logic_HTML_1)
values_array, err := Read_Interface("values.json")
if err != nil {
R_LOG(err.Error())
} else {
keys, _ := Iterate_Map(values_array)
for _, key := range keys {
s_builder.WriteString(" <option value='" + key + "'>" + key + "</option>\n")
}
s_builder.WriteString(`
</select>
<label for='READING'>READING </label>
<select name='READING' id='READING'>
`)
v_keys, v_values := Iterate_Map(values_array)
for v_index, v_name := range v_keys {
temp_value, err := Iterate_Interface(v_values[v_index])
if err != nil {
return "Could not find values, wait a minute and try again."
}
for _, v_value := range temp_value {
s_builder.WriteString(" <option value='" + v_value + "' class='hidden-option' data-group='" + v_name + "'>" + v_value + "</option>\n")
}
}
s_builder.WriteString(`
</select>
<label for='EQU'>IS </label>
<select name='EQU' id='EQU'>
<option value='less'>Less than</option>
<option value='lessequ'>Less than equal</option>
<option value='great'>Greater than</option>
<option value='greatequ'>Greater than equal</option>
<option value='equa'>Equal to</option>
<option value='nequa'>Not equal to</option>
</select>
`)
s_builder.WriteString(" <input type='text' id='VALUE' name='VALUE' placeholder='Enter numbers (0-15000)' oninput='validateInput(this)'>\n")
s_builder.WriteString(`
<label for='PIN'>TURN PIN </label>
<select name='PIN' id='PIN'>
`)
for x := 0; x <= 50; x++ {
str := strconv.Itoa(x)
s_builder.WriteString(" <option value='" + str + "'>" + str + "</option>\n")
}
s_builder.WriteString(" </select>")
}
s_builder.WriteString(Logic_HTML_2)
s_builder.WriteString(`
<label for='WEIGHT'>| WEIGHT </label>
<select name='WEIGHT' id='WEIGHT'>
`)
for x := 0; x <= 100; x++ {
str := strconv.Itoa(x)
s_builder.WriteString(" <option value='" + str + "'>" + str + "</option>\n")
}
s_builder.WriteString(" </select>")
s_builder.WriteString(" <br /><br />\n")
json_data, err := os.ReadFile("logic.json")
if err != nil {
R_LOG("Error: " + err.Error())
} else {
s_builder.WriteString(" <table>\n")
s_builder.WriteString(`
<thead>
<tr>
<th>Sensor</th>
<th>Sensor reading</th>
<th>Compare</th>
<th>Sensor value</th>
<th>Pin</th>
<th>Pin state</th>
<th>Power Controller</th>
<th>Then</th>
<th>Weight</th>
<th>Delete logic</th>
</tr>
</thead>
`)
s_builder.WriteString(" <tbody>\n")
json_map := orderedmap.New[string, []string]()
if err := json.Unmarshal(json_data, &json_map); err != nil {
R_LOG("Error: " + err.Error())
} else {
for el := json_map.Oldest(); el != nil; el = el.Next() {
s_builder.WriteString(" <tr>\n")
uuid := el.Key
logic_slice := el.Value
for _, l_value := range logic_slice {
s_builder.WriteString(" <td>" + l_value + "</td>\n")
}
s_builder.WriteString(" <td><form method='POST'><input type='hidden' name='uuid' value='" + uuid + "'><input type='submit' name='submit' value='Delete'></form></td>\n")
s_builder.WriteString(" </tr>\n")
}
s_builder.WriteString(" </tbody>\n")
s_builder.WriteString(" </table>\n")
}
}
s_builder.WriteString(Logic_HTML_END)
return s_builder.String()
}
func Build_Sched() string {
var s_builder strings.Builder
s_builder.WriteString(Sched_HTML)
s_builder.WriteString(`
<br><br>
<label for='PIN'>Turn PIN </label>
<select name='PIN' id='PIN'>
`)
for x := 0; x <= 50; x++ {
str := strconv.Itoa(x)
s_builder.WriteString("<option value='" + str + "'>" + str + "</option>\n")
}
s_builder.WriteString("</select>")
s_builder.WriteString(Sched_HTML_2)
json_data, err := os.ReadFile("sched.json")
if err != nil {
R_LOG("Error: " + err.Error())
} else {
s_builder.WriteString("<br><table>\n")
s_builder.WriteString(`
<thead>
<tr>
<th>Day</th>
<th>Time (24hr)</th>
<th>Pin</th>
<th>Pin Status</th>
<th>Power Controller</th>
<th>Zone</th>
<th>Delete Schedule</th>
</tr>
</thead>
`)
s_builder.WriteString("<tbody>\n")
json_map := orderedmap.New[string, []string]()
if err := json.Unmarshal(json_data, &json_map); err != nil {
R_LOG("Error: " + err.Error())
} else {
for el := json_map.Oldest(); el != nil; el = el.Next() {
s_builder.WriteString("<tr>\n")
uuid := el.Key
logic_slice := el.Value
for _, l_value := range logic_slice {
s_builder.WriteString("<td>" + l_value + "</td>\n")
}
s_builder.WriteString("<td><form method='POST'><input type='hidden' name='uuid' value='" + uuid + "'><input type='submit' name='submit' value='Delete'></form></td>\n")
s_builder.WriteString("</tr>\n")
}
s_builder.WriteString("</tbody>\n")
s_builder.WriteString("</table>\n")
}
}
s_builder.WriteString(Sched_HTML_END)
return s_builder.String()
}