|
| 1 | +# telemachus-client |
| 2 | + |
| 3 | +TypeScript client for the [Telemachus Reborn](https://github.com/TeleIO/Telemachus-1) KSP telemetry API. Covers all three transport layers — REST, batch datalink, and WebSocket streaming — with fully typed accessors generated from the API schema. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```bash |
| 8 | +bun add telemachus-client |
| 9 | +``` |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +```ts |
| 14 | +import { createClient } from "telemachus-client"; |
| 15 | + |
| 16 | +const ksp = createClient({ host: "http://localhost:8085" }); |
| 17 | +``` |
| 18 | + |
| 19 | +### REST — Single Key |
| 20 | + |
| 21 | +```ts |
| 22 | +const alt = await ksp.vessel.vAltitude(); // typed: number |
| 23 | +const pe = await ksp.orbit.oPeA(); // typed: number |
| 24 | +const mods = await ksp.system.aMods(); // typed: Record<string, unknown> |
| 25 | + |
| 26 | +// Actions (POST) |
| 27 | +await ksp.flight.fStage(); |
| 28 | +await ksp.flight.fSetThrottle(0.5); |
| 29 | + |
| 30 | +// Raw key access |
| 31 | +const val = await ksp.query("v.altitude"); |
| 32 | +await ksp.action("f.stage"); |
| 33 | +``` |
| 34 | + |
| 35 | +### Batch Datalink |
| 36 | + |
| 37 | +Query multiple keys in a single HTTP call: |
| 38 | + |
| 39 | +```ts |
| 40 | +const data = await ksp.batch(["v.altitude", "o.PeA", "o.ApA"]); |
| 41 | +// → { "v.altitude": 12500, "o.PeA": 75000, "o.ApA": 120000 } |
| 42 | +``` |
| 43 | + |
| 44 | +With scaling (for embedded controllers): |
| 45 | + |
| 46 | +```ts |
| 47 | +const data = await ksp.batch( |
| 48 | + ["v.altitude", "o.PeA"], |
| 49 | + { precision: 2, int: true } |
| 50 | +); |
| 51 | +``` |
| 52 | + |
| 53 | +Per-key pipe modifiers via `batchRaw`: |
| 54 | + |
| 55 | +```ts |
| 56 | +const data = await ksp.batchRaw({ |
| 57 | + alt: "v.altitude|precision:2|int", |
| 58 | + throttle: "f.setThrottle[512]|scale:0,1023", |
| 59 | +}); |
| 60 | +``` |
| 61 | + |
| 62 | +### WebSocket Streaming |
| 63 | + |
| 64 | +```ts |
| 65 | +const stream = ksp.stream({ |
| 66 | + subscribe: ["v.altitude", "o.PeA"], |
| 67 | + rate: 200, |
| 68 | +}); |
| 69 | + |
| 70 | +stream.on((data) => { |
| 71 | + console.log(data["v.altitude"], data["o.PeA"]); |
| 72 | +}); |
| 73 | + |
| 74 | +// Dynamic subscription |
| 75 | +stream.subscribe("v.verticalSpeed"); |
| 76 | +stream.unsubscribe("o.PeA"); |
| 77 | +stream.setRate(100); |
| 78 | + |
| 79 | +// Cleanup |
| 80 | +stream.close(); |
| 81 | +``` |
| 82 | + |
| 83 | +Auto-reconnects with exponential backoff by default. Disable with `reconnect: false`. |
| 84 | + |
| 85 | +## Scaling (Embedded Controllers) |
| 86 | + |
| 87 | +For integer-only boards (Pico, MicroBlocks, etc.), use scaling options on any endpoint: |
| 88 | + |
| 89 | +```ts |
| 90 | +// REST with scaling |
| 91 | +const val = await ksp.queryScaled("v.altitude", [], { |
| 92 | + precision: 2, |
| 93 | + int: true, |
| 94 | +}); |
| 95 | +// 12345.678 → 1234568 (divide by 100 on board) |
| 96 | + |
| 97 | +// Input scaling for actions |
| 98 | +const val = await ksp.queryScaled("f.setThrottle", [512], { |
| 99 | + scale: [0, 1023], |
| 100 | +}); |
| 101 | +// Maps 10-bit ADC value to 0.0–1.0 |
| 102 | +``` |
| 103 | + |
| 104 | +## API Categories |
| 105 | + |
| 106 | +All 520 API keys are organized into typed accessor groups: |
| 107 | + |
| 108 | +| Accessor | Keys | Example | |
| 109 | +|----------|------|---------| |
| 110 | +| `ksp.vessel` | `v.*` | `vAltitude()`, `vVerticalSpeed()` | |
| 111 | +| `ksp.orbit` | `o.*` | `oPeA()`, `oApA()`, `oInclination()` | |
| 112 | +| `ksp.flight` | `f.*` | `fSetThrottle(n)`, `fStage()` | |
| 113 | +| `ksp.body` | `b.*` | `bName(id)`, `bRadius(id)` | |
| 114 | +| `ksp.resource` | `r.*` | `rGetCurrentAmount(name)` | |
| 115 | +| `ksp.maneuver` | `m.*` | `mDeltaV()`, `mTimeTo()` | |
| 116 | +| `ksp.target` | `tar.*` | `tarName()`, `tarDistance()` | |
| 117 | +| `ksp.docking` | `dock.*` | `dockAngle()` | |
| 118 | +| `ksp.navigation` | `n.*` | `nHeading()`, `nPitch()` | |
| 119 | +| `ksp.system` | `a.*` | `aApi()`, `aMods()` | |
| 120 | + |
| 121 | +Mod-dependent keys (MechJeb, FAR, Principia, etc.) are included and documented — check `a.mods` at runtime to see what's available. |
| 122 | + |
| 123 | +## Key Metadata |
| 124 | + |
| 125 | +Access key metadata at runtime: |
| 126 | + |
| 127 | +```ts |
| 128 | +import { KEY_META } from "telemachus-client"; |
| 129 | + |
| 130 | +KEY_META["v.altitude"]; |
| 131 | +// { description: "Altitude", units: "m", isAction: false, plotable: true, category: "vessel", returnType: "double" } |
| 132 | +``` |
| 133 | + |
| 134 | +## Regenerating Types |
| 135 | + |
| 136 | +When the upstream API schema changes: |
| 137 | + |
| 138 | +```bash |
| 139 | +cp ../Telemachus-1/docs/api-schema.json tools/ |
| 140 | +bun run generate |
| 141 | +bun run build |
| 142 | +``` |
| 143 | + |
| 144 | +## License |
| 145 | + |
| 146 | +MIT |
0 commit comments