Browse Source

HTML and JS now minified and put into sketch using python script, JS served from ESP

Thomas Buck 7 years ago
parent
commit
7203100fca
6 changed files with 171 additions and 63 deletions
  1. 9
    2
      ESP-Weather.ino
  2. 0
    2
      README.md
  3. 45
    48
      client-script.js
  4. 5
    11
      config.h
  5. 64
    0
      convert-static.py
  6. 48
    0
      template.html

+ 9
- 2
ESP-Weather.ino View File

@@ -54,6 +54,11 @@ void handleRoot() {
54 54
     waitingForReplies = true;
55 55
 }
56 56
 
57
+void handleJS() {
58
+    String message = F(JS_FILE);
59
+    server.send(200, "text/javascript", message);
60
+}
61
+
57 62
 void handleNotFound() {
58 63
     String message = "File Not Found\n\n";
59 64
     message += "URI: ";
@@ -106,6 +111,8 @@ void setup(void) {
106 111
     broadcastIP = ~WiFi.subnetMask() | WiFi.gatewayIP();
107 112
 
108 113
     server.on("/", handleRoot);
114
+    server.on("/index.html", handleRoot);
115
+    server.on("/view.js", handleJS);
109 116
     server.onNotFound(handleNotFound);
110 117
     server.begin();
111 118
 
@@ -197,7 +204,7 @@ void loop(void) {
197 204
     if (((millis() - lastTime) >= MAX_BROADCAST_WAIT_TIME) && (waitingForReplies == true)) {
198 205
         Serial.println("Timeout, sending response...");
199 206
         waitingForReplies = false;
200
-        String message = HTML_BEGIN;
207
+        String message = F(HTML_BEGIN);
201 208
         message += "var clients = Array(";
202 209
         message += "\"" + WiFi.localIP().toString() + "\"";
203 210
         if (vecClients.size() > 0) {
@@ -210,7 +217,7 @@ void loop(void) {
210 217
             }
211 218
         }
212 219
         message += ");";
213
-        message += HTML_END;
220
+        message += F(HTML_END);
214 221
 
215 222
         vecClients.clear();
216 223
 

+ 0
- 2
README.md View File

@@ -20,8 +20,6 @@ The JavaScript client code includes the following libraries, using their officia
20 20
 * [jQuery](https://jquery.com/)
21 21
 * [Chart.js](https://github.com/chartjs/Chart.js)
22 22
 
23
-The client script from this repository is served using the [RawGit Service](https://rawgit.com) development URLs. Consider using their production CDN when using this project seriously.
24
-
25 23
 ## License
26 24
 
27 25
     ----------------------------------------------------------------------------

+ 45
- 48
client-script.js View File

@@ -1,37 +1,34 @@
1 1
 var arrSensor = Array();
2 2
 
3
-$(document).ready(function() {
3
+textAvailableSensors = "Available Sensors";
4
+textButtonNext = "Continue";
4 5
 
5
-    $('body').append(`<div class="container-fluid">
6
-            <div class="page-header">
7
-                <h2>Systemadministration</h2>
8
-            </div>
9
-            <div class="row" id="contentDiv">
10
-                <div class="col-md-5 col-lg-5">
11
-                    <div class="panel panel-primary">
12
-                        <div class="panel-heading" id="listSensorsHeading" style="font-size: 18px;">Verf&uuml;gbare Sensoren (0/0)</div>
13
-                        <div class="panel-body">
14
-                            <ul class="list-group" id="listSensors"></ul>
15
-                            <div id="alertDiv"></div>
16
-                            <button class="btn btn-primary" disabled="" id="btnSubmit">Weiter</button>
17
-                        </div>
6
+$(document).ready(function() {
7
+    $('#main-part').append(`
8
+        <div class="row" id="contentDiv">
9
+            <div class="col-md-5 col-lg-5">
10
+                <div class="panel panel-primary">
11
+                    <div class="panel-heading" id="listSensorsHeading" style="font-size: 18px;">
12
+                        ` + textAvailableSensors + ` (0/0)
13
+                    </div>
14
+                    <div class="panel-body">
15
+                        <ul class="list-group" id="listSensors"></ul>
16
+                        <div id="alertDiv"></div>
17
+                        <button class="btn btn-primary" disabled="" id="btnSubmit">
18
+                            ` + textButtonNext + `
19
+                        </button>
18 20
                     </div>
19 21
                 </div>
20 22
             </div>
21
-        </div>
22
-        <div class="footer">
23
-            <div class="container-fluid">
24
-                <p class="text-muted">&copy; Copyright 2016 by Christian H&ouml;gerle und Thomas Buck</p>
25
-            </div>
26 23
         </div>`);
27 24
 
28 25
     // aktuelle Uhrzeit fuer die Graphen ermitteln
29 26
     var actTime = new Date();
30
-    actTime = actTime.getHours() + ":" + (actTime.getMinutes() < 10 ? '0':'') + actTime.getMinutes(); 
27
+    actTime = actTime.getHours() + ":" + (actTime.getMinutes() < 10 ? '0':'') + actTime.getMinutes();
31 28
 
32 29
     $('#listSensorsHeading').empty();
33
-    $('#listSensorsHeading').html("Verf&uuml;gbare Sensoren (0/" + clients.length + ")");
34
-    
30
+    $('#listSensorsHeading').html(textAvailableSensors + " (0/" + clients.length + ")");
31
+
35 32
     // Alle clients im Array iterieren und Websocket abfragen
36 33
     var count = [0];
37 34
     jQuery.each(clients, function(index, client) {
@@ -59,12 +56,12 @@ function webSocket(wsUri, wsPort, count, clientsCount) {
59 56
         });
60 57
         sensor.arrEEPROM = arrEEPROM;
61 58
         arrSensor.push(sensor);
62
-        $('#listSensorsHeading').html("Verf&uuml;gbare Sensoren (" + sensor.id + "/" + clientsCount + ")");
59
+        $('#listSensorsHeading').html(textAvailableSensors + " (" + sensor.id + "/" + clientsCount + ")");
63 60
         $('#listSensors').append('<li class="list-group-item">' +
64
-                                    ' Sensor ' + sensor.id +  
65
-                                    ' | IP: ' + sensor.ip + 
66
-                                    ' | aktuelle Temperatur: ' + sensor.actualTemp + 
67
-                                    ' | aktuelle Luftfeuchtigkeit: ' + sensor.actualHum + 
61
+                                    ' Sensor ' + sensor.id +
62
+                                    ' | IP: ' + sensor.ip +
63
+                                    ' | aktuelle Temperatur: ' + sensor.actualTemp +
64
+                                    ' | aktuelle Luftfeuchtigkeit: ' + sensor.actualHum +
68 65
                                 '</li>');
69 66
 
70 67
         // Alle Sensoren erfolgreich abgefragt
@@ -82,7 +79,7 @@ function webSocket(wsUri, wsPort, count, clientsCount) {
82 79
         }
83 80
         console.log(evt.data);
84 81
     };
85
-} 
82
+}
86 83
 
87 84
 function generateView(arrSensor, actTime) {
88 85
     $('#contentDiv').append(`<div class="col-md-12 col-lg-12">
@@ -100,10 +97,10 @@ function generateView(arrSensor, actTime) {
100 97
     jQuery.each(arrSensor, function(index, sensor) {
101 98
         $('.nav-pills').append('<li><a class="navtab" data-toggle="tab" href="#' + sensor.id + '">Sensor ' + sensor.id + '</a></li>');
102 99
     });
103
-    
100
+
104 101
     // Flag fuer gemeinsamer Graph -> true
105 102
     generateGraph(true, arrSensor, actTime);
106
-    
103
+
107 104
     $(".navtab").click(function(event) {
108 105
         $('#contentPanel').empty();
109 106
         if(event.target.text == "Home") {
@@ -126,17 +123,17 @@ function generateGraph(flag, sensor, actTime) {
126 123
                             </div>`);
127 124
     if(flag) { // ein Graph für alle Sensoren
128 125
         var length = 0;
129
-        jQuery.each(sensor, function(index, tmp) { 
126
+        jQuery.each(sensor, function(index, tmp) {
130 127
             if(length < tmp.arrEEPROM.length) {
131 128
                 length = tmp.arrEEPROM.length;
132 129
             }
133 130
         });
134 131
 
135
-        var labels = Array();       
132
+        var labels = Array();
136 133
         actHour = actTime.split(":")[0];
137 134
         for(var i = length; i > 0; i--) {
138
-            labels.unshift(actHour + ":00"); 
139
-            actHour = (actHour - 1).mod(24); 
135
+            labels.unshift(actHour + ":00");
136
+            actHour = (actHour - 1).mod(24);
140 137
         }
141 138
         labels.push(actTime);
142 139
 
@@ -145,18 +142,18 @@ function generateGraph(flag, sensor, actTime) {
145 142
 
146 143
         var tmpDataTemperature = Array();
147 144
         var tmpDataHumidity = Array();
148
-        jQuery.each(sensor, function(index, tmp) { 
145
+        jQuery.each(sensor, function(index, tmp) {
149 146
             for(var i = 0; i < (length - tmp.arrEEPROM.length); i++) {
150 147
                 tmpDataTemperature.push([]);
151 148
                 tmpDataHumidity.push([]);
152 149
             }
153
-            jQuery.each(tmp.arrEEPROM, function(index, value) { 
150
+            jQuery.each(tmp.arrEEPROM, function(index, value) {
154 151
                 tmpDataTemperature.push(value['T']);
155
-                tmpDataHumidity.push(value['H']);  
152
+                tmpDataHumidity.push(value['H']);
156 153
             });
157 154
             tmpDataTemperature.push(tmp.actualTemp);
158 155
             tmpDataHumidity.push(tmp.actualHum);
159
-            
156
+
160 157
             var lineColor = getRandomColor();
161 158
             dataTemperature.push({label: "Sensor " + tmp.id, data: tmpDataTemperature, fill: false,
162 159
                 borderWidth: 3, borderColor : lineColor,});
@@ -168,31 +165,31 @@ function generateGraph(flag, sensor, actTime) {
168 165
         });
169 166
     } else { // Graph für einzelnen Sensor
170 167
         var labels = Array();
171
-        
168
+
172 169
         var tmpDataTemperature = Array();
173 170
         var tmpDataHumidity = Array();
174 171
         actHour = actTime.split(":")[0];
175 172
         actHour = (actHour - sensor.arrEEPROM.length).mod(24);
176
-        jQuery.each(sensor.arrEEPROM, function(index, value) { 
173
+        jQuery.each(sensor.arrEEPROM, function(index, value) {
177 174
             actHour = (actHour + 1).mod(24);
178
-            labels.push(actHour + ":00");  
175
+            labels.push(actHour + ":00");
179 176
             tmpDataTemperature.push(value['T']);
180
-            tmpDataHumidity.push(value['H']);  
177
+            tmpDataHumidity.push(value['H']);
181 178
         });
182
-        
179
+
183 180
         labels.push(actTime);
184 181
         tmpDataTemperature.push(sensor.actualTemp);
185 182
         tmpDataHumidity.push(sensor.actualHum);
186 183
 
187
-        var dataTemperature = [{label: 'Temperatur [C]', data: tmpDataTemperature, 
184
+        var dataTemperature = [{label: 'Temperatur [C]', data: tmpDataTemperature,
185
+                                fill: false, borderWidth: 3, borderColor: '#337ab7',}];
186
+        var dataHumidity = [{label: 'Luftfeuchtigkeit [%RH]', data: tmpDataHumidity,
188 187
                                 fill: false, borderWidth: 3, borderColor: '#337ab7',}];
189
-        var dataHumidity = [{label: 'Luftfeuchtigkeit [%RH]', data: tmpDataHumidity, 
190
-                                fill: false, borderWidth: 3, borderColor: '#337ab7',}]; 
191 188
     }
192 189
 
193 190
     var tempCtx = $('#temperaturChart');
194 191
     var humCtx = $('#humidityChart');
195
-    
192
+
196 193
     var tempChart = new Chart(tempCtx, {
197 194
         type: 'line',
198 195
         data: {
@@ -234,4 +231,4 @@ function getRandomColor() {
234 231
         color += letters[Math.floor(Math.random() * 16)];
235 232
     }
236 233
     return color;
237
-}
234
+}

+ 5
- 11
config.h
File diff suppressed because it is too large
View File


+ 64
- 0
convert-static.py View File

@@ -0,0 +1,64 @@
1
+#!/usr/bin/env python3
2
+
3
+# convert-static.py
4
+#
5
+# This program reads the static html and js files and converts them into
6
+# minified strings defined in a C header file.
7
+#
8
+# ----------------------------------------------------------------------------
9
+# "THE BEER-WARE LICENSE" (Revision 42):
10
+# <xythobuz@xythobuz.de> & <ghost-ghost@web.de> wrote this file.  As long as
11
+# you retain this notice you can do whatever you want with this stuff. If we
12
+# meet some day, and you think this stuff is worth it, you can buy us a beer
13
+# in return.                                   Thomas Buck & Christian Högerle
14
+# ----------------------------------------------------------------------------
15
+
16
+def fileToString(filename, js = False):
17
+    f = open(filename, "r")
18
+    sf = ""
19
+    for s in f.readlines():
20
+        st = s
21
+        if js == True:
22
+            ss = s.split("//", 1)
23
+            if ss[0][-1:] == ':':
24
+                st = ss[0] + '//' + ss[1]
25
+            else:
26
+                st = ss[0]
27
+        sf += st
28
+    return sf
29
+
30
+def minify(text, js = False):
31
+    text = text.replace("\r", "")
32
+    text = text.replace("\t", "")
33
+    text = text.replace("  ", " ")
34
+    if js == True:
35
+        text = text.replace("\\", "\\\\")
36
+        text = text.replace("\n", "\\n")
37
+    else:
38
+        text = text.replace("\n", " ")
39
+    text = text.replace("  ", " ")
40
+    text = text.replace("  ", " ")
41
+    text = text.replace("  ", " ")
42
+    text = text.replace("  ", " ")
43
+    text = text.replace("  ", " ")
44
+    text = text.replace("> <", "><")
45
+    if js == True:
46
+        text = text.replace("\\n ", "\\n")
47
+    if (text[-1:] == ' '):
48
+        text = text[:-1]
49
+    return text
50
+
51
+def getAsDefine(name, text):
52
+    return "#define " + name + " \"" + text.replace("\"", "\\\"") + "\""
53
+
54
+template = fileToString("template.html")
55
+template = minify(template)
56
+templates = template.split(" /* %% INSERT_CLIENT_LIST_HERE %% */ ")
57
+
58
+print(getAsDefine("HTML_BEGIN", templates[0]))
59
+print(getAsDefine("HTML_END", templates[1]))
60
+
61
+js = fileToString("client-script.js", True)
62
+js = minify(js, True)
63
+print(getAsDefine("JS_FILE", js))
64
+

+ 48
- 0
template.html View File

@@ -0,0 +1,48 @@
1
+<!DOCTYPE html>
2
+<html lang="en">
3
+    <head>
4
+        <meta charset="utf-8">
5
+        <meta name="viewport" content="width=device-width, initial-scale=1">
6
+        <title>ESP-Weather</title>
7
+        <script src="https://code.jquery.com/jquery-3.1.1.min.js"
8
+                integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
9
+                crossorigin="anonymous"
10
+                defer>
11
+        </script>
12
+        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
13
+            integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
14
+            crossorigin="anonymous"
15
+            defer>
16
+        </script>
17
+        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.bundle.min.js"
18
+            integrity="sha256-RASNMNlmRtIreeznffYMDUxBXcMRjijEaaGF/gxT6vw="
19
+            crossorigin="anonymous"
20
+            defer>
21
+        </script>
22
+        <script src="view.js" defer>
23
+        </script>
24
+        <link rel="stylesheet"
25
+            href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
26
+            integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
27
+            crossorigin="anonymous">
28
+    </head>
29
+    <body>
30
+        <script type="text/javascript">
31
+            /* %% INSERT_CLIENT_LIST_HERE %% */
32
+        </script>
33
+
34
+        <div id="main-part" class="container-fluid">
35
+            <div class="page-header">
36
+                <h2>ESP-Weather</h2>
37
+            </div>
38
+        </div>
39
+        <div class="footer">
40
+            <div class="container-fluid">
41
+                <p class="text-muted">
42
+                    &copy; Copyright 2016 by Christian H&ouml;gerle und Thomas Buck
43
+                </p>
44
+            </div>
45
+        </div>
46
+    </body>
47
+</html>
48
+

Loading…
Cancel
Save