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.commands3.proto;
006
007import edu.wpi.first.util.protobuf.Protobuf;
008import org.wpilib.commands3.Command;
009import org.wpilib.commands3.Scheduler;
010import org.wpilib.commands3.proto.ProtobufCommands.ProtobufScheduler;
011import us.hebi.quickbuf.Descriptors;
012
013/**
014 * Serializes a {@link Scheduler} to a protobuf message. Deserialization is not supported. A
015 * serialized message will include information about commands that are currently running or
016 * scheduled (but not yet started), as well as how long the most recent call to {@link
017 * Scheduler#run()} took to execute.
018 */
019public class SchedulerProto implements Protobuf<Scheduler, ProtobufScheduler> {
020  @Override
021  public Class<Scheduler> getTypeClass() {
022    return Scheduler.class;
023  }
024
025  @Override
026  public Descriptors.Descriptor getDescriptor() {
027    return ProtobufScheduler.getDescriptor();
028  }
029
030  @Override
031  public ProtobufScheduler createMessage() {
032    return ProtobufScheduler.newInstance();
033  }
034
035  @Override
036  public Scheduler unpack(ProtobufScheduler msg) {
037    throw new UnsupportedOperationException("Deserialization not supported");
038  }
039
040  @Override
041  public void pack(ProtobufScheduler msg, Scheduler scheduler) {
042    msg.clear();
043
044    var commandProto = new CommandProto(scheduler);
045
046    Protobuf.packArray(
047        msg.getMutableQueuedCommands(),
048        scheduler.getQueuedCommands().toArray(new Command[0]),
049        commandProto);
050
051    Protobuf.packArray(
052        msg.getMutableRunningCommands(),
053        scheduler.getRunningCommands().toArray(new Command[0]),
054        commandProto);
055
056    msg.setLastTimeMs(scheduler.lastRuntimeMs());
057  }
058}