This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger.js
261 lines (224 loc) · 7.29 KB
/
trigger.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
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
/*jslint browser:true*/
(function($, Konami) {
'use strict';
var closing_time;
var url_twitter = "https://fixme.ch/cgi-bin/twitter.pl";
var url_api = "https://fixme.ch/status.json";
var url_led = "http://led.fixme.ch/rgb/";
var updateBlock= $("#update-block");
var msgBlock = $("#msg-block");
var loadBlock = $("#loading-block");
var closeBlock = $("#close-block");
var openBlock = $("#open-block");
var slider = $("#breathSlider");
function displayError(err) {
loadBlock.hide();
msgBlock.text("Error connecting to fixme server: "+ err);
}
function checkHours() {
var hoursForm = $('#hours');
var hoursOpen = Math.floor(hoursForm.val());
var openButton = $('#btn-open');
if (isNaN(hoursOpen) || hoursOpen < 1) {
openButton.attr('disabled', 'disabled');
} else {
openButton.removeAttr('disabled');
}
}
function changeHour(inc) {
var hours = $("#hours");
if (inc) {
hours.val(parseInt(hours.val(), 10) + 1);
} else if (hours.val() > 1) {
hours.val(parseInt(hours.val(), 10) - 1);
}
checkHours();
}
function pad(n) {
return ("0" + n).slice(-2);
}
function updateSpaceInformation() {
loadBlock.show();
$.getJSON(url_api, function(data){
var isOpen = data.state.open;
var open_duration = data.state.ext_duration;
closing_time = new Date(data.state.lastchange * 1000);
closing_time.setHours(closing_time.getHours() + open_duration);
update_date(calcDiff(closing_time));
if (isOpen) {
openBlock.hide();
closeBlock.show();
} else {
openBlock.show();
closeBlock.hide();
document.hoursform.hours.focus();
}
updateBlock.html('Last update:<br/>'.concat(
closing_time.toDateString(), ' ',
pad(closing_time.getHours()), ':',
pad(closing_time.getMinutes())
));
msgBlock.html(data.state.message);
loadBlock.hide();
checkHours();
});
}
function calcDiff() {
if(closing_time === undefined) return new Date(0);
var diff_time = Number(closing_time.getTime()) - new Date().getTime();
if (diff_time > 0) {
return new Date(diff_time);
} else {
return new Date(0);
}
}
function launchAutoRefresh() {
setInterval(function () {
update_date(calcDiff(closing_time));
}, 300);
}
function openSpace(extend) {
var hoursOpen = document.hoursform.hours.value;
if (hoursOpen === undefined || extend) {
hoursOpen = prompt("Hours:", 1);
if (hoursOpen === null) return;
}
var confirm_return = confirm("Are you sure you want to open the hackerspace ?");
if (confirm_return) {
msgBlock.html("Opening space...");
var requestUrl = url_twitter + "?do=custom&hours=" + hoursOpen,
requestObject = new XMLHttpRequest();
$.ajax(requestUrl, {
type: 'GET',
complete: function(data){
msgBlock.html(data.responseText);
},
});
}
}
function closeSpace() {
var confirm_value = confirm("Are you sure you want to close the space ?");
if (!confirm_value) {
return;
}
msgBlock.text("Closing space...");
var requestUrl = url_twitter + "?do=close";
$.ajax(requestUrl, {
type: 'GET',
complete: function(data){
msgBlock.html(data.responseText);
},
});
}
function update_date(date) {
var hours = String(date.getHours() - 1); // Time Zone GMT-1
if (hours.length == 1) {
hours = "0" + hours;
}
$("#hour").text(hours);
var minutes = String(date.getMinutes());
if (minutes.length == 1) {
minutes = "0" + minutes;
}
$("#minute").text(minutes);
var seconds = String(date.getSeconds());
if (seconds.length == 1) {
seconds = "0" + seconds;
}
$("#second").text(seconds);
}
function switchTheLight(red, green, blue) {
$.ajax(url_led, {
type: 'POST',
data: {red: red, green: green, blue: blue, breathe: slider.val()},
});
}
function changeBreathSpeed(){
$('#breathValue').text(slider.val() + ' ms');
}
function setTheBreathSpeed() {
$.ajax(url_led, {
type: 'POST',
data: {breathe: slider.val()},
});
}
function switchTheLightColor(color) {
Police.switchOff();
switch(color) {
case 'red':
switchTheLight(255, 0, 0);
break;
case 'green':
switchTheLight(0, 255, 0);
break;
case 'blue':
switchTheLight(0, 0, 255);
break;
}
}
function switchTheLightOn() {
switchTheLight(255, 255, 255);
}
function switchTheLightOff() {
Police.switchOff();
switchTheLight(0, 0, 0);
}
function switchTheLightToPolice() {
Police.switchOn();
}
var Police = {
policeEvent: 0,
delay: 100,
switchOff: function switchOff() {
window.clearInterval(Police.policeEvent);
Police.policeEvent = 0;
},
switchOn: function switchOn(red) {
if(Police.policeEvent !== 0) {
if (red) {
switchTheLight(255, 0, 0);
} else {
switchTheLight(0, 0, 255);
}
}
Police.policeEvent =
window.setTimeout(function() { Police.switchOn(!red); }, Police.delay);
},
};
msgBlock.show();
updateBlock.show();
// LEDs
var ledsButtons = {
red: switchTheLightColor.bind(null, 'red'),
green: switchTheLightColor.bind(null, 'green'),
blue: switchTheLightColor.bind(null, 'blue'),
white: switchTheLightOn,
off: switchTheLightOff,
police: switchTheLightToPolice
};
Object.keys(ledsButtons).forEach(function(buttonName) {
var fn = ledsButtons[buttonName];
$('#btn-' + buttonName).click(fn);
});
$('#breathSlider').mouseup(setTheBreathSpeed());
$('#breathSlider').on('change mousemove', function(){ changeBreathSpeed(); });
// Open/Close buttons
var triggerButtons = {
minus: changeHour.bind(null, 0),
plus: changeHour.bind(null, 1),
open: openSpace.bind(null, 0),
extend: openSpace.bind(null, 1),
close: closeSpace
};
Object.keys(triggerButtons).forEach(function(buttonName) {
var fn = triggerButtons[buttonName];
$('#btn-' + buttonName).click(fn);
});
$('#hours').keyup(checkHours);
// Set up trigger
launchAutoRefresh();
updateSpaceInformation();
setInterval(updateSpaceInformation, 60 * 1000);
// Egging
(new Konami()).load("https://www.youtube.com/watch?v=1Wytn-_MSBo");
})(jQuery, Konami);