Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SleepSchedeule refactored and fixed #5180

Merged
merged 9 commits into from
Sep 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 43 additions & 34 deletions pokemongo_bot/sleep_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ class SleepSchedule(object):

def __init__(self, bot, config):
self.bot = bot
self._last_index = -1
self._next_index = -1
self._process_config(config)
self._schedule_next_sleep()
self._calculate_current_sleep()

def work(self):
if self._should_sleep_now():
Expand Down Expand Up @@ -97,27 +98,24 @@ def _process_config(self, config):
self.entries.append(prepared)

def _schedule_next_sleep(self):
self._next_sleep, self._next_duration, self._wake_up_at_location = self._get_next_sleep_schedule()
self.bot.event_manager.emit(
'next_sleep',
sender=self,
formatted="Next sleep at {time}, for a duration of {duration}",
data={
'time': str(self._next_sleep.strftime("%H:%M:%S")),
'duration': str(timedelta(seconds=self._next_duration))
}
)

def _calculate_current_sleep(self):
self._current_sleep = self._next_sleep - timedelta(days=1)
current_duration = self._next_duration
self._current_end = self._current_sleep + timedelta(seconds = current_duration)
self._next_sleep, self._next_duration, self._next_end, self._wake_up_at_location, sleep_now = self._get_next_sleep_schedule()

if not sleep_now:
self.bot.event_manager.emit(
'next_sleep',
sender=self,
formatted="Next sleep at {time}, for a duration of {duration}",
data={
'time': str(self._next_sleep.strftime("%H:%M:%S")),
'duration': str(timedelta(seconds=self._next_duration))
}
)

def _should_sleep_now(self):
if datetime.now() >= self._next_sleep:
return True
if datetime.now() >= self._current_sleep and datetime.now() < self._current_end:
self._next_duration = (self._current_end - datetime.now()).total_seconds()
now = datetime.now()

if now >= self._next_sleep and now < self._next_end:
self._next_duration = (self._next_end - now).total_seconds()
return True

return False
Expand All @@ -129,25 +127,34 @@ def _get_next_sleep_schedule(self):
for index in range(len(self.entries)):
next_time = now.replace(hour=self.entries[index]['time'].hour, minute=self.entries[index]['time'].minute)
next_time += timedelta(seconds=self._get_random_offset(self.entries[index]['time_random_offset']))
next_duration = self._get_next_duration(self.entries[index])
next_end = next_time + timedelta(seconds=next_duration)
location = self.entries[index]['wake_up_at_location'] if 'wake_up_at_location' in self.entries[index] else ''

# If sleep time is passed add one day
if next_time <= now:
next_time += timedelta(days=1)

times.append(next_time)
diff = next_time - now

diffs = {}
for index in range(len(self.entries)):
diff = (times[index]-now).total_seconds()
if diff >= 0: diffs[index] = diff
# If sleep time is passed or time to sleep less than SCHEDULING_MARGIN then add one day
if (next_time <= now and now > next_end) or (diff > timedelta(0) and diff < self.SCHEDULING_MARGIN):
next_time += timedelta(days=1)
next_end += timedelta(days=1)
diff = next_time - now
# If now is sleeping time
elif next_time <= now and now < next_end:
if index == self._last_index: # If it is still the same sleep entry, but now < next_end because of random offset
next_time += timedelta(days=1)
next_end += timedelta(days=1)
diff = next_time - now
else:
self._next_index = index
return next_time, next_duration, next_end, location, True

closest = min(diffs.iterkeys(), key=lambda x: diffs[x])
prepared = {'index': index, 'time': next_time, 'duration': next_duration, 'end': next_end, 'location': location, 'diff': diff}
times.append(prepared)

next_time = times[closest]
next_duration = self._get_next_duration(self.entries[closest])
location = self.entries[closest]['wake_up_at_location'] if 'wake_up_at_location' in self.entries[closest] else ''
closest = min(times, key=lambda x: x['diff'])
self._next_index = closest['index']

return next_time, next_duration, location
return closest['time'], closest['duration'], closest['end'], closest['location'], False

def _get_next_duration(self, entry):
duration = entry['duration'] + self._get_random_offset(entry['duration_random_offset'])
Expand Down Expand Up @@ -183,3 +190,5 @@ def _sleep(self):
else:
sleep(self.LOG_INTERVAL_SECONDS)
sleep_to_go -= self.LOG_INTERVAL_SECONDS

self._last_index = self._next_index
10 changes: 2 additions & 8 deletions pokemongo_bot/test/sleep_schedule_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,10 @@ def test_get_next_time(self, mock_datetime):
self.assertGreaterEqual(next_time, from_date)
self.assertLessEqual(next_time, to_date)

@unittest.skip("Will rewrite test later")
@patch('pokemongo_bot.sleep_schedule.datetime')
def test_get_next_time_called_near_activation_time(self, mock_datetime):
mock_datetime.now.return_value = datetime(year=2016, month=8, day=1, hour=12, minute=25)

next = self.worker._get_next_sleep_schedule()[0]
from_date = datetime(year=2016, month=8, day=02, hour=12, minute=15)
to_date = datetime(year=2016, month=8, day=02, hour=12, minute=25)

self.assertGreaterEqual(next, from_date)
self.assertLessEqual(next, to_date)
pass

@patch('pokemongo_bot.sleep_schedule.datetime')
def test_get_next_time_called_when_this_days_time_passed(self, mock_datetime):
Expand Down
2 changes: 1 addition & 1 deletion web