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 best timestamp available.
034     *
035     * @return Best available timestamp
036     */
037    public Timestamp getBestTimestamp() {
038        if (deviceTimestamp.isValid()) {
039            return deviceTimestamp;
040        }
041        if (canivoreTimestamp.isValid()) {
042            return canivoreTimestamp;
043        }
044        /* System timestamp is always available */
045        return systemTimestamp;
046    }
047
048    /**
049     * Get the timestamp as reported by the system.
050     *
051     * @return {@link Timestamp.TimestampSource#System System} timestamp
052     */
053    public Timestamp getSystemTimestamp() {
054        return systemTimestamp;
055    }
056    /**
057     * Get the timestamp as reported by the CANivore.
058     *
059     * @return {@link Timestamp.TimestampSource#CANivore CANivore} timestamp
060     */
061    public Timestamp getCANivoreTimestamp() {
062        return canivoreTimestamp;
063    }
064    /**
065     * Get the timestamp as reported by the device.
066     *
067     * @return {@link Timestamp.TimestampSource#Device Device} timestamp
068     */
069    public Timestamp getDeviceTimestamp() {
070        return deviceTimestamp;
071    }
072
073    @Override
074    public AllTimestamps clone() {
075        var toReturn = new AllTimestamps();
076        toReturn.update(
077            systemTimestamp.getTime(), systemTimestamp.getSource(), systemTimestamp.isValid(),
078            canivoreTimestamp.getTime(), canivoreTimestamp.getSource(), canivoreTimestamp.isValid(),
079            deviceTimestamp.getTime(), deviceTimestamp.getSource(), deviceTimestamp.isValid());
080        return toReturn;
081    }
082}