File size: 5,366 Bytes
9425aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**

 * ISS Telemetry Integration

 * Fetches live ISS position from BOB VOYAGER and renders on Bloch sphere

 * NORAD 25544 · ISS ZARYA

 */

export class ISSTelemetry {
  constructor(scene, options = {}) {
    this.scene = scene;
    this.voyagerUrl = options.voyagerUrl || 'http://localhost:4299';
    this.issMesh = null;
    this.trailPoints = [];
    this.trailGeometry = null;
    this.trailMesh = null;
    this.hudElement = options.hudElement || null;
    this.isLive = false;
    this.lastUpdate = null;
  }

  /**

   * Map lat/lon/alt to Bloch sphere coordinates

   * Normalize satellite position to unit sphere surface

   */
  latLonToBloch(lat, lon, alt) {
    // Convert to radians
    const latRad = (lat * Math.PI) / 180;
    const lonRad = (lon * Math.PI) / 180;

    // Normalize altitude (0-500km) to 0-0.3 sphere offset
    const altNorm = Math.min(alt / 500, 1.0) * 0.3;

    // Map to sphere surface with altitude offset
    const radius = 1.0 + altNorm;
    const x = radius * Math.cos(latRad) * Math.cos(lonRad);
    const y = radius * Math.sin(latRad);
    const z = radius * Math.cos(latRad) * Math.sin(lonRad);

    return { x, y, z };
  }

  /**

   * Create ISS position marker (glowing dot)

   */
  createISSTelemetryMarker() {
    if (this.issMesh) {
      this.scene.remove(this.issMesh);
    }

    // Glowing sphere
    const geo = new THREE.SphereGeometry(0.08, 16, 16);
    const mat = new THREE.MeshPhongMaterial({
      color: 0xff00ff,
      emissive: 0xff00ff,
      emissiveIntensity: 0.8,
      transparent: true,
      opacity: 0.9,
    });
    this.issMesh = new THREE.Mesh(geo, mat);
    this.scene.add(this.issMesh);

    // Glow effect (outer halo)
    const glowGeo = new THREE.SphereGeometry(0.12, 16, 16);
    const glowMat = new THREE.MeshBasicMaterial({
      color: 0xff00ff,
      transparent: true,
      opacity: 0.3,
    });
    const glowMesh = new THREE.Mesh(glowGeo, glowMat);
    this.issMesh.add(glowMesh);
  }

  /**

   * Initialize trail line (orbit path)

   */
  initializeTrail() {
    if (this.trailMesh) {
      this.scene.remove(this.trailMesh);
    }

    this.trailPoints = [];
    this.trailGeometry = new THREE.BufferGeometry();
    const trailMat = new THREE.LineBasicMaterial({
      color: 0xff00ff,
      transparent: true,
      opacity: 0.4,
      linewidth: 1,
    });
    this.trailMesh = new THREE.Line(this.trailGeometry, trailMat);
    this.scene.add(this.trailMesh);
  }

  /**

   * Add position to trail

   */
  addToTrail(pos) {
    this.trailPoints.push(new THREE.Vector3(pos.x, pos.y, pos.z));

    // Keep max 200 trail points
    if (this.trailPoints.length > 200) {
      this.trailPoints.shift();
    }

    if (this.trailGeometry) {
      this.trailGeometry.setFromPoints(this.trailPoints);
    }
  }

  /**

   * Fetch live ISS telemetry from BOB VOYAGER

   */
  async fetchISSTelemetry() {
    try {
      const res = await fetch(`${this.voyagerUrl}/api/telemetry`);
      const data = await res.json();

      if (!data.ok || !data.telemetry) {
        console.error('[ISS] Invalid telemetry response');
        return null;
      }

      return {
        lat: data.telemetry.latitude,
        lon: data.telemetry.longitude,
        alt: data.telemetry.altitude,
        vel: data.telemetry.velocity,
        timestamp: data.telemetry.fetched_at,
        worm: data.worm_head,
      };
    } catch (e) {
      console.error('[ISS] Fetch failed:', e.message);
      return null;
    }
  }

  /**

   * Update ISS position on sphere

   */
  async update() {
    const telemetry = await this.fetchISSTelemetry();
    if (!telemetry) return;

    if (!this.issMesh) {
      this.createISSTelemetryMarker();
      this.initializeTrail();
    }

    // Convert to Bloch sphere coordinates
    const pos = this.latLonToBloch(telemetry.lat, telemetry.lon, telemetry.alt);
    this.issMesh.position.set(pos.x, pos.y, pos.z);

    // Animate glow
    if (this.issMesh.children[0]) {
      this.issMesh.children[0].material.opacity = 0.3 + 0.2 * Math.sin(Date.now() * 0.005);
    }

    // Add to trail
    this.addToTrail(pos);

    // Update HUD
    if (this.hudElement) {
      this.hudElement.innerHTML = `

        <div><span class="label">ISS:</span> <span class="value">${telemetry.lat.toFixed(2)}° ${telemetry.lon.toFixed(2)}°</span></div>

        <div><span class="label">ALT:</span> <span class="value">${telemetry.alt.toFixed(0)}km VEL ${telemetry.vel.toFixed(2)}km/s</span></div>

        <div><span class="label">WORM:</span> <span class="value">${telemetry.worm}</span></div>

      `;
    }

    this.lastUpdate = telemetry;
    this.isLive = true;
  }

  /**

   * Start continuous updates

   */
  startPolling(intervalMs = 4500) {
    console.log('[ISS] Starting telemetry polling at', intervalMs, 'ms interval');
    this.pollingInterval = setInterval(() => this.update(), intervalMs);
    // Do first update immediately
    this.update();
  }

  /**

   * Stop polling

   */
  stopPolling() {
    if (this.pollingInterval) {
      clearInterval(this.pollingInterval);
    }
  }

  /**

   * Get last telemetry

   */
  getLastTelemetry() {
    return this.lastUpdate;
  }
}