-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
app/components/boundaries/ConnectedDevices/ConnectedDevicesBoundarie.client.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use client'; | ||
import { useSetAtom, atom } from 'jotai'; | ||
import { useEffect, useMemo } from 'react'; | ||
import { getConnectedDevices } from '@/lib/get-connected-devices'; | ||
export const allConnectedDevices = atom<Array<string>>([]); | ||
|
||
export default function ConnectedDevicesWrapper({ | ||
children, | ||
connectedDevices: connectedDevices | ||
}: { | ||
children: React.ReactNode; | ||
connectedDevices: string[]; | ||
}) { | ||
const setConnectedDevices = useSetAtom(allConnectedDevices); | ||
useMemo(() => { | ||
setConnectedDevices(connectedDevices); | ||
}, []); | ||
useEffect(() => { | ||
setInterval(() => { | ||
getConnectedDevices().then((connectedDevices) => { | ||
if ('error' in connectedDevices) return; | ||
setConnectedDevices(connectedDevices); | ||
}); | ||
}, 5000); | ||
}); | ||
return <>{children}</>; | ||
} |
21 changes: 21 additions & 0 deletions
21
app/components/boundaries/ConnectedDevices/ConnectedDevicesBoundarie.server.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getConnectedDevices } from '@/lib/get-connected-devices'; | ||
import ConnectedDevicesWrapper from './ConnectedDevicesBoundarie.client'; | ||
|
||
export default async function ConnectedDevicesBoundarie({ | ||
children | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const connectedDevices = await getConnectedDevices(); | ||
if ('error' in connectedDevices) | ||
return ( | ||
<ConnectedDevicesWrapper connectedDevices={[]}> | ||
{children} | ||
</ConnectedDevicesWrapper> | ||
); | ||
return ( | ||
<ConnectedDevicesWrapper connectedDevices={connectedDevices}> | ||
{children} | ||
</ConnectedDevicesWrapper> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use server'; | ||
import { getToken } from './get-token'; | ||
|
||
type connectedDeviceResponseType = { | ||
id: number; | ||
result?: [0, { stdout: string }?]; | ||
}; | ||
|
||
export async function getConnectedDevices() { | ||
let token = (await getToken()) as string; | ||
let getConnectedDevicesResponse = await getConnectedDevicesRequest(token); | ||
if ( | ||
!getConnectedDevicesResponse.result || | ||
!getConnectedDevicesResponse.result[1] | ||
) { | ||
let newToken = await getToken(true); | ||
if (!newToken) { | ||
console.log('getConnectedDevices: Token not found'); | ||
return { error: 'Token not found' }; | ||
} | ||
getConnectedDevicesResponse = await getConnectedDevicesRequest(newToken); | ||
if ( | ||
!getConnectedDevicesResponse.result || | ||
!getConnectedDevicesResponse.result[1] | ||
) { | ||
console.log('getConnectedDevices: Something went wrong'); | ||
return { error: 'Something went wrong' }; | ||
} | ||
} | ||
const connectedDevicesSplit = | ||
getConnectedDevicesResponse.result[1].stdout.split('\n'); | ||
let connectedDevices = connectedDevicesSplit | ||
.map((device) => { | ||
if (!device) return; | ||
const deviceSplit = device.split(' '); | ||
return deviceSplit[4]; | ||
}) | ||
.filter((device) => device !== undefined) as string[]; | ||
return connectedDevices; | ||
} | ||
|
||
async function getConnectedDevicesRequest(token: string) { | ||
let requestBody = { | ||
jsonrpc: '2.0', | ||
id: 1, | ||
method: 'call', | ||
params: [token, 'file', 'exec', { command: '/etc/neigh-probe' }] | ||
}; | ||
|
||
let makeRequest = await fetch(`${process.env.ROUTER_URL}/ubus`, { | ||
method: 'POST', | ||
body: JSON.stringify(requestBody), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
cache: 'no-cache' | ||
}); | ||
|
||
return (await makeRequest.json()) as connectedDeviceResponseType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters