From 18c6fd83b52e7dedcf898663502d704f1dd5b78d Mon Sep 17 00:00:00 2001 From: "Michael J. Roberts" Date: Thu, 20 Apr 2023 12:30:23 +0100 Subject: [PATCH] feat: Added client.telescope.slewToHorizontalCoordinate() route handler. feat: Added client.telescope.slewToHorizontalCoordinate() route handler. Includes associated test suite for module export definition and expected output from API route. --- src/routes/telescope.ts | 22 +++++++++++++++++++ tests/mocks/telescope.ts | 41 +++++++++++++++++++++++++++++++++++ tests/telescopeRoutes.spec.ts | 9 ++++++++ 3 files changed, 72 insertions(+) diff --git a/src/routes/telescope.ts b/src/routes/telescope.ts index d4dfb9e..92c425b 100644 --- a/src/routes/telescope.ts +++ b/src/routes/telescope.ts @@ -133,6 +133,28 @@ export const telescope = ( const data = JSON.stringify(body) + return dispatchRequest( + url, + { ...init, method: 'PUT', body: JSON.stringify(body) }, + headers, + data + ) + } + }, + { + name: 'slewToHorizontalCoordinate', + action: < + T = { + slewing: true + } + >(body: { + az: number + alt: number + }) => { + const url = new URL('telescope/slew/horizontal', base) + + const data = JSON.stringify(body) + return dispatchRequest( url, { ...init, method: 'PUT', body: JSON.stringify(body) }, diff --git a/tests/mocks/telescope.ts b/tests/mocks/telescope.ts index 2c6b7fe..78f9bff 100644 --- a/tests/mocks/telescope.ts +++ b/tests/mocks/telescope.ts @@ -132,6 +132,47 @@ export const telescopeHandlers: Handler[] = [ }) } + return { + slewing: true + } + }) + }, + { + method: 'PUT', + url: '/api/v1/telescope/slew/horizontal', + handler: eventHandler(async event => { + const method = getMethod(event) + + if (method !== 'PUT') { + return new Response('Method Not Allowed', { + status: 405, + statusText: 'Method Not Allowed' + }) + } + + const body = await readBody<{ az: number; alt: number }>(event) + + if (!body) { + return new Response('Bad Request', { + status: 400, + statusText: 'Bad Request' + }) + } + + if (body.az > 360 || body.az < 0) { + return new Response('Bad Request', { + status: 400, + statusText: 'Bad Request' + }) + } + + if (body.alt > 90 || body.alt < -90) { + return new Response('Bad Request', { + status: 400, + statusText: 'Bad Request' + }) + } + return { slewing: true } diff --git a/tests/telescopeRoutes.spec.ts b/tests/telescopeRoutes.spec.ts index 69a578e..383e05c 100644 --- a/tests/telescopeRoutes.spec.ts +++ b/tests/telescopeRoutes.spec.ts @@ -69,5 +69,14 @@ suite('@observerly/hyper Fiber API Telescope Client', () => { }) expect(slewing).toBe(true) }) + + it('should be able to set the horizontal coordinates of the telescope', async () => { + const client = setupClient(getURL('/api/v1/')) + const { slewing } = await client.telescope.slewToHorizontalCoordinate({ + az: 23.012001, + alt: 33.511206 + }) + expect(slewing).toBe(true) + }) }) })