Skip to content

Commit 571281a

Browse files
committed
added multi-blox support
1 parent 3891f32 commit 571281a

File tree

5 files changed

+59
-38
lines changed

5 files changed

+59
-38
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"[typescriptreact]": {
55
"editor.defaultFormatter": "esbenp.prettier-vscode"
66
},
7-
"java.configuration.updateBuildConfiguration": "interactive"
7+
"java.configuration.updateBuildConfiguration": "interactive",
8+
"java.compile.nullAnalysis.mode": "automatic"
89
}

apps/box/src/screens/Blox/components/BloxHeader.tsx

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@ import {
66
FxPressableOpacity,
77
FxChevronDownIcon,
88
useFxTheme,
9+
FxSvg,
10+
FxSvgProps,
911
} from '@functionland/component-library';
10-
import { EBloxInteractionType } from '../../../models';
12+
import { useNavigation } from '@react-navigation/native';
13+
import { Routes } from '../../../navigation/navigationConfig';
14+
import { DynamicIcon } from '../../../components/Icons';
15+
16+
const PLUS_ICON_PATH = "M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z";
1117

1218
type TBloxHeader = {
1319
selectedMode: EBloxInteractionType;
1420
onChangeMode: VoidFunction;
1521
onAvatarPress?: VoidFunction;
16-
1722
};
1823

