Skip to content

Commit

Permalink
Added the device command and fixed a problem with scrolling in the te…
Browse files Browse the repository at this point in the history
…rminal window
  • Loading branch information
Burzo committed Apr 7, 2024
1 parent 12f3c6c commit 4291293
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 43 deletions.
13 changes: 13 additions & 0 deletions src/ble/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ export const parseLogs = (finishedLog: string, lastLog: string) => {
}
}

// DEVICE
const lastDeviceLine = checkForLastLine(COMMANDS.DEVICE.readCommand!, lines)

if (lastDeviceLine) {
const value = valueChecker(lastDeviceLine, COMMANDS.DEVICE)
if (value) {
results.push({
value,
command: COMMANDS.DEVICE,
})
}
}

// SENSOR

const lastSensorLine = checkForLastLine(COMMANDS.SENSOR.readCommand!, lines)
Expand Down
5 changes: 5 additions & 0 deletions src/ble/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum CommandNames {
SENSOR = "SENSOR",
TRAP = "TRAP",
LORAWAN = "LORAWAN",
DEVICE = "DEVICE",
}

/**
Expand Down Expand Up @@ -144,6 +145,10 @@ export const COMMANDS: {
writeCommand: () => "dfu",
readRegex: /(Device will enter DFU mode after disconnecting.)\s*/,
},
[CommandNames.DEVICE]: {
name: CommandNames.DEVICE,
readCommand: "device",
},
}

type CharacteristicProperty =
Expand Down
4 changes: 3 additions & 1 deletion src/navigation/screens/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ export const Home = memo(() => {
}, [isFocused])

useEffect(() => {
if (!isFocused) return

const interval = setInterval(() => {
if (!isBleBusy) {
if (!isBleBusy && isFocused) {
startScan(10)
} else {
log("Scanning already taking place, skipping.")
Expand Down
126 changes: 84 additions & 42 deletions src/navigation/screens/TerminalScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useRoute, useIsFocused } from "@react-navigation/native"
import * as React from "react"
import { useState } from "react"
import { useCallback } from "react"
import { useEffect } from "react"
import { useEffect, useRef } from "react"

import {
Keyboard,
Expand Down Expand Up @@ -38,7 +37,7 @@ type Props = {
}

export const Terminal = ({ embed }: Props) => {
const scrollViewRef = React.useRef<any>()
const scrollViewRef = useRef<any>()
const {
params: { deviceId },
} = useRoute<AppParams<"Terminal">>()
Expand All @@ -47,12 +46,13 @@ export const Terminal = ({ embed }: Props) => {
const isFocused = useIsFocused()
const { write, pingsPause, disconnectDevice } = useBleActions()
const device = useSelectDevice({ deviceId })
const [offset, setOffset] = useState(0)
const logs = deviceLogs[deviceId]
const configuration = useAppSelector((state) => state.configuration)
const config = configuration[deviceId]
const { spacing, colors, appPadding } = useExtendedTheme()

const offset = useRef(0)

const { get: getBattery, commandLoading: batteryLoading } = useCommand({
deviceId,
command: COMMANDS.BATTERY,
Expand All @@ -61,6 +61,10 @@ export const Terminal = ({ embed }: Props) => {
deviceId,
command: COMMANDS.VERSION,
})
const { get: getDevice, commandLoading: deviceLoading } = useCommand({
deviceId,
command: COMMANDS.DEVICE,
})
const { get: getId, commandLoading: idLoading } = useCommand({
deviceId,
command: COMMANDS.ID,
Expand Down Expand Up @@ -135,7 +139,7 @@ export const Terminal = ({ embed }: Props) => {

scrollViewRef.current &&
scrollViewRef.current.scrollToEnd({ animated: true })
setOffset(0)
offset.current = 0
}, [autoscroll, deviceLogs])

const [isKeyboardVisible, setIsKeyboardVisible] = useState(false)
Expand Down Expand Up @@ -176,10 +180,10 @@ export const Terminal = ({ embed }: Props) => {
}: NativeSyntheticEvent<NativeScrollEvent>) => {
const { layoutMeasurement, contentOffset, contentSize } = nativeEvent
const currentOffset = nativeEvent.contentOffset.y
const goingUp = currentOffset < offset
const goingUp = currentOffset < offset.current
const isOnBottom =
layoutMeasurement.height + contentOffset.y >= contentSize.height - 15
setOffset(currentOffset)
layoutMeasurement.height + contentOffset.y >= contentSize.height - 50
offset.current = currentOffset

if (goingUp) {
toggleAutoscroll(false)
Expand All @@ -204,8 +208,17 @@ export const Terminal = ({ embed }: Props) => {
}
}, [])

const { HEARTBEAT, APPEUI, SENSOR, LORAWAN, DEVEUI, BATTERY, VERSION, ID } =
config
const {
HEARTBEAT,
APPEUI,
SENSOR,
LORAWAN,
DEVEUI,
BATTERY,
VERSION,
ID,
DEVICE,
} = config

if (
!HEARTBEAT?.loaded ||
Expand All @@ -215,6 +228,7 @@ export const Terminal = ({ embed }: Props) => {
!DEVEUI?.loaded ||
!BATTERY?.loaded ||
!VERSION?.loaded ||
!DEVICE?.loaded ||
!ID?.loaded
) {
return <AppLoading />
Expand All @@ -223,7 +237,7 @@ export const Terminal = ({ embed }: Props) => {
return (
<CustomKeyboardAvoidingView style={styles.scroll}>
<WWScreenView>
<View style={{ margin: spacing }}>
<View style={(styles.container, { margin: spacing })}>
{!isKeyboardVisible && (
<View style={styles.view}>
<WWScrollView
Expand All @@ -250,7 +264,7 @@ export const Terminal = ({ embed }: Props) => {
)}
</View>
)}
<View style={styles.input}>
<View style={[{ marginVertical: spacing }, styles.input]}>
<WWTextInput
autoCorrect={false}
autoCapitalize="none"
Expand All @@ -272,6 +286,35 @@ export const Terminal = ({ embed }: Props) => {

<View style={styles.scrollContainer}>
<WWScrollView style={styles.scroll}>
<View style={{ paddingVertical: spacing }}>
<WWText variant="titleMedium">Actions</WWText>
<Divider />
<View style={[styles.buttons, { marginVertical: spacing }]}>
<View style={styles.button}>
<Button mode="outlined" onPress={() => reset()}>
Reset
</Button>
</View>
<View style={styles.button}>
<Button mode="outlined" onPress={() => erase()}>
Erase
</Button>
</View>
<View style={styles.button}>
<Button
mode="outlined"
onPress={() => disconnectDevice(device)}
>
Disconnect
</Button>
</View>
<View style={styles.button}>
<Button mode="outlined" onPress={() => triggerDfu()}>
DFU mode
</Button>
</View>
</View>
</View>
<View style={{ paddingVertical: spacing }}>
<WWText variant="titleMedium">ID</WWText>
<Divider />
Expand All @@ -295,6 +338,29 @@ export const Terminal = ({ embed }: Props) => {
</Button>
</View>
</View>
<View style={{ paddingVertical: spacing }}>
<WWText variant="titleMedium">Device</WWText>
<Divider />
<View style={[styles.buttons, { marginVertical: spacing }]}>
<View style={{ padding: spacing }}>
{DEVICE.loaded && (
<WWText>
Device name:{" "}
{deviceLoading ? (
"Loading..."
) : (
<WWText style={styles.bold}>{DEVICE.value}</WWText>
)}
</WWText>
)}
</View>
</View>
<View style={styles.heartbeat}>
<Button mode="outlined" onPress={getDevice}>
Refresh device
</Button>
</View>
</View>
<View style={{ paddingVertical: spacing }}>
<WWText variant="titleMedium">Version</WWText>
<Divider />
Expand Down Expand Up @@ -341,35 +407,6 @@ export const Terminal = ({ embed }: Props) => {
</Button>
</View>
</View>
<View style={{ paddingVertical: spacing }}>
<WWText variant="titleMedium">Actions</WWText>
<Divider />
<View style={[styles.buttons, { marginVertical: spacing }]}>
<View style={styles.button}>
<Button mode="outlined" onPress={() => reset()}>
Reset
</Button>
</View>
<View style={styles.button}>
<Button mode="outlined" onPress={() => erase()}>
Erase
</Button>
</View>
<View style={styles.button}>
<Button
mode="outlined"
onPress={() => disconnectDevice(device)}
>
Disconnect
</Button>
</View>
<View style={styles.button}>
<Button mode="outlined" onPress={() => triggerDfu()}>
DFU mode
</Button>
</View>
</View>
</View>
<View style={{ paddingVertical: spacing }}>
<WWText variant="titleMedium">Heartbeat</WWText>
<Divider />
Expand Down Expand Up @@ -545,6 +582,12 @@ const formatHeartbeat = (s?: string) => {
}

const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
},
scrollContainer: { flex: 1 },
scroll: { flex: 1 },
view: { height: 150 },
Expand All @@ -556,7 +599,6 @@ const styles = StyleSheet.create({
input: {
flexDirection: "row",
alignItems: "center",
height: 40,
},
inputText: { flex: 2 },
buttons: {
Expand Down

0 comments on commit 4291293

Please sign in to comment.