Skip to content

Commit

Permalink
Refactor TethrManager
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Jun 11, 2024
1 parent 794715e commit 01c9600
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 84 deletions.
66 changes: 52 additions & 14 deletions demo/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@
</main>

<aside>
<ul>
<li v-for="(cam, i) in pairedCameras" :key="i">
<button>{{ i }}</button>
</li>
</ul>
<dl>
<dt>Request</dt>
<dt style="display: flex; gap: 1em">
<dt>Connect to</dt>
<dd class="connect-to">
<button class="connect-button" @click="requestCameras('usbptp')">
USB
</button>
<button class="connect-button" @click="requestCameras('webcam')">
Webcam
</button>
</dt>
</dd>
</dl>
<ul class="webcam-list">
<li v-for="(cam, i) in pairedCameras" :key="i">
<button @click="onClickCamera(cam)">
[{{ cam.type }}] {{ cam.name }}
</button>
</li>
</ul>

<template v-if="camera">
<h2>Actions</h2>
Expand All @@ -46,7 +48,7 @@
</template>
<template v-if="configs.canRunAutoFocus.value">
<dt>autoFocus</dt>
<dd><button @click="runAutoFocus">Run</button></dd>
<dd><button @click="camera.runAutoFocus">Run</button></dd>
</template>
<template v-if="configs.canRunManualFocus.value">
<dt>manualFocus</dt>
Expand Down Expand Up @@ -112,7 +114,7 @@
v-if="config.value !== null"
:key="name"
:label="name"
:config="config as any"
:config="config"

Check failure on line 117 in demo/src/App.vue

View workflow job for this annotation

GitHub Actions / deploy

Type '{ readonly writable: boolean; readonly value: string | null; readonly update: (value: string) => void; readonly option?: { readonly type: "enum"; readonly values: readonly string[]; } | { readonly type: "range"; readonly min: string; readonly max: string; readonly step: string; } | undefined; } | ... 11 more ... | {...' is not assignable to type 'TethrConfig<any>'.
/>
</template>
</dl>
Expand All @@ -123,7 +125,7 @@

<script lang="ts" setup>
import {saveAs} from 'file-saver'
import {TethrObject} from 'tethr'
import {Tethr, TethrObject} from 'tethr'
import {ref} from 'vue'
import TethrConfig from './TethrConfig.vue'
Expand Down Expand Up @@ -167,14 +169,35 @@ async function onSave(object: TethrObject) {
const {
pairedCameras,
camera,
openCamera,
closeCamera,
requestCameras,
configs,
liveviewMediaStream,
photoURL,
runAutoFocus,
toggleLiveview,
takePhoto,
} = useTethr(onSave)
} = useTethr()
async function takePhoto() {
if (!camera.value) return
const result = await camera.value.takePhoto()
if (result.status === 'ok') {
for (const object of result.value) {
if (object.format !== 'raw') {
photoURL.value = URL.createObjectURL(object.blob)
}
onSave(object)
}
}
}
async function onClickCamera(cam: Tethr) {
if (cam.opened) {
closeCamera(cam)
} else {
openCamera(cam)
}
}
</script>

<style lang="stylus" scoped>
Expand Down Expand Up @@ -220,6 +243,7 @@ dl
&:deep(dt)
height 2em
line-height 2em
white-space nowrap
&:deep(dd)
display flex
Expand All @@ -231,4 +255,18 @@ dl
display block
width 100%
text-align center
.connect-to
display: flex
gap: 1em
margin-bottom 1em
.webcam-list
grid-column: 1 / span 2
li
margin-bottom 0.2em
button
width 100%
</style>
47 changes: 17 additions & 30 deletions demo/src/useTethr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ConfigType,
Tethr,
TethrManager,
TethrObject,
} from 'tethr'
import {onUnmounted, reactive, readonly, Ref, ref, shallowRef, watch} from 'vue'

Expand Down Expand Up @@ -60,12 +59,12 @@ export function useTethrConfig<N extends ConfigName>(
return readonly(config)
}

export function useTethr(onSave: (object: TethrObject) => void) {
export function useTethr() {
const manager = new TethrManager()

const pairedCameras = shallowRef<Tethr[]>([])

manager.getPairedCameras().then(cameras => {
manager.addListener('pairedCameraChange', cameras => {
pairedCameras.value = cameras
})

Expand All @@ -84,26 +83,33 @@ export function useTethr(onSave: (object: TethrObject) => void) {
}

async function openCamera(cam: Tethr) {
if (camera.value) {
await closeCamera(camera.value)
}

await cam.open()
camera.value = cam
cam.on('disconnect', onDisconnect)
cam.on('liveviewStreamUpdate', onLivewviewStreamUpdate)
}

async function closeCurrentSelectedCamera() {
async function closeCamera(cam: Tethr) {
if (!camera.value) return

camera.value.off('disconnect', onDisconnect)
camera.value.off('liveviewStreamUpdate', onLivewviewStreamUpdate)
await camera.value.close()
camera.value = null

if (camera.value === cam) {
camera.value = null
}
}

async function requestCameras(type: 'usbptp' | 'webcam') {
let cams: Tethr[]
let cam: Tethr | null
try {
cams = await manager.requestCameras(type)
if (cams.length === 0) {
cam = await manager.requestCamera(type)
if (!cam) {
return
}
} catch (err) {
Expand All @@ -113,26 +119,7 @@ export function useTethr(onSave: (object: TethrObject) => void) {
return
}

await closeCurrentSelectedCamera()

await openCamera(cams[0])
}

async function takePhoto() {
if (!camera.value) return
const result = await camera.value.takePhoto()
if (result.status === 'ok') {
for (const object of result.value) {
if (object.format !== 'raw') {
photoURL.value = URL.createObjectURL(object.blob)
}
onSave(object)
}
}
}

async function runAutoFocus() {
await camera.value?.runAutoFocus()
await openCamera(cam)
}

async function toggleLiveview() {
Expand All @@ -158,6 +145,8 @@ export function useTethr(onSave: (object: TethrObject) => void) {

return {
pairedCameras,
openCamera,
closeCamera,
camera,
requestCameras,
// DPC
Expand Down Expand Up @@ -199,8 +188,6 @@ export function useTethr(onSave: (object: TethrObject) => void) {
},
liveviewMediaStream,
photoURL,
runAutoFocus,
toggleLiveview,
takePhoto,
}
}
2 changes: 2 additions & 0 deletions src/Tethr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export abstract class Tethr
abstract close(): Promise<void>

abstract get opened(): boolean
abstract get type(): 'usbptp' | 'webcam'
abstract get name(): string

// eslint-disable-next-line @typescript-eslint/no-unused-vars
setLog(log: boolean) {
Expand Down
Loading

0 comments on commit 01c9600

Please sign in to comment.