From 16785dd239ad6a80235ba4c32896411a0f608961 Mon Sep 17 00:00:00 2001 From: Miha Drofenik Date: Sun, 7 Apr 2024 11:34:01 +0200 Subject: [PATCH] Changed stay awake command to use battery instead of id (#43) --- src/ble/types.ts | 2 +- src/hooks/useBle.ts | 4 +- src/navigation/screens/TerminalScreen.tsx | 81 +++++++++++++++++++++-- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/ble/types.ts b/src/ble/types.ts index 11571df..222e90d 100644 --- a/src/ble/types.ts +++ b/src/ble/types.ts @@ -100,7 +100,7 @@ export const COMMANDS: { [CommandNames.HEARTBEAT]: { name: CommandNames.HEARTBEAT, readCommand: "get heartbeat", - readRegex: /\bheartbeat\s+is\s+(\d+s)\b/, + readRegex: /\bheartbeat\s+is\s+(\d+d|\d+h|\d+m|\d+s)\b/, writeCommand: (value?: string) => `heartbeat ${value}`, }, [CommandNames.DEVEUI]: { diff --git a/src/hooks/useBle.ts b/src/hooks/useBle.ts index 2c07f3e..0971746 100644 --- a/src/hooks/useBle.ts +++ b/src/hooks/useBle.ts @@ -314,7 +314,7 @@ export const useBle = (): ReturnType => { const ping = () => guard(() => write(newPeripheral, [ - [CommandNames.ID, { control: CommandControlTypes.READ }], + [CommandNames.BATTERY, { control: CommandControlTypes.READ }], ]), ) @@ -322,7 +322,7 @@ export const useBle = (): ReturnType => { newPeripheral.connected = true newPeripheral.intervals = { - ping: setInterval(async () => await ping(), 20000), + ping: setInterval(async () => await ping(), 40000), } dispatch(deviceUpdate({ ...newPeripheral })) diff --git a/src/navigation/screens/TerminalScreen.tsx b/src/navigation/screens/TerminalScreen.tsx index e172f76..58bbce1 100644 --- a/src/navigation/screens/TerminalScreen.tsx +++ b/src/navigation/screens/TerminalScreen.tsx @@ -22,6 +22,7 @@ import { Button, Divider, IconButton, + RadioButton, Switch, TextInput, } from "react-native-paper" @@ -51,6 +52,7 @@ export const Terminal = ({ embed }: Props) => { const configuration = useAppSelector((state) => state.configuration) const config = configuration[deviceId] const { spacing, colors, appPadding } = useExtendedTheme() + const { get: getBattery, commandLoading: batteryLoading } = useCommand({ deviceId, command: COMMANDS.BATTERY, @@ -59,6 +61,10 @@ export const Terminal = ({ embed }: Props) => { deviceId, command: COMMANDS.VERSION, }) + const { get: getId, commandLoading: idLoading } = useCommand({ + deviceId, + command: COMMANDS.ID, + }) const { set: setHb, commandLoading: hbLoading } = useCommand({ deviceId, command: COMMANDS.HEARTBEAT, @@ -85,6 +91,7 @@ export const Terminal = ({ embed }: Props) => { const [autoscroll, setAutoscroll] = useState(true) const [heartbeat, setHeartbeat] = useState() + const [heartbeatTime, setHeartbeatTime] = useState("s") const [appEui, setAppEui] = useState("") const [devEui, setDevEui] = useState("") const [localSensor, setLocalSensor] = useState() @@ -102,7 +109,7 @@ export const Terminal = ({ embed }: Props) => { const triggerHeartbeat = () => { if (heartbeat && !hbLoading) { - setHb(`${heartbeat}s`) + setHb(`${heartbeat}${heartbeatTime}`) } } @@ -266,16 +273,32 @@ export const Terminal = ({ embed }: Props) => { - ID and Version + ID - + {ID.loaded && ( - ID: {ID.value} + ID:{" "} + {idLoading ? ( + "Loading..." + ) : ( + {ID.value} + )} )} + + + + + + + Version + + {VERSION.loaded && ( @@ -358,7 +381,9 @@ export const Terminal = ({ embed }: Props) => { {hbLoading ? ( "Loading..." ) : ( - {HEARTBEAT.value} + + {formatHeartbeat(HEARTBEAT.value)} + )} )} @@ -376,6 +401,28 @@ export const Terminal = ({ embed }: Props) => { Change Heartbeat + + + You are setting the value in: {formatHeartbeat(heartbeatTime)} + + + + + + + + + + + + + + + + App EUI @@ -477,6 +524,26 @@ export const Terminal = ({ embed }: Props) => { ) } +const formatHeartbeat = (s?: string) => { + if (!s) return "" + + const heartbeat = s.slice(0, -1) + const time = s.slice(-1) + + if (!["d", "h", "m", "s"].includes(time)) return "Invalid input" + + switch (time) { + case "d": + return `${heartbeat} days`.trim() + case "h": + return `${heartbeat} hours`.trim() + case "m": + return `${heartbeat} minutes`.trim() + default: + return `${heartbeat} seconds`.trim() + } +} + const styles = StyleSheet.create({ scrollContainer: { flex: 1 }, scroll: { flex: 1 }, @@ -512,4 +579,8 @@ const styles = StyleSheet.create({ idversion: { width: "100%", }, + radioButton: { + flexDirection: "row", + alignItems: "center", + }, })