2 /*global ExercisePlanner: true, ONE_DAY:false, TIME_OF_SLEEP:false */
11 * @param availableTime
15 ExercisePlanner.prototype.calculateFactor = function calculateFactors(availableTime, typeOfPeriod) {
22 if (availableTime === 0) {
26 factor = availableTime / (this.ONE_DAY - this.TIME_OF_SLEEP);
27 result.proportionalFrequency = this.getNumberOfWorkoutsByFrequency(this.config.frequency[typeOfPeriod]) * factor;
29 if ((Math.round(result.proportionalFrequency) - 1) > 0) {
30 result.count = (Math.round(result.proportionalFrequency));
31 result.period = availableTime / (result.count - 1);
43 * @param availableTime
46 ExercisePlanner.prototype.calculateFactors = function calculateFactors(availableTime) {
48 weekend: this.calculateFactor(availableTime.weekend, 'weekend'),
49 workday: this.calculateFactor(availableTime.workday, 'workday'),
50 everyday: this.calculateFactor(availableTime.everyday, 'workday')
61 ExercisePlanner.prototype.generateAlarmsByFactor = function (factor, tmpPeriods) {
62 var i, numberOfPeriods = tmpPeriods.length,
69 dayTime = this.beginDate || new Date();
71 if (numberOfPeriods === 0) {
75 begin = tmpPeriods[0].start;
77 for (i = 0; i < numberOfPeriods; i += 1) {
78 if (tmpPeriods[i].stop > end) {
79 end = tmpPeriods[i].stop;
83 dayTime.setSeconds(0);
85 if (factor.count === 1) {
86 // One alarm per day, default placed in middle of available time;
87 optimalTime = this.findNearestTimeRange((end + begin) / 2, tmpPeriods);
89 dayTime.setHours(parseInt(optimalTime.optimalHour, 10));
90 dayTime.setMinutes(60 * (optimalTime.optimalHour - parseInt(optimalTime.optimalHour, 10)));
91 tableOfAlarms.push(new Date(dayTime.getTime()));
93 // set time for begin;
94 dayTime.setHours(tmpPeriods[0].start);
95 dayTime.setMinutes(0);
96 tableOfAlarms.push(new Date(dayTime.getTime()));
98 // set time for next hop;
99 for (i = 0; i < numberOfPeriods; i += 1) {
100 period = tmpPeriods[i];
101 deltaTime += period.duration;
102 // if available period is too small, then accumulate time
103 // and continue to next period;
104 while (deltaTime >= factor.period * 0.999) {
105 deltaTime -= factor.period;
107 dayTime.setHours(parseInt(period.stop - deltaTime, 10));
108 dayTime.setMinutes(60 * (period.stop - deltaTime - parseInt(period.stop - deltaTime, 10)));
110 tableOfAlarms.push(new Date(dayTime.getTime()));
115 return tableOfAlarms;
119 * Generate table of alarms => this.alarms
120 * @param {Date} customDate;
122 ExercisePlanner.prototype.generateAlarms = function () {
124 alarms = this.alarms,
131 // some periods may overlap, must be merged
132 periodsWeek = this.mergePeriods();
133 // store in cache for later reuse
134 this.cache.periodsWeek = periodsWeek;
136 // factors to correct how often may workouts per day
137 factors = this.calculateFactors(this.getSummaryAvailableTime());
140 if (periodsWeek.everyday.length > 0) {
141 alarms.everyday = this.generateAlarmsByFactor(factors.everyday, periodsWeek.everyday);
143 alarms.workday = this.generateAlarmsByFactor(factors.workday, periodsWeek.workday);
144 alarms.weekend = this.generateAlarmsByFactor(factors.weekend, periodsWeek.weekend);
147 // if trainig is in run then resinstall alarm imediately
148 if (this.config.trainingEnabled) {