001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package org.wpilib.simulation;
006
007import org.wpilib.driverstation.Alert.Level;
008import org.wpilib.hardware.hal.AlertJNI;
009import org.wpilib.hardware.hal.simulation.AlertDataJNI;
010
011/** Simulation for alerts. */
012public final class AlertSim {
013  private AlertSim() {
014    throw new UnsupportedOperationException("This is a utility class!");
015  }
016
017  /** Information about an alert. */
018  public static class AlertInfo {
019    AlertInfo(AlertDataJNI.AlertInfo info) {
020      this.handle = info.handle;
021      this.group = info.group;
022      this.text = info.text;
023      this.activeStartTime = info.activeStartTime;
024      this.level =
025          switch (info.level) {
026            case AlertJNI.LEVEL_HIGH -> Level.HIGH;
027            case AlertJNI.LEVEL_MEDIUM -> Level.MEDIUM;
028            case AlertJNI.LEVEL_LOW -> Level.LOW;
029            default -> throw new IllegalArgumentException("Unknown alert level: " + info.level);
030          };
031    }
032
033    /** The handle of the alert. */
034    @SuppressWarnings("MemberName")
035    public final int handle;
036
037    /** The group of the alert. */
038    @SuppressWarnings("MemberName")
039    public final String group;
040
041    /** The text of the alert. */
042    @SuppressWarnings("MemberName")
043    public final String text;
044
045    /** The time the alert became active. 0 if not active. */
046    @SuppressWarnings("MemberName")
047    public final long activeStartTime;
048
049    /** The level of the alert (HIGH, MEDIUM, or LOW). */
050    @SuppressWarnings("MemberName")
051    public final Level level;
052
053    /**
054     * Returns whether the alert is currently active.
055     *
056     * @return true if the alert is active, false otherwise
057     */
058    public boolean isActive() {
059      return activeStartTime != 0;
060    }
061  }
062
063  /**
064   * Gets the number of alerts. Note: this is not guaranteed to be consistent with the number of
065   * alerts returned by GetAll.
066   *
067   * @return the number of alerts
068   */
069  public static int getCount() {
070    return AlertDataJNI.getNumAlerts();
071  }
072
073  /**
074   * Gets detailed information about each alert.
075   *
076   * @return Alerts
077   */
078  public static AlertInfo[] getAll() {
079    AlertDataJNI.AlertInfo[] alertInfos = AlertDataJNI.getAlerts();
080    AlertInfo[] infos = new AlertInfo[alertInfos.length];
081    for (int i = 0; i < alertInfos.length; i++) {
082      infos[i] = new AlertInfo(alertInfos[i]);
083    }
084    return infos;
085  }
086
087  /** Resets all alert simulation data. */
088  public static void resetData() {
089    AlertDataJNI.resetData();
090  }
091}