-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathontime-tracker
executable file
·90 lines (71 loc) · 2.15 KB
/
ontime-tracker
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
#!/usr/bin/env python2
import datetime
import hal
import os
import sys
import time
def update_pins():
h['total-seconds'] = total_seconds + seconds_this_time
total_minutes, h['seconds'] = divmod((total_seconds + seconds_this_time), 60)
h['hours'], h['minutes'] = divmod(total_minutes, 60)
def update_savefile():
global total_seconds
global name
global comments
with open(name+".save", "w") as savefile:
for c in comments:
savefile.write(c)
savefile.write("%d\n" % int(total_seconds))
name = "ontime-tracker"
if len(sys.argv) > 1:
name = name + "." + sys.argv[1]
total_seconds = 0
seconds_this_time = 0
comments = []
try:
with open(name+".save") as savefile:
while True:
line = savefile.readline()
if line[0] == '#':
comments.append(line)
continue
total_seconds = int(line.strip())
break
except IOError as e:
# It's ok if the file doesn't exist, we'll start with 0 seconds of
# runtime and create the file.
pass
h = hal.component(name)
h.newpin("on", hal. HAL_BIT, hal.HAL_IN)
h.newpin("hours", hal.HAL_U32, hal.HAL_OUT)
h.newpin("minutes", hal.HAL_U32, hal.HAL_OUT)
h.newpin("seconds", hal.HAL_U32, hal.HAL_OUT)
h.newpin("total-seconds", hal.HAL_U32, hal.HAL_OUT)
update_pins()
h.ready()
old_on = False
try:
while True:
if h['on'] and not old_on:
# It's started!
start_time = datetime.datetime.now()
seconds_this_time = 0
elif old_on and not h['on']:
# It stopped.
dt = datetime.datetime.now() - start_time
seconds_this_time = dt.total_seconds()
total_seconds += seconds_this_time
seconds_this_time = 0
update_pins()
update_savefile()
elif h['on']:
# Still on.
dt = datetime.datetime.now() - start_time
seconds_this_time = dt.total_seconds()
update_pins()
update_savefile()
old_on = h['on']
time.sleep(1)
except KeyboardInterrupt:
total_seconds += seconds_this_time
update_savefile()