-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
rpieTime.py
113 lines (99 loc) · 3.1 KB
/
rpieTime.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
#!/usr/bin/env python3
#############################################################################
###################### Helper Library for Time functions ####################
#############################################################################
#
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
from datetime import datetime
from datetime import timedelta
from threading import Timer
import time
import rpieGlobals
start_time = datetime.now()
def millis():
dt = datetime.now() - start_time
ms = int((dt.days * 86400000) + (dt.seconds * 1000) + (dt.microseconds / 1000))
return ms
def getuptime(form=0):
rs = datetime.now() - start_time
upts = ""
if form==0:
upts = (rs.days * 86400) + rs.seconds
elif form==1:
hours, remainder = divmod(rs.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
upts = str(rs.days) + " days " + str(hours) + " hours " + str(minutes) + " minutes"
return upts
class timer:
def __init__(self,timerid):
self.timerid = timerid
self.state = 0 # 0:off,1:running,2:paused
self.starttime = 0
self.pausetime = 0
self.lefttime = 0
self.timer = None
self.timeractive = False
self.callback = None
self.retvalue = [-1,-1]
def addcallback(self,callback):
self.callback = callback
def setretvalue(self,retvalue):
self.retvalue = retvalue
def __del__(self):
if self.timer:
self.timer.cancel()
def start(self,timeout):
# print("Timer",self.timerid,"started with timeout:",timeout)
self.timer = Timer(timeout,self.stop)
self.starttime = time.time()
self.lefttime = timeout
self.state = 1
self.timeractive = True
self.timer.start()
def stop(self):
# print("Timer",self.timerid,"stopped")
self.state = 0
self.starttime = 0
self.timeractive = False
if self.callback:
if self.retvalue[0] > -1:
self.callback(self.timerid,self.retvalue) # callbacks with saved return value
else:
self.callback(self.timerid) # call rules with timer id only
if self.timer:
self.timer.cancel()
def pause(self):
if self.state == 1:
lefttime = time.time()-self.starttime
# print("Timer",self.timerid,"runnning paused at",lefttime)
if lefttime<self.lefttime:
self.lefttime = self.lefttime - lefttime
self.state = 2
self.timer.cancel()
def resume(self):
if self.state == 2:
# print("Timer",self.timerid,"runnning continues for",self.lefttime)
self.timer = Timer(self.lefttime,self.stop)
self.starttime = time.time()
self.state = 1
self.pausetime = 0
self.timer.start()
def addsystemtimer(timeout,callbackfunc,retvaluearray):
result = False
for t in range(0,rpieGlobals.SYSTEM_TIMER_MAX):
if SysTimers[t].timeractive==False:
SysTimers[t].addcallback(callbackfunc)
SysTimers[t].setretvalue(retvaluearray)
SysTimers[t].start(timeout)
result = True
break
if result==False:
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"No more system timers for: "+str(retvaluearray))
return result
Timers = []
for t in range(0,rpieGlobals.RULES_TIMER_MAX):
Timers.append(timer(t+1))
SysTimers = []
for t in range(0,rpieGlobals.SYSTEM_TIMER_MAX):
SysTimers.append(timer(t))