Sun, 28 Jan 2018 11:01:45 +0000
DreamTime.monotonic() no longer needs to call time.monotonic()
so is more "static" within wakeup periods, and so comparable to
time.monotonic() realisations. Also some PyObjC weirdness handling.
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 |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
10 | import weakref |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
11 | import datetime |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
12 | import borgend |
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 | |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
16 | _dreamtime_monitor=None |
76
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 | |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
22 | # Return difference (delay) of "dreamtime" to monotonic time |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
23 | def dreamtime_difference(): |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
24 | if _dreamtime_monitor: |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
25 | return _dreamtime_monitor.diff() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
26 | else: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
27 | return time.monotonic() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
28 | |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
29 | # Return "dreamtime" |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
30 | def dreamtime(): |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
31 | return max(0, time.monotonic()-dreamtime_difference()) |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
32 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
33 | class Time: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
34 | def realtime(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
35 | raise NotImplementedError |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
36 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
37 | def monotonic(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
38 | raise NotImplementedError |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
39 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
40 | @staticmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
41 | def _now(): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
42 | raise NotImplementedError |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
43 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
44 | @classmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
45 | def now(cls): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
46 | return cls(cls._now()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
47 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
48 | @classmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
49 | def from_realtime(cls, realtime): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
50 | return cls(realtime-time.time()+cls._now()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
51 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
52 | @classmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
53 | def from_monotonic(cls, monotonic): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
54 | return cls(monotonic-time.monotonic()+cls._now()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
55 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
56 | @classmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
57 | def after(cls, seconds): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
58 | return cls(cls._now()+seconds) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
59 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
60 | def datetime(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
61 | return datetime.datetime.fromtimestamp(self.realtime()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
62 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
63 | def seconds_to(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
64 | return self._value-self._now() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
65 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
66 | def __lt__(self, other): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
67 | return self.monotonic() < other.monotonic() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
68 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
69 | def __gt__(self, other): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
70 | return self.monotonic() > other.monotonic() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
71 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
72 | def __le__(self, other): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
73 | return self.monotonic() <= other.monotonic() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
74 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
75 | def __ge__(self, other): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
76 | return self.monotonic() >= other.monotonic() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
77 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
78 | def __eq__(self, other): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
79 | return self.monotonic() == other.realtime() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
80 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
81 | class RealTime(Time): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
82 | def __init__(self, when): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
83 | self._value=when |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
84 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
85 | def realtime(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
86 | return self._value |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
87 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
88 | def monotonic(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
89 | return self._value+(time.monotonic()-time.time()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
90 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
91 | @staticmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
92 | def _now(): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
93 | return time.time() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
94 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
95 | class MonotonicTime(Time): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
96 | def __init__(self, when): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
97 | self._value=when |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
98 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
99 | def realtime(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
100 | return self._value+(time.time()-time.monotonic()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
101 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
102 | def monotonic(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
103 | return self._value |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
104 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
105 | @staticmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
106 | def _now(): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
107 | return time.monotonic() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
108 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
109 | class DreamTime(Time): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
110 | def __init__(self, when): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
111 | self._value=when |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
112 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
113 | def realtime(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
114 | return self._value+(time.time()-dreamtime()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
115 | |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
116 | # Important: monotonic is "static" within a wakeup period |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
117 | # and does not need to call time.monotonic(), as it gets compared |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
118 | # to a specific time.monotonic() realisation |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
119 | def monotonic(self): |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
120 | return self._value+dreamtime_difference() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
121 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
122 | @staticmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
123 | def _now(): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
124 | return dreamtime() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
125 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
126 | # |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
127 | # Wake up / sleep handling |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
128 | # |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
129 | |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
130 | class SleepHandler(Foundation.NSObject): |
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
131 | """ Handle wake/sleep notifications """ |
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
132 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
133 | def init(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
134 | self.__sleeptime=None |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
135 | self.__slept=0 |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
136 | self.__epoch=time.monotonic() |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
137 | self.__lock=threading.Lock() |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
138 | self.__callbacks=weakref.WeakKeyDictionary() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
139 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
140 | return self |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
141 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
142 | def handleSleepNotification_(self, aNotification): |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
143 | logger.info("System going to sleep") |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
144 | now=time.monotonic() |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
145 | with self.__lock: |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
146 | self.__sleeptime=now |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
147 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
148 | def handleWakeNotification_(self, aNotification): |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
149 | logger.info("System waking up from sleep") |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
150 | try: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
151 | now=time.monotonic() |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
152 | with self.__lock: |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
153 | if self.__sleeptime: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
154 | slept=max(0, now-self.__sleeptime) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
155 | logger.info("Slept %f seconds" % slept) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
156 | self.__slept=self.__slept+slept |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
157 | self.__sleeptime=None |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
158 | callbacks=self.__callbacks.copy() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
159 | except: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
160 | logger.exception("Bug in wakeup handler") |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
161 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
162 | for callback in callbacks.values(): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
163 | try: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
164 | callback() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
165 | except Exception: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
166 | 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
|
167 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
168 | # Return difference to time.monotonic() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
169 | def diff(self): |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
170 | with self.__lock: |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
171 | diff=self.__epoch+self.__slept |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
172 | return diff |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
173 | |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
174 | # Weirdo (Py)ObjC naming to stop it form choking up |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
175 | def addForObj_aCallback_(self, obj, callback): |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
176 | with self.__lock: |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
177 | self.__callbacks[obj]=callback |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
178 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
179 | # 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
|
180 | def add_callback(obj, callback): |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
181 | global _dreamtime_monitor |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
182 | |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
183 | monitor=_dreamtime_monitor |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
184 | if not monitor: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
185 | raise Exception("Dreamtime monitor not started") |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
186 | else: |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
187 | monitor.addForObj_aCallback_(obj, callback) |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
188 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
189 | def start_monitoring(): |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
190 | global _dreamtime_monitor |
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
191 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
192 | if platform.system()=='Darwin': |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
193 | logger.debug("Starting to monitor system sleep") |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
194 | workspace = AppKit.NSWorkspace.sharedWorkspace() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
195 | notification_center = workspace.notificationCenter() |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
196 | _dreamtime_monitor = SleepHandler.new() |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
197 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
198 | notification_center.addObserver_selector_name_object_( |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
199 | _dreamtime_monitor, |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
200 | "handleSleepNotification:", |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
201 | AppKit.NSWorkspaceWillSleepNotification, |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
202 | None) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
203 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
204 | notification_center.addObserver_selector_name_object_( |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
205 | _dreamtime_monitor, |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
206 | "handleWakeNotification:", |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
207 | AppKit.NSWorkspaceDidWakeNotification, |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
208 | None) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
209 | else: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
210 | 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
|
211 | % platform.system())) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
212 |