-
Notifications
You must be signed in to change notification settings - Fork 0
/
eye-strain-alerter.py
141 lines (113 loc) · 3.79 KB
/
eye-strain-alerter.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
import paho.mqtt.client as mqtt
import signal
import subprocess
import time
# global init state
init = False
class Timer:
start_time = 0
total_pause_time = 0
pause_start = 0
paused = False
running = False
def reset(self):
self.total_pause_time = 0
self.start_time = time.time()
def start(self):
self.total_pause_time = 0
self.start_time = time.time()
self.running = True
def stop(self):
self.running = False
def time(self):
return (time.time() - self.start_time) - self.total_pause_time
def pause(self):
if self.paused is False:
self.pause_start = time.time()
self.paused = True
def unpause(self):
if (self.paused is True):
self.total_pause_time += (time.time() - self.pause_start)
self.paused = False
def current_pause_time(self):
if (self.paused):
return time.time() - self.pause_start
else:
return 0
# global state variable
user_on_break = False # user was alerted and started looking away
# global time tracking variable
timer = Timer()
# global constants
MAX_SCREEN_TIME = 20
LOOK_AWAY_TIME = 15
#global process for music
#music_process = None
### This happens each time the user starts looking or looks away
def message_received(client, userdata, message):
global user_on_break
global timer
global init
if init is False:
init = True
if (str(message.payload.decode("utf-8")) == "1"): # user started looking
print('looking at screen')
if user_on_break:
# user ended break too soon
print('Resetting break timer. Try looking away for the full ' +
str(LOOK_AWAY_TIME) + ' seconds.')
timer.stop()
timer.reset()
else: # user started looking again while not on break
if timer.paused: # user looked away briefly and is now looking back
timer.unpause()
else: # user is coming off of break
timer.reset()
timer.start()
else: # user looked away
print('looking away from screen')
if user_on_break:
# user restarted break
timer.reset()
timer.start()
else:
# user looks aways without it being time for a break
timer.pause()
client = mqtt.Client("listener")
client.connect("172.20.10.171", port=30300)
client.on_message=message_received
client.loop_start()
client.subscribe("eyes")
play_ding_cmd = "omxplayer -o local ding.m4a"
#play_break_music_cmd = "omxplayer -o local controlla.m4a"
def main():
global init
global user_on_break
global timer
global LOOK_AWAY_TIME
global MAX_SCREEN_TIME
#global music_process
while init is False:
time.sleep(1)
### This is happening over and over again forever
while(True):
if user_on_break:
if timer.time() >= LOOK_AWAY_TIME:
print('Break is over! Get back to work.')
user_on_break = False
process = subprocess.Popen(play_ding_cmd.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
timer.stop()
timer.reset()
else:
if timer.time() >= MAX_SCREEN_TIME:
print('You have been looking at the screen for ' +
str(MAX_SCREEN_TIME) + ' minutes. Try looking ' +
'at something far away for ' + str(LOOK_AWAY_TIME) +
'minutes.')
process = subprocess.Popen(play_ding_cmd.split(), stdout=subprocess.PIPE)
timer.stop()
timer.reset()
user_on_break = True
if __name__ == "__main__":
main()