forked from cdump/radiacode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.html
198 lines (196 loc) · 7.24 KB
/
webserver.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>RadiaCode demo</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-apexcharts"></script>
<style>
#app > div {
margin: 5px auto;
width: 80%;
text-align: center;
padding: 5px;
border: 1px #aaa dashed;
}
#app fieldset {
display: inline-block;
border: 0;
padding: 0;
margin-left: 20px;
}
#app .view_controls {
padding-bottom: 1.5em;
}
#app .control_buttons {
padding-bottom: 1.5em;
}
.loading {
color: #666;
}
.error {
color: #df0e0e;
}
</style>
</head>
<body>
<div id="app">
<div v-if="loading" class="loading">Fetching data from Radiacode, please wait (it might take up to 1 minute over bluetooth)...</div>
<div v-else-if="error" class="error">Error reading data, try restarting the application...</div>
<div v-else>
<div>
<apexchart type="bar" height="350" :options="spectrumChartOptions" :series="spectrum_series"></apexchart>
<div class="view_controls">
<fieldset>
<input type="checkbox" id="spectrum_x_accum" v-model="spectrum_accum">
<label for="spectrum_x_accum">Accumulated</label>
</fieldset>
<fieldset>
<input type="radio" id="spectrum_x_channel" :value="false" v-model="spectrum_energy">
<label for="spectrum_x_channel">Channel</label>
<input type="radio" id="spectrum_x_energy" :value="true" v-model="spectrum_energy">
<label for="spectrum_x_energy">Energy</label>
</fieldset>
<fieldset>
<input type="radio" id="spectrum_linear" :value="false" v-model="spectrum_logarithmic">
<label for="spectrum_linear">Linear</label>
<input type="radio" id="spectrum_log" :value="true" v-model="spectrum_logarithmic">
<label for="spectrum_log">Logarithmic</label>
</fieldset>
</div>
<div class="control_buttons">
<button @click="updateSpectrum">Update spectrum</button>
<button @click="resetSpectrum">Reset spectrum</button>
</div>
</div>
<div>
<apexchart type="line" height="350" :options="ratesChartOptions" :series="rates_series"></apexchart>
<div class="control_buttons">
<button @click="toggleRatesAutoupdate">Rates autoupdate: {{ rates_autoupdate ? 'ON' : 'OFF' }}</button>
</div>
</div>
</div>
</div>
<script>
Vue.use(VueApexCharts);
var app = new Vue({
el: '#app',
components: {
apexchart: VueApexCharts,
},
data: function () {
return {
ws: null,
loading: false,
error: false,
spectrum_duration: 0,
rates_autoupdate: true,
rates_series: [],
spectrum_accum: false,
spectrum_series: [],
spectrum_coef: [0, 0, 0],
spectrum_logarithmic: true,
spectrum_energy: true,
ratesChartOptions: {
chart: {
animations: {enabled: false},
zoom: {autoScaleYaxis: true},
},
tooltip: {intersect: false},
grid: {xaxis: {lines: {show: true}}},
dataLabels: {enabled: false},
title: {text: 'CountRate & DoseRate'},
xaxis: {type: 'datetime'},
yaxis: [
{seriesName: 'countrate', title: {text: 'CPS'}, labels: {formatter: (v) => v.toFixed(2) + ' CPS'}},
{seriesName: 'doserate', title: {text: 'μSv/h'}, labels: {formatter: (v) => v.toFixed(4) + ' μSv/h'}, opposite: true},
],
},
};
},
computed: {
spectrumChartOptions: function () {
const a0 = this.spectrum_coef[0], a1 = this.spectrum_coef[1], a2 = this.spectrum_coef[2];
const fmt = this.spectrum_energy ? ((c) => (a0 + a1 * c + a2 * c * c).toFixed(0)) : undefined;
const title = this.spectrum_energy ? 'keV' : 'channel';
return {
chart: {
animations: {enabled: false},
zoom: {autoScaleYaxis: true},
},
tooltip: {intersect: false},
grid: {xaxis: {lines: {show: true}}},
dataLabels: {enabled: false},
title: {text: `Spectrum, ${this.spectrum_duration} seconds`},
xaxis: {type: 'numeric', title: {text: title}, tickAmount: 25, labels: {formatter: fmt}},
yaxis: {logarithmic: this.spectrum_logarithmic, decimalsInFloat: 0},
plotOptions: {bar: {columnWidth: '95%'}},
};
}
},
watch: {
spectrum_accum: function () {
this.updateSpectrum();
}
},
created: function () {
this.ws = new WebSocket('ws://' + window.location.host + '/ws');
this.ws.onmessage = this.onmessage;
this.updateSpectrum();
},
beforeDestroy: function () {
if (this.ws) {
this.ws.close();
}
},
methods: {
onmessage: function (ev) {
if (!this.rates_autoupdate) {
return;
}
const d = JSON.parse(ev.data);
this.rates_series = d.series;
},
updateSpectrum: function () {
this.loading = true;
this.error = false;
fetch(`/spectrum?accum=${this.spectrum_accum}`)
.then(response => {
if (!response.ok) throw new Error('Network response was not ok.');
return response.json();
})
.then(data => {
this.spectrum_duration = data.duration;
this.spectrum_coef = data.coef;
this.spectrum_series = data.series;
this.loading = false;
})
.catch(error => {
console.error('Error fetching spectrum data:', error);
this.loading = false;
this.error = true;
});
},
resetSpectrum: function () {
this.loading = true;
this.error = false;
fetch(`/spectrum/reset`, {method: 'POST'})
.then(response => {
if (!response.ok) throw new Error('Reset failed.');
this.updateSpectrum();
})
.catch(error => {
console.error('Error resetting spectrum data:', error);
this.loading = false;
this.error = true;
});
},
toggleRatesAutoupdate: function () {
this.rates_autoupdate = !this.rates_autoupdate;
}
}
});
</script>
</body>
</html>