Skip to content

Commit b8ad383

Browse files
redux fix for alerts (#632)
* redux fix for alerts * add min and max altitudes additionally improve number input in settings * optimise altitude alert effect * ensure min altitude alerts are properly initialised --------- Co-authored-by: Kush Makkapati <kush.makkapati@icloud.com>
1 parent 484dd7f commit b8ad383

4 files changed

Lines changed: 78 additions & 48 deletions

File tree

gcs/data/default_settings.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,25 @@
3636
}
3737
},
3838
"Dashboard": {
39-
"altitudeAlerts": {
40-
"description": "Add as many altitude alerts as you want, they'll show when you fall below the set limit",
41-
"default": [100, 90, 80],
39+
"maxAltitudeAlert": {
40+
"description": "You'll be notified when the relative altitude of a drone exceeds this value.",
41+
"default": 120,
42+
"type": "number",
43+
"range": [
44+
0,
45+
100000
46+
],
47+
"display": "Maximum Altitude Alert"
48+
},
49+
"minAltitudeAlerts": {
50+
"description": "You'll be notified when the relative altitude of a drone goes below these values.",
51+
"default": [],
4252
"type": "extendableNumber",
4353
"range": [
4454
0,
4555
100000
4656
],
47-
"display": "Altitude Alerts"
57+
"display": "Minimum Altitude Alerts"
4858
}
4959
},
5060
"Params": {},

gcs/src/components/dashboard/statusBar.jsx

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@
55

66
// Base imports
77
import moment from "moment"
8-
import { cloneElement, useEffect, useState } from "react"
8+
import { cloneElement, useEffect, useRef, useState } from "react"
99

1010
// Third party imports
1111
import { Tooltip } from "@mantine/core"
1212
import { useInterval } from "@mantine/hooks"
1313
import { IconClock, IconNetwork, IconNetworkOff } from "@tabler/icons-react"
1414

15+
// Redux
16+
import { useSelector } from "react-redux"
17+
import { selectTelemetry } from "../../redux/slices/droneInfoSlice"
18+
1519
// Helper imports
1620
import { socket } from "../../helpers/socket"
1721
import GetOutsideVisibilityColor from "../../helpers/outsideVisibility"
18-
import AlertSection from "./alert"
22+
import AlertSection, { AlertCategory, AlertSeverity } from "./alert"
23+
import { useSettings } from "../../helpers/settings"
24+
25+
import { useAlerts } from "./alertProvider"
1926

2027
export function StatusSection({ icon, value, tooltip }) {
2128
return (
@@ -31,13 +38,59 @@ export function StatusSection({ icon, value, tooltip }) {
3138
export default function StatusBar(props) {
3239
const [time, setTime] = useState(moment())
3340
const updateClock = useInterval(() => setTime(moment()), 1000)
41+
const telemetryData = useSelector(selectTelemetry)
3442

3543
// Start clock
3644
useEffect(() => {
3745
updateClock.start()
3846
return updateClock.stop
3947
}, [])
4048

49+
// Alerts
50+
const { getSetting } = useSettings()
51+
const { dispatchAlert, dismissAlert } = useAlerts()
52+
const highestAltitudeRef = useRef(0)
53+
54+
useEffect(() => {
55+
const maxAltitude = getSetting("Dashboard.maxAltitudeAlert")
56+
if (telemetryData.alt > maxAltitude) {
57+
dispatchAlert({
58+
category: AlertCategory.Altitude,
59+
severity: AlertSeverity.Red,
60+
jsx: <>Caution! You've exceeded {maxAltitude}m</>,
61+
})
62+
return
63+
} else {
64+
dismissAlert(AlertCategory.Altitude)
65+
}
66+
67+
if (telemetryData.alt > highestAltitudeRef.current) {
68+
highestAltitudeRef.current = telemetryData.alt
69+
return
70+
}
71+
72+
const minAltitudes = (getSetting("Dashboard.minAltitudeAlerts") ?? []).slice()
73+
minAltitudes.sort((a1, a2) => a1 - a2)
74+
75+
for (const [i, altitude] of minAltitudes.entries()) {
76+
if (highestAltitudeRef.current > altitude && telemetryData.alt < altitude) {
77+
dispatchAlert({
78+
category: AlertCategory.Altitude,
79+
severity:
80+
i == 0
81+
? AlertSeverity.Red
82+
: i == minAltitudes.length - 1
83+
? AlertSeverity.Yellow
84+
: AlertSeverity.Orange,
85+
jsx: <>Caution! You've fallen below {altitude}m</>,
86+
})
87+
return
88+
}
89+
}
90+
91+
dismissAlert(AlertCategory.Altitude)
92+
}, [telemetryData.alt]);
93+
4194
return (
4295
<div className={`${props.className} flex flex-col items-end`}>
4396
<div

gcs/src/components/settingsModal.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ function ExtendableNumberSetting({ settingName, range }) {
102102
<IconTrash size={20} />
103103
</button>
104104
<NumberInput
105-
value={value}
105+
defaultValue={value}
106+
min={range ? range[0] : null}
107+
max={range ? range[1] : null}
106108
onChange={(num) => {
107109
if (!isValidNumber(num, range)) return
108110
updateValue(id, parseInt(num))

gcs/src/dashboard.jsx

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { ResizableBox } from "react-resizable"
2727

2828
// Redux
2929
import { useDispatch, useSelector } from "react-redux"
30+
import { selectConnectedToDrone } from "./redux/slices/droneConnectionSlice"
3031
import {
3132
selectAircraftTypeString,
3233
selectBatteryData,
@@ -35,14 +36,13 @@ import {
3536
selectGPSRawInt,
3637
selectNotificationSound,
3738
selectRSSI,
38-
soundPlayed,
39+
soundPlayed
3940
} from "./redux/slices/droneInfoSlice"
40-
import { selectMessages } from "./redux/slices/statusTextSlice"
4141
import {
4242
notificationShown,
4343
selectNotificationQueue,
4444
} from "./redux/slices/notificationSlice"
45-
import { selectConnectedToDrone } from "./redux/slices/droneConnectionSlice"
45+
import { selectMessages } from "./redux/slices/statusTextSlice"
4646

4747
// Helper javascript files
4848
import { GPS_FIX_TYPES } from "./helpers/mavlinkConstants"
@@ -70,9 +70,6 @@ const tailwindColors = resolveConfig(tailwindConfig).theme.colors
7070
// Sounds
7171
import armSound from "./assets/sounds/armed.mp3"
7272
import disarmSound from "./assets/sounds/disarmed.mp3"
73-
// import { AlertCategory, AlertSeverity } from "./components/dashboard/alert"
74-
// import { useAlerts } from "./components/dashboard/alertProvider"
75-
// import { useSettings } from "./helpers/settings"
7673

7774
export default function Dashboard() {
7875
const dispatch = useDispatch()
@@ -135,7 +132,7 @@ export default function Dashboard() {
135132
// Show queued notifications
136133
useEffect(() => {
137134
if (notificationQueue.length !== 0) {
138-
;(notificationQueue[0].type === "error"
135+
; (notificationQueue[0].type === "error"
139136
? showErrorNotification
140137
: showSuccessNotification)(notificationQueue[0].message)
141138
dispatch(notificationShown())
@@ -155,37 +152,6 @@ export default function Dashboard() {
155152
})
156153
}
157154

158-
// NEED TO FIX THIS BEFORE WE MERGE KUSH/BEN
159-
// Alerts
160-
// const { getSetting } = useSettings()
161-
// const { dispatchAlert, dismissAlert } = useAlerts()
162-
// const highestAltitudeRef = useRef(0)
163-
164-
// function updateAltitudeAlert(msg) {
165-
// if (msg.alt > highestAltitudeRef.current)
166-
// return (highestAltitudeRef.current = msg.alt)
167-
// const altitudes = getSetting("Dashboard.altitudeAlerts")
168-
// altitudes.sort((a1, a2) => a1 - a2)
169-
170-
// for (const [i, altitude] of altitudes.entries()) {
171-
// if (highestAltitudeRef.current > altitude && msg.alt < altitude) {
172-
// dispatchAlert({
173-
// category: AlertCategory.Altitude,
174-
// severity:
175-
// i == 0
176-
// ? AlertSeverity.Red
177-
// : i == altitudes.length - 1
178-
// ? AlertSeverity.Yellow
179-
// : AlertSeverity.Orange,
180-
// jsx: <>Caution! You've fallen below {altitude}m</>,
181-
// })
182-
// return
183-
// }
184-
// }
185-
186-
// dismissAlert(AlertCategory.Altitude)
187-
// }
188-
189155
function calcBigTextFontSize() {
190156
let w = telemetryPanelSize.width
191157
const BREAKPOINT_SM = 350.0
@@ -248,9 +214,8 @@ export default function Dashboard() {
248214
/>
249215
<StatusSection
250216
icon={<IconGps />}
251-
value={`(${lat !== undefined ? lat.toFixed(6) : 0}, ${
252-
lon !== undefined ? lon.toFixed(6) : 0
253-
})`}
217+
value={`(${lat !== undefined ? lat.toFixed(6) : 0}, ${lon !== undefined ? lon.toFixed(6) : 0
218+
})`}
254219
tooltip="GPS (lat, lon)"
255220
/>
256221
<StatusSection

0 commit comments

Comments
 (0)