-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraf.html
175 lines (156 loc) · 5.85 KB
/
graf.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
<html>
<head>
<title>BlueSensor</title>
<style type="text/css">
table td {
text-align: right;
}
.demo-container {
box-sizing: border-box;
width: 1000px;
height: 400px;
padding: 20px 15px 15px 15px;
margin: 15px auto 30px auto;
border: 1px solid #ddd;
background: #fff;
background: linear-gradient(#f6f6f6 0, #fff 50px);
background: -o-linear-gradient(#f6f6f6 0, #fff 50px);
background: -ms-linear-gradient(#f6f6f6 0, #fff 50px);
background: -moz-linear-gradient(#f6f6f6 0, #fff 50px);
background: -webkit-linear-gradient(#f6f6f6 0, #fff 50px);
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);
-o-box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
-ms-box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
-moz-box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
-webkit-box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
}
.demo-graph {
width: 100%;
height: 100%;
font-size: 14px;
line-height: 1.2em;
}
</style>
<script language="javascript" type="text/javascript" src="/static/jquery.js"></script>
<script language="javascript" type="text/javascript" src="/static/jquery.flot.js"></script>
<script language="javascript" type="text/javascript" src="/static/jquery.flot.time.js"></script>
<script type="text/javascript">
<!--
data = {
"metadata": {
"device_name": "Raw sensor",
"device_id": "RAW1",
"device_location": "IJS",
"sensors": {
// "key": [ "Label", "Sensor name", "Unit", "Color" ]
"gas1": [ "Gas 1", "Gas 1", "raw value", "black" ],
"gas2": [ "Gas 2", "Gas 2", "raw value", "green" ],
"humidity": [ "Hum 1", "DHT-22", "%", "blue" ],
"temp1": [ "Temp 1", "DHT-22", "\\u00b0C", "orange" ],
"temp2": [ "Temp 2", "DS18B20", "\\u00b0C", "red" ],
"temp3": [ "Temp 3", "DS18B20", "\\u00b0C", "yellow" ]
}
},
"time": 1518648710000, // msec
"data": {
// "key": "Value %.2f"
"gas1": 2.62,
"gas2": 3.75,
"humidity": 49.71,
"temp1": 25.07,
"temp2": 26,
"temp3": 23.1
}
}
-->
$(function () {
var maxrecords = 300;
var ds = {};
// "key": { label: "Label", color: "#000000", data: [] }
var choiceContainer = $("#choices");
var container_exist = false;
$("#title").html("BlueSensor (<i>waiting for data from USB...</i>)");
wsock = new WebSocket("ws://" + document.location.host + "/data");
wsock.onmessage = function (event) {
var rs = JSON.parse(event.data);
var skey = "metadata";
if (skey in rs) {
rm = rs[skey];
skey = "sensors";
if (skey in rm) {
rms = rm[skey];
for (var dkey in rms) {
if (!ds[dkey]) {
ds[dkey] = { label: dkey, color: "#000000", data: [] };
}
ds[dkey].label = rms[dkey][0];
ds[dkey].color = rms[dkey][3];
}
}
if (!container_exist) {
for (var key in ds) {
choiceContainer.append("<input type='checkbox' name='" +
key + "' checked='checked' id='id_" +
key + "' style='display:inline-block; font-size: small;'></input>" +
" <label for='id_" + key + "' id='lb_" +
key + "' style='display:inline-block; font-size: small;'>" +
ds[key].label + "</label><br>");
}
}
container_exist = true;
skey = "device_name";
if (skey in rm) {
var titl = rm[skey];
skey = "device_location";
if (skey in rm) {
titl = titl + " (" + rm[skey] + ")";
}
$("#title").html(titl);
}
}
skey = "time";
if (skey in rs) { t = rs[skey]; }
else { t = Date.now(); }
skey = "data";
if (skey in rs) {
rd = rs[skey];
for (var dkey in rd) {
if (!ds[dkey]) {
ds[dkey] = { label: dkey, color: "#000000", data: [] };
}
ds[dkey].data.push([t, rd[dkey]]);
if (ds[dkey].data.length > maxrecords) {
ds[dkey].data = ds[dkey].data.slice(ds[dkey].data.length - maxrecords, ds[dkey].data.length);
}
}
}
drawGraph();
};
drawGraph = function () {
var disp = [];
choiceContainer.find("input:checked").each(function () {
if ($(this).is(":visible")) {
var key = $(this).attr("name");
if (key && ds[key]) {
disp.push(ds[key]);
}
}
});
$.plot("#graf", disp, {
lines: { show: true},
xaxis: { mode: "time", minTickSize: [1, "second"] }
});
};
});
</script>
</head>
<body>
<h1 id="title" style="color: #993300; text-align: center; font-family:verdana;">
BlueSensor initialisation...
</h1>
<div class="demo-container">
<div id="graf" class="demo-graph" style="float:left; width:85%;"></div>
<p id="choices" style="float:right; width:15%;"></p>
</div>
</body>
</html>