Skip to content

Commit c7c26de

Browse files
committed
feat: begin laying infrastructure for REST and mDNS
1 parent a890581 commit c7c26de

File tree

8 files changed

+135
-4
lines changed

8 files changed

+135
-4
lines changed

GUI/ETVR/src/components/Camera/index.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { setOpenModal } from '@src/store/ui/ui'
66
import { ActiveStatus } from '@src/utils/utils'
77

88
// TODO: switch camera based on status
9-
// TODO: add modal for settings
109
// TODO: create grid to make it flexible
1110

1211
export const Camera = (props: ICamera) => {
@@ -23,11 +22,11 @@ export const Camera = (props: ICamera) => {
2322
</div>
2423
<div class="bg-[#292D36] ml-[14px] rounded-[14px] h-[100%] p-[14px] ">
2524
<div class="text-center pb-[14px]">
26-
<div class=" text-[#FFFF]">camera 1</div>
25+
<div class=" text-[#FFFF]"> {props.activeCameraSection} </div>
2726
</div>
2827
<div>
2928
<div class="flex text-[#FFFF] justify-between">
30-
<div class="pr-[25px] pb-[14px]">Camera IP</div>
29+
<div class="pr-[25px] pb-[14px]">Camera Address</div>
3130
<div>{props.address}</div>
3231
</div>
3332
<div class="flex text-[#FFFF] justify-between">

GUI/ETVR/src/store/api/endpoints.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export enum RESTType {
2+
GET = 'GET',
3+
POST = 'POST',
4+
PUT = 'PUT',
5+
DELETE = 'DELETE',
6+
}
7+
8+
export interface IEndpoint {
9+
name: string
10+
url: string
11+
type: RESTType
12+
}
13+
14+
export const endpoints: IEndpoint[] = [
15+
{
16+
name: 'heartbeat',
17+
url: '/ping',
18+
type: RESTType.GET,
19+
},
20+
{
21+
name: 'getCamera',
22+
url: '/camera',
23+
type: RESTType.GET,
24+
}
25+
]

GUI/ETVR/src/store/api/restAPI.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { createMemo } from 'solid-js'
2+
import { createStore, produce } from 'solid-js/store'
3+
4+
export enum RESTStatus {
5+
ACTIVE = 'ACTIVE',
6+
DISABLED = 'DISABLED',
7+
LOADING = 'LOADING',
8+
FAILED = 'FAILED',
9+
}
10+
11+
export interface IRest {
12+
status: RESTStatus
13+
type: RESTType
14+
endpoints: IEndpoint[]
15+
data: object
16+
}
17+
18+
export const defaultState = {
19+
status: RESTStatus.DISABLED,
20+
type: RESTType.GET,
21+
endpoints: [],
22+
data: {},
23+
}
24+
25+
const [state, setState] = createStore<IRest>(defaultState)
26+
27+
export const setRestStatus = (status: RESTStatus) => {
28+
setState(
29+
produce((s) => {
30+
s.status = status
31+
})
32+
)
33+
}
34+
35+
export const restState = createMemo(() => state)

GUI/ETVR/src/store/api/selectors.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createMemo } from 'solid-js'
2+
import { restState } from './restAPI'
3+
4+
export const restStatus = createMemo(() => restState().status)
5+
export const cameras = createMemo(() => restState().cameras)

GUI/ETVR/src/store/mdns/mdns.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const staticCamerasGenerator = new Array(5).fill(0).map(() => ({
3131
type: CameraType.WIRELESS,
3232
address: '192.168.0.204',
3333
name: 'left-eye',
34-
activeCameraSection: 'left eye',
34+
activeCameraSection: 'Left Eye',
3535
}))
3636

3737
export const defaultState = {

GUI/ETVR/src/store/mdns/selectors.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ import { mdnsState } from './mdns'
33

44
export const connectedUserName = createMemo(() => mdnsState().connectedUser)
55
export const cameras = createMemo(() => mdnsState().cameras)
6+
export const cameraAddresses = createMemo(() => cameras().map((c) => c.address))
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { invoke } from '@tauri-apps/api/tauri'
2+
import { createSignal } from 'solid-js'
3+
4+
export const useMDNSScanner = (serviceType: string, scanTime: number) => {
5+
const [res, setRes] = createSignal(null)
6+
const [loading, setLoading] = createSignal(false)
7+
const [error, setError] = createSignal(null)
8+
9+
const scan = () => {
10+
setLoading(true)
11+
invoke('run_mdns_query', {
12+
serviceType,
13+
scanTime,
14+
})
15+
.then((response) => {
16+
if (typeof response === 'string') {
17+
const parsedResponse = JSON.parse(response)
18+
setRes(parsedResponse)
19+
}
20+
})
21+
.catch((err) => {
22+
setError(err)
23+
})
24+
.finally(() => {
25+
setLoading(false)
26+
})
27+
}
28+
return { res, loading, error, scan }
29+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { invoke } from '@tauri-apps/api/tauri'
2+
import { createSignal } from 'solid-js'
3+
import { endpoints } from '@src/store/api/selectors'
4+
import { cameras } from '@src/store/mdns/selectors'
5+
6+
export function useChartRequestHook() {
7+
const [data, setData] = createSignal({})
8+
const [loading, setLoading] = createSignal(false)
9+
const [error, setError] = createSignal(null)
10+
function doRequest() {
11+
cameras().forEach((data) => {
12+
setLoading(true)
13+
invoke('do_rest_request', {
14+
endpoint: data.address + data['endpoint'],
15+
//deviceName: chartData['deviceName'],
16+
method: 'GET',
17+
})
18+
.then((response) => {
19+
if (typeof response === 'string') {
20+
const parsedResponse = JSON.parse(response)
21+
setData((prevData) => ({
22+
...prevData,
23+
[data['msg']]: parsedResponse,
24+
}))
25+
}
26+
})
27+
.catch((err) => {
28+
console.log(err)
29+
setError(err)
30+
})
31+
.finally(() => {
32+
setLoading(false)
33+
})
34+
})
35+
}
36+
return { data, loading, error, doRequest }
37+
}

0 commit comments

Comments
 (0)