zwo_mount_control 0.1.0

Rust library for controlling ZWO AM5/AM3 telescope mounts with satellite tracking support
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
//! Satellite tracking module using keplemon for TLE propagation.
//!
//! This module provides satellite tracking capabilities for ZWO mounts,
//! allowing you to track satellites, ISS, and other space objects using
//! Two-Line Element (TLE) data.
//!
//! # Example
//!
//! ```rust,ignore
//! use zwo_mount_control::{SatelliteTracker, Mount, MockMount};
//!
//! // ISS TLE data
//! let tle_line1 = "1 25544U 98067A   24001.50000000  .00016717  00000-0  10270-3 0  9025";
//! let tle_line2 = "2 25544  51.6400 208.9163 0006703  35.6028  75.3281 15.49560066429339";
//!
//! let mut tracker = SatelliteTracker::from_tle(tle_line1, tle_line2)?;
//!
//! // Set observer location (Los Angeles)
//! tracker.set_observer_location(34.0522, -118.2437, 71.0);
//!
//! // Get current satellite position
//! let position = tracker.get_current_position()?;
//! println!("Satellite position: {}", position);
//!
//! // Start tracking with a mount
//! let mut mount = MockMount::new();
//! mount.connect()?;
//! tracker.start_tracking(&mut mount)?;
//! ```

use std::time::{Duration, Instant};

use chrono::{DateTime, Utc};
use keplemon::bodies::{Observatory, Satellite};
use keplemon::elements::TLE;
use keplemon::enums::{ReferenceFrame, TimeSystem};
use keplemon::time::{Epoch, TimeSpan};

use crate::coordinates::{Coordinates, EquatorialPosition, HorizontalPosition, TrackingRates};
use crate::error::{MountError, MountResult};
use crate::mount::{Mount, SiteLocation};

/// Minimum elevation angle (degrees) for satellite visibility.
pub const DEFAULT_MIN_ELEVATION: f64 = 10.0;

/// Default tracking update interval in milliseconds.
pub const DEFAULT_UPDATE_INTERVAL_MS: u64 = 100;

/// Satellite pass information.
#[derive(Debug, Clone)]
pub struct SatellitePass {
    /// Name of the satellite
    pub name: String,
    /// NORAD catalog ID
    pub norad_id: i32,
    /// Start time of the pass (when satellite rises above minimum elevation)
    pub aos_time: DateTime<Utc>,
    /// Time of closest approach / maximum elevation
    pub tca_time: DateTime<Utc>,
    /// End time of the pass (when satellite sets below minimum elevation)
    pub los_time: DateTime<Utc>,
    /// Maximum elevation during the pass (degrees)
    pub max_elevation: f64,
    /// Azimuth at AOS (degrees)
    pub aos_azimuth: f64,
    /// Azimuth at LOS (degrees)
    pub los_azimuth: f64,
}

impl SatellitePass {
    /// Get the duration of the pass.
    pub fn duration(&self) -> Duration {
        let duration_secs = (self.los_time - self.aos_time).num_seconds();
        Duration::from_secs(duration_secs.max(0) as u64)
    }

    /// Check if the pass is currently active.
    pub fn is_active(&self, now: DateTime<Utc>) -> bool {
        now >= self.aos_time && now <= self.los_time
    }

    /// Check if this is a good pass (high elevation).
    pub fn is_good_pass(&self, min_max_elevation: f64) -> bool {
        self.max_elevation >= min_max_elevation
    }
}

impl std::fmt::Display for SatellitePass {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{} ({}): AOS {} @ {:.1}° Az, Max El {:.1}°, LOS {} @ {:.1}° Az, Duration {:?}",
            self.name,
            self.norad_id,
            self.aos_time.format("%H:%M:%S"),
            self.aos_azimuth,
            self.max_elevation,
            self.los_time.format("%H:%M:%S"),
            self.los_azimuth,
            self.duration()
        )
    }
}

/// Tracking state information.
#[derive(Debug, Clone)]
pub struct TrackingState {
    /// Current RA/Dec position
    pub equatorial: EquatorialPosition,
    /// Current Az/Alt position
    pub horizontal: HorizontalPosition,
    /// Current RA tracking rate (arcsec/sec)
    pub ra_rate: f64,
    /// Current Dec tracking rate (arcsec/sec)
    pub dec_rate: f64,
    /// Range to satellite (km)
    pub range_km: f64,
    /// Range rate (km/s, positive = moving away)
    pub range_rate_km_s: f64,
    /// Current time
    pub timestamp: DateTime<Utc>,
    /// Is satellite above horizon?
    pub is_visible: bool,
}

impl std::fmt::Display for TrackingState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{} | {} | Range: {:.1} km | Rates: RA {:.2}\"/s, Dec {:.2}\"/s | {}",
            self.equatorial,
            self.horizontal,
            self.range_km,
            self.ra_rate,
            self.dec_rate,
            if self.is_visible {
                "VISIBLE"
            } else {
                "BELOW HORIZON"
            }
        )
    }
}

/// Configuration for satellite tracking.
#[derive(Debug, Clone)]
pub struct TrackingConfig {
    /// Minimum elevation for tracking (degrees)
    pub min_elevation: f64,
    /// Update interval for tracking loop (milliseconds)
    pub update_interval_ms: u64,
    /// Lead time for position calculation (seconds)
    /// Accounts for mount slew delay
    pub lead_time_sec: f64,
    /// Whether to use rate tracking (vs. continuous goto)
    pub use_rate_tracking: bool,
    /// Maximum slew rate (deg/sec) - limits goto speed
    pub max_slew_rate: f64,
}

impl Default for TrackingConfig {
    fn default() -> Self {
        Self {
            min_elevation: DEFAULT_MIN_ELEVATION,
            update_interval_ms: DEFAULT_UPDATE_INTERVAL_MS,
            lead_time_sec: 0.5,
            use_rate_tracking: true,
            max_slew_rate: 5.0,
        }
    }
}

/// Satellite tracker using keplemon for orbit propagation.
pub struct SatelliteTracker {
    /// The satellite being tracked
    satellite: Satellite,
    /// TLE data
    tle: TLE,
    /// Observer location as keplemon Observatory
    observatory: Observatory,
    /// Observer site location
    site_location: SiteLocation,
    /// Tracking configuration
    config: TrackingConfig,
    /// Last calculated state
    last_state: Option<TrackingState>,
    /// Time of last update
    last_update: Option<Instant>,
}

impl SatelliteTracker {
    /// Create a new satellite tracker from TLE lines.
    ///
    /// # Arguments
    /// * `line1` - First line of TLE
    /// * `line2` - Second line of TLE
    ///
    /// # Example
    /// ```rust,ignore
    /// let tracker = SatelliteTracker::from_tle(
    ///     "1 25544U 98067A   24001.50000000  .00016717  00000-0  10270-3 0  9025",
    ///     "2 25544  51.6400 208.9163 0006703  35.6028  75.3281 15.49560066429339"
    /// )?;
    /// ```
    pub fn from_tle(line1: &str, line2: &str) -> MountResult<Self> {
        let tle = TLE::from_two_lines(line1, line2).map_err(|e| MountError::satellite_error(e))?;

        let satellite = Satellite::from(tle.clone());

        Ok(Self {
            satellite,
            tle,
            observatory: Observatory::new(0.0, 0.0, 0.0),
            site_location: SiteLocation::default(),
            config: TrackingConfig::default(),
            last_state: None,
            last_update: None,
        })
    }

    /// Create a new satellite tracker from three-line TLE (with name).
    ///
    /// # Arguments
    /// * `name` - Satellite name
    /// * `line1` - First line of TLE
    /// * `line2` - Second line of TLE
    pub fn from_tle_with_name(name: &str, line1: &str, line2: &str) -> MountResult<Self> {
        let tle = TLE::from_three_lines(name, line1, line2)
            .map_err(|e| MountError::satellite_error(e))?;

        let satellite = Satellite::from(tle.clone());

        Ok(Self {
            satellite,
            tle,
            observatory: Observatory::new(0.0, 0.0, 0.0),
            site_location: SiteLocation::default(),
            config: TrackingConfig::default(),
            last_state: None,
            last_update: None,
        })
    }

    /// Set the observer location.
    ///
    /// # Arguments
    /// * `latitude` - Observer latitude in degrees (positive = North)
    /// * `longitude` - Observer longitude in degrees (positive = East)
    /// * `altitude` - Observer altitude in meters
    pub fn set_observer_location(&mut self, latitude: f64, longitude: f64, altitude: f64) {
        self.observatory = Observatory::new(latitude, longitude, altitude / 1000.0); // keplemon uses km
        self.site_location = SiteLocation::new(latitude, longitude, altitude);
        log::info!(
            "Observer location set to lat={:.4}°, lon={:.4}°, alt={:.1}m",
            latitude,
            longitude,
            altitude
        );
    }

    /// Set the observer location from a SiteLocation.
    pub fn set_site_location(&mut self, location: SiteLocation) {
        self.set_observer_location(location.latitude, location.longitude, location.altitude);
    }

    /// Set tracking configuration.
    pub fn set_config(&mut self, config: TrackingConfig) {
        self.config = config;
    }

    /// Get the satellite name.
    pub fn get_name(&self) -> String {
        self.tle
            .get_name()
            .unwrap_or_else(|| format!("NORAD {}", self.tle.norad_id))
    }

    /// Get the NORAD catalog ID.
    pub fn get_norad_id(&self) -> i32 {
        self.tle.norad_id
    }

    /// Get the TLE epoch.
    pub fn get_tle_epoch(&self) -> DateTime<Utc> {
        let epoch = self.tle.get_epoch();
        datetime_from_epoch(epoch)
    }

    /// Get the current tracking state.
    pub fn get_current_state(&mut self) -> MountResult<TrackingState> {
        let now = Utc::now();
        self.get_state_at_time(now)
    }

    /// Get the tracking state at a specific time.
    pub fn get_state_at_time(&mut self, time: DateTime<Utc>) -> MountResult<TrackingState> {
        let epoch = epoch_from_datetime(time);

        // Get topocentric position from observatory
        let topo = self
            .observatory
            .get_topocentric_to_satellite(epoch, &self.satellite, ReferenceFrame::TEME)
            .map_err(|e| MountError::satellite_error(e))?;

        let ra = topo.right_ascension;
        let dec = topo.declination;
        let range = topo.range.unwrap_or(0.0);
        let range_rate = topo.range_rate.unwrap_or(0.0);

        // Calculate rates by computing position slightly in the future
        let dt_sec = 1.0;
        let future_epoch = epoch + TimeSpan::from_seconds(dt_sec);

        let future_topo = self
            .observatory
            .get_topocentric_to_satellite(future_epoch, &self.satellite, ReferenceFrame::TEME)
            .map_err(|e| MountError::satellite_error(e))?;

        // Calculate rates in arcsec/sec
        let ra_rate = (future_topo.right_ascension - ra) * 3600.0 / dt_sec;
        let dec_rate = (future_topo.declination - dec) * 3600.0 / dt_sec;

        // Convert RA from degrees to hours
        let ra_hours = ra / 15.0;

        // Calculate Az/Alt from RA/Dec
        let equatorial = EquatorialPosition::new(ra_hours, dec);
        let lst = Coordinates::julian_to_lst(
            epoch.days_since_1950 + 2433281.5, // Convert to JD
            self.site_location.longitude,
        );
        let horizontal = equatorial.to_horizontal(self.site_location.latitude, lst);

        let state = TrackingState {
            equatorial,
            horizontal,
            ra_rate,
            dec_rate,
            range_km: range,
            range_rate_km_s: range_rate,
            timestamp: time,
            is_visible: horizontal.altitude > self.config.min_elevation,
        };

        self.last_state = Some(state.clone());
        self.last_update = Some(Instant::now());

        Ok(state)
    }

    /// Get the current position as EquatorialPosition.
    pub fn get_current_position(&mut self) -> MountResult<EquatorialPosition> {
        let state = self.get_current_state()?;
        Ok(state.equatorial)
    }

    /// Get the current horizontal position (Az/Alt).
    pub fn get_current_altaz(&mut self) -> MountResult<HorizontalPosition> {
        let state = self.get_current_state()?;
        Ok(state.horizontal)
    }

    /// Get current tracking rates.
    pub fn get_current_rates(&mut self) -> MountResult<TrackingRates> {
        let state = self.get_current_state()?;
        Ok(TrackingRates::new(state.ra_rate, state.dec_rate))
    }

    /// Check if the satellite is currently visible.
    pub fn is_visible(&mut self) -> MountResult<bool> {
        let state = self.get_current_state()?;
        Ok(state.is_visible)
    }

    /// Calculate the position with lead time compensation.
    ///
    /// This accounts for mount slew delay by calculating where the satellite
    /// will be slightly in the future.
    pub fn get_lead_position(&mut self) -> MountResult<(EquatorialPosition, TrackingRates)> {
        let future_time = Utc::now()
            + chrono::Duration::milliseconds((self.config.lead_time_sec * 1000.0) as i64);

        let state = self.get_state_at_time(future_time)?;
        Ok((
            state.equatorial,
            TrackingRates::new(state.ra_rate, state.dec_rate),
        ))
    }

    /// Find the next satellite pass.
    ///
    /// # Arguments
    /// * `start_time` - Start searching from this time
    /// * `search_hours` - How many hours to search ahead
    pub fn find_next_pass(
        &mut self,
        start_time: DateTime<Utc>,
        search_hours: f64,
    ) -> MountResult<Option<SatellitePass>> {
        let step_seconds = 60.0; // 1-minute steps for initial search
        let _fine_step_seconds = 1.0; // 1-second steps for refinement

        let mut current_time = start_time;
        let end_time = start_time + chrono::Duration::seconds((search_hours * 3600.0) as i64);

        let mut pass_start: Option<DateTime<Utc>> = None;
        let mut max_elevation = 0.0f64;
        let mut tca_time = start_time;
        let mut aos_azimuth = 0.0;

        while current_time < end_time {
            let state = self.get_state_at_time(current_time)?;

            if state.is_visible {
                if pass_start.is_none() {
                    // Refine AOS time
                    let refined_aos = self.refine_crossing_time(
                        current_time - chrono::Duration::seconds(step_seconds as i64),
                        current_time,
                        true,
                    )?;
                    pass_start = Some(refined_aos);

                    let aos_state = self.get_state_at_time(refined_aos)?;
                    aos_azimuth = aos_state.horizontal.azimuth;
                }

                if state.horizontal.altitude > max_elevation {
                    max_elevation = state.horizontal.altitude;
                    tca_time = current_time;
                }
            } else if pass_start.is_some() {
                // End of pass - refine LOS time
                let refined_los = self.refine_crossing_time(
                    current_time - chrono::Duration::seconds(step_seconds as i64),
                    current_time,
                    false,
                )?;

                let los_state = self.get_state_at_time(refined_los)?;

                return Ok(Some(SatellitePass {
                    name: self.get_name(),
                    norad_id: self.get_norad_id(),
                    aos_time: pass_start.unwrap(),
                    tca_time,
                    los_time: refined_los,
                    max_elevation,
                    aos_azimuth,
                    los_azimuth: los_state.horizontal.azimuth,
                }));
            }

            current_time = current_time + chrono::Duration::seconds(step_seconds as i64);
        }

        // Check if we're still in a pass at the end of search
        if let Some(aos) = pass_start {
            let final_state = self.get_state_at_time(end_time)?;
            return Ok(Some(SatellitePass {
                name: self.get_name(),
                norad_id: self.get_norad_id(),
                aos_time: aos,
                tca_time,
                los_time: end_time,
                max_elevation,
                aos_azimuth,
                los_azimuth: final_state.horizontal.azimuth,
            }));
        }

        Ok(None)
    }

    /// Find all passes within a time window.
    pub fn find_passes(
        &mut self,
        start_time: DateTime<Utc>,
        search_hours: f64,
        min_max_elevation: f64,
    ) -> MountResult<Vec<SatellitePass>> {
        let mut passes = Vec::new();
        let mut current_search_time = start_time;
        let end_time = start_time + chrono::Duration::seconds((search_hours * 3600.0) as i64);

        while current_search_time < end_time {
            let remaining_hours = (end_time - current_search_time).num_seconds() as f64 / 3600.0;

            if let Some(pass) = self.find_next_pass(current_search_time, remaining_hours)? {
                current_search_time = pass.los_time + chrono::Duration::seconds(60);

                if pass.max_elevation >= min_max_elevation {
                    passes.push(pass);
                }
            } else {
                break;
            }
        }

        Ok(passes)
    }

    /// Refine the horizon crossing time using binary search.
    fn refine_crossing_time(
        &mut self,
        before: DateTime<Utc>,
        after: DateTime<Utc>,
        rising: bool,
    ) -> MountResult<DateTime<Utc>> {
        let mut low = before;
        let mut high = after;

        for _ in 0..20 {
            // ~1ms precision
            let mid =
                low + chrono::Duration::milliseconds(((high - low).num_milliseconds() / 2) as i64);

            let state = self.get_state_at_time(mid)?;
            let is_visible = state.horizontal.altitude > self.config.min_elevation;

            if rising {
                if is_visible {
                    high = mid;
                } else {
                    low = mid;
                }
            } else {
                if is_visible {
                    low = mid;
                } else {
                    high = mid;
                }
            }

            if (high - low).num_milliseconds() < 100 {
                break;
            }
        }

        Ok(if rising { high } else { low })
    }

    /// Start continuous tracking on a mount.
    ///
    /// This method runs a tracking loop that continuously updates the mount
    /// position to follow the satellite. It returns when the satellite sets
    /// below the minimum elevation.
    ///
    /// # Arguments
    /// * `mount` - The mount to control
    ///
    /// # Returns
    /// The total duration of tracking
    pub fn track_pass(&mut self, mount: &mut dyn Mount) -> MountResult<Duration> {
        let start_time = Instant::now();

        // Ensure mount is ready
        if mount.is_parked()? {
            mount.unpark()?;
        }

        // Initial slew to satellite position
        let initial_state = self.get_current_state()?;
        if !initial_state.is_visible {
            return Err(MountError::BelowHorizon(initial_state.horizontal.altitude));
        }

        log::info!(
            "Starting tracking of {} at {}",
            self.get_name(),
            initial_state
        );

        // Slew to initial position
        mount.goto_equatorial(initial_state.equatorial)?;

        // Wait for initial slew to complete
        while mount.is_slewing()? {
            std::thread::sleep(Duration::from_millis(100));
        }

        // Main tracking loop
        let update_interval = Duration::from_millis(self.config.update_interval_ms);

        loop {
            let loop_start = Instant::now();

            // Get current state with lead time
            let (position, rates) = self.get_lead_position()?;

            // Check if still visible
            let state = self.get_current_state()?;
            if !state.is_visible {
                log::info!(
                    "Satellite {} below horizon, stopping tracking",
                    self.get_name()
                );
                break;
            }

            // Update mount
            if self.config.use_rate_tracking && rates.is_valid_for_satellite() {
                // Use rate tracking if supported and rates are reasonable
                mount.set_custom_tracking_rates(rates)?;
            } else {
                // Fall back to continuous goto
                mount.goto_equatorial(position)?;
            }

            // Log status periodically
            log::debug!("Tracking: {}", state);

            // Wait for next update
            let elapsed = loop_start.elapsed();
            if elapsed < update_interval {
                std::thread::sleep(update_interval - elapsed);
            }
        }

        // Stop tracking
        mount.tracking_off()?;

        let tracking_duration = start_time.elapsed();
        log::info!(
            "Tracking complete. Duration: {:.1}s",
            tracking_duration.as_secs_f64()
        );

        Ok(tracking_duration)
    }

    /// Perform a single tracking update (for custom tracking loops).
    ///
    /// Returns the updated tracking state.
    pub fn update_tracking(&mut self, mount: &mut dyn Mount) -> MountResult<TrackingState> {
        let (position, rates) = self.get_lead_position()?;
        let state = self.get_current_state()?;

        if !state.is_visible {
            return Err(MountError::BelowHorizon(state.horizontal.altitude));
        }

        if self.config.use_rate_tracking && rates.is_valid_for_satellite() {
            mount.set_custom_tracking_rates(rates)?;
        } else {
            mount.goto_equatorial(position)?;
        }

        Ok(state)
    }
}

/// Convert keplemon Epoch to chrono DateTime.
fn datetime_from_epoch(epoch: Epoch) -> DateTime<Utc> {
    // Days since 1950 to Unix timestamp
    // Jan 1, 1950 00:00:00 UTC = -631152000 Unix seconds
    let unix_seconds = (epoch.days_since_1950 * 86400.0) - 631152000.0;
    let secs = unix_seconds.floor() as i64;
    let nsecs = ((unix_seconds - secs as f64) * 1_000_000_000.0) as u32;

    DateTime::from_timestamp(secs, nsecs).unwrap_or_else(|| Utc::now())
}

/// Convert chrono DateTime to keplemon Epoch.
fn epoch_from_datetime(dt: DateTime<Utc>) -> Epoch {
    // Unix timestamp to days since 1950
    let unix_seconds = dt.timestamp() as f64 + dt.timestamp_subsec_nanos() as f64 / 1_000_000_000.0;
    let days_since_1950 = (unix_seconds + 631152000.0) / 86400.0;

    Epoch::from_days_since_1950(days_since_1950, TimeSystem::UTC)
}

#[cfg(test)]
mod tests {
    use super::*;

    // ISS TLE for testing
    const ISS_LINE1: &str = "1 25544U 98067A   24001.50000000  .00016717  00000-0  10270-3 0  9025";
    const ISS_LINE2: &str = "2 25544  51.6400 208.9163 0006703  35.6028  75.3281 15.49560066429339";

    #[test]
    fn test_create_tracker() {
        let result = SatelliteTracker::from_tle(ISS_LINE1, ISS_LINE2);
        assert!(result.is_ok());

        let tracker = result.unwrap();
        assert_eq!(tracker.get_norad_id(), 25544);
    }

    #[test]
    fn test_create_tracker_with_name() {
        let result = SatelliteTracker::from_tle_with_name("ISS (ZARYA)", ISS_LINE1, ISS_LINE2);
        assert!(result.is_ok());

        let tracker = result.unwrap();
        assert_eq!(tracker.get_name(), "ISS (ZARYA)");
    }

    #[test]
    fn test_set_observer_location() {
        let mut tracker = SatelliteTracker::from_tle(ISS_LINE1, ISS_LINE2).unwrap();
        tracker.set_observer_location(34.0522, -118.2437, 71.0);

        assert!((tracker.site_location.latitude - 34.0522).abs() < 0.0001);
        assert!((tracker.site_location.longitude - (-118.2437)).abs() < 0.0001);
    }

    #[test]
    fn test_tracking_config_default() {
        let config = TrackingConfig::default();
        assert!((config.min_elevation - DEFAULT_MIN_ELEVATION).abs() < 0.01);
        assert_eq!(config.update_interval_ms, DEFAULT_UPDATE_INTERVAL_MS);
    }

    #[test]
    fn test_satellite_pass_duration() {
        let pass = SatellitePass {
            name: "ISS".to_string(),
            norad_id: 25544,
            aos_time: Utc::now(),
            tca_time: Utc::now() + chrono::Duration::seconds(300),
            los_time: Utc::now() + chrono::Duration::seconds(600),
            max_elevation: 75.0,
            aos_azimuth: 180.0,
            los_azimuth: 45.0,
        };

        assert_eq!(pass.duration(), Duration::from_secs(600));
        assert!(pass.is_good_pass(45.0));
        assert!(!pass.is_good_pass(80.0));
    }

    #[test]
    fn test_epoch_conversion() {
        let now = Utc::now();
        let epoch = epoch_from_datetime(now);
        let converted = datetime_from_epoch(epoch);

        // Should be within 1 second
        let diff = (now - converted).num_seconds().abs();
        assert!(diff <= 1);
    }
}