-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
164 lines (154 loc) · 4.25 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
<html>
<head>
<title>Space Heater Dashboard</title>
<meta charset="UTF-8">
<script src="node_modules/chart.js/dist/Chart.bundle.js"></script>
<script src="node_modules/chartjs-plugin-colorschemes/dist/chartjs-plugin-colorschemes.min.js"></script>
<link rel="stylesheet" type="text/css" href="node_modules/chart.js/dist/Chart.bundle.css"></head>
<style type="text/css">
body {
background-color: #121212;
color: #aaa;
margin: 0 auto;
text-align: center;
}
.wrap {
margin: 0 auto;
width: 90%;
}
#status {
width: 100px;
margin: 0 auto;
}
#statusIcon {
width: 10px;
height: 10px;
border: 1px solid #eee;
float: left;
}
.connected #statusIcon {
background-color: green;
}
.disconnected #statusIcon {
background-color: red;
}
#currentTemp {
font-size: 10em;
font-family: sans-serif;
color: #aaa;
}
</style>
</head>
<body>
<div class='wrap'>
<canvas id="myChart"></canvas>
<div id="status">
<div id="statusIcon"></div>
<pre id="statusText"></pre>
</div>
<div id="currentTemp"></div>
</div>
</body>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: {
datasets:[{
fill: 'origin',
label: "Current temp (F)",
data: []
},{
fill: false,
borderDash: [3,3],
label: "Max temp (F)",
data: [],
type: 'line',
},{
fill: false,
borderDash: [3,3],
label: "Min temp (F)",
data: [],
type: 'line',
}]
},
options: {
plugins: {
colorschemes: {
scheme: 'brewer.Accent5'
}
},
elements: {
point: {
radius: 0
}
},
scales: {
xAxes: [
{
type: 'time',
distribution: 'linear',
time: {
unit: 'minute'
}
}
]
}
}
});
function updateStatus(str) {
var parentStatusElement = document.getElementById("status");
var statusText = document.getElementById("statusText");
statusText.innerHTML = str;
if(str == "disconnected" || str == "connected") {
parentStatusElement.className = str;
} else {
parentStatusElement.className = "";
}
console.log(str);
}
function updateTempDisplay(newTemp) {
document.getElementById("currentTemp").innerHTML = newTemp + "° F";
}
function updateChart(object) {
var time = new Date();
console.log("Rolling: " + object.rollingAvgTempF + " Actual: " + object.tempF);
chart.data.datasets[0].data.push({
x: time,
y: parseFloat(object.rollingAvgTempF)
});
chart.data.datasets[1].data.push({
x: time,
y: parseFloat(object.desiredTemp)
});
chart.data.datasets[2].data.push({
x: time,
y: parseFloat(object.lowTemp)
});
chart.update();
};
function connectToServer() {
var ws = new WebSocket('ws://localhost:9898/');
var interval;
updateStatus("connecting...");
ws.onopen = function() {
updateStatus("connected");
ws.send("HELO");
};
ws.onclose = function() {
updateStatus("disconnected");
clearInterval(interval);
setTimeout(connectToServer, 1000);
};
ws.onmessage = function(e) {
var object = JSON.parse(e.data);
updateTempDisplay(object.tempF);
updateChart(object);
};
interval = setInterval(function() {
ws.send("HELO");
}, 15000);
}
connectToServer();
</script>
</html>