-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmenu.html
More file actions
153 lines (142 loc) · 3.89 KB
/
Copy pathmenu.html
File metadata and controls
153 lines (142 loc) · 3.89 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
<html>
<head>
<title>Should I Turn It On?</title>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script type="text/javascript" src="https://cdn.firebase.com/v0/firebase.js"></script>
<link rel="stylesheet" href="css/menu.css">
<script type='text/javascript' src='scripts/test.js'></script>
<script type="text/javascript">
var times = []
var appData = new Array(61+1).join('0').split('').map(parseFloat);
var requestRef = new Firebase('https://intense-fire-7795.firebaseio.com/Request');
var off = true;
$(document).ready(function(){
$('#button').on('click', function(){
$(this).toggleClass('on');
if (off == true){
var p = $('#watts').val();
var d = getDateTime();
requestRef.push({Date:d, Power:p});
if ((appData[i] + p < appData[i-1] * 1.1) || (appData[i-1] == 0 && Math.random() < 0.5)){
document.getElementById('txtOutput').value = 'You can turn it on now!';
off = false;
} else {
document.getElementById('txtOutput').value = 'Wait one minute please!';
$(this).toggleClass('on');
off = true;
}
}
else {
document.getElementById('txtOutput').value = '';
off = true;
}
});
});
requestRef.on('child_added', function (snapshot) {
var d = snapshot.val();
for (i = 1; i < times.length; i++){
if (d.Date.substring(11,16) == times[i]){
if (appData[i] < appData[i-1] * 1.1 || (appData[i-1] == 0 && Math.random() < .5)) {
appData[i] = appData[i] + parseInt(d.Power);
} else {
appData[i+1] = appData[i+1] + parseInt(d.Power);
}
}
}
});
function getTimeArray() {
var now = new Date();
var currentHour = now.getHours();
var currentMinute = now.getMinutes();
var hour;
var minute;
if (currentHour == 0) {
hour = 23;
} else {
hour = currentHour - 1;
}
if (currentMinute == 59) {
minute = 0;
} else {
minute = currentMinute + 1;
}
for (i = 0; i < 60; i++) {
if (hour.toString().length == 1){
var hour = '0'+hour;
}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
times[i] = hour + ":" + minute;
if (minute == 59) {
minute = 00;
if (hour == 23) {
hour = 0;
} else {
hour ++;
}
} else {
minute++;
}
}
}
function getDateTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(month.toString().length == 1) {
var month = '0' + month;
}
if(day.toString().length == 1) {
var day = '0' + day;
}
if(hour.toString().length == 1) {
var hour = '0' + hour;
}
if(minute.toString().length == 1) {
var minute = '0' + minute;
}
if(second.toString().length == 1) {
var second = '0' + second;
}
var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;
return dateTime;
}
$(document).keypress(function (e) {
if (e.keyCode == 99) {
window.location.href = 'index.html';
}
});
setInterval(function() {
if (new Date().getSeconds() == 0){
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
if (hour.toString().length == 1){
var hour = '0'+hour;
}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
times.push(hour + ":" + minute);
times.shift();
appData.push(0);
appData.shift();
}
}, 1000);
window.onload = getTimeArray;
</script>
</head>
<body>
<input type='text' class = 'watts' id='watts' placeholder='Wattage (W)'>
<input type = "text" class = 'txtOutput' id = "txtOutput" placeholder='Waiting for Request'>
<section>
<a href="#" id="button"></a>
<span></span>
</section>
</body>
</html>