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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
<template>
<div class="map-wrapper">
<img src="g7-map.svg">
<div class="rooms">
<span class="title">Rooms</span>
<ul>
<li
:key="room_nr"
v-for="[room_nr, room] in Object.entries(rooms)"
>
<span class="room-nr">{{ room_nr }}. {{ room }}</span>
<span
:style="{color: get_temperature_color(minion_data_map[room_nr])}"
v-if="minion_data_map[room_nr] !== undefined"
>
{{ minion_data_map[room_nr] }}°C
</span>
</li>
</ul>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent, Ref, ref } from "vue";
type MinionInfo = {
ip_address: string
position: number
};
type MinionInfoResponse = {
data: MinionInfo[]
}
type MinionTemperatureResponse = {
data: { temperature: number } | null
error: string | null
}
const rooms = {
1: "Studio / G3",
2: "Studio 1",
3: "Hall",
4: "Studio 2",
5: "Studio 3",
6: "Grupprum",
7: "Verkstad",
8: "Serverrum",
9: "Kurator",
10: "Pannrum",
11: "Toalett",
12: "Inspelningsbås",
13: "G2",
14: "Trapphus"
};
const temperature_colors = {
extremly_hot: "#d00000",
hot: "#dc2f02",
mildly_hot: "#e85d04",
mild: "#52b69a",
cold: "#0077b6",
freezing_cold: "#168aad"
};
export default defineComponent(
{
name: "G7Map",
data() {
return {
rooms: rooms
};
},
methods: {
get_temperature_color(temperature: number) {
if(temperature < 10) {
return temperature_colors.freezing_cold;
}
if(temperature < 20) {
return temperature_colors.cold;
}
if(temperature > 27) {
return temperature_colors.mildly_hot;
}
if(temperature > 31) {
return temperature_colors.hot;
}
if(temperature > 38) {
return temperature_colors.extremly_hot;
}
return temperature_colors.mild;
}
},
setup(props) {
const minion_data_map: Ref<Record<number, number>> = ref({});
async function fetch_minion_ip_addresses() {
let response: Response;
const api_endpoint_url =
`${window.location.protocol}//${window.location.host}/api/v1/minions`;
try {
response = await fetch(api_endpoint_url);
}
catch (err) {
const error = err as Error;
throw Error(`Failed to fetch minion IP addresses. ${error.message}`);
}
const data = await response.json() as MinionInfoResponse;
if(!response.ok) {
throw Error(
"Failed to get minion IP addresses. Responded with status " +
`${response.status}`
);
}
return data.data;
}
async function fetch_minion_temperature(
minion_name: string,
minion_ip_address: string
) {
let response: Response;
try {
response = await fetch(`http://${minion_ip_address}`);
}
catch (err) {
const error = err as Error;
throw Error(
`Failed to fetch temperature for minion "${minion_name}". ` +
`${error.message}`
);
}
const data = await response.json() as MinionTemperatureResponse;
if(!response.ok) {
if(data.error !== null) {
throw Error(
`Minion "${minion_name}" responded with status ` +
`${response.status} and error "${data.error}"`
);
}
throw Error(
`Minion "${minion_name}" responded with status ` +
`${response.status} and a unknown error`
);
}
if(data.data == null) {
throw Error(`Minion "${minion_name}" has no data in response`);
}
return data.data.temperature;
}
async function fetch_all_minion_data() {
const minion_info_arr = await fetch_minion_ip_addresses();
const minion_data_map_buf: Record<number, number> = {};
for(const minion_info of minion_info_arr) {
const temperature =
await fetch_minion_temperature("lol", minion_info.ip_address);
minion_data_map_buf[minion_info.position] = temperature;
}
minion_data_map.value = minion_data_map_buf;
}
return { minion_data_map, fetch_all_minion_data };
},
created() {
this.fetch_all_minion_data();
}
}
);
</script>
<style scoped lang="scss">
@use "sass:map";
@use "@/scss/colors";
@import "@/scss/breakpoints";
.map-wrapper {
display: flex;
flex-wrap: wrap;
row-gap: 5em;
column-gap: 10em;
img {
max-width: 50%;
}
.rooms {
min-width: 40%;
ul li {
line-height: 2.25em;
list-style: none;
.room-nr {
margin-right: 1.5em;
}
}
.title {
font-size: 2rem;
font-weight: 700;
color: colors.$primary;
}
}
}
@media (max-width: map.get($breakpoints, "xl")) {
.map-wrapper img {
max-width: 90%;
}
}
</style>
|