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    public final int handle;
035
036    /** The group of the alert. */
037    public final String group;
038
039    /** The text of the alert. */
040    public final String text;
041
042    /** The time the alert became active. 0 if not active. */
043    public final long activeStartTime;
044
045    /** The level of the alert (HIGH, MEDIUM, or LOW). */
046    public final Level level;
047
048    /**
049     * Returns whether the alert is currently active.
050     *
051     * @return true if the alert is active, false otherwise
052     */
053    public boolean isActive() {
054      return activeStartTime != 0;
055    }
056  }
057
058  /**
059   * Gets the number of alerts. Note: this is not guaranteed to be consistent with the number of
060   * alerts returned by GetAll.
061   *
062   * @return the number of alerts
063   */
064  public static int getCount() {
065    return AlertDataJNI.getNumAlerts();
066  }
067
068  /**
069   * Gets detailed information about each alert (including inactive ones).
070   *
071   * @return Alerts
072   */
073  public static AlertInfo[] getAll() {
074    AlertDataJNI.AlertInfo[] alertInfos = AlertDataJNI.getAlerts();
075    AlertInfo[] infos = new AlertInfo[alertInfos.length];
076    for (int i = 0; i < alertInfos.length; i++) {
077      infos[i] = new AlertInfo(alertInfos[i]);
078    }
079    return infos;
080  }
081
082  /**
083   * Gets detailed information about all active alerts.
084   *
085   * @return Alerts
086   */
087  public static AlertInfo[] getActive() {
088    AlertInfo[] alertInfos = getAll();
089    return java.util.Arrays.stream(alertInfos)
090        .filter(info -> info.activeStartTime != 0)
091        .toArray(AlertInfo[]::new);
092  }
093
094  /** Resets all alert simulation data. */
095  public static void resetData() {
096    AlertDataJNI.resetData();
097  }
098}