Skip to content

Commit 2191284

Browse files
committed
implement indi simulator
1 parent d5eed82 commit 2191284

6 files changed

Lines changed: 509 additions & 290 deletions

File tree

src/alpaca.client.ts

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { handleDefNumberVector, handleDefSwitchVector, handleDefTextVector, hand
1010
import type { Camera, Client, Device, Focuser, Mount, Rotator, Wheel } from './indi.device'
1111
import type { DeviceProvider } from './indi.manager'
1212
// biome-ignore format: too long!
13-
import { type DefBlob, type DefBlobVector, type DefNumber, type DefNumberVector, type DefSwitch, type DefSwitchVector, type DefText, type DefTextVector, type DefVector, type EnableBlob, findOnSwitch, type GetProperties, type NewNumberVector, type NewSwitchVector, type NewTextVector, type OneBlob, type PropertyPermission, type PropertyState, type SetBlobVector, type SwitchRule, type ValueType, type VectorType } from './indi.types'
13+
import { type DefSwitchVector, type DefVector, type EnableBlob, findOnSwitch, type GetProperties, makeBlobVector, makeNumberVector, makeSwitchVector, makeTextVector, type NewNumberVector, type NewSwitchVector, type NewTextVector, type PropertyState, type ValueType, type VectorType } from './indi.types'
1414
import { roundToNthDecimal } from './math'
1515
import { formatTemporal, TIMEZONE } from './temporal'
1616
import { type Time, timeNow } from './time'
@@ -51,7 +51,7 @@ export class AlpacaClient implements Client {
5151
if (command?.device) {
5252
this.devices.get(command.device)?.sendProperties(command.name)
5353
} else {
54-
for (const [, device] of this.devices) device.sendProperties(command?.name)
54+
for (const device of this.devices) device[1].sendProperties(command?.name)
5555
}
5656
}
5757

@@ -111,15 +111,15 @@ export class AlpacaClient implements Client {
111111
}
112112

113113
private update() {
114-
for (const [, device] of this.devices) device.update()
114+
for (const device of this.devices) device[1].update()
115115
}
116116

117117
stop(server: boolean = false) {
118118
if (this.timer) {
119119
clearInterval(this.timer)
120120
this.timer = undefined
121121

122-
for (const [, device] of this.devices) device.close()
122+
for (const device of this.devices) device[1].close()
123123
this.devices.clear()
124124

125125
this.options?.handler?.close?.(this, server)
@@ -1797,30 +1797,6 @@ function normalizeLongitude(angle: number) {
17971797
return angle
17981798
}
17991799

1800-
function makeSwitchVector(device: string, name: string, label: string, group: string, rule: SwitchRule, permission: PropertyPermission, ...properties: readonly [string, string, boolean][]): DefSwitchVector & { type: 'SWITCH' } {
1801-
const elements: Record<string, DefSwitch> = {}
1802-
for (const [name, label, value] of properties) elements[name] = { name, label, value }
1803-
return { type: 'SWITCH', device, name, label, group, permission, rule, state: 'Idle', timeout: 60, elements }
1804-
}
1805-
1806-
function makeNumberVector(device: string, name: string, label: string, group: string, permission: PropertyPermission, ...properties: readonly [string, string, number, number, number, number, string][]): DefNumberVector & { type: 'NUMBER' } {
1807-
const elements: Record<string, DefNumber> = {}
1808-
for (const [name, label, value, min, max, step, format] of properties) elements[name] = { name, label, value, min, max, step, format }
1809-
return { type: 'NUMBER', device, name, label, group, permission, state: 'Idle', timeout: 60, elements }
1810-
}
1811-
1812-
function makeTextVector(device: string, name: string, label: string, group: string, permission: PropertyPermission, ...properties: readonly [string, string, string][]): DefTextVector & { type: 'TEXT' } {
1813-
const elements: Record<string, DefText> = {}
1814-
for (const [name, label, value] of properties) elements[name] = { name, label, value }
1815-
return { type: 'TEXT', device, name, label, group, permission, state: 'Idle', timeout: 60, elements }
1816-
}
1817-
1818-
function makeBlobVector(device: string, name: string, label: string, group: string, permission: PropertyPermission, ...properties: readonly [string, string][]): Omit<DefBlobVector, 'elements'> & SetBlobVector & { type: 'BLOB' } {
1819-
const elements: Record<string, Omit<DefBlob, 'value'> & OneBlob> = {}
1820-
for (const [name, label] of properties) elements[name] = { name, label, size: '0', format: 'fits', value: '' }
1821-
return { type: 'BLOB', device, name, label, group, permission, state: 'Idle', timeout: 60, elements }
1822-
}
1823-
18241800
// https://github.com/ASCOMInitiative/ASCOMRemote/blob/main/Documentation/AlpacaImageBytes.pdf
18251801

18261802
export function makeFitsFromImageBytes(data: ArrayBuffer, time?: Time, camera?: Camera, mount?: Mount, wheel?: Wheel, focuser?: Focuser, rotator?: Rotator, lastExposureDuration: number = 0) {

src/indi.client.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,92 @@ export class IndiClient implements Client {
362362
}
363363
}
364364

365+
export class IndiClientHandlerSet extends Set<IndiClientHandler> implements IndiClientHandler {
366+
message(client: Client, message: Message) {
367+
for (const handler of this) handler.message?.(client, message)
368+
}
369+
370+
delProperty(client: Client, message: DelProperty) {
371+
for (const handler of this) handler.delProperty?.(client, message)
372+
}
373+
374+
vector(client: Client, message: DefVector | SetVector, tag: `def${VectorType}Vector` | `set${VectorType}Vector`) {
375+
for (const handler of this) handler.vector?.(client, message, tag)
376+
}
377+
378+
defTextVector(client: Client, message: DefTextVector) {
379+
for (const handler of this) handler.defTextVector?.(client, message)
380+
}
381+
382+
defNumberVector(client: Client, message: DefNumberVector) {
383+
for (const handler of this) handler.defNumberVector?.(client, message)
384+
}
385+
386+
defSwitchVector(client: Client, message: DefSwitchVector) {
387+
for (const handler of this) handler.defSwitchVector?.(client, message)
388+
}
389+
390+
defLightVector(client: Client, message: DefLightVector) {
391+
for (const handler of this) handler.defLightVector?.(client, message)
392+
}
393+
394+
defBlobVector(client: Client, message: DefBlobVector) {
395+
for (const handler of this) handler.defBlobVector?.(client, message)
396+
}
397+
398+
defVector(client: Client, message: DefVector, tag: `def${VectorType}Vector`) {
399+
for (const handler of this) handler.defVector?.(client, message, tag)
400+
}
401+
402+
setTextVector(client: Client, message: SetTextVector) {
403+
for (const handler of this) handler.setTextVector?.(client, message)
404+
}
405+
406+
setNumberVector(client: Client, message: SetNumberVector) {
407+
for (const handler of this) handler.setNumberVector?.(client, message)
408+
}
409+
410+
setSwitchVector(client: Client, message: SetSwitchVector) {
411+
for (const handler of this) handler.setSwitchVector?.(client, message)
412+
}
413+
414+
setLightVector(client: Client, message: SetLightVector) {
415+
for (const handler of this) handler.setLightVector?.(client, message)
416+
}
417+
418+
setBlobVector(client: Client, message: SetBlobVector) {
419+
for (const handler of this) handler.setBlobVector?.(client, message)
420+
}
421+
422+
setVector(client: Client, message: SetVector, tag: `set${VectorType}Vector`) {
423+
for (const handler of this) handler.setVector?.(client, message, tag)
424+
}
425+
426+
textVector(client: Client, message: DefTextVector | SetTextVector, tag: 'defTextVector' | 'setTextVector') {
427+
for (const handler of this) handler.textVector?.(client, message, tag)
428+
}
429+
430+
numberVector(client: Client, message: DefNumberVector | SetNumberVector, tag: 'defNumberVector' | 'setNumberVector') {
431+
for (const handler of this) handler.numberVector?.(client, message, tag)
432+
}
433+
434+
switchVector(client: Client, message: DefSwitchVector | SetSwitchVector, tag: 'defSwitchVector' | 'setSwitchVector') {
435+
for (const handler of this) handler.switchVector?.(client, message, tag)
436+
}
437+
438+
lightVector(client: Client, message: DefLightVector | SetLightVector, tag: 'defLightVector' | 'setLightVector') {
439+
for (const handler of this) handler.lightVector?.(client, message, tag)
440+
}
441+
442+
blobVector(client: Client, message: DefBlobVector | SetBlobVector, tag: 'defBLOBVector' | 'setBLOBVector') {
443+
for (const handler of this) handler.blobVector?.(client, message, tag)
444+
}
445+
446+
close(client: Client, server: boolean) {
447+
for (const handler of this) handler.close?.(client, server)
448+
}
449+
}
450+
365451
export function handleDefVector(client: Client, handler: IndiClientHandler, message: DefVector, tag: `def${VectorType}Vector`) {
366452
handler.defVector?.(client, message, tag)
367453
handler.vector?.(client, message, tag)

src/indi.manager.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,19 @@ export abstract class DeviceManager<D extends Device> implements IndiClientHandl
200200
}
201201

202202
added(device: D) {
203-
this.handlers.forEach((e) => e.added(device))
203+
for (const handler of this.handlers) handler.added(device)
204204
}
205205

206206
updated(device: D, property: keyof D & string, state?: PropertyState) {
207-
this.handlers.forEach((e) => e.updated?.(device, property, state))
207+
for (const handler of this.handlers) handler.updated?.(device, property, state)
208208
}
209209

210210
removed(device: D) {
211-
this.handlers.forEach((e) => e.removed(device))
211+
for (const handler of this.handlers) handler.removed(device)
212212
}
213213

214214
blobReceived(device: D, data: string | Buffer<ArrayBuffer>) {
215-
this.handlers.forEach((e) => e.blobReceived?.(device, data))
215+
for (const handler of this.handlers) handler.blobReceived?.(device, data)
216216
}
217217

218218
list(client?: Client | string) {
@@ -363,8 +363,8 @@ export abstract class DeviceManager<D extends Device> implements IndiClientHandl
363363
const devices = this.devices.get(client)
364364

365365
if (devices) {
366-
for (const [_, device] of devices) {
367-
this.remove(device)
366+
for (const device of devices) {
367+
this.remove(device[1])
368368
}
369369
}
370370

@@ -1094,11 +1094,11 @@ export class MountManager extends DeviceManager<Mount> {
10941094
this.updated(device, 'canHome', message.state)
10951095
}
10961096

1097-
if (handleSwitchValue(device, 'canHome', 'FIND' in elements)) {
1097+
if (handleSwitchValue(device, 'canFindHome', 'FIND' in elements)) {
10981098
this.updated(device, 'canFindHome', message.state)
10991099
}
11001100

1101-
if (handleSwitchValue(device, 'canHome', 'SET' in elements)) {
1101+
if (handleSwitchValue(device, 'canSetHome', 'SET' in elements)) {
11021102
this.updated(device, 'canSetHome', message.state)
11031103
}
11041104
}

0 commit comments

Comments
 (0)