Skip to content

Commit

Permalink
Merge pull request #10 from observerly/feature/composables/useStatus
Browse files Browse the repository at this point in the history
Added the useStatus() composable.
  • Loading branch information
michealroberts authored Nov 29, 2021
2 parents 2cac13d + 5e409f5 commit 539db8b
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 57 deletions.
110 changes: 56 additions & 54 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"dependencies": {
"@observerly/celestia": "^0.17.0",
"@vueuse/core": "^7.1.1",
"@vueuse/core": "^7.1.2",
"vue": "^3.2.23"
}
}
24 changes: 24 additions & 0 deletions src/composables/useStatus/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
category: Integration
---

# useStatus

Reactive Planewave API status client.

## Usage

```ts
import { watch } from 'vue'

import { useStatus } from '@observerly/useaestrium

const { status } = useStatus({ url: 'http://127.0.0.1:5000' })

// watch for a change in the status:
watch(status, () => {
console.log(status)
})
```

See the [Type Declarations](#type-declarations) for more options.
15 changes: 15 additions & 0 deletions src/composables/useStatus/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
describe,
expect,
it
} from '@jest/globals'

import {
useStatus
} from './'

describe('useStatus', () => {
it('Should Be Defined', () => {
expect(useStatus).toBeDefined()
})
})
79 changes: 79 additions & 0 deletions src/composables/useStatus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
onUnmounted, ref, Ref, watch
} from 'vue'

import {
useEventSource
} from '@vueuse/core'

import {
UseStatusResponse
} from '../../types'

export interface UseStatusOptions {
/**
*
* URL/ip:port/host to the remote observatory NAT
* @default 'http://0.0.0.0:8220'
*/
url: string
}

export type UseStatusReturn = {
status: Ref<UseStatusResponse>
}

export interface UseStatusProps extends Partial<UseStatusReturn> {}

// Default status:
const defaultStatus = {
isOffline: true,
isConnected: false,
isSlewing: false,
isTracking: false,
alt: Infinity,
az: Infinity
}

/**
* Reactive useTelescopeStatus()
* Returns the reactive status of a PlaneWave Astrograph Telescope
*
* @param options of type UseTelescopeStatusOptions
* @output reactive status
*/
export const useStatus = (
options: UseStatusOptions
): UseStatusReturn => {
const { url } = options

// Connect to the Telescope Status over server-sent events:
const { data, close, error } = useEventSource(`${url}/status`)

const status = ref<UseStatusResponse>(defaultStatus)

// Close the event source onUnmounted lifecycle hook:
onUnmounted(() => {
close()
})

// Watch for an error signal from the status server-sent event:
watch(error, () => {
if (error.value) {
if (error.value.type === 'error') {
status.value = defaultStatus
}
}
})

// Watch for an change of response data from the status server-sent event:
watch(data, () => {
if (data.value) {
status.value = JSON.parse(data.value)
}
})

return {
status
}
}
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ export {
UseMountReturn,
UseMountProps
} from './composables/useMount'

export {
useStatus,
UseStatusOptions,
UseStatusReturn,
UseStatusProps
} from './composables/useStatus'
4 changes: 2 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface UseStatusResponse {
*
* If PWI4 is not connected to the mount, this value will be 0.
*
* @default 0
* @default Infinity
*/
alt: number
/**
Expand All @@ -52,7 +52,7 @@ export interface UseStatusResponse {
*
* If PWI4 is not connected to the mount, this value will be 0.
*
* @default 0
* @default Infinity
*/
az: number
}

0 comments on commit 539db8b

Please sign in to comment.