-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeb_interface.html
More file actions
213 lines (191 loc) · 6.25 KB
/
Web_interface.html
File metadata and controls
213 lines (191 loc) · 6.25 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fish Feeder - Settings</title>
<script src="https://www.gstatic.com/firebasejs/7.15.5/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.15.5/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.15.5/firebase-database.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
/* Add your custom CSS styles here */
</style>
</head>
<body>
<!-- Heading -->
<h1>Fish Feeder Settings</h1>
<h2>Time is about 1h and 2 min in the back</h2>
<!-- Time Display -->
<div id="time"></div>
<!-- Settings Form -->
<form id="settings-form">
<!-- Time Input and Remove Button for Time 0 -->
<label for="time0">Time 0:</label>
<input type="time" id="time0" name="time0" />
<button type="button" onclick="removeTime('time0')">Remove</button
><br /><br />
<!-- Time Input and Remove Button for Time 1 -->
<label for="time1">Time 1:</label>
<input type="time" id="time1" name="time1" />
<button type="button" onclick="removeTime('time1')">Remove</button
><br /><br />
<!-- Time Input and Remove Button for Time 2 -->
<label for="time2">Time 2:</label>
<input type="time" id="time2" name="time2" />
<button type="button" onclick="removeTime('time2')">Remove</button
><br /><br />
<!-- Save Settings Button -->
<button type="button" onclick="saveSettings()">Save Settings</button>
</form>
<!-- Feed Now Button -->
<button type="button" onclick="feedNow()">Feed Now</button>
<!-- Timers Display Wrapper -->
<div id="wrapper" class="wrapper"></div>
<script>
// Firebase configuration
const firebaseConfig = {};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.database();
let count = 0;
/**
* Save user settings to Firebase
*/
function saveSettings() {
const times = {};
for (let i = 0; i < 3; i++) {
const timeInput = document.getElementById(`time${i}`);
times[`time${i}`] = timeInput.value;
}
db.ref('times')
.set(times)
.then(() => console.log('Settings saved!'))
.catch((error) => console.error('Error saving settings:', error));
}
/**
* Load user settings from Firebase
*/
db.ref('times').on('value', (snapshot) => {
const times = snapshot.val();
for (let i = 0; i < 3; i++) {
const timeInput = document.getElementById(`time${i}`);
timeInput.value = times[`time${i}`];
}
});
/**
* Add timers to display on page load
*/
$(document).ready(function () {
addDiv();
});
/**
* Add a new timer to Firebase
* @param {number} count - The timer count
* @param {object} e - The timer object
*/
function addStore(count, e) {
db.ref('timers/timer' + count).set({
time: e.time,
});
addDiv();
}
/**
* Toggle visibility of timer details
* @param {string} id - The timer ID
*/
function showShort(id) {
const idv = $(id)[0]['id'];
$('#time_' + idv).toggle();
$('#short_' + idv).toggle();
}
/**
* Remove a timer
* @param {string} id - The timer ID
*/
function removeDiv(id) {
const idv = $(id)[0]['id'];
db.ref('timers/' + idv).remove();
if (count >= 0) {
count--;
}
db.ref().update({
count: parseInt(count),
});
$(id).fadeOut(1, 0).fadeTo(500, 0);
}
/**
* Display timers on the page
*/
function addDiv() {
const divRef = db.ref('timers');
divRef.on('value', function (snapshot) {
const obj = snapshot.val();
let i = 0;
$('#wrapper').html('');
while (i <= count) {
const propertyValues = Object.entries(obj);
let ts = propertyValues[i][1]['time'];
let H = +ts.substr(0, 2);
let h = H % 12 || 12;
h = h < 10 ? '0' + h : h;
let ampm = H < 12 ? ' AM' : ' PM';
ts = h + ts.substr(2, 3) + ampm;
const x = `
<div id=${propertyValues[i][0]}>
<div class="btn2 btn__secondary2" onclick=showShort(${propertyValues[i][0]}) id="main_${propertyValues[i][0]}">
<div id="time_${propertyValues[i][0]}">
${ts}
</div>
<div class="icon2" id="short_${propertyValues[i][0]}" onclick=removeDiv(${propertyValues[i][0]})>
<div class="icon__add">
X <!-- Remove Icon -->
</div>
</div>
</div>
</div>`;
$('#wrapper').append(x);
i++;
}
});
}
/**
* Perform immediate feeding
*/
function feedNow() {
db.ref()
.update({
feednow: 1,
})
.then(() => {
console.log('Feeding now!');
})
.catch((error) => {
console.error('Error while feeding:', error);
});
}
/**
* Refresh and display the current time
*/
function refreshTime() {
const timeDisplay = document.getElementById('time');
const dateString = new Date().toLocaleString();
const formattedString = dateString.replace(', ', ' - ');
timeDisplay.textContent = formattedString;
}
setInterval(refreshTime, 1000);
var datetime = new Date();
document.getElementById('time').textContent = datetime;
/**
* Remove a specific time setting
* @param {string} id - ID of the time setting to remove
*/
function removeTime(id) {
db.ref('times')
.update({ [id]: null })
.then(() => console.log(`Time ${id} removed.`))
.catch((error) => console.error(`Error removing time ${id}:`, error));
}
</script>
</body>
</html>