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
|
<template>
<div class="map-wrapper">
<img src="g7-map.svg">
<div class="rooms">
<span class="title">Rooms</span>
<ul>
<li
:key="room"
v-for="[index, room] in Object.entries(rooms)"
>
{{ index }}. {{ room }}
</li>
</ul>
</div>
<div>
{{ minion_data_arr }}
</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
}
type MinionData = {
temperature: number
position: number
}
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"
};
export default defineComponent(
{
name: "G7Map",
data() {
return {
rooms: rooms
};
},
setup(props) {
const minion_data_arr: Ref<MinionData[]> = 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_arr_buf: MinionData[] = [];
for(const minion_info of minion_info_arr) {
const temperature =
await fetch_minion_temperature("lol", minion_info.ip_address);
minion_data_arr_buf.push({
temperature: temperature,
position: minion_info.position
});
}
minion_data_arr.value = minion_data_arr_buf;
}
return { minion_data_arr, 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: 1.75em;
list-style: none;
}
.title {
font-size: 2rem;
font-weight: 700;
color: colors.$primary;
}
}
}
@media (max-width: map.get($breakpoints, "xl")) {
.map-wrapper img {
max-width: 90%;
}
}
</style>
|