7 #include "Commands/Scheduler.h"
9 #include "Buttons/ButtonScheduler.h"
10 #include "Commands/Subsystem.h"
11 #include "NetworkCommunication/UsageReporting.h"
12 #include "Synchronized.h"
13 #include "WPIErrors.h"
21 Scheduler::Scheduler() :
22 m_buttonsLock(NULL), m_additionsLock(NULL), m_adding(false) {
23 m_buttonsLock = semMCreate(
24 SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);
25 m_additionsLock = semMCreate(
26 SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);
28 nUsageReporting::report(nUsageReporting::kResourceType_Command,
29 nUsageReporting::kCommand_Scheduler);
35 Scheduler::~Scheduler() {
36 semTake(m_additionsLock, WAIT_FOREVER);
37 semDelete(m_additionsLock);
39 semTake(m_buttonsLock, WAIT_FOREVER);
40 semDelete(m_buttonsLock);
48 if (_instance == NULL)
53 void Scheduler::SetEnabled(
bool enabled) {
65 if (std::find(m_additions.begin(), m_additions.end(), command)
68 m_additions.push_back(command);
73 m_buttons.push_back(button);
76 void Scheduler::ProcessCommandAddition(
Command *command) {
82 wpi_setWPIErrorWithContext(IncompatibleState,
"Can not start command from cancel method");
87 CommandSet::iterator found = m_commands.find(command);
88 if (found == m_commands.end()) {
91 Command::SubsystemSet::iterator iter;
92 for (iter = requirements.begin(); iter != requirements.end(); iter++) {
101 for (iter = requirements.begin(); iter != requirements.end(); iter++) {
111 m_commands.insert(command);
113 command->StartRunning();
114 m_runningCommandsChanged =
true;
137 ButtonVector::reverse_iterator rButtonIter = m_buttons.rbegin();
138 for (; rButtonIter != m_buttons.rend(); rButtonIter++) {
139 (*rButtonIter)->Execute();
143 m_runningCommandsChanged =
false;
146 CommandSet::iterator commandIter = m_commands.begin();
147 for (; commandIter != m_commands.end();) {
148 Command *command = *commandIter;
151 if (!command->
Run()) {
153 m_runningCommandsChanged =
true;
160 CommandVector::iterator additionsIter = m_additions.begin();
161 for (; additionsIter != m_additions.end(); additionsIter++) {
162 ProcessCommandAddition(*additionsIter);
168 Command::SubsystemSet::iterator subsystemIter = m_subsystems.begin();
169 for (; subsystemIter != m_subsystems.end(); subsystemIter++) {
174 lock->ConfirmCommand();
186 if (subsystem == NULL) {
187 wpi_setWPIErrorWithContext(NullParameter,
"subsystem");
190 m_subsystems.insert(subsystem);
198 if (command == NULL) {
199 wpi_setWPIErrorWithContext(NullParameter,
"command");
203 if (!m_commands.erase(command))
207 Command::SubsystemSet::iterator iter = requirements.begin();
208 for (; iter != requirements.end(); iter++) {
216 void Scheduler::RemoveAll() {
217 while (m_commands.size() > 0) {
218 Remove(*m_commands.begin());
226 CommandSet::iterator commandIter;
227 if (m_table != NULL) {
229 m_table->RetrieveValue(
"Cancel", *toCancel);
234 if (toCancel->
size() > 0) {
235 for (commandIter = m_commands.begin(); commandIter
236 != m_commands.end(); ++commandIter) {
237 for (
unsigned i = 0; i < toCancel->
size(); i++) {
239 if (c->
GetID() == toCancel->get(i)) {
245 m_table->
PutValue(
"Cancel", *toCancel);
249 if (m_runningCommandsChanged) {
252 for (commandIter = m_commands.begin(); commandIter != m_commands.end(); ++commandIter) {
255 ids->add(c->
GetID());
257 m_table->
PutValue(
"Names", *commands);
267 std::string Scheduler::GetType() {
281 m_table->
PutValue(
"Names", *commands);
283 m_table->
PutValue(
"Cancel", *toCancel);
void setSize(unsigned int size)
Command * GetCurrentCommand()
void AddCommand(Command *command)
virtual std::string GetName()
std::string GetSmartDashboardType()
Command * GetDefaultCommand()
void InitTable(ITable *subTable)
void Remove(Command *command)
virtual void PutValue(std::string key, ComplexData &value)=0
void RegisterSubsystem(Subsystem *subsystem)
void SetCurrentCommand(Command *command)
static Scheduler * GetInstance()
SubsystemSet GetRequirements()