job scheduler works with run once and run periodic schedules. Stop logic is not fully thought through.

Dependencies:   LinkedList

Dependents:   JobSchedulerDemo Borsch

Committer:
sgnezdov
Date:
Thu Aug 03 00:00:13 2017 +0000
Revision:
16:f61b62b119dd
Parent:
8:4ead1f4ab741
fixed timeline order of scheduled items.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgnezdov 0:806403f3d0d1 1 #pragma once
sgnezdov 0:806403f3d0d1 2
sgnezdov 2:9bf5366ad5a2 3 #include "scheduler.h"
sgnezdov 2:9bf5366ad5a2 4
sgnezdov 0:806403f3d0d1 5 namespace JobScheduler {
sgnezdov 0:806403f3d0d1 6
sgnezdov 2:9bf5366ad5a2 7 class RunOnceSchedule: public ISchedule {
sgnezdov 2:9bf5366ad5a2 8 public:
sgnezdov 16:f61b62b119dd 9 RunOnceSchedule(time_t time): _time(time) {}
sgnezdov 2:9bf5366ad5a2 10
sgnezdov 16:f61b62b119dd 11 time_t AtTime() {
sgnezdov 16:f61b62b119dd 12 return _time;
sgnezdov 16:f61b62b119dd 13 }
sgnezdov 16:f61b62b119dd 14
sgnezdov 16:f61b62b119dd 15 virtual ~RunOnceSchedule() {}
sgnezdov 2:9bf5366ad5a2 16
sgnezdov 2:9bf5366ad5a2 17 virtual time_t NextRunTime(time_t from) {
sgnezdov 2:9bf5366ad5a2 18 time_t current = _time;
sgnezdov 2:9bf5366ad5a2 19 _time = 0;
sgnezdov 2:9bf5366ad5a2 20 return current;
sgnezdov 16:f61b62b119dd 21 }
sgnezdov 16:f61b62b119dd 22
sgnezdov 16:f61b62b119dd 23 virtual int ScheduleType() {
sgnezdov 16:f61b62b119dd 24 return 1; // matches protocol_ScheduleType_RunOnce in job.pb.h
sgnezdov 16:f61b62b119dd 25 }
sgnezdov 16:f61b62b119dd 26
sgnezdov 2:9bf5366ad5a2 27 private:
sgnezdov 2:9bf5366ad5a2 28 time_t _time;
sgnezdov 2:9bf5366ad5a2 29 };
sgnezdov 2:9bf5366ad5a2 30
sgnezdov 8:4ead1f4ab741 31 class RunPeriodicSchedule: public ISchedule {
sgnezdov 8:4ead1f4ab741 32 public:
sgnezdov 8:4ead1f4ab741 33 RunPeriodicSchedule(time_t period, int limit = 0)
sgnezdov 8:4ead1f4ab741 34 : _period(period), _limit(limit), _counter(0) {};
sgnezdov 8:4ead1f4ab741 35
sgnezdov 16:f61b62b119dd 36 virtual ~RunPeriodicSchedule() {}
sgnezdov 8:4ead1f4ab741 37
sgnezdov 8:4ead1f4ab741 38 virtual time_t NextRunTime(time_t from) {
sgnezdov 8:4ead1f4ab741 39 if (_limit > 0 && _counter == _limit) {
sgnezdov 8:4ead1f4ab741 40 // no next run
sgnezdov 8:4ead1f4ab741 41 return 0;
sgnezdov 8:4ead1f4ab741 42 }
sgnezdov 8:4ead1f4ab741 43 _counter++;
sgnezdov 8:4ead1f4ab741 44 time_t next = from + _period;
sgnezdov 8:4ead1f4ab741 45 return next;
sgnezdov 16:f61b62b119dd 46 }
sgnezdov 16:f61b62b119dd 47
sgnezdov 16:f61b62b119dd 48 virtual int ScheduleType() {
sgnezdov 16:f61b62b119dd 49 return 2; // matches protocol_ScheduleType_Periodic in job.pb.h
sgnezdov 16:f61b62b119dd 50 }
sgnezdov 8:4ead1f4ab741 51
sgnezdov 8:4ead1f4ab741 52 private:
sgnezdov 8:4ead1f4ab741 53 time_t _period;
sgnezdov 8:4ead1f4ab741 54 /** limit limits number of times to execute NextRunTime.
sgnezdov 8:4ead1f4ab741 55 If set to zero, then it is unlimitted.
sgnezdov 8:4ead1f4ab741 56 */
sgnezdov 8:4ead1f4ab741 57 int _limit;
sgnezdov 8:4ead1f4ab741 58 int _counter;
sgnezdov 8:4ead1f4ab741 59 };
sgnezdov 8:4ead1f4ab741 60
sgnezdov 8:4ead1f4ab741 61 } // end of namespace