sleep.py

Sun, 28 Jan 2018 00:11:18 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 28 Jan 2018 00:11:18 +0000
changeset 76
4b08fca3ce34
parent 69
8705e296c7a0
child 77
e8773133bf79
permissions
-rw-r--r--

Dreamtime scheduling: discount system sleep periods

68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 #
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Wake/sleep detection for scheduling adjustments
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 #
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 import Foundation
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 import AppKit
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
7 import platform
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
8 import time
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
9 import threading
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 import borgend
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
11 import weakref
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
12 import datetime
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 logger=borgend.logger.getChild(__name__)
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
16 _dreamtime_monitor=[None]
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
17
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
18 #
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
19 # Support classes for dealing with different times
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
20 #
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
21
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
22 def dreamtime():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
23 if _dreamtime_monitor[0]:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
24 return max(0, time.monotonic()-_dreamtime_monitor[0].diff())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
25 else:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
26 return time.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
27
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
28 class Time:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
29 def realtime(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
30 raise NotImplementedError
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
31
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
32 def monotonic(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
33 raise NotImplementedError
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
34
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
35 @staticmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
36 def _now():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
37 raise NotImplementedError
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
38
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
39 @classmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
40 def now(cls):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
41 return cls(cls._now())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
42
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
43 @classmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
44 def from_realtime(cls, realtime):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
45 return cls(realtime-time.time()+cls._now())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
46
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
47 @classmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
48 def from_monotonic(cls, monotonic):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
49 return cls(monotonic-time.monotonic()+cls._now())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
50
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
51 @classmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
52 def after(cls, seconds):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
53 return cls(cls._now()+seconds)
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
54
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
55 def datetime(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
56 return datetime.datetime.fromtimestamp(self.realtime())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
57
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
58 def seconds_to(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
59 return self._value-self._now()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
60
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
61 def __lt__(self, other):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
62 return self.monotonic() < other.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
63
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
64 def __gt__(self, other):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
65 return self.monotonic() > other.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
66
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
67 def __le__(self, other):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
68 return self.monotonic() <= other.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
69
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
70 def __ge__(self, other):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
71 return self.monotonic() >= other.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
72
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
73 def __eq__(self, other):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
74 return self.monotonic() == other.realtime()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
75
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
76 class RealTime(Time):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
77 def __init__(self, when):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
78 self._value=when
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
79
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
80 def realtime(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
81 return self._value
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
82
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
83 def monotonic(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
84 return self._value+(time.monotonic()-time.time())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
85
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
86 @staticmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
87 def _now():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
88 return time.time()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
89
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
90 class MonotonicTime(Time):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
91 def __init__(self, when):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
92 self._value=when
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
93
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
94 def realtime(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
95 return self._value+(time.time()-time.monotonic())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
96
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
97 def monotonic(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
98 return self._value
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
99
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
100 @staticmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
101 def _now():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
102 return time.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
103
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
104 class DreamTime(Time):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
105 def __init__(self, when):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
106 self._value=when
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
107
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
108 def realtime(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
109 return self._value+(time.time()-dreamtime())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
110
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
111 def monotonic(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
112 return self._value+(time.monotonic()-dreamtime())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
113
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
114 @staticmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
115 def _now():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
116 return dreamtime()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
117
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
118 #
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
119 # Wake up / sleep handling
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
120 #
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
121
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
122 class SleepHandler(Foundation.NSObject):
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
123 """ Handle wake/sleep notifications """
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
124
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
125 def init(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
126 self.__sleeptime=None
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
127 self.__slept=0
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
128 self.__epoch=time.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
129 self._lock=threading.Lock()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
130 self._callbacks=weakref.WeakKeyDictionary()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
131
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
132 return self
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
133
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
134 def handleSleepNotification_(self, aNotification):
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
135 logger.info("System going to sleep")
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
136 now=time.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
137 with self._lock:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
138 self.__sleeptime=now
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
139
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
140 def handleWakeNotification_(self, aNotification):
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
141 logger.info("System waking up from sleep")
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
142 try:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
143 now=time.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
144 with self._lock:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
145 if self.__sleeptime:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
146 slept=max(0, now-self.__sleeptime)
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
147 logger.info("Slept %f seconds" % slept)
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
148 self.__slept=self.__slept+slept
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
149 self.__sleeptime=None
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
150 callbacks=self._callbacks.copy()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
151 except:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
152 logger.exception("Bug in wakeup handler")
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
154 for callback in callbacks.values():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
155 try:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
156 callback()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
157 except Exception:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
158 logger.exception("Error in wake notification callback")
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
159
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
160 # Return difference to time.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
161 def diff(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
162 with self._lock:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
163 diff=self.__epoch+self.__slept
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
164 return diff
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
165
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
166 # Obj-C hates this
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
167 # def add_callback(self, obj, callback):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
168 # with self.lock:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
169 # self.__callbacks[obj]=callback
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
170
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
171 # obj is to use a a key in a weak key dictionary
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
172 def add_callback(obj, callback):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
173 monitor=_dreamtime_monitor[0]
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
174 if not monitor:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
175 raise Exception("Dreamtime monitor not started")
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
176 else:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
177 #monitor.add_my_callback(obj, callback)
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
178 with monitor._lock:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
179 monitor._callbacks[obj]=callback
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
180
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
181 def start_monitoring():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
182 if platform.system()=='Darwin':
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
183 logger.debug("Starting to monitor system sleep")
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
184 workspace = AppKit.NSWorkspace.sharedWorkspace()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
185 notification_center = workspace.notificationCenter()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
186 _dreamtime_monitor[0] = SleepHandler.new()
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
187
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
188 notification_center.addObserver_selector_name_object_(
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
189 _dreamtime_monitor[0],
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
190 "handleSleepNotification:",
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
191 AppKit.NSWorkspaceWillSleepNotification,
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
192 None)
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
193
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
194 notification_center.addObserver_selector_name_object_(
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
195 _dreamtime_monitor[0],
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
196 "handleWakeNotification:",
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
197 AppKit.NSWorkspaceDidWakeNotification,
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
198 None)
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
199 else:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
200 logger.warning(("No system sleep monitor implemented for '%s'"
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
201 % platform.system()))
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
202

mercurial