-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: begin laying infrastructure for REST and mDNS
- Loading branch information
1 parent
a890581
commit c7c26de
Showing
8 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
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,25 @@ | ||
export enum RESTType { | ||
GET = 'GET', | ||
POST = 'POST', | ||
PUT = 'PUT', | ||
DELETE = 'DELETE', | ||
} | ||
|
||
export interface IEndpoint { | ||
name: string | ||
url: string | ||
type: RESTType | ||
} | ||
|
||
export const endpoints: IEndpoint[] = [ | ||
{ | ||
name: 'heartbeat', | ||
url: '/ping', | ||
type: RESTType.GET, | ||
}, | ||
{ | ||
name: 'getCamera', | ||
url: '/camera', | ||
type: RESTType.GET, | ||
} | ||
] |
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,35 @@ | ||
import { createMemo } from 'solid-js' | ||
import { createStore, produce } from 'solid-js/store' | ||
|
||
export enum RESTStatus { | ||
ACTIVE = 'ACTIVE', | ||
DISABLED = 'DISABLED', | ||
LOADING = 'LOADING', | ||
FAILED = 'FAILED', | ||
} | ||
|
||
export interface IRest { | ||
status: RESTStatus | ||
type: RESTType | ||
endpoints: IEndpoint[] | ||
data: object | ||
} | ||
|
||
export const defaultState = { | ||
status: RESTStatus.DISABLED, | ||
type: RESTType.GET, | ||
endpoints: [], | ||
data: {}, | ||
} | ||
|
||
const [state, setState] = createStore<IRest>(defaultState) | ||
|
||
export const setRestStatus = (status: RESTStatus) => { | ||
setState( | ||
produce((s) => { | ||
s.status = status | ||
}) | ||
) | ||
} | ||
|
||
export const restState = createMemo(() => state) |
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,5 @@ | ||
import { createMemo } from 'solid-js' | ||
import { restState } from './restAPI' | ||
|
||
export const restStatus = createMemo(() => restState().status) | ||
export const cameras = createMemo(() => restState().cameras) |
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,29 @@ | ||
import { invoke } from '@tauri-apps/api/tauri' | ||
import { createSignal } from 'solid-js' | ||
|
||
export const useMDNSScanner = (serviceType: string, scanTime: number) => { | ||
const [res, setRes] = createSignal(null) | ||
const [loading, setLoading] = createSignal(false) | ||
const [error, setError] = createSignal(null) | ||
|
||
const scan = () => { | ||
setLoading(true) | ||
invoke('run_mdns_query', { | ||
serviceType, | ||
scanTime, | ||
}) | ||
.then((response) => { | ||
if (typeof response === 'string') { | ||
const parsedResponse = JSON.parse(response) | ||
setRes(parsedResponse) | ||
} | ||
}) | ||
.catch((err) => { | ||
setError(err) | ||
}) | ||
.finally(() => { | ||
setLoading(false) | ||
}) | ||
} | ||
return { res, loading, error, scan } | ||
} |
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,37 @@ | ||
import { invoke } from '@tauri-apps/api/tauri' | ||
import { createSignal } from 'solid-js' | ||
import { endpoints } from '@src/store/api/selectors' | ||
import { cameras } from '@src/store/mdns/selectors' | ||
|
||
export function useChartRequestHook() { | ||
const [data, setData] = createSignal({}) | ||
const [loading, setLoading] = createSignal(false) | ||
const [error, setError] = createSignal(null) | ||
function doRequest() { | ||
cameras().forEach((data) => { | ||
setLoading(true) | ||
invoke('do_rest_request', { | ||
endpoint: data.address + data['endpoint'], | ||
//deviceName: chartData['deviceName'], | ||
method: 'GET', | ||
}) | ||
.then((response) => { | ||
if (typeof response === 'string') { | ||
const parsedResponse = JSON.parse(response) | ||
setData((prevData) => ({ | ||
...prevData, | ||
[data['msg']]: parsedResponse, | ||
})) | ||
} | ||
}) | ||
.catch((err) => { | ||
console.log(err) | ||
setError(err) | ||
}) | ||
.finally(() => { | ||
setLoading(false) | ||
}) | ||
}) | ||
} | ||
return { data, loading, error, doRequest } | ||
} |