Skip to content

Commit

Permalink
Merge pull request #44 from wildlifeai/40-improve-how-the-heartbeat-c…
Browse files Browse the repository at this point in the history
…ommand-works
  • Loading branch information
Burzo authored Apr 7, 2024
2 parents 909ae0a + 16785dd commit 12f3c6c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ble/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand Down
54 changes: 52 additions & 2 deletions src/navigation/screens/TerminalScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
Button,
Divider,
IconButton,
RadioButton,
Switch,
TextInput,
} from "react-native-paper"
Expand Down Expand Up @@ -90,6 +91,7 @@ export const Terminal = ({ embed }: Props) => {
const [autoscroll, setAutoscroll] = useState(true)

const [heartbeat, setHeartbeat] = useState<string>()
const [heartbeatTime, setHeartbeatTime] = useState<string>("s")
const [appEui, setAppEui] = useState<string>("")
const [devEui, setDevEui] = useState<string>("")
const [localSensor, setLocalSensor] = useState<boolean>()
Expand All @@ -107,7 +109,7 @@ export const Terminal = ({ embed }: Props) => {

const triggerHeartbeat = () => {
if (heartbeat && !hbLoading) {
setHb(`${heartbeat}s`)
setHb(`${heartbeat}${heartbeatTime}`)
}
}

Expand Down Expand Up @@ -379,7 +381,9 @@ export const Terminal = ({ embed }: Props) => {
{hbLoading ? (
"Loading..."
) : (
<WWText style={styles.bold}>{HEARTBEAT.value}</WWText>
<WWText style={styles.bold}>
{formatHeartbeat(HEARTBEAT.value)}
</WWText>
)}
</WWText>
)}
Expand All @@ -397,6 +401,28 @@ export const Terminal = ({ embed }: Props) => {
Change Heartbeat
</Button>
</View>
<View style={{ marginVertical: spacing }}>
<WWText variant="bodyLarge">
You are setting the value in: {formatHeartbeat(heartbeatTime)}
</WWText>
<RadioButton.Group
onValueChange={setHeartbeatTime}
value={heartbeatTime}
>
<View style={styles.radioButton}>
<RadioButton.Item label="Days" value="d" />
</View>
<View style={styles.radioButton}>
<RadioButton.Item label="Hours" value="h" />
</View>
<View style={styles.radioButton}>
<RadioButton.Item label="Minutes" value="m" />
</View>
<View style={styles.radioButton}>
<RadioButton.Item label="Seconds" value="s" />
</View>
</RadioButton.Group>
</View>
</View>
<View style={{ paddingVertical: spacing }}>
<WWText variant="titleMedium">App EUI</WWText>
Expand Down Expand Up @@ -498,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 },
Expand Down Expand Up @@ -533,4 +579,8 @@ const styles = StyleSheet.create({
idversion: {
width: "100%",
},
radioButton: {
flexDirection: "row",
alignItems: "center",
},
})

0 comments on commit 12f3c6c

Please sign in to comment.