-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
243 lines (203 loc) · 7.81 KB
/
index.html
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
<!-- © Copyright 2015 Robin Kearney -->
<!-- Licensed under the MIT license with an additional non-advertising clause -->
<!-- See https://github.com/rk295/maplin-webremote -->
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Maplin Light Switch</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black"/>
<link rel="apple-touch-icon" href="touch-icon.png"/>
<!-- My JS -->
<script src="mqttws31.js" type="text/javascript"></script>
<script src="config.js" type="text/javascript"></script>
<!-- Others JS -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js" type="text/javascript"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js" type="text/javascript"></script>
<script type="text/javascript">
var mqtt;
var reconnectTimeout = 2000;
function MQTTconnect() {
mqtt = new Paho.MQTT.Client(
host,
port,
"web_" + parseInt(Math.random() * 100,
10));
var options = {
timeout: 3,
useSSL: useTLS,
cleanSession: cleansession,
onSuccess: onConnect,
onFailure: function (message) {
$('#status').val("Connection failed: " + message.errorMessage + "Retrying");
setTimeout(MQTTconnect, reconnectTimeout);
}
};
mqtt.onConnectionLost = onConnectionLost;
mqtt.onMessageArrived = onMessageArrived;
if (username != null) {
options.userName = username;
options.password = password;
}
// console.log(options);
mqtt.connect(options);
}
// Called upon initial successful connection
function onConnect() {
$('#status').val('Connected to ' + host + ':' + port);
mqtt.subscribe('rooms/switches', {qos: 0});
display_alert('success', 'Connected successfully');
}
// Called when connection to the broker has been lost
function onConnectionLost(response) {
setTimeout(MQTTconnect, reconnectTimeout);
display_alert('danger', 'Connected lost: ' + response.errorMessage + ' Reconnecting');
};
// Builds a list of buttons, passed an hash
// containing room name, channel, button
function build_buttons(roomList){
console.log(roomList);
// Clear the button list
// console.log("Clearing button list")
$('#buttonlist').empty()
rooms = roomList['rooms']
var keys = [];
for (var key in rooms) {
// console.log("Sorting :" + key)
if (rooms.hasOwnProperty(key)) {
keys.push(key);
}
}
keys.sort();
for (i in keys) {
prop = keys[i]
channel = rooms[prop]['channel']
button = rooms[prop]['button']
state = rooms[prop]['state']
// console.log("Building button for:" + name + " channel:" + channel + " button:" + button + " state:" + state)
$('#buttonlist').append('<li> \
<div class="btn-group" role="group"> \
<button id="'+prop+'-on" name="'+prop+'" channel="'+channel+'" button="'+button+'" state="on" type="button" class="btn btn-lg btn-success onoff" aria-label="Left Align">On</button> \
<button id="'+prop+'-off" name="'+prop+'" channel="'+channel+'" button="'+button+'" state="off" type="button" class="btn btn-lg btn-danger onoff" aria-label="Left Align">Off</button> \
</div> \
<img src="img/bulb-' + state + '.png" class="img-rounded" height="40"> \
' + prop);
}
}
function build_scenes(roomList){
console.log(roomList);
// Clear the scene list
// console.log("Clearing button list")
$('#scenelist').empty()
rooms = roomList['scenes']
var keys = [];
for (var key in rooms) {
// console.log("Sorting :" + key)
if (rooms.hasOwnProperty(key)) {
keys.push(key);
}
}
keys.sort();
for (i in keys) {
prop = keys[i]
// console.log("Building button for:" + name + " channel:" + channel + " button:" + button + " state:" + state)
$('#scenelist').append('<li> \
<div class="btn-group" role="group"> \
<button id="'+prop+'-on" name="'+prop+'" state="on" type="button" class="btn btn-lg btn-success onoff" aria-label="Left Align">On</button> \
<button id="'+prop+'-off" name="'+prop+'" state="off" type="button" class="btn btn-lg btn-danger onoff" aria-label="Left Align">Off</button> \
</div> \
' + prop);
}
}
// Called when a message arrives. Used to retrive the
// retained message of all the possible rooms
function onMessageArrived(message) {
var topic = message.destinationName;
var payload = message.payloadString;
roomList = JSON.parse(payload);
build_buttons(roomList)
build_scenes(roomList)
};
// Actually send a MQTT message
function send_message(button, action){
var payload = ( action == 'on' ) ? "true" : "false";
var topic = "rooms/" + button
console.log("Sending " + topic + " to: " + payload);
msg = new Paho.MQTT.Message(payload);
msg.destinationName = topic
msg.retained = true;
mqtt.send(msg);
}
// Displays a bootstrap alert to confirm connection/disconnection
function display_alert(state,message) {
$('#alert').html('\
<div class="alert alert-'+state+' alert-dismissible" role="alert"> \
<button type="button" class="close" data-dismiss="alert"> \
<span aria-hidden="true">×</span> \
<span class="sr-only">Close</span> \
</button>'+message+'</div>').show(200)
// Removed this because I find it nicer
// without this closing
/*
setTimeout(function () {
$('#alert').toggle(200);
}, 2000);
*/
}
// Displays a bootstrap alert to confirm connection/disconnection
function display_status(message) {
$('#status-message').html('\
<div class="alert alert-info alert-dismissible" role="alert"> \
<button type="button" class="close" data-dismiss="alert"> \
<span aria-hidden="true">×</span> \
<span class="sr-only">Close</span> \
</button>'+message+'</div>')
// Removed this because I find it nicer
// without this closing
setTimeout(function () {
$('#status-message').toggle(200);
}, 2000);
}
$(document).ready(function() {
MQTTconnect();
// Bind click events to the buttons created by build_buttons()
// above.
$('#buttonlist').on("click", '.onoff' , function(event){
var input = $( this );
// Send the mqtt message
send_message(input.attr("name"), input.attr("state"))
display_status(input.attr("name")+" turned "+input.attr("state"))
});
$('#scenelist').on("click", '.onoff' , function(event){
var input = $( this );
// Send the mqtt message
send_message(input.attr("name"), input.attr("state"))
display_status(input.attr("name")+" turned "+input.attr("state"))
});
});
</script>
</head>
<body>
<div class="row">
<div id="alert"></div>
</div><!-- alert row -->
<div class="row">
<div class="col-md-6">
<h3>Switches</h3>
<ul id="buttonlist"><!--build_buttons populates--></ul>
</div>
<div class="col-md-6">
<h3>Scenes</h3>
<ul id="scenelist"><!--build_scenes populates--></ul>
</div>
</div><!-- button row -->
<div class="row">
<div class="col-md-12">
<div id="status-message"></div>
</div>
</div><!-- status row -->
</body>
</html>