|
|
@ -7,6 +7,8 @@ |
|
|
|
#include "preferences.h" |
|
|
|
#include "preferences.h" |
|
|
|
#include <iostream> |
|
|
|
#include <iostream> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define SCHED_INTERVAL 2500 |
|
|
|
|
|
|
|
|
|
|
|
class BandwidthScheduler: public QTimer { |
|
|
|
class BandwidthScheduler: public QTimer { |
|
|
|
Q_OBJECT |
|
|
|
Q_OBJECT |
|
|
|
|
|
|
|
|
|
|
@ -19,7 +21,7 @@ public: |
|
|
|
// Signal shot, we call start() again manually
|
|
|
|
// Signal shot, we call start() again manually
|
|
|
|
setSingleShot(true); |
|
|
|
setSingleShot(true); |
|
|
|
// Connect Signals/Slots
|
|
|
|
// Connect Signals/Slots
|
|
|
|
connect(this, SIGNAL(timeout()), this, SLOT(switchMode())); |
|
|
|
connect(this, SIGNAL(timeout()), this, SLOT(start())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public slots: |
|
|
|
public slots: |
|
|
@ -27,87 +29,45 @@ public slots: |
|
|
|
const Preferences pref; |
|
|
|
const Preferences pref; |
|
|
|
Q_ASSERT(pref.isSchedulerEnabled()); |
|
|
|
Q_ASSERT(pref.isSchedulerEnabled()); |
|
|
|
|
|
|
|
|
|
|
|
QTime startAltSpeeds = pref.getSchedulerStartTime(); |
|
|
|
QTime start = pref.getSchedulerStartTime(); |
|
|
|
QTime endAltSpeeds = pref.getSchedulerEndTime(); |
|
|
|
QTime end = pref.getSchedulerEndTime(); |
|
|
|
if (startAltSpeeds == endAltSpeeds) { |
|
|
|
|
|
|
|
std::cerr << "Error: bandwidth scheduler have the same start time and end time." << std::endl; |
|
|
|
|
|
|
|
std::cerr << "The bandwidth scheduler will be disabled" << std::endl; |
|
|
|
|
|
|
|
stop(); |
|
|
|
|
|
|
|
emit switchToAlternativeMode(false); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Determine what the closest QTime is
|
|
|
|
|
|
|
|
QTime now = QTime::currentTime(); |
|
|
|
QTime now = QTime::currentTime(); |
|
|
|
uint time_to_start = secsTo(now, startAltSpeeds); |
|
|
|
int sched_days = pref.getSchedulerDays(); |
|
|
|
uint time_to_end = secsTo(now, endAltSpeeds); |
|
|
|
int day = QDateTime::currentDateTime().toLocalTime().date().dayOfWeek(); |
|
|
|
if (time_to_end < time_to_start) { |
|
|
|
bool new_mode = false; |
|
|
|
// We should be in alternative mode
|
|
|
|
|
|
|
|
in_alternative_mode = true; |
|
|
|
|
|
|
|
// Start counting
|
|
|
|
|
|
|
|
QTimer::start(time_to_end*1000); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// We should be in normal mode
|
|
|
|
|
|
|
|
in_alternative_mode = false; |
|
|
|
|
|
|
|
// Start counting
|
|
|
|
|
|
|
|
QTimer::start(time_to_start*1000); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Send signal to notify BTSession
|
|
|
|
|
|
|
|
emit switchToAlternativeMode(in_alternative_mode); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void switchMode() { |
|
|
|
if (start < end && start <= now && end >= now) { |
|
|
|
const Preferences pref; |
|
|
|
switch(sched_days) { |
|
|
|
// Get the day this mode was started (either today or yesterday)
|
|
|
|
|
|
|
|
QDate current_date = QDateTime::currentDateTime().toLocalTime().date(); |
|
|
|
|
|
|
|
int day = current_date.dayOfWeek(); |
|
|
|
|
|
|
|
if (in_alternative_mode) { |
|
|
|
|
|
|
|
// It is possible that starttime was yesterday
|
|
|
|
|
|
|
|
if (QTime::currentTime().secsTo(pref.getSchedulerStartTime()) > 0) { |
|
|
|
|
|
|
|
current_date.addDays(-1); // Go to yesterday
|
|
|
|
|
|
|
|
day = current_date.day(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Check if the day is in scheduler days
|
|
|
|
|
|
|
|
// Notify BTSession only if necessary
|
|
|
|
|
|
|
|
switch(pref.getSchedulerDays()) { |
|
|
|
|
|
|
|
case EVERY_DAY: |
|
|
|
case EVERY_DAY: |
|
|
|
emit switchToAlternativeMode(!in_alternative_mode); |
|
|
|
new_mode = true; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case WEEK_ENDS: |
|
|
|
case WEEK_ENDS: |
|
|
|
if (day == Qt::Saturday || day == Qt::Sunday) |
|
|
|
if (day == 6 || day == 7) |
|
|
|
emit switchToAlternativeMode(!in_alternative_mode); |
|
|
|
new_mode = true; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case WEEK_DAYS: |
|
|
|
case WEEK_DAYS: |
|
|
|
if (day != Qt::Saturday && day != Qt::Sunday) |
|
|
|
if (day != 6 && day != 7) |
|
|
|
emit switchToAlternativeMode(!in_alternative_mode); |
|
|
|
new_mode = true; |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
// Convert our enum index to Qt enum index
|
|
|
|
if (day == sched_days - 2) |
|
|
|
int scheduler_day = ((int)pref.getSchedulerDays()) - 2; |
|
|
|
new_mode = true; |
|
|
|
if (day == scheduler_day) |
|
|
|
|
|
|
|
emit switchToAlternativeMode(!in_alternative_mode); |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
// Call start again
|
|
|
|
|
|
|
|
start(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
signals: |
|
|
|
if (new_mode != in_alternative_mode) { |
|
|
|
void switchToAlternativeMode(bool alternative); |
|
|
|
in_alternative_mode = new_mode; |
|
|
|
|
|
|
|
emit switchToAlternativeMode(new_mode); |
|
|
|
private: |
|
|
|
|
|
|
|
// Qt function can return negative values and we
|
|
|
|
|
|
|
|
// don't want that
|
|
|
|
|
|
|
|
uint secsTo(QTime now, QTime t) { |
|
|
|
|
|
|
|
int diff = now.secsTo(t); |
|
|
|
|
|
|
|
if (diff < 0) { |
|
|
|
|
|
|
|
// 86400 seconds in a day
|
|
|
|
|
|
|
|
diff += 86400; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
Q_ASSERT(diff >= 0); |
|
|
|
|
|
|
|
return diff; |
|
|
|
// Timeout regularly to accomodate for external system clock changes
|
|
|
|
|
|
|
|
// eg from the user or from a timesync utility
|
|
|
|
|
|
|
|
QTimer::start(SCHED_INTERVAL); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
signals: |
|
|
|
|
|
|
|
void switchToAlternativeMode(bool alternative); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
#endif // BANDWIDTHSCHEDULER_H
|
|
|
|
#endif // BANDWIDTHSCHEDULER_H
|
|
|
|