-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdashboard.jsx
More file actions
362 lines (324 loc) · 11.1 KB
/
dashboard.jsx
File metadata and controls
362 lines (324 loc) · 11.1 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
The dashboard screen. This is the first screen to be loaded in and is where the user will
spend most of their time.
This contains the map, live indicator, and GPS data. All of these are imported as components
and are integrated in this file with logic linking them together.
*/
// Base imports
import { useEffect, useRef, useState } from "react"
// 3rd Party Imports
import { Divider } from "@mantine/core"
import {
useLocalStorage,
useSessionStorage,
useViewportSize,
} from "@mantine/hooks"
import {
IconAntenna,
IconBattery2,
IconGps,
IconRadar,
IconSatellite,
IconTarget,
} from "@tabler/icons-react"
import { ResizableBox } from "react-resizable"
// Redux
import { useDispatch, useSelector } from "react-redux"
import { selectConnectedToDrone } from "./redux/slices/droneConnectionSlice"
import {
selectAircraftTypeString,
selectBatteryData,
selectDroneCoords,
selectFlightMode,
selectGPS2RawInt,
selectGPSRawInt,
selectHasSecondaryGps,
selectNotificationSound,
selectRSSI,
soundPlayed,
} from "./redux/slices/droneInfoSlice"
import { selectCurrentMission } from "./redux/slices/missionSlice"
import { selectMessages } from "./redux/slices/statusTextSlice"
import { useSettings } from "./helpers/settings"
// Helper javascript files
import { GPS_FIX_TYPES } from "./helpers/mavlinkConstants"
// Import components
import ForceDisarmModal from "./components/dashboard/ForceDisarmModal"
// Custom component
import useSound from "use-sound"
import FloatingToolbar from "./components/dashboard/floatingToolbar"
import MapSection from "./components/dashboard/map"
import ResizableInfoBox from "./components/dashboard/resizableInfoBox"
import StatusBar, { StatusSection } from "./components/dashboard/statusBar"
import StatusMessages from "./components/dashboard/statusMessages"
import TabsSection from "./components/dashboard/tabsSection"
import TelemetrySection from "./components/dashboard/telemetrySection/telemetry"
import VideoWidget from "./components/dashboard/videoWidget"
import Layout from "./components/layout"
// Tailwind styling
import resolveConfig from "tailwindcss/resolveConfig"
import tailwindConfig from "../tailwind.config"
const tailwindColors = resolveConfig(tailwindConfig).theme.colors
// Sounds
import armSound from "./assets/sounds/armed.mp3"
import disarmSound from "./assets/sounds/disarmed.mp3"
import flightModeChangedSound from "./assets/sounds/flightmodechanged.mp3"
import lowBatterySound from "./assets/sounds/lowbattery.mp3"
import waypointReachedSound from "./assets/sounds/waypointreached.mp3"
export default function Dashboard() {
const dispatch = useDispatch()
const rssi = useSelector(selectRSSI)
const currentFlightModeNumber = useSelector(selectFlightMode)
const aircraftTypeString = useSelector(selectAircraftTypeString)
const { lat, lon } = useSelector(selectDroneCoords)
const batteryData = useSelector(selectBatteryData)
const statustextMessages = useSelector(selectMessages)
const armedNotification = useSelector(selectNotificationSound)
const { fixType, satellitesVisible, hdop } = useSelector(selectGPSRawInt)
const hdopDisplay = hdop != null ? hdop.toFixed(2) : "0.00"
const connectedToDrone = useSelector(selectConnectedToDrone)
const currentMission = useSelector(selectCurrentMission)
const { getSetting } = useSettings()
const gps2 = useSelector(selectGPS2RawInt)
const hasSecondaryGps = useSelector(selectHasSecondaryGps)
const secondaryGpsFixLabel = GPS_FIX_TYPES[gps2.fixType]
// Telemetry panel sizing
const [telemetryPanelSize, setTelemetryPanelSize] = useLocalStorage({
key: "telemetryPanelSize",
defaultValue: { width: 400, height: Infinity },
deserialize: (value) => {
const parsed = JSON.parse(value)
if (parsed === null || parsed === undefined)
return { width: 400, height: Infinity }
return { ...parsed, width: Math.max(parsed["width"], 275) }
},
})
const [telemetryFontSize, setTelemetryFontSize] = useState(
calcBigTextFontSize(),
)
const sideBarRef = useRef()
const [messagesPanelSize, setMessagesPanelSize] = useLocalStorage({
key: "messagesPanelSize",
defaultValue: { width: 600, height: 150 },
})
const { height: viewportHeight, width: viewportWidth } = useViewportSize()
// Following Drone
const [followDrone, setFollowDrone] = useSessionStorage({
key: "followDroneBool",
defaultValue: false,
})
// Map and messages
const mapRef = useRef()
// Sounds
const [playArmed] = useSound(armSound, { volume: 0.1 })
const [playDisarmed] = useSound(disarmSound, { volume: 0.1 })
const [playLowBattery] = useSound(lowBatterySound, { volume: 0.1 })
const [playWaypointReached] = useSound(waypointReachedSound, { volume: 0.1 })
const [playFlightModeChanged] = useSound(flightModeChangedSound, {
volume: 0.1,
})
const currentMissionSeqRef = useRef(currentMission.seq)
// Play queued arming sounds
useEffect(() => {
if (!getSetting("General.speechAnnouncements")) {
return
}
switch (armedNotification) {
case "armed":
playArmed()
break
case "disarmed":
playDisarmed()
break
case "mode_change":
playFlightModeChanged()
break
case "low_battery":
playLowBattery()
break
default:
break
}
dispatch(soundPlayed())
}, [
armedNotification,
playArmed,
playDisarmed,
playLowBattery,
dispatch,
getSetting("General.speechAnnouncements"),
])
useEffect(() => {
if (currentMission.seq > currentMissionSeqRef.current) {
if (!getSetting("General.speechAnnouncements")) {
return
}
playWaypointReached()
dispatch(soundPlayed())
currentMissionSeqRef.current = currentMission.seq
}
}, [
currentMission.seq,
playWaypointReached,
dispatch,
getSetting("General.speechAnnouncements"),
])
// Following drone logic
useEffect(() => {
if (mapRef.current && followDrone && lon !== 0 && lat !== 0) {
mapRef.current.setCenter({ lng: lon, lat: lat })
}
}, [followDrone, lon, lat])
function centerMapOnDrone() {
mapRef.current.getMap().flyTo({
center: [lon, lat],
})
}
function calcBigTextFontSize() {
let w = telemetryPanelSize.width
const BREAKPOINT_SM = 350.0
if (w < BREAKPOINT_SM) return 1.0 - (BREAKPOINT_SM - w) / BREAKPOINT_SM
return 1.0
}
function calcIndicatorSize() {
let sideBarWidth = sideBarRef.current ? sideBarRef.current.clientWidth : 56
return Math.min(telemetryPanelSize.width - (sideBarWidth + 24) * 2, 190)
}
function calcIndicatorPadding() {
let sideBarHeight = sideBarRef.current
? sideBarRef.current.clientHeight
: 164
return (190 - Math.max(calcIndicatorSize(), sideBarHeight)) / 2
}
return (
<Layout currentPage="dashboard">
<div
className="relative flex flex-auto w-full h-full overflow-hidden"
data-testid="dashboard"
>
<div className="w-full">
<MapSection
passedRef={mapRef}
onDragstart={() => {
setFollowDrone(false)
}}
mapId="dashboard"
/>
</div>
<ResizableInfoBox
telemetryPanelSize={telemetryPanelSize}
viewportWidth={viewportWidth}
setTelemetryPanelSize={setTelemetryPanelSize}
setTelemetryFontSize={setTelemetryFontSize}
calcBigTextFontSize={calcBigTextFontSize}
>
{/* Telemetry Information */}
<TelemetrySection
calcIndicatorSize={calcIndicatorSize}
calcIndicatorPadding={calcIndicatorPadding}
telemetryFontSize={telemetryFontSize}
sideBarRef={sideBarRef}
/>
<Divider className="my-2" />
{/* Actions */}
<TabsSection currentFlightModeNumber={currentFlightModeNumber} />
</ResizableInfoBox>
{/* Status Bar */}
<StatusBar className="absolute top-0 right-0">
<StatusSection
icon={<IconRadar />}
value={GPS_FIX_TYPES[fixType]}
tooltip="GPS fix type"
/>
{hasSecondaryGps && (
<StatusSection
icon={<IconRadar />}
value={secondaryGpsFixLabel}
tooltip="GPS2 fix type"
/>
)}
<StatusSection
icon={<IconTarget />}
value={hdopDisplay}
tooltip="GPS HDoP"
/>
<StatusSection
icon={<IconGps />}
value={`(${lat !== undefined ? lat.toFixed(7) : 0}, ${
lon !== undefined ? lon.toFixed(7) : 0
})`}
tooltip="GPS (lat, lon)"
/>
<StatusSection
icon={<IconSatellite />}
value={satellitesVisible}
tooltip="Satellites visible"
/>
<StatusSection
icon={<IconAntenna />}
value={rssi}
tooltip="RC RSSI"
/>
<StatusSection
icon={<IconBattery2 />}
value={
batteryData[0]?.battery_remaining
? `${batteryData[0].battery_remaining}%`
: "0%"
}
tooltip="Battery remaining"
/>
</StatusBar>
{/* Right side floating toolbar */}
<FloatingToolbar
centerMapOnDrone={centerMapOnDrone}
followDrone={followDrone}
setFollowDrone={setFollowDrone}
mapRef={mapRef}
/>
{/* Video Widget for RTSP streams */}
<VideoWidget telemetryPanelWidth={telemetryPanelSize.width} />
<div className="absolute bottom-0 right-0 z-20">
<ResizableBox
height={messagesPanelSize.height}
width={messagesPanelSize.width}
minConstraints={[600, 150]}
maxConstraints={[viewportWidth - 200, viewportHeight - 200]}
resizeHandles={["nw"]}
handle={(_, ref) => (
<span className={"custom-handle-nw"} ref={ref} />
)}
handleSize={[32, 32]}
onResize={(_, { size }) => {
setMessagesPanelSize({ width: size.width, height: size.height })
}}
>
<>
{/* Show a "Waiting for message area" */}
{statustextMessages.length == 0 && (
<StatusMessages
messages={[
{
timestamp: null,
text: connectedToDrone
? `Waiting for messages from ${aircraftTypeString}`
: "Not connected to drone",
severity: 7,
},
]}
className={`bg-[${tailwindColors.falcongrey["TRANSLUCENT"]}] h-full lucent max-w-1/2 object-fill text-xl`}
/>
)}
{/* Show real messages */}
<StatusMessages
messages={statustextMessages}
className={`bg-[${tailwindColors.falcongrey["TRANSLUCENT"]}] h-full lucent max-w-1/2 object-fill text-xl`}
/>
</>
</ResizableBox>
</div>
</div>
<ForceDisarmModal />
</Layout>
)
}