19-
export const BloxHeader = ({ selectedMode, onChangeMode ,onAvatarPress}: TBloxHeader) => {
24+
export const BloxHeader = ({ selectedMode, onChangeMode, onAvatarPress }: TBloxHeader) => {
2025
const { colors } = useFxTheme();
26+
const navigation = useNavigation();
2127

2228
return (
2329
<FxBox
@@ -32,24 +38,28 @@ export const BloxHeader = ({ selectedMode, onChangeMode ,onAvatarPress}: TBloxHe
3238
alignItems="center"
3339
onPress={onChangeMode}
3440
>
35-
{/* <FxText variant="h300">
36-
{selectedMode === EBloxInteractionType.HomeBloxSetup
37-
? 'Home Blox Setup'
38-
: 'Office Blox Unit'}
39-
</FxText>
40-
<FxChevronDownIcon
41-
width={16}
42-
height={16}
43-
marginLeft="4"
44-
fill={colors.content1}
45-
/> */}
4641
</FxPressableOpacity>
4742

48-
<FxAvatar
49-
source={require('../../../api/mockAssets/sample.png')}
50-
size="small"
51-
onPress={onAvatarPress}
52-
/>
43+
<FxBox flexDirection="row" alignItems="center">
44+
<FxPressableOpacity
45+
onPress={() => navigation.navigate(Routes.InitialSetup, {
46+
screen: Routes.ConnectToBlox,
47+
})}
48+
marginRight="12"
49+
>
50+
<DynamicIcon
51+
iconPath={PLUS_ICON_PATH}
52+
fill={colors.content1}
53+
width={24}
54+
height={24}
55+
/>
56+
</FxPressableOpacity>
57+
<FxAvatar
58+
source={require('../../../api/mockAssets/sample.png')}
59+
size="small"
60+
onPress={onAvatarPress}
61+
/>
62+
</FxBox>
5363
</FxBox>
5464
);
5565
};

apps/box/src/screens/InitialSetup/ConnectToWifi/ConnectToWifi.screen.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ export const ConnectToWifiScreen = () => {
146146
paddingVertical="8"
147147
onPress={() => setEnableHiddenNetwork(!enabledHiddenNetwork)}
148148
>
149-
{enabledHiddenNetwork ? "Show Network Names" : "Manually Enter Wifi Name"}
149+
{enabledHiddenNetwork
150+
? 'Show Network Names'
151+
: 'Manually Enter Wifi Name'}
150152
</FxButton>
151153
{enabledHiddenNetwork && (
152154
<FxBox>
@@ -155,14 +157,14 @@ export const ConnectToWifiScreen = () => {
155157
value={selectedSsid}
156158
onChange={(e) => setSelectedSsid(e.nativeEvent.text)}
157159
onSubmitEditing={() => handleSelectedWifiDevice(selectedSsid)}
158-
placeholder='Enter Wifi Name'
160+
placeholder="Enter Wifi Name"
159161
/>
160162
<FxButton
161163
onPress={() => {
162164
handleSelectedWifiDevice(selectedSsid);
163165
}}
164166
>
165-
Enter Password {selectedSsid ? 'for': ''} {selectedSsid}
167+
Enter Password {selectedSsid ? 'for' : ''} {selectedSsid}
166168
</FxButton>
167169
</FxBox>
168170
)}

apps/box/src/screens/InitialSetup/SetBloxAuthorizer.screen.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import {
1515
import BleManager from 'react-native-ble-manager';
1616
import { ResponseAssembler } from '../../utils/ble';
1717

18-
import { useFetch, useInitialSetupNavigation, useLogger, useFetchWithBLE } from '../../hooks';
18+
import {
19+
useFetch,
20+
useInitialSetupNavigation,
21+
useLogger,
22+
useFetchWithBLE,
23+
} from '../../hooks';
1924
import {
2025
InitialSetupStackParamList,
2126
Routes,
@@ -79,12 +84,12 @@ export const SetBloxAuthorizerScreen = ({ route }: Props) => {
7984
`Blox Unit #${bloxsArray.length + 1}`
8085
);
8186

82-
const blePeerExchange = async (params: { peer_id: string, seed: string }) => {
87+
const blePeerExchange = async (params: { peer_id: string; seed: string }) => {
8388
const connectedPeripherals = await BleManager.getConnectedPeripherals([]);
8489
if (connectedPeripherals.length === 0) {
8590
throw new Error('No BLE devices connected');
8691
}
87-
92+
8893
const responseAssembler = new ResponseAssembler();
8994
try {
9095
const command = `peer/exchange ${params.peer_id} ${params.seed}`;
@@ -109,7 +114,6 @@ export const SetBloxAuthorizerScreen = ({ route }: Props) => {
109114
bleMethod: blePeerExchange,
110115
});
111116

112-
113117
const {
114118
loading: loading_bloxFormatDisk,
115119
data: data_bloxFormatDisk,
@@ -187,10 +191,10 @@ export const SetBloxAuthorizerScreen = ({ route }: Props) => {
187191
}, [data_bloxProperties, error_bloxProperties]);
188192

189193
useEffect(() => {
190-
console.log('inside data_exchange useEffect', {data_exchange});
194+
console.log('inside data_exchange useEffect', { data_exchange });
191195
if (data_exchange?.data?.peer_id) {
192196
const peer_id = data_exchange?.data?.peer_id?.trim()?.split(/\r?\n/)?.[0];
193-
console.log({data_exchange, peer_id});
197+
console.log({ data_exchange, peer_id });
194198
if (!peer_id || peer_id?.length !== 52) {
195199
queueToast({
196200
type: 'error',
@@ -202,7 +206,7 @@ export const SetBloxAuthorizerScreen = ({ route }: Props) => {
202206
setNewBloxPeerId(peer_id);
203207
}
204208
} else if (error_exchange) {
205-
console.log('data exchange error' , {data_exchange, error_exchange});
209+
console.log('data exchange error', { data_exchange, error_exchange });
206210
queueToast({
207211
type: 'error',
208212
title: 'Set authotizer',
@@ -238,7 +242,7 @@ export const SetBloxAuthorizerScreen = ({ route }: Props) => {
238242
const { secretKey } = Helper.getMyDIDKeyPair(password, signiture);
239243
const peer_id = newPeerId;
240244
const seed = secretKey.toString();
241-
245+
242246
refetch_exchangeConfig({
243247
params: {
244248
peer_id,
@@ -274,8 +278,12 @@ export const SetBloxAuthorizerScreen = ({ route }: Props) => {
274278
newPeerId &&
275279
newBloxName
276280
) {
281+
const bloxsCount = Object.values(bloxs).length;
282+
const bloxExists = Object.values(bloxs).some(
283+
(blox) => blox.peerId === newBloxPeerId
284+
);
277285
setAppPeerId(newPeerId);
278-
if (currentBloxPeerId) {
286+
if (currentBloxPeerId === newBloxPeerId) {
279287
removeBlox(currentBloxPeerId);
280288
}
281289
addBlox({

apps/box/src/utils/ble.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class ResponseAssembler {
250250

251251
export class BleManagerWrapper extends ResponseAssembler {
252252
private DEVICE_NAME = 'fulatower';
253-
private DEVICE_MAC = '2C:05:47:85:39:3F';
253+
private DEVICE_NAME2 = 'fxblox-rk1';
254254
private onStatusChange?: (status: EConnectionStatus) => void;
255255

256256
constructor(statusCallback?: (status: EConnectionStatus) => void) {
@@ -345,7 +345,7 @@ export class BleManagerWrapper extends ResponseAssembler {
345345
// Check existing connections
346346
const connectedPeripherals = await BleManager.getConnectedPeripherals([]);
347347
const existingDevice = connectedPeripherals.find(
348-
device => device.name === this.DEVICE_NAME || device.id === this.DEVICE_MAC
348+
device => device.name === this.DEVICE_NAME || device.name === this.DEVICE_NAME2
349349
);
350350

351351
if (existingDevice) {
@@ -374,13 +374,13 @@ export class BleManagerWrapper extends ResponseAssembler {
374374
timestamp: number;
375375
}> = [];
376376

377-
const SCAN_DURATION = Platform.OS === 'ios' ? 10000 : 4000; // Longer scan for iOS
377+
const SCAN_DURATION = Platform.OS === 'ios' ? 5000 : 5000; // Longer scan for iOS
378378

379379
const discoveryListener = bleManagerEmitter.addListener(
380380
'BleManagerDiscoverPeripheral',
381381
(peripheral) => {
382382
if (peripheral.name === this.DEVICE_NAME ||
383-
peripheral.id === this.DEVICE_MAC) {
383+
peripheral.name === this.DEVICE_NAME2) {
384384
// On iOS, only add if not already discovered
385385
if (!discoveredDevices.some(d => d.peripheral === peripheral.id)) {
386386
discoveredDevices.push({
@@ -436,12 +436,12 @@ export class BleManagerWrapper extends ResponseAssembler {
436436
console.log({strongestDevice});
437437

438438
if (Platform.OS === 'ios') {
439-
await new Promise(resolve => setTimeout(resolve, 1000));
439+
await new Promise(resolve => setTimeout(resolve, 2000));
440440
}
441441

442442
await BleManager.connect(strongestDevice.peripheral);
443443
console.log('connected to ble');
444-
await new Promise(resolve => setTimeout(resolve, 4000));
444+
await new Promise(resolve => setTimeout(resolve, 5000));
445445
console.log('retrieveServices started');
446446
await BleManager.retrieveServices(strongestDevice.peripheral);
447447
console.log('retrieveServices done');

0 commit comments

Comments
 (0)