This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobservations.html
183 lines (154 loc) · 6.44 KB
/
observations.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
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>EntraOS Observations</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.highcharts.com/highcharts.src.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.2/mqttws31.js" type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="config.js" type="text/javascript"></script>
<script type="text/javascript">
var mqtt;
var reconnectTimeout = 2000;
let count = 0;
let chart;
function MQTTconnect() {
mqtt = new Paho.MQTT.Client(
host,
port,
subscriptionClientId);
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 (subscriptionClientId != null) {
options.userName = subscriptionClientId;
options.password = subscriptionClientPassword;
}
console.log("Host=" + host + ", port=" + port + " TLS = " + useTLS + " username=" + subscriptionClientId + " password=" + subscriptionClientPassword);
mqtt.connect(options);
}
function onConnect() {
$('#status').val('Connected to ' + host + ':' + port);
// Connection succeeded; subscribe to our topic
mqtt.subscribe(topic, {qos: 0});
$('#topic').val(topic);
}
function onConnectionLost(response) {
setTimeout(MQTTconnect, reconnectTimeout);
$('#status').val("connection lost: " + response.errorMessage + ". Reconnecting");
};
function onMessageArrived(message) {
var topic = message.destinationName;
var payload = message.payloadString;
try {
const observation = JSON.parse(payload);
count++;
console.log("Received ", count, " observations.");
const observationName = observation.floor + "-" + observation.placementRoom + "-" + observation.climateZone;
// const observationName = "Temp";
const seriesFilter = (element) => element.name == observationName;
var seriesIndex = options.series.findIndex(seriesFilter);
console.log("SeriesIndex for ", observationName, " is: ", seriesIndex);
if (seriesIndex == -1) {
options.series.push({
name: observationName,
data: []
});
chart.addSeries({
name: observationName,
data: []
});
seriesIndex = options.series.findIndex(seriesFilter);
}
const observationValue = observation.observedValue;
const point = [new Date().getTime(), observationValue];
const series = chart.series[seriesIndex];
const shift = series.data && series.data.length > 200; // shift if the series is longer than 200
series.addPoint(point, true, shift);
} catch (e) {
console.warn("Failed to add point. Payload ", payload, " reason: ", e);
}
$('#ws').prepend('<li>' + topic + ' = ' + payload + '</li>');
};
$(document).ready(function () {
MQTTconnect();
});
/**
* Request data from the server, add it to the graph and set a timeout to request again
*/
async function requestData() {
const result = await fetch('https://demo-live-data.highcharts.com/time-rows.json');
if (result.ok) {
const data = await result.json();
const [date, value] = data[0];
const point = [new Date(date).getTime(), value * 10];
const series = chart.series[0],
shift = series.data.length > 200; // shift if the series is longer than 20
// add the point
chart.series[0].addPoint(point, true, shift);
// call it again after one second
// setTimeout(requestData, 1000);
}
}
function generateRandomTemp() {
const point = [new Date().getTime(), (Math.random() * 10) + 10];
const series = chart.series[0],
shift = series.data.length > 200; // shift if the series is longer than 20
chart.series[0].addPoint(point, true, shift);
// call it again after one second
setTimeout(generateRandomTemp, 30000);
};
var options = {
chart: {
renderTo: 'container',
defaultSeriesType: 'spline',
events: {
// load: requestData
}
},
title: {
text: 'Observations'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150,
maxZoom: 20 * 1000
},
yAxis: {
minPadding: 0.2,
maxPadding: 0.2,
title: {
text: 'Value',
margin: 80
}
},
series: [{
name: 'Random Temp',
data: []
}]
};
window.addEventListener('load', function () {
chart = new Highcharts.Chart(options);
generateRandomTemp();
});
</script>
</head>
<body>
<h1>EntraOS Observations</h1>
<div id="container" style="width:100%; height:400px;"></div>
<div>
<div>Subscribed to <input type='text' id='topic' disabled/>
Status: <input type='text' id='status' size="80" disabled/></div>
<ul id='ws' style="font-family: 'Courier New', Courier, monospace;"></ul>
</div>
</body>
</html>