-
Notifications
You must be signed in to change notification settings - Fork 42
/
PowerTimer.py
1232 lines (1124 loc) · 44.9 KB
/
PowerTimer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
from boxbranding import getMachineBrand, getMachineName
import xml.etree.cElementTree
from datetime import datetime
from time import ctime, time, strftime, localtime, mktime
from bisect import insort
from enigma import eActionMap, quitMainloop
from Components.UsageConfig import ConfigSelection, ConfigSelectionNumber
from Components.config import config
from Components.TimerSanityCheck import TimerSanityCheck
from Screens.MessageBox import MessageBox
import Screens.Standby
from Tools import Directories, Notifications
from Tools.XMLTools import stringToXML
import timer
import NavigationInstance
#global variables begin
DSsave = False
RSsave = False
RBsave = False
aeDSsave = False
wasTimerWakeup = False
def powermessagetimeout(self):
messagetimeout = config.usage.window_timeout.value
#try:
# from Screens.InfoBar import InfoBar
#except Exception, e:
# print "[PowerTimer] import from 'Screens.InfoBar import InfoBar' failed:", e
InfoBar = False
#+++
debug = False
#+++
#global variables end
#----------------------------------------------------------------------------------------------------
#Timer shutdown, reboot and restart priority
#1. wakeup
#2. wakeuptostandby -> (same as 1.)
#3. deepstandby -> DSsave
#4. deppstandby after event -> aeDSsave
#5. reboot system -> RBsave
#6. restart gui -> RSsave
#7. standby
#8. autostandby
#9. nothing (no function, only for suppress autodeepstandby timer)
#10. autodeepstandby
#-overlapping timers or next timer start is within 15 minutes, will only the high-order timer executed (at same types will executed the next timer)
#-autodeepstandby timer is only effective if no other timer is active or current time is in the time window
#-priority for repeated timer: shift from begin and end time only temporary, end-action priority is higher as the begin-action
#----------------------------------------------------------------------------------------------------
#reset wakeup state after ending timer
def resetTimerWakeup():
global wasTimerWakeup
if os.path.exists("/tmp/was_powertimer_wakeup"):
os.remove("/tmp/was_powertimer_wakeup")
if debug:
print "[POWERTIMER] reset wakeup state"
wasTimerWakeup = False
# parses an event, and gives out a (begin, end, name, duration, eit)-tuple.
# begin and end will be corrected
def parseEvent(ev):
begin = ev.getBeginTime()
end = begin + ev.getDuration()
return begin, end
class AFTEREVENT:
def __init__(self):
pass
NONE = 0
WAKEUP = 1
WAKEUPTOSTANDBY = 2
STANDBY = 3
DEEPSTANDBY = 4
class TIMERTYPE:
def __init__(self):
pass
NONE = 0
WAKEUP = 1
WAKEUPTOSTANDBY = 2
AUTOSTANDBY = 3
AUTODEEPSTANDBY = 4
STANDBY = 5
DEEPSTANDBY = 6
REBOOT = 7
RESTART = 8
# please do not translate log messages
class PowerTimerEntry(timer.TimerEntry, object):
def __init__(self, begin, end, disabled=False, afterEvent=AFTEREVENT.NONE, timerType=TIMERTYPE.WAKEUP, checkOldTimers=False, autosleepdelay=60):
timer.TimerEntry.__init__(self, int(begin), int(end))
if checkOldTimers:
if self.begin < time() - 1209600:
self.begin = int(time())
#check autopowertimer
if (timerType == TIMERTYPE.AUTOSTANDBY or timerType == TIMERTYPE.AUTODEEPSTANDBY) and not disabled and time() > 3600 and self.begin > time():
self.begin = int(time()) #the begin is in the future -> set to current time = no start delay of this timer
if self.end < self.begin:
self.end = self.begin
self.dontSave = False
self.disabled = disabled
self.timer = None
self.__record_service = None
self.start_prepare = 0
self.timerType = timerType
self.afterEvent = afterEvent
self.autoincrease = False
self.autoincreasetime = 3600 * 24 # 1 day
self.autosleepinstandbyonly = 'no'
self.autosleepdelay = autosleepdelay
self.autosleeprepeat = 'once'
self.autosleepwindow = 'no'
self.autosleepbegin = self.begin
self.autosleepend = self.end
self.nettraffic = 'no'
self.trafficlimit = 100
self.netip = 'no'
self.ipadress = "0.0.0.0"
self.log_entries = []
self.resetState()
self.messageBoxAnswerPending = False
def __repr__(self, getType=False):
timertype = {
TIMERTYPE.NONE: "nothing",
TIMERTYPE.WAKEUP: "wakeup",
TIMERTYPE.WAKEUPTOSTANDBY: "wakeuptostandby",
TIMERTYPE.AUTOSTANDBY: "autostandby",
TIMERTYPE.AUTODEEPSTANDBY: "autodeepstandby",
TIMERTYPE.STANDBY: "standby",
TIMERTYPE.DEEPSTANDBY: "deepstandby",
TIMERTYPE.REBOOT: "reboot",
TIMERTYPE.RESTART: "restart"
}[self.timerType]
if getType:
return timertype
if not self.disabled:
return "PowerTimerEntry(type=%s, begin=%s)" % (timertype, ctime(self.begin))
else:
return "PowerTimerEntry(type=%s, begin=%s Disabled)" % (timertype, ctime(self.begin))
def log(self, code, msg):
self.log_entries.append((int(time()), code, msg))
def do_backoff(self):
if Screens.Standby.inStandby and not wasTimerWakeup or RSsave or RBsave or aeDSsave or DSsave:
self.backoff = 300
else:
if self.backoff == 0:
self.backoff = 300
else:
self.backoff += 300
if self.backoff > 900:
self.backoff = 900
self.log(10, "backoff: retry in %d minutes" % (int(self.backoff) / 60))
def activate(self):
global RSsave, RBsave, DSsave, aeDSsave, wasTimerWakeup, InfoBar
if not InfoBar:
try:
from Screens.InfoBar import InfoBar
except Exception, e:
print "[PowerTimer] import from 'Screens.InfoBar import InfoBar' failed:", e
isRecTimerWakeup = breakPT = shiftPT = False
now = time()
next_state = self.state + 1
self.log(5, "activating state %d" % next_state)
if next_state == self.StatePrepared and (self.timerType == TIMERTYPE.AUTOSTANDBY or self.timerType == TIMERTYPE.AUTODEEPSTANDBY):
eActionMap.getInstance().bindAction('', -0x7FFFFFFF, self.keyPressed)
if self.autosleepwindow == 'yes':
ltm = localtime(now)
asb = strftime("%H:%M", localtime(self.autosleepbegin)).split(':')
ase = strftime("%H:%M", localtime(self.autosleepend)).split(':')
self.autosleepbegin = int(mktime(datetime(ltm.tm_year, ltm.tm_mon, ltm.tm_mday, int(asb[0]), int(asb[1])).timetuple()))
self.autosleepend = int(mktime(datetime(ltm.tm_year, ltm.tm_mon, ltm.tm_mday, int(ase[0]), int(ase[1])).timetuple()))
if self.autosleepend <= self.autosleepbegin:
self.autosleepbegin -= 86400
if self.getAutoSleepWindow():
if now < self.autosleepbegin and now > self.autosleepbegin - self.prepare_time - 3: #begin is in prepare time window
self.begin = self.end = self.autosleepbegin + int(self.autosleepdelay) * 60
else:
self.begin = self.end = int(now) + int(self.autosleepdelay) * 60
else:
return False
if self.timerType == TIMERTYPE.AUTODEEPSTANDBY:
self.getNetworkTraffic(getInitialValue=True)
if next_state == self.StateRunning or next_state == self.StateEnded:
if NavigationInstance.instance.PowerTimer is None:
#TODO: running/ended timer at system start has no nav instance
#First fix: crash in getPriorityCheck (NavigationInstance.instance.PowerTimer...)
#Second fix: suppress the message (A finished powertimer wants to ...)
if debug:
print "*****NavigationInstance.instance.PowerTimer is None*****", self.timerType, self.state, ctime(self.begin), ctime(self.end)
return True
elif (next_state == self.StateRunning and abs(self.begin - now) > 900) or (next_state == self.StateEnded and abs(self.end - now) > 900):
if self.timerType == TIMERTYPE.AUTODEEPSTANDBY or self.timerType == TIMERTYPE.AUTOSTANDBY:
print '[Powertimer] time warp detected - set new begin time for %s timer' % self.__repr__(True)
if not self.getAutoSleepWindow():
return False
else:
self.begin = self.end = int(now) + int(self.autosleepdelay) * 60
return False
print '[Powertimer] time warp detected - timer %s ending without action' % self.__repr__(True)
return True
if NavigationInstance.instance.isRecordTimerImageStandard:
isRecTimerWakeup = NavigationInstance.instance.RecordTimer.isRecTimerWakeup()
if isRecTimerWakeup:
wasTimerWakeup = True
elif os.path.exists("/tmp/was_powertimer_wakeup") and not wasTimerWakeup:
wasTimerWakeup = int(open("/tmp/was_powertimer_wakeup", "r").read()) and True or False
if next_state == self.StatePrepared:
self.log(6, "prepare ok, waiting for begin: %s" % ctime(self.begin))
self.backoff = 0
return True
elif next_state == self.StateRunning:
# if this timer has been cancelled, just go to "end" state.
if self.cancelled:
return True
if self.failed:
return True
if self.timerType == TIMERTYPE.NONE:
return True
elif self.timerType == TIMERTYPE.WAKEUP:
if debug:
print "self.timerType == TIMERTYPE.WAKEUP:"
Screens.Standby.TVinStandby.skipHdmiCecNow('wakeuppowertimer')
if Screens.Standby.inStandby:
Screens.Standby.inStandby.Power()
return True
elif self.timerType == TIMERTYPE.WAKEUPTOSTANDBY:
if debug:
print "self.timerType == TIMERTYPE.WAKEUPTOSTANDBY:"
return True
elif self.timerType == TIMERTYPE.STANDBY:
if debug:
print "self.timerType == TIMERTYPE.STANDBY:"
prioPT = [TIMERTYPE.WAKEUP, TIMERTYPE.RESTART, TIMERTYPE.REBOOT, TIMERTYPE.DEEPSTANDBY]
prioPTae = [AFTEREVENT.WAKEUP, AFTEREVENT.DEEPSTANDBY]
shiftPT, breakPT = self.getPriorityCheck(prioPT, prioPTae)
if not Screens.Standby.inStandby and not breakPT: # not already in standby
callback = self.sendStandbyNotification
message = _("A finished powertimer wants to set your\n%s %s to standby. Do that now?") % (getMachineBrand(), getMachineName())
messageboxtyp = MessageBox.TYPE_YESNO
timeout = (config.usage.window_timeout.value)
default = True
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarMessageWithCallback(callback, message, messageboxtyp, timeout, default)
else:
Notifications.AddNotificationWithCallback(callback, MessageBox, message, messageboxtyp, timeout=timeout, default=default)
return True
elif self.timerType == TIMERTYPE.AUTOSTANDBY:
if debug:
print "self.timerType == TIMERTYPE.AUTOSTANDBY:"
if not self.getAutoSleepWindow():
return False
if not Screens.Standby.inStandby and not self.messageBoxAnswerPending: # not already in standby
self.messageBoxAnswerPending = True
callback = self.sendStandbyNotification
message = _("A finished powertimer wants to set your\n%s %s to standby. Do that now?") % (getMachineBrand(), getMachineName())
messageboxtyp = MessageBox.TYPE_YESNO
timeout = (config.usage.window_timeout.value)
default = True
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarMessageWithCallback(callback, message, messageboxtyp, timeout, default)
else:
Notifications.AddNotificationWithCallback(callback, MessageBox, message, messageboxtyp, timeout=timeout, default=default)
if self.autosleeprepeat == "once":
eActionMap.getInstance().unbindAction('', self.keyPressed)
return True
else:
self.begin = self.end = int(now) + int(self.autosleepdelay) * 60
else:
self.begin = self.end = int(now) + int(self.autosleepdelay) * 60
elif self.timerType == TIMERTYPE.AUTODEEPSTANDBY:
if debug:
print "self.timerType == TIMERTYPE.AUTODEEPSTANDBY:"
if not self.getAutoSleepWindow():
return False
if isRecTimerWakeup or (self.autosleepinstandbyonly == 'yes' and not Screens.Standby.inStandby) \
or NavigationInstance.instance.PowerTimer.isProcessing() or abs(NavigationInstance.instance.PowerTimer.getNextPowerManagerTime() - now) <= 900 or self.getNetworkAdress() or self.getNetworkTraffic() \
or NavigationInstance.instance.RecordTimer.isRecording() or abs(NavigationInstance.instance.RecordTimer.getNextRecordingTime() - now) <= 900 or abs(NavigationInstance.instance.RecordTimer.getNextZapTime() - now) <= 900:
self.do_backoff()
# retry
self.begin = self.end = int(now) + self.backoff
return False
elif not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
if self.autosleeprepeat == "once":
self.disabled = True
if Screens.Standby.inStandby or self.autosleepinstandbyonly == 'noquery': # in standby or option 'without query' is enabled
print "[PowerTimer] quitMainloop #1"
quitMainloop(1)
return True
elif not self.messageBoxAnswerPending:
self.messageBoxAnswerPending = True
callback = self.sendTryQuitMainloopNotification
message = _("A finished powertimer wants to shutdown your %s %s.\nDo that now?") % (getMachineBrand(), getMachineName())
messageboxtyp = MessageBox.TYPE_YESNO
timeout = (config.usage.window_timeout.value)
default = True
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarMessageWithCallback(callback, message, messageboxtyp, timeout, default)
else:
Notifications.AddNotificationWithCallback(callback, MessageBox, message, messageboxtyp, timeout=timeout, default=default)
if self.autosleeprepeat == "once":
eActionMap.getInstance().unbindAction('', self.keyPressed)
return True
self.begin = self.end = int(now) + int(self.autosleepdelay) * 60
elif self.timerType == TIMERTYPE.RESTART:
if debug:
print "self.timerType == TIMERTYPE.RESTART:"
#check priority
prioPT = [TIMERTYPE.RESTART, TIMERTYPE.REBOOT, TIMERTYPE.DEEPSTANDBY]
prioPTae = [AFTEREVENT.DEEPSTANDBY]
shiftPT, breakPT = self.getPriorityCheck(prioPT, prioPTae)
#a timer with higher priority was shifted - no execution of current timer
if RBsave or aeDSsave or DSsave:
if debug:
print "break#1"
breakPT = True
#a timer with lower priority was shifted - shift now current timer and wait for restore the saved time values from other timer
if False:
if debug:
print "shift#1"
breakPT = False
shiftPT = True
#shift or break
if isRecTimerWakeup or shiftPT or breakPT \
or NavigationInstance.instance.RecordTimer.isRecording() or abs(NavigationInstance.instance.RecordTimer.getNextRecordingTime() - now) <= 900 or abs(NavigationInstance.instance.RecordTimer.getNextZapTime() - now) <= 900:
if self.repeated and not RSsave:
self.savebegin = self.begin
self.saveend = self.end
RSsave = True
if not breakPT:
self.do_backoff()
#check difference begin to end before shift begin time
if RSsave and self.end - self.begin > 3 and self.end - now - self.backoff <= 240:
breakPT = True
#breakPT
if breakPT:
if self.repeated and RSsave:
try:
self.begin = self.savebegin
self.end = self.saveend
except:
pass
RSsave = False
return True
# retry
oldbegin = self.begin
self.begin = int(now) + self.backoff
if abs(self.end - oldbegin) <= 3:
self.end = self.begin
else:
if not self.repeated and self.end < self.begin + 300:
self.end = self.begin + 300
return False
elif not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
if self.repeated and RSsave:
try:
self.begin = self.savebegin
self.end = self.saveend
except:
pass
if Screens.Standby.inStandby: # in standby
print "[PowerTimer] quitMainloop #4"
quitMainloop(3)
else:
callback = self.sendTryToRestartNotification
message = _("A finished powertimer wants to restart the user interface.\nDo that now?")
messageboxtyp = MessageBox.TYPE_YESNO
timeout = (config.usage.window_timeout.value)
default = True
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarMessageWithCallback(callback, message, messageboxtyp, timeout, default)
else:
Notifications.AddNotificationWithCallback(callback, MessageBox, message, messageboxtyp, timeout=timeout, default=default)
RSsave = False
return True
elif self.timerType == TIMERTYPE.REBOOT:
if debug:
print "self.timerType == TIMERTYPE.REBOOT:"
#check priority
prioPT = [TIMERTYPE.REBOOT, TIMERTYPE.DEEPSTANDBY]
prioPTae = [AFTEREVENT.DEEPSTANDBY]
shiftPT, breakPT = self.getPriorityCheck(prioPT, prioPTae)
#a timer with higher priority was shifted - no execution of current timer
if aeDSsave or DSsave:
if debug:
print "break#1"
breakPT = True
#a timer with lower priority was shifted - shift now current timer and wait for restore the saved time values from other timer
if RSsave:
if debug:
print "shift#1"
breakPT = False
shiftPT = True
#shift or break
if isRecTimerWakeup or shiftPT or breakPT \
or NavigationInstance.instance.RecordTimer.isRecording() or abs(NavigationInstance.instance.RecordTimer.getNextRecordingTime() - now) <= 900 or abs(NavigationInstance.instance.RecordTimer.getNextZapTime() - now) <= 900:
if self.repeated and not RBsave:
self.savebegin = self.begin
self.saveend = self.end
RBsave = True
if not breakPT:
self.do_backoff()
#check difference begin to end before shift begin time
if RBsave and self.end - self.begin > 3 and self.end - now - self.backoff <= 240:
breakPT = True
#breakPT
if breakPT:
if self.repeated and RBsave:
try:
self.begin = self.savebegin
self.end = self.saveend
except:
pass
RBsave = False
return True
# retry
oldbegin = self.begin
self.begin = int(now) + self.backoff
if abs(self.end - oldbegin) <= 3:
self.end = self.begin
else:
if not self.repeated and self.end < self.begin + 300:
self.end = self.begin + 300
return False
elif not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
if self.repeated and RBsave:
try:
self.begin = self.savebegin
self.end = self.saveend
except:
pass
if Screens.Standby.inStandby: # in standby
print "[PowerTimer] quitMainloop #3"
quitMainloop(2)
else:
callback = self.sendTryToRebootNotification
message = _("A finished powertimer wants to reboot your %s %s.\nDo that now?") % (getMachineBrand(), getMachineName())
messageboxtyp = MessageBox.TYPE_YESNO
timeout = (config.usage.window_timeout.value)
default = True
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarMessageWithCallback(callback, message, messageboxtyp, timeout, default)
else:
Notifications.AddNotificationWithCallback(callback, MessageBox, message, messageboxtyp, timeout=timeout, default=default)
RBsave = False
return True
elif self.timerType == TIMERTYPE.DEEPSTANDBY:
if debug:
print "self.timerType == TIMERTYPE.DEEPSTANDBY:"
#check priority
prioPT = [TIMERTYPE.WAKEUP, TIMERTYPE.WAKEUPTOSTANDBY, TIMERTYPE.DEEPSTANDBY]
prioPTae = [AFTEREVENT.WAKEUP, AFTEREVENT.WAKEUPTOSTANDBY, AFTEREVENT.DEEPSTANDBY]
shiftPT, breakPT = self.getPriorityCheck(prioPT, prioPTae)
#a timer with higher priority was shifted - no execution of current timer
if False:
if debug:
print "break#1"
breakPT = True
#a timer with lower priority was shifted - shift now current timer and wait for restore the saved time values from other timer
if RSsave or RBsave or aeDSsave:
if debug:
print "shift#1"
breakPT = False
shiftPT = True
#shift or break
if isRecTimerWakeup or shiftPT or breakPT \
or NavigationInstance.instance.RecordTimer.isRecording() or abs(NavigationInstance.instance.RecordTimer.getNextRecordingTime() - now) <= 900 or abs(NavigationInstance.instance.RecordTimer.getNextZapTime() - now) <= 900:
if self.repeated and not DSsave:
self.savebegin = self.begin
self.saveend = self.end
DSsave = True
if not breakPT:
self.do_backoff()
#check difference begin to end before shift begin time
if DSsave and self.end - self.begin > 3 and self.end - now - self.backoff <= 240:
breakPT = True
#breakPT
if breakPT:
if self.repeated and DSsave:
try:
self.begin = self.savebegin
self.end = self.saveend
except:
pass
DSsave = False
return True
# retry
oldbegin = self.begin
self.begin = int(now) + self.backoff
if abs(self.end - oldbegin) <= 3:
self.end = self.begin
else:
if not self.repeated and self.end < self.begin + 300:
self.end = self.begin + 300
return False
elif not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
if self.repeated and DSsave:
try:
self.begin = self.savebegin
self.end = self.saveend
except:
pass
if Screens.Standby.inStandby: # in standby
print "[PowerTimer] quitMainloop #2"
quitMainloop(1)
else:
callback = self.sendTryQuitMainloopNotification
message = _("A finished powertimer wants to shutdown your %s %s.\nDo that now?") % (getMachineBrand(), getMachineName())
messageboxtyp = MessageBox.TYPE_YESNO
timeout = (config.usage.window_timeout.value)
default = True
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarMessageWithCallback(callback, message, messageboxtyp, timeout, default)
else:
Notifications.AddNotificationWithCallback(callback, MessageBox, message, messageboxtyp, timeout=timeout, default=default)
DSsave = False
return True
elif next_state == self.StateEnded:
if self.afterEvent == AFTEREVENT.WAKEUP:
Screens.Standby.TVinStandby.skipHdmiCecNow('wakeuppowertimer')
if Screens.Standby.inStandby:
Screens.Standby.inStandby.Power()
elif self.afterEvent == AFTEREVENT.STANDBY:
if not Screens.Standby.inStandby: # not already in standby
callback = self.sendStandbyNotification
message = _("A finished powertimer wants to set your\n%s %s to standby. Do that now?") % (getMachineBrand(), getMachineName())
messageboxtyp = MessageBox.TYPE_YESNO
timeout = (config.usage.window_timeout.value)
default = True
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarMessageWithCallback(callback, message, messageboxtyp, timeout, default)
else:
Notifications.AddNotificationWithCallback(callback, MessageBox, message, messageboxtyp, timeout=timeout, default=default)
elif self.afterEvent == AFTEREVENT.DEEPSTANDBY:
if debug:
print "self.afterEvent == AFTEREVENT.DEEPSTANDBY:"
#check priority
prioPT = [TIMERTYPE.WAKEUP, TIMERTYPE.WAKEUPTOSTANDBY, TIMERTYPE.DEEPSTANDBY]
prioPTae = [AFTEREVENT.WAKEUP, AFTEREVENT.WAKEUPTOSTANDBY, AFTEREVENT.DEEPSTANDBY]
shiftPT, breakPT = self.getPriorityCheck(prioPT, prioPTae)
#a timer with higher priority was shifted - no execution of current timer
if DSsave:
if debug:
print "break#1"
breakPT = True
#a timer with lower priority was shifted - shift now current timer and wait for restore the saved time values
if RSsave or RBsave:
if debug:
print "shift#1"
breakPT = False
shiftPT = True
#shift or break
runningPT = False
#option: check other powertimer is running (current disabled)
#runningPT = NavigationInstance.instance.PowerTimer.isProcessing(exceptTimer = TIMERTYPE.NONE, endedTimer = self.timerType)
if isRecTimerWakeup or shiftPT or breakPT or runningPT \
or NavigationInstance.instance.RecordTimer.isRecording() or abs(NavigationInstance.instance.RecordTimer.getNextRecordingTime() - now) <= 900 or abs(NavigationInstance.instance.RecordTimer.getNextZapTime() - now) <= 900:
if self.repeated and not aeDSsave:
self.savebegin = self.begin
self.saveend = self.end
aeDSsave = True
if not breakPT:
self.do_backoff()
#breakPT
if breakPT:
if self.repeated and aeDSsave:
try:
self.begin = self.savebegin
self.end = self.saveend
except:
pass
aeDSsave = False
return True
# retry
self.end = int(now) + self.backoff
return False
elif not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
if self.repeated and aeDSsave:
try:
self.begin = self.savebegin
self.end = self.saveend
except:
pass
if Screens.Standby.inStandby: # in standby
print "[PowerTimer] quitMainloop #5"
quitMainloop(1)
else:
callback = self.sendTryQuitMainloopNotification
message = _("A finished powertimer wants to shutdown your %s %s.\nDo that now?") % (getMachineBrand(), getMachineName())
messageboxtyp = MessageBox.TYPE_YESNO
timeout = (config.usage.window_timeout.value)
default = True
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarMessageWithCallback(callback, message, messageboxtyp, timeout, default)
else:
Notifications.AddNotificationWithCallback(callback, MessageBox, message, messageboxtyp, timeout=timeout, default=default)
aeDSsave = False
NavigationInstance.instance.PowerTimer.saveTimer()
resetTimerWakeup()
return True
def setAutoincreaseEnd(self, entry=None):
if not self.autoincrease:
return False
if entry is None:
new_end = int(time()) + self.autoincreasetime
else:
new_end = entry.begin - 30
dummyentry = PowerTimerEntry(self.begin, new_end, disabled=True, afterEvent=self.afterEvent, timerType=self.timerType)
dummyentry.disabled = self.disabled
timersanitycheck = TimerSanityCheck(NavigationInstance.instance.PowerManager.timer_list, dummyentry)
if not timersanitycheck.check():
simulTimerList = timersanitycheck.getSimulTimerList()
if simulTimerList is not None and len(simulTimerList) > 1:
new_end = simulTimerList[1].begin
new_end -= 30 # 30 Sekunden Prepare-Zeit lassen
if new_end <= time():
return False
self.end = new_end
return True
def sendStandbyNotification(self, answer):
self.messageBoxAnswerPending = False
if answer:
session = Screens.Standby.Standby
option = None
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarSession(session, option)
else:
Notifications.AddNotification(session)
def sendTryQuitMainloopNotification(self, answer):
self.messageBoxAnswerPending = False
if answer:
session = Screens.Standby.TryQuitMainloop
option = 1
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarSession(session, option)
else:
Notifications.AddNotification(session, option)
def sendTryToRebootNotification(self, answer):
if answer:
session = Screens.Standby.TryQuitMainloop
option = 2
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarSession(session, option)
else:
Notifications.AddNotification(session, option)
def sendTryToRestartNotification(self, answer):
if answer:
session = Screens.Standby.TryQuitMainloop
option = 3
if InfoBar and InfoBar.instance:
InfoBar.instance.openInfoBarSession(session, option)
else:
Notifications.AddNotification(session, option)
def keyPressed(self, key, tag):
if self.getAutoSleepWindow():
self.begin = self.end = int(time()) + int(self.autosleepdelay) * 60
def getAutoSleepWindow(self):
now = time()
if self.autosleepwindow == 'yes':
if now < self.autosleepbegin and now < self.autosleepend:
self.begin = self.autosleepbegin
self.end = self.autosleepend
elif now > self.autosleepbegin and now > self.autosleepend:
while self.autosleepend < now:
self.autosleepend += 86400
while self.autosleepbegin + 86400 < self.autosleepend:
self.autosleepbegin += 86400
self.begin = self.autosleepbegin
self.end = self.autosleepend
if not (now > self.autosleepbegin - self.prepare_time - 3 and now < self.autosleepend):
eActionMap.getInstance().unbindAction('', self.keyPressed)
self.state = 0
self.timeChanged()
return False
return True
def getPriorityCheck(self, prioPT, prioPTae):
shiftPT = breakPT = False
nextPTlist = NavigationInstance.instance.PowerTimer.getNextPowerManagerTime(getNextTimerTyp=True)
for entry in nextPTlist:
#check timers within next 15 mins will started or ended
if abs(entry[0] - time()) > 900:
continue
#faketime
if entry[1] is None and entry[2] is None and entry[3] is None:
if debug:
print "shift#2 - entry is faketime", ctime(entry[0]), entry
shiftPT = True
continue
#is timer in list itself?
if entry[0] == self.begin and entry[1] == self.timerType and entry[2] is None and entry[3] == self.state \
or entry[0] == self.end and entry[1] is None and entry[2] == self.afterEvent and entry[3] == self.state:
if debug:
print "entry is itself", ctime(entry[0]), entry
nextPTitself = True
else:
nextPTitself = False
if (entry[1] in prioPT or entry[2] in prioPTae) and not nextPTitself:
if debug:
print "break#2 <= 900", ctime(entry[0]), entry
breakPT = True
break
return shiftPT, breakPT
def getNextActivation(self):
if self.state == self.StateEnded or self.state == self.StateFailed:
return self.end
next_state = self.state + 1
return {self.StatePrepared: self.start_prepare,
self.StateRunning: self.begin,
self.StateEnded: self.end}[next_state]
def getNextWakeup(self, getNextStbPowerOn=False):
next_state = self.state + 1
if getNextStbPowerOn:
if next_state == 3 and (self.timerType == TIMERTYPE.WAKEUP or self.timerType == TIMERTYPE.WAKEUPTOSTANDBY or self.afterEvent == AFTEREVENT.WAKEUP or self.afterEvent == AFTEREVENT.WAKEUPTOSTANDBY):
if self.start_prepare > time() and (self.timerType == TIMERTYPE.WAKEUP or self.timerType == TIMERTYPE.WAKEUPTOSTANDBY): #timer start time is later as now - begin time was changed while running timer
return self.start_prepare
elif self.begin > time() and (self.timerType == TIMERTYPE.WAKEUP or self.timerType == TIMERTYPE.WAKEUPTOSTANDBY): #timer start time is later as now - begin time was changed while running timer
return self.begin
if self.afterEvent == AFTEREVENT.WAKEUP or self.afterEvent == AFTEREVENT.WAKEUPTOSTANDBY:
return self.end
next_day = 0
count_day = 0
wd_timer = datetime.fromtimestamp(self.begin).isoweekday() * -1
wd_repeated = bin(128 + self.repeated)
for s in range(wd_timer - 1, -8, -1):
count_day += 1
if int(wd_repeated[s]):
next_day = s
break
if next_day == 0:
for s in range(-1, wd_timer - 1, -1):
count_day += 1
if int(wd_repeated[s]):
next_day = s
break
#return self.begin + 86400 * count_day
return self.start_prepare + 86400 * count_day
elif next_state == 2 and (self.timerType == TIMERTYPE.WAKEUP or self.timerType == TIMERTYPE.WAKEUPTOSTANDBY):
return self.begin
elif next_state == 1 and (self.timerType == TIMERTYPE.WAKEUP or self.timerType == TIMERTYPE.WAKEUPTOSTANDBY):
return self.start_prepare
elif next_state < 3 and (self.afterEvent == AFTEREVENT.WAKEUP or self.afterEvent == AFTEREVENT.WAKEUPTOSTANDBY):
return self.end
else:
return -1
if self.state == self.StateEnded or self.state == self.StateFailed:
return self.end
return {self.StatePrepared: self.start_prepare,
self.StateRunning: self.begin,
self.StateEnded: self.end}[next_state]
def timeChanged(self):
old_prepare = self.start_prepare
self.start_prepare = self.begin - self.prepare_time
self.backoff = 0
if int(old_prepare) > 60 and int(old_prepare) != int(self.start_prepare):
self.log(15, "time changed, start prepare is now: %s" % ctime(self.start_prepare))
def getNetworkAdress(self):
ret = False
if self.netip == 'yes':
try:
for ip in self.ipadress.split(','):
if not os.system("ping -q -w1 -c1 " + ip):
ret = True
break
except:
print '[PowerTimer] Error reading ip! -> %s' % self.ipadress
return ret
def getNetworkTraffic(self, getInitialValue=False):
now = time()
newbytes = 0
if self.nettraffic == 'yes':
try:
if os.path.exists('/proc/net/dev'):
f = open('/proc/net/dev', 'r')
temp = f.readlines()
f.close()
for lines in temp:
lisp = lines.split()
if lisp[0].endswith(':') and (lisp[0].startswith('eth') or lisp[0].startswith('wlan')):
newbytes += long(lisp[1]) + long(lisp[9])
if getInitialValue:
self.netbytes = newbytes
self.netbytes_time = now
print '[PowerTimer] Receive/Transmit initialBytes=%d, time is %s' % (self.netbytes, ctime(self.netbytes_time))
return
oldbytes = self.netbytes
seconds = int(now - self.netbytes_time)
self.netbytes = newbytes
self.netbytes_time = now
diffbytes = float(newbytes - oldbytes) * 8 / 1024 / seconds #in kbit/s
if diffbytes < 0:
print '[PowerTimer] Receive/Transmit -> overflow interface counter, waiting for next value'
return True
else:
print '[PowerTimer] Receive/Transmit kilobits per second: %0.2f (%0.2f MByte in %d seconds), actualBytes=%d, time is %s' % (diffbytes, diffbytes / 8 / 1024 * seconds, seconds, self.netbytes, ctime(self.netbytes_time))
if diffbytes > self.trafficlimit:
return True
except:
print '[PowerTimer] Receive/Transmit Bytes: Error reading values! Use "cat /proc/net/dev" for testing on command line.'
return False
def createTimer(xml):
timertype = str(xml.get("timertype") or "wakeup")
timertype = {
"nothing": TIMERTYPE.NONE,
"wakeup": TIMERTYPE.WAKEUP,
"wakeuptostandby": TIMERTYPE.WAKEUPTOSTANDBY,
"autostandby": TIMERTYPE.AUTOSTANDBY,
"autodeepstandby": TIMERTYPE.AUTODEEPSTANDBY,
"standby": TIMERTYPE.STANDBY,
"deepstandby": TIMERTYPE.DEEPSTANDBY,
"reboot": TIMERTYPE.REBOOT,
"restart": TIMERTYPE.RESTART
}[timertype]
begin = int(xml.get("begin"))
end = int(xml.get("end"))
repeated = xml.get("repeated").encode("utf-8")
disabled = long(xml.get("disabled") or "0")
afterevent = str(xml.get("afterevent") or "nothing")
afterevent = {
"nothing": AFTEREVENT.NONE,
"wakeup": AFTEREVENT.WAKEUP,
"wakeuptostandby": AFTEREVENT.WAKEUPTOSTANDBY,
"standby": AFTEREVENT.STANDBY,
"deepstandby": AFTEREVENT.DEEPSTANDBY
}[afterevent]
autosleepinstandbyonly = str(xml.get("autosleepinstandbyonly") or "no")
autosleepdelay = str(xml.get("autosleepdelay") or "0")
autosleeprepeat = str(xml.get("autosleeprepeat") or "once")
autosleepwindow = str(xml.get("autosleepwindow") or "no")
autosleepbegin = int(xml.get("autosleepbegin") or begin)
autosleepend = int(xml.get("autosleepend") or end)
nettraffic = str(xml.get("nettraffic") or "no")
trafficlimit = int(xml.get("trafficlimit") or 100)
netip = str(xml.get("netip") or "no")
ipadress = str(xml.get("ipadress") or "0.0.0.0")
entry = PowerTimerEntry(begin, end, disabled, afterevent, timertype)
entry.repeated = int(repeated)
entry.autosleepinstandbyonly = autosleepinstandbyonly
entry.autosleepdelay = int(autosleepdelay)
entry.autosleeprepeat = autosleeprepeat
entry.autosleepwindow = autosleepwindow
entry.autosleepbegin = autosleepbegin
entry.autosleepend = autosleepend
entry.nettraffic = nettraffic
entry.trafficlimit = trafficlimit
entry.netip = netip
entry.ipadress = ipadress
for l in xml.findall("log"):
ltime = int(l.get("time"))
code = int(l.get("code"))
msg = l.text.strip().encode("utf-8")
entry.log_entries.append((ltime, code, msg))
return entry
class PowerTimer(timer.Timer):
def __init__(self):
timer.Timer.__init__(self)
self.Filename = Directories.resolveFilename(Directories.SCOPE_CONFIG, "pm_timers.xml")
try:
self.loadTimer()
except IOError:
print "unable to load timers from file!"
def doActivate(self, w):
# when activating a timer which has already passed,
# simply abort the timer. don't run trough all the stages.
if w.shouldSkip():
w.state = PowerTimerEntry.StateEnded
else:
# when active returns true, this means "accepted".
# otherwise, the current state is kept.
# the timer entry itself will fix up the delay then.
if w.activate():
w.state += 1
try:
self.timer_list.remove(w)
except:
print '[PowerManager]: Remove list failed'
# did this timer reached the last state?
if w.state < PowerTimerEntry.StateEnded:
# no, sort it into active list
insort(self.timer_list, w)
else:
# yes. Process repeated, and re-add.
if w.repeated:
w.processRepeated()
w.state = PowerTimerEntry.StateWaiting
self.addTimerEntry(w)
else:
# Remove old timers as set in config
self.cleanupDaily(config.recording.keep_timers.value)
insort(self.processed_timers, w)
self.stateChanged(w)
def loadTimer(self):
# TODO: PATH!
if not Directories.fileExists(self.Filename):
return
try:
file = open(self.Filename, 'r')
doc = xml.etree.cElementTree.parse(file)
file.close()
except SyntaxError:
from Tools.Notifications import AddPopup
from Screens.MessageBox import MessageBox
AddPopup(_("The timer file (pm_timers.xml) is corrupt and could not be loaded."), type=MessageBox.TYPE_ERROR, timeout=0, id="TimerLoadFailed")
print "pm_timers.xml failed to load!"
try:
import os
os.rename(self.Filename, self.Filename + "_old")
except (IOError, OSError):
print "renaming broken timer failed"
return
except IOError:
print "pm_timers.xml not found!"
return
root = doc.getroot()
# put out a message when at least one timer overlaps
checkit = True
for timer in root.findall("timer"):
newTimer = createTimer(timer)
if (self.record(newTimer, True, dosave=False) is not None) and (checkit == True):
from Tools.Notifications import AddPopup