Skip to content

Commit

Permalink
feat: setup mdns scanner at boot
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanzyTHEbar committed Mar 30, 2023
1 parent 0c071bf commit e543bee
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 57 deletions.
2 changes: 1 addition & 1 deletion GUI/ETVR/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"solid-spinner": "^0.1.9",
"solid-transition-group": "^0.0.12",
"solidjs-icons": "^1.0.11",
"tauri-plugin-store-api": "github:tauri-apps/tauri-plugin-store#9bd993a",
"tauri-plugin-store-api": "https://github.com/tauri-apps/tauri-plugin-store",
"tauri-plugin-upload-api": "https://github.com/tauri-apps/tauri-plugin-upload",
"undici": "^5.11.0",
"yarn": "^1.22.19",
Expand Down
6 changes: 3 additions & 3 deletions GUI/ETVR/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions GUI/ETVR/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ branch = "dev"
git = "https://github.com/tauri-apps/tauri-plugin-single-instance/"
branch = "dev"

[dependencies.tauri-plugin-store]
git = "https://github.com/tauri-apps/tauri-plugin-store/"
rev = "9bd993a"

[dependencies.tauri-plugin-upload]
git = "https://github.com/tauri-apps/plugins-workspace"
branch = "dev"

[dependencies.tauri-plugin-store]
git = "https://github.com/tauri-apps/plugins-workspace"
branch = "dev"

[dependencies.reqwest]
version = "0.11"
features = [ "json", "rustls-tls",]
Expand Down
2 changes: 1 addition & 1 deletion GUI/ETVR/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fn main() {
});
}))
// persistent storage with file system
.plugin(tauri_plugin_store::PluginBuilder::default().build())
.plugin(tauri_plugin_store::Builder::default().build())
.plugin(tauri_plugin_upload::init())
// save window position and size between sessions
.plugin(tauri_plugin_window_state::Builder::default().build())
Expand Down
3 changes: 3 additions & 0 deletions GUI/ETVR/src/store/app/settings/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ export const enableNotificationsSounds = createMemo(
export const getGlobalNotificationsType = createMemo(
() => appSettingsState().globalNotificationsType,
)
export const enableMDNS = createMemo(() => appSettingsState().enableMDNS)
export const scanForCamerasOnStartup = createMemo(() => appSettingsState().scanForCamerasOnStartup)
export const stopAlgoBackend = createMemo(() => appSettingsState().stopAlgoBackend)
24 changes: 24 additions & 0 deletions GUI/ETVR/src/store/app/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,28 @@ export const setGlobalNotificationsType = (type: ENotificationAction) => {
)
}

export const setEnableMDNS = (flag: boolean) => {
setState(
produce((s) => {
s.enableMDNS = flag
}),
)
}

export const setScanForCamerasOnStartup = (flag: boolean) => {
setState(
produce((s) => {
s.scanForCamerasOnStartup = flag
}),
)
}

export const setStopAlgoBackend = (flag: boolean) => {
setState(
produce((s) => {
s.stopAlgoBackend = flag
}),
)
}

export const appSettingsState = createMemo(() => state)
18 changes: 18 additions & 0 deletions GUI/ETVR/src/store/mdns/mdns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ export enum MdnsStatus {
FAILED = 'FAILED',
}

interface IMdnsResponse {
ips: string[]
urls: string[]
}

interface IMdnsStore {
mdnsStatus: MdnsStatus
mdnsData: IMdnsResponse
}

export const defaultState: IMdnsStore = {
mdnsStatus: MdnsStatus.DISABLED,
mdnsData: {
ips: [],
urls: [],
},
}

const [state, setState] = createStore<IMdnsStore>(defaultState)
Expand All @@ -26,4 +36,12 @@ export const setMdnsStatus = (status: MdnsStatus) => {
)
}

export const setMdnsData = (data: IMdnsResponse) => {
setState(
produce((s) => {
s.mdnsData = data
}),
)
}

export const mdnsState = createMemo(() => state)
1 change: 1 addition & 0 deletions GUI/ETVR/src/store/mdns/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import { createMemo } from 'solid-js'
import { mdnsState } from './mdns'

export const mdnsStatus = createMemo(() => mdnsState().mdnsStatus)
export const mdnsData = createMemo(() => mdnsState().mdnsData)
43 changes: 17 additions & 26 deletions GUI/ETVR/src/utils/hooks/api/useMDNSScanner.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
import { invoke } from '@tauri-apps/api/tauri'
import { createMemo, createResource, createSignal } from 'solid-js'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const scan = async (source, { value, refetching }) => {
const { serviceType, scanTime } = source
import { setMdnsStatus, setMdnsData, MdnsStatus } from '@store/mdns/mdns'

export const useMDNSScanner = (serviceType: string, scanTime: number) => {
if (serviceType === '' || scanTime === 0) {
return []
}

console.log('scanning for', serviceType, scanTime)
console.log('[MDNS Scanner]: scanning for', serviceType, scanTime)

const res = await invoke('run_mdns_query', {
setMdnsStatus(MdnsStatus.LOADING)
invoke('run_mdns_query', {
serviceType,
scanTime,
})

if (typeof res === 'string') {
const parsedResponse = JSON.parse(res)
console.log('parsedResponse', parsedResponse)
return parsedResponse
}
return []
}

export const useMDNSScanner = () => {
const [service, setService] = createSignal<string>('')
const [scanTime, setScanTime] = createSignal<number>(0)

const mdnsInitState = createMemo(() => ({
serviceType: service(),
scanTime: scanTime(),
}))
const [data, { mutate, refetch }] = createResource(mdnsInitState, scan)
return { data, mutate, refetch, setService, setScanTime }
.then((res) => {
if (typeof res === 'string') {
const parsedResponse = JSON.parse(res)
console.log('[MDNS Scanner]: parsedResponse', parsedResponse)
setMdnsStatus(MdnsStatus.ACTIVE)
setMdnsData(parsedResponse)
}
})
.catch((e) => {
console.error('[MDNS Scanner]: error', e)
setMdnsStatus(MdnsStatus.FAILED)
})
}
29 changes: 7 additions & 22 deletions GUI/ETVR/src/utils/hooks/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { enableNotificationsSounds } from '@store/app/settings/selectors'
import { getActiveWindows } from '@store/app/windows/selectors'
import { removeWindow } from '@store/app/windows/windows'
import { doGHRequest } from '@utils/hooks/api/useGHReleases'
import { useMDNSScanner } from '@utils/hooks/api/useMDNSScanner'
import { checkPermission } from '@utils/hooks/notifications'
import { generateWebsocketClients } from '@utils/hooks/websocket'

Expand All @@ -32,12 +33,15 @@ export const handleAppBoot = () => {
localStorage.setItem('settings', JSON.stringify(config))
})
.catch((e) => console.error(e))
setTimeout(() => invoke('close_splashscreen'), 500)
setTimeout(() => invoke('close_splashscreen'), 25000)
})

// TODO: call these after the MDNS service is up and running and discoveres the camera's
checkPermission()
// TODO: call generateWebSocketClients() after the MDNS service is up and running and discoveres the camera's
useMDNSScanner('_openiristracker._tcp', 5)
generateWebsocketClients()

// TODO: check notif perms and request GH data
checkPermission()
doGHRequest()
}

Expand Down Expand Up @@ -74,22 +78,3 @@ export const handleSound = async (
return
}
}

//! Not supported in Edge yet - but will be eventually (https://developer.microsoft.com/en-us/microsoft-edge/platform/status/selectaudiooutput/)
/* export const handleAudioDevices = async () => {
if (!navigator.mediaDevices.selectAudioOutput) {
console.log('selectAudioOutput() not supported.')
return
}
const devices = await navigator.mediaDevices.enumerateDevices()
const audioDevices = devices.filter((device) => device.kind === 'audioinput')
navigator.mediaDevices
.selectAudioOutput()
.then((device) => {
console.log(`${device.kind}: ${device.label} id = ${device.deviceId}`)
})
.catch((err) => {
console.error(`${err.name}: ${err.message}`)
})
} */
6 changes: 6 additions & 0 deletions GUI/ETVR/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4884,6 +4884,12 @@ tapable@^2.2.0:
"@tauri-apps/api" "1.1.0"
tslib "2.4.0"

"tauri-plugin-store-api@https://github.com/tauri-apps/tauri-plugin-store":
version "0.0.0"
resolved "https://github.com/tauri-apps/tauri-plugin-store#d21d55684ad444d8cba322f0013e1b2b7e0630a7"
dependencies:
"@tauri-apps/api" "^1.2.0"

"tauri-plugin-upload-api@https://github.com/tauri-apps/tauri-plugin-upload":
version "0.0.0"
resolved "https://github.com/tauri-apps/tauri-plugin-upload#483a46c5df6a4ef3918726fa22e3f5df29c54aef"
Expand Down

0 comments on commit e543bee

Please sign in to comment.