forked from ChaseDurand/Pool-Pi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoolpi.js
More file actions
executable file
·161 lines (142 loc) · 7.11 KB
/
Copy pathpoolpi.js
File metadata and controls
executable file
·161 lines (142 loc) · 7.11 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
document.addEventListener("DOMContentLoaded", function() {
// Connect to the Socket.IO server.
// The connection URL has the following format, relative to the current page:
// http[s]://<domain>:<port>[/<namespace>]
var socket = io();
// Diconnect callback function
// This is called by the timeout function if a model hasn't been received recently
function noConnection() {
document.getElementsByClassName('overlay')[0].style.display = 'flex';
document.getElementById('display1').innerHTML = ' NO CONNECTION ';
document.getElementById('display2').innerHTML = ' FROM RASPBERRY PI ';
}
// Timeout ID for disconnect timer
var timeoutID;
// Disconnect timer function to notify user if connection has been lost
// Reset when the model update is received
function resetTimeout() {
clearTimeout(timeoutID)
timeoutID = window.setTimeout(
function() {
noConnection();
}, 8000);
}
// Model version
var modelVersion;
socket.on("message", function(msg) {
console.log("Message", msg);
});
socket.on("connect", function() {
console.log("Connected");
});
socket.on("disconnect", function() {
console.log("Disconnected");
});
// Handler for the model update
socket.on('model', function(msg) {
resetTimeout();
msgObj = JSON.parse(msg["data"]);
document.getElementsByClassName('overlay')[0].style.display = 'none';
// Parse version
modelVersion = msgObj['version'];
delete msgObj['version'];
// Parse display into two lines and blink if necessary
len = msgObj['display_mask'].length;
document.getElementById('display1').innerHTML = '';
document.getElementById('display2').innerHTML = '';
// Parse top row of display
for (var i = 0; i < len / 2; ++i) {
if (msgObj['display_mask'][i] == '1') {
document.getElementById('display1').innerHTML += '<span class=' + 'blinkingText' + '>' + msgObj['display'].charAt(i) + '</span>';
} else {
document.getElementById('display1').innerHTML += msgObj['display'].charAt(i);
}
}
// Parse bottom row of display
for (var i = len / 2; i < len; ++i) {
if (msgObj['display_mask'][i] == '1') {
document.getElementById('display2').innerHTML += '<span class=' + 'blinkingText' + '>' + msgObj['display'].charAt(i) + '</span>';
} else {
document.getElementById('display2').innerHTML += msgObj['display'].charAt(i);
}
}
delete msgObj['display'];
delete msgObj['display_mask'];
// TODO can this be cleaned up?
// Parse every item in json message
for (var i = 0, len = Object.keys(msgObj).length; i < len; ++i) {
attributeName = Object.keys(msgObj)[i];
// Parse sending_message flag (no version)
if (attributeName == 'sending_message') {
if (msgObj[attributeName] == true) {
document.getElementsByClassName('overlay')[0].style.display = 'none'
} else {
document.getElementsByClassName('overlay')[0].style.display = 'none'
}
continue
}
// Parse service
else if (attributeName == 'service') {
if (msgObj['service'].state == 'ON') {
$('#led-service').removeClass('off').addClass('red');
// document.getElementById('service').parentElement.children['led'].className = 'LED red' + ' toggle-element';
} else if (msgObj['service'].state == 'OFF') {
$('#led-service').removeClass('red').addClass('off');
// document.getElementById('service').parentElement.children['led'].className = 'LED off' + ' toggle-element';
} else if (msgObj['service'].state == 'BLINK') {
$('#led-service').removeClass('off').addClass('red blink');
// document.getElementById('service').parentElement.children['led'].className = 'LED red blink' + ' toggle-element';
} else if (msgObj['service'].state == 'INIT') {
$('#led-service').removeClass('red blink').addClass('off');
// document.getElementById('service').parentElement.children['led'].className = 'LED off' + ' toggle-element';
}
}
// Parse pool/spa/spillover
else if ((attributeName == 'pool') || (attributeName == 'spa') || (attributeName == 'spillover')) {
if (msgObj[attributeName].state == 'ON') {
// Using jQuery, get the element that has the
$('#led-' + attributeName).removeClass('off').addClass('green');
//document.getElementById(attributeName).children['led'].className = 'LED green' + ' toggle-element';
} else {
$('#led-' + attributeName).removeClass('green').addClass('off');
// document.getElementById(attributeName).children['led'].className = 'LED off' + ' toggle-element';
}
}
// Parse check system
else if (attributeName == 'checksystem') {
if (msgObj['checksystem'] == 'ON') {
$('#checksystem').className = 'LED orange';
} else {
$('#checksystem').className = 'LED off';
}
}
// Parse other buttons
else {
if (msgObj[attributeName].state == 'ON') {
$('#led-' + attributeName).removeClass('off').addClass('green');
//document.getElementById(attributeName).parentElement.children['led'].className = 'LED green' + ' toggle-element';
} else {
// using jQuery: get the parent of the button, then get the children of the parent with class 'led'
$('#led-' + attributeName).removeClass('green').addClass('off');
//$('#'+attributeName).parents.children['led'].className = 'LED off' + ' toggle-element';
}
}
}
});
// Handler for menu buttons
document.querySelectorAll('.button-menu').forEach(element => element.addEventListener('click', function() {
buttonID = this.getAttribute('id');
socket.emit('webCommand', { 'id': buttonID, 'modelVersion': modelVersion });
}));
// Handler for toggle buttons
document.querySelectorAll('.button-toggle').forEach(element => element.addEventListener('click', function() {
// Double check that overlay is not present
if (document.getElementsByClassName('overlay')[0].style.display != 'none') {
return
}
document.getElementsByClassName('overlay')[0].style.display = 'flex';
buttonID = this.getAttribute('id');
socket.emit('webCommand', { 'id': buttonID, 'modelVersion': modelVersion });
console.log('Toggled ' + buttonID);
}));
});