001/*
002 * Copyright (C) Cross The Road Electronics.  All rights reserved.
003 * License information can be found in CTRE_LICENSE.txt
004 * For support and suggestions contact support@ctr-electronics.com or file
005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
006 */
007package com.ctre.phoenix6;
008
009/**
010 * A collection of timestamps for a received signal.
011 */
012public class AllTimestamps implements Cloneable {
013    private Timestamp systemTimestamp = new Timestamp();
014    private Timestamp canivoreTimestamp = new Timestamp();
015    private Timestamp deviceTimestamp = new Timestamp();
016
017    void update(Timestamp newSystemTimestamp, Timestamp newCanivoreTimestamp, Timestamp newDeviceTimestamp) {
018        systemTimestamp = newSystemTimestamp;
019        canivoreTimestamp = newCanivoreTimestamp;
020        deviceTimestamp = newDeviceTimestamp;
021    }
022    void update(
023        double systemTimestampSeconds, Timestamp.TimestampSource systemTimestampSource, boolean systemTimestampValid,
024        double canivoreTimestampSeconds, Timestamp.TimestampSource canivoreTimestampSource, boolean canivoreTimestampValid,
025        double deviceTimestampSeconds, Timestamp.TimestampSource deviceTimestampSource, boolean deviceTimestampValid
026    ) {
027        this.systemTimestamp.update(systemTimestampSeconds, systemTimestampSource, systemTimestampValid);
028        this.canivoreTimestamp.update(canivoreTimestampSeconds, canivoreTimestampSource, canivoreTimestampValid);
029        this.deviceTimestamp.update(deviceTimestampSeconds, deviceTimestampSource, deviceTimestampValid);
030    }
031
032    /**
033     * Get the most accurate timestamp available.
034     * <p>
035     * The timestamp sources from most to least accurate are:
036     * <ul>
037     *   <li> {@link Timestamp.TimestampSource#Device}
038     *   <li> {@link Timestamp.TimestampSource#CANivore}
039     *   <li> {@link Timestamp.TimestampSource#System}
040     * </ul>
041     * Note that some of these sources may not be available.
042     *
043     * @return The most accurate timestamp available
044     */
045    public Timestamp getBestTimestamp() {
046        if (deviceTimestamp.isValid()) {
047            return deviceTimestamp;
048        }
049        if (canivoreTimestamp.isValid()) {
050            return canivoreTimestamp;
051        }
052        /* System timestamp is always available */
053        return systemTimestamp;
054    }
055
056    /**
057     * Get the timestamp as reported by the {@link Timestamp.TimestampSource#System} source.
058     *
059     * @return Timestamp as reported by the System
060     */
061    public Timestamp getSystemTimestamp() {
062        return systemTimestamp;
063    }
064    /**
065     * Get the timestamp as reported by the {@link Timestamp.TimestampSource#CANivore} source.
066     *
067     * @return Timestamp as reported by the CANivore
068     */
069    public Timestamp getCANivoreTimestamp() {
070        return canivoreTimestamp;
071    }
072    /**
073     * Get the timestamp as reported by the {@link Timestamp.TimestampSource#Device} source.
074     *
075     * @return Timestamp as reported by the Device
076     */
077    public Timestamp getDeviceTimestamp() {
078        return deviceTimestamp;
079    }
080
081    @Override
082    public AllTimestamps clone() {
083        var toReturn = new AllTimestamps();
084        toReturn.update(
085            systemTimestamp.getTime(), systemTimestamp.getSource(), systemTimestamp.isValid(),
086            canivoreTimestamp.getTime(), canivoreTimestamp.getSource(), canivoreTimestamp.isValid(),
087            deviceTimestamp.getTime(), deviceTimestamp.getSource(), deviceTimestamp.isValid());
088        return toReturn;
089    }
090}