Browse Source

Working on JS, added test.html for local use.

Thomas Buck 7 years ago
parent
commit
2d72356640
4 changed files with 148 additions and 48 deletions
  1. 70
    38
      client-script.js
  2. 3
    3
      static.h
  3. 15
    7
      template.html
  4. 60
    0
      test.html

+ 70
- 38
client-script.js View File

@@ -10,8 +10,9 @@
10 10
 // in return.                                   Thomas Buck & Christian Högerle
11 11
 // ----------------------------------------------------------------------------
12 12
 
13
-var arrSensor = Array();
13
+var arrSensor = Array(); // Data received from Websockets
14 14
 
15
+// Text Strings. Change these to translate.
15 16
 textAvailableSensors = "Available Sensors";
16 17
 textButtonNext = "Continue";
17 18
 homeTabName = "Home";
@@ -23,28 +24,36 @@ humidityLabel = 'Humidity [%RH]';
23 24
 temperatureHeading = "Temperature";
24 25
 humidityHeading = "Humidity";
25 26
 
26
-$(document).ready(function() {
27
-    $('#main-part').append(`
28
-        <div class="row" id="contentDiv">
29
-            <div class="col-md-5 col-lg-5">
30
-                <div class="panel panel-primary">
31
-                    <div class="panel-heading" id="listSensorsHeading">
32
-                        ` + textAvailableSensors + ` (0/0)
33
-                    </div>
34
-                    <div class="panel-body">
35
-                        <ul class="list-group" id="listSensors"></ul>
36
-                        <div id="alertDiv"></div>
37
-                        <button class="btn btn-primary" disabled="" id="btnSubmit">
38
-                            ` + textButtonNext + `
39
-                        </button>
40
-                    </div>
41
-                </div>
42
-            </div>
43
-        </div>`);
27
+// Colors
28
+singleChartTempColor = "#337ab7";
29
+singleChartHumidColor = "#337ab7";
30
+preDefinedColors = Array(
31
+    "#337ab7", "#ff0000", "#00ff00"
32
+);
33
+
34
+// Get current client-time for the graph X-axis labels
35
+var actTime = new Date();
36
+actTime = actTime.getHours() + ":" + (actTime.getMinutes() < 10 ? '0':'') + actTime.getMinutes();
44 37
 
45
-    // Get current client-time for the graph X-axis labels
46
-    var actTime = new Date();
47
-    actTime = actTime.getHours() + ":" + (actTime.getMinutes() < 10 ? '0':'') + actTime.getMinutes();
38
+// Draw initial view when the page has been loaded
39
+$(document).ready(initialView);
40
+
41
+function initialView() {
42
+    $('#contentDiv').empty();
43
+    $('#contentDiv').append(`<div class="col-sm-12 col-md-12 col-lg-12">
44
+        <div class="panel panel-primary">
45
+            <div class="panel-heading" id="listSensorsHeading">
46
+                ` + textAvailableSensors + ` (0/0)
47
+            </div>
48
+            <div class="panel-body">
49
+                <ul class="list-group" id="listSensors"></ul>
50
+                <div id="alertDiv"></div>
51
+                <button class="btn btn-primary" disabled="" id="btnSubmit">
52
+                    ` + textButtonNext + `
53
+                </button>
54
+            </div>
55
+        </div>
56
+    </div>`);
48 57
 
49 58
     $('#listSensorsHeading').empty();
50 59
     $('#listSensorsHeading').html(textAvailableSensors + " (0/" + clients.length + ")");
@@ -58,9 +67,9 @@ $(document).ready(function() {
58 67
     // Button to continue to graph view
59 68
     $("#btnSubmit").click(function(event) {
60 69
         $('#contentDiv').empty();
61
-        generateView(arrSensor, actTime);
70
+        generateView(arrSensor);
62 71
     });
63
-});
72
+}
64 73
 
65 74
 function webSocket(wsUri, wsPort, count, clientsCount) {
66 75
     websocket = new WebSocket("ws://" + wsUri + ":" + wsPort + "/");
@@ -102,8 +111,8 @@ function webSocket(wsUri, wsPort, count, clientsCount) {
102 111
     };
103 112
 }
104 113
 
105
-function generateView(arrSensor, actTime) {
106
-    $('#contentDiv').append(`<div class="col-md-12 col-lg-12">
114
+function generateView(arrSensor) {
115
+    $('#contentDiv').append(`<div class="col-sm-12 col-md-12 col-lg-12">
107 116
                                 <div class="panel panel-primary">
108 117
                                     <ul class="nav nav-pills">
109 118
                                         <li class="active"><a class="navtab" data-toggle="tab" href="#home">` + homeTabName + `</a></li>
@@ -120,26 +129,30 @@ function generateView(arrSensor, actTime) {
120 129
     });
121 130
 
122 131
     // flag for combined plot -> true
123
-    generateGraph(true, arrSensor, actTime);
132
+    generateGraph(true, arrSensor);
124 133
 
125 134
     $(".navtab").click(function(event) {
126 135
         $('#contentPanel').empty();
127 136
         if(event.target.text == homeTabName) {
128 137
             // flag for combined plot -> true
129
-            generateGraph(true, arrSensor, actTime);
138
+            generateGraph(true, arrSensor);
130 139
         } else {
131
-            generateGraph(false, arrSensor[(event.target.text.split(" ")[1] - 1)], actTime);
140
+            generateGraph(false, arrSensor[(event.target.text.split(" ")[1] - 1)]);
132 141
         }
133 142
     });
134 143
 }
135 144
 
136
-function generateGraph(flag, sensor, actTime) {
145
+function generateGraph(flag, sensor) {
137 146
     $('#contentPanel').append(`<div class="row">
138 147
                                 <div class="col-sm-12 col-md-12 col-lg-6">
139
-                                    <canvas id="temperaturChart"></canvas>
148
+                                    <div id="temperatureDiv" class="embed-responsive embed-responsive-4by3">
149
+                                        <canvas id="temperatureChart"></canvas>
150
+                                    </div>
140 151
                                 </div>
141 152
                                 <div class="col-sm-12 col-md-12 col-lg-6">
142
-                                    <canvas id="humidityChart"></canvas>
153
+                                    <div id="humidityDiv" class="embed-responsive embed-responsive-4by3">
154
+                                        <canvas id="humidityChart"></canvas>
155
+                                    </div>
143 156
                                 </div>
144 157
                             </div>`);
145 158
     if (flag) {
@@ -176,7 +189,7 @@ function generateGraph(flag, sensor, actTime) {
176 189
             tmpDataTemperature.push(tmp.currentTemp);
177 190
             tmpDataHumidity.push(tmp.currentHum);
178 191
 
179
-            var lineColor = getRandomColor();
192
+            var lineColor = getColor(index);
180 193
             dataTemperature.push({label: sensorTabName + " " + tmp.id, data: tmpDataTemperature, fill: false,
181 194
                 borderWidth: 3, borderColor : lineColor,});
182 195
             dataHumidity.push({label: sensorTabName + " " + tmp.id, data: tmpDataHumidity, fill: false,
@@ -205,14 +218,19 @@ function generateGraph(flag, sensor, actTime) {
205 218
         tmpDataHumidity.push(sensor.currentHum);
206 219
 
207 220
         var dataTemperature = [{label: temperatureLabel, data: tmpDataTemperature,
208
-                                fill: false, borderWidth: 3, borderColor: '#337ab7',}];
221
+                                fill: false, borderWidth: 3, borderColor: singleChartTempColor,}];
209 222
         var dataHumidity = [{label: humidityLabel, data: tmpDataHumidity,
210
-                                fill: false, borderWidth: 3, borderColor: '#337ab7',}];
223
+                                fill: false, borderWidth: 3, borderColor: singleChartHumidColor,}];
211 224
     }
212 225
 
213
-    var tempCtx = $('#temperaturChart');
226
+    var tempCtx = $('#temperatureChart');
214 227
     var humCtx = $('#humidityChart');
215 228
 
229
+    //tempCtx.attr('width', $('#temperatureDiv').width());
230
+    //tempCtx.attr('height', $('#temperatureDiv').height());
231
+    //humCtx.attr('width', $('#humidityDiv').width());
232
+    //humCtx.attr('height', $('#humidityDiv').height());
233
+
216 234
     var tempChart = new Chart(tempCtx, {
217 235
         type: 'line',
218 236
         data: {
@@ -223,7 +241,10 @@ function generateGraph(flag, sensor, actTime) {
223 241
             title: {
224 242
                 display: true,
225 243
                 text: temperatureHeading
226
-            }
244
+            },
245
+            responsive: true,
246
+            maintainAspectRatio: true,
247
+            scaleOverride: true
227 248
         }
228 249
     });
229 250
 
@@ -237,7 +258,10 @@ function generateGraph(flag, sensor, actTime) {
237 258
             title: {
238 259
                 display: true,
239 260
                 text: humidityHeading
240
-            }
261
+            },
262
+            responsive: true,
263
+            maintainAspectRatio: true,
264
+            scaleOverride: true
241 265
         }
242 266
     });
243 267
 }
@@ -247,6 +271,14 @@ Number.prototype.mod = function(n) {
247 271
     return ((this%n)+n)%n;
248 272
 }
249 273
 
274
+function getColor(index) {
275
+    if (index < preDefinedColors.length) {
276
+        return preDefinedColors[index];
277
+    } else {
278
+        return getRandomColor();
279
+    }
280
+}
281
+
250 282
 function getRandomColor() {
251 283
     var letters = '0123456789ABCDEF'.split('');
252 284
     var color = '#';

+ 3
- 3
static.h
File diff suppressed because it is too large
View File


+ 15
- 7
template.html View File

@@ -19,13 +19,13 @@
19 19
             crossorigin="anonymous"
20 20
             defer>
21 21
         </script>
22
-        <script src="view.js" defer>
23
-        </script>
24 22
         <link rel="stylesheet"
25 23
             href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
26 24
             integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
27 25
             crossorigin="anonymous">
28 26
         <link rel="stylesheet" href="style.css" type="text/css">
27
+        <script src="view.js" defer>
28
+        </script>
29 29
         <link rel="icon" href="favicon.ico" type="image/x-icon">
30 30
     </head>
31 31
     <body>
@@ -33,15 +33,23 @@
33 33
             /* %% INSERT_CLIENT_LIST_HERE %% */
34 34
         </script>
35 35
 
36
-        <div id="main-part" class="container-fluid">
36
+        <div id="main-part" class="container">
37 37
             <div class="page-header">
38
-                <h2>ESP-Weather</h2>
38
+                <h2>ESP-Weather <small>v2</small></h2>
39 39
             </div>
40 40
         </div>
41 41
         <div class="footer">
42
-            <div class="container-fluid">
43
-                <p class="text-muted">
44
-                    &copy; Copyright 2016 by Christian H&ouml;gerle und Thomas Buck
42
+            <div class="container">
43
+                <p class="text-muted text-center">
44
+                    &copy; Copyright 2016 by Christian H&ouml;gerle and Thomas Buck
45
+                    <br>
46
+                    <small>
47
+                        <small>
48
+                            <a href="https://github.com/xythobuz/ESP-Weather">
49
+                                ESP-Weather is free software!
50
+                            </a>
51
+                        </small>
52
+                    </small>
45 53
                 </p>
46 54
             </div>
47 55
         </div>

+ 60
- 0
test.html View File

@@ -0,0 +1,60 @@
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
+        <link rel="stylesheet"
23
+            href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
24
+            integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
25
+            crossorigin="anonymous">
26
+        <link rel="stylesheet" href="client-style.css" type="text/css">
27
+        <script src="client-script.js" defer>
28
+        </script>
29
+        <link rel="icon" href="favicon.ico" type="image/x-icon">
30
+    </head>
31
+    <body>
32
+        <script type="text/javascript">
33
+            var clients = Array('192.168.0.173');
34
+        </script>
35
+
36
+        <div id="main-part" class="container">
37
+            <div class="page-header">
38
+                <h2>ESP-Weather <small>v2</small></h2>
39
+            </div>
40
+            <div class="row" id="contentDiv">
41
+            </div>
42
+        </div>
43
+        <div class="footer">
44
+            <div class="container">
45
+                <p class="text-muted text-center">
46
+                    &copy; Copyright 2016 by Christian H&ouml;gerle and Thomas Buck
47
+                    <br>
48
+                    <small>
49
+                        <small>
50
+                            <a href="https://github.com/xythobuz/ESP-Weather">
51
+                                ESP-Weather is free software!
52
+                            </a>
53
+                        </small>
54
+                    </small>
55
+                </p>
56
+            </div>
57
+        </div>
58
+    </body>
59
+</html>
60
+

Loading…
Cancel
Save