Arduino DLNA Server
Scheduler.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Arduino.h" // for millis
4 #include "DLNADevice.h"
5 #include "IUDPService.h"
6 #include "Schedule.h"
7 
8 namespace tiny_dlna {
9 
15 class Scheduler {
16  public:
18  void add(Schedule *schedule) {
19  schedule->active = true;
20  DlnaLogger.log(DlnaInfo, "Schedule %s", schedule->name());
21  queue.push_back(schedule);
22  }
23 
25  void execute(IUDPService &udp) {
26  // DlnaLogger.log(DlnaDebug, "Scheduler::execute");
27  bool is_cleanup = false;
28  for (auto &p_s : queue) {
29  if (p_s == nullptr) continue;
30  Schedule &s = *p_s;
31  if (millis() >= s.time) {
32  // handle end time: if expired set inactive
33  if (s.end_time > millis()) {
34  s.active = false;
35  }
36  // process active schedules
37  if (s.active) {
38  DlnaLogger.log(DlnaDebug, "Executing %s", s.name());
39 
40  s.process(udp);
41  // reschedule if necessary
42  if (s.repeat_ms > 0) {
43  s.time = millis() + s.repeat_ms;
44  } else {
45  s.active = false;
46  is_cleanup = true;
47  }
48  }
49  }
50  }
51 
52  if (is_cleanup) cleanup();
53  }
54 
55  protected:
57 
58  void cleanup() {
59  for (auto it = queue.begin(); it != queue.end(); ++it) {
60  auto p_rule = *it;
61  if (!(p_rule)->active) {
62  DlnaLogger.log(DlnaDebug, "cleanup queue: %s", p_rule->name());
63  // remove schedule from collection
64  queue.erase(it);
65  // delete schedule
66  delete p_rule;
67  break;
68  }
69  }
70  }
71 };
72 
73 } // namespace tiny_dlna
Abstract Interface for UDP API.
Definition: IUDPService.h:34
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
Scheduler which processes all due Schedules (to send out UDP replies)
Definition: Scheduler.h:15
Vector< Schedule * > queue
Definition: Scheduler.h:56
void cleanup()
Definition: Scheduler.h:58
void execute(IUDPService &udp)
Execute all due schedules.
Definition: Scheduler.h:25
void add(Schedule *schedule)
Add a schedule to the scheduler.
Definition: Scheduler.h:18
Vector implementation which provides the most important methods as defined by std::vector....
Definition: Vector.h:21
Definition: Allocator.h:6
@ DlnaDebug
Definition: Logger.h:16
@ DlnaInfo
Definition: Logger.h:16
LoggerClass DlnaLogger
Definition: Logger.cpp:5
An individual Schedule (to send out UDP messages)
Definition: Schedule.h:17
uint64_t end_time
Definition: Schedule.h:24
bool active
Definition: Schedule.h:26
virtual const char * name()
Definition: Schedule.h:30
virtual bool process(IUDPService &udp)
Definition: Schedule.h:28
uint64_t time
Definition: Schedule.h:20
uint32_t repeat_ms
Definition: Schedule.h:22