-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
179 lines (149 loc) · 6.38 KB
/
main.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
import time
import ast
import math
import sys
from random import shuffle
import datetime
from threading import Thread
import PyHook3 as pyHook
import pythoncom
import http.client as httplib
from utils import getAbsPath
from notificationsManager import notify
from server import run
from tray import Tray
import config
def has_internet():
conn = httplib.HTTPConnection("www.google.com", timeout=5)
try:
conn.request("HEAD", "/")
conn.close()
return True
except:
conn.close()
return False
def init_keyboard(keyboard = True, mouse = True):
hm = pyHook.HookManager()
if keyboard:
hm.KeyDown = OnEvent
hm.KeyUp = OnEvent
hm.HookKeyboard()
if mouse:
hm.MouseAll = OnEvent
hm.HookMouse()
pythoncom.PumpMessages()
def OnEvent(event):
return False
#Load EXERCISES configurations
def getListOfEXERCISES(EXERCISES):
# Init the basic mathematical functions :
ln = math.log
log = math.log
e = math.exp
sin = math.sin
cos = math.cos
# Let's create a list of every exercise's configuration :
ListEXERCISESHTML = []
for name in EXERCISES:
exercise_config = config.getExerciseByName(name)
if exercise_config != False:
TYPE, FORMULA, x = exercise_config
FORMULA.replace("^","**")
FORMULA.replace("x", "*")
value = eval(FORMULA)
ListEXERCISESHTML.append((name, value, TYPE)) #, int(startPos)
continue
notify("Workout Starter Pack", "Error: " + name + " not found.", 5)
return ListEXERCISESHTML
def start():
#First, we get the basic information:
info = config.getBasicInfo()
MODE = info[0]
lastTrigger = config.getLastTrigger()
# Goal : Get basic config, load config, load tray and determine the TIMER
if MODE == 0:
EXERCISES_MODE, EXERCISES_SELECTED, NUMBER_TO_PICK_PER_SESSION, TIMER = info[1:]
# Get List EXERCISES Config:
ListEXERCISES = getListOfEXERCISES(EXERCISES_SELECTED)
# Start STRAY as THREAD
tray = Tray(MODE, TIMER, EXERCISES_MODE, EXERCISES_SELECTED)
elif MODE == 1:
EXERCISES_MODE, EXERCISES_SELECTED, NUMBER_TO_PICK_PER_SESSION, HOURS, MAXIMUM_NUMBER_OF_SESSIONS_PER_INTERVAL, RECUPERATION_TIME_SECONDS = info[1:]
# Get List EXERCISES Config:
ListEXERCISES = getListOfEXERCISES(EXERCISES_SELECTED)
# Start STRAY as THREAD
tray = Tray(MODE, [HOURS, MAXIMUM_NUMBER_OF_SESSIONS_PER_INTERVAL, RECUPERATION_TIME_SECONDS], EXERCISES_MODE, EXERCISES_SELECTED)
# Get current time and hour : determine TIMER
HOURS.sort()
expandedIntervals = [[j for j in range(*HOURS[i:i+2])] for i in range(0, len(HOURS), 2)]
currentDT = datetime.datetime.now()
n = 0
if lastTrigger == None:
lastTrigger = [0, -1]
notify("Workout Starter Pack", "Error: Couldn't get the last trigger from config. Switching to [0, -1] by default.", 5)
for i in range(len(expandedIntervals)):
expandedInterval = expandedIntervals[i]
if currentDT.hour in expandedInterval :
if lastTrigger[0] < MAXIMUM_NUMBER_OF_SESSIONS_PER_INTERVAL and lastTrigger[1] == i :
TIMER = RECUPERATION_TIME_SECONDS
lastTrigger = [lastTrigger[0] + 1, i]
elif lastTrigger[1] != i:
lastTrigger = [1, i]
TIMER = 5
else:
nextHour = HOURS[0]
nextDay = datetime.datetime(currentDT.year, currentDT.month, currentDT.day + 1).day
for i in HOURS[::2]:
if i > currentDT.hour:
nextHour = i
nextDay = currentDT.day
nextDateTime = datetime.datetime(currentDT.year, currentDT.month, nextDay, nextHour, 0)
TIMER = (nextDateTime - currentDT).total_seconds()
print("Skipping to next interval because finished this one")
# config.updateLastTrigger(0, -1)
else:
n += 1
# Update current interval and interation in config if currentDT not in any interval
if n == len(expandedIntervals):
print("Not in any intverval")
lastTrigger = [0, -1]
nextHour = HOURS[0]
nextDay = datetime.datetime(currentDT.year, currentDT.month, currentDT.day + 1).day
for i in HOURS[::2]:
if i >= currentDT.hour:
nextHour = i
nextDay = currentDT.day
nextDateTime = datetime.datetime(currentDT.year, currentDT.month, nextDay, nextHour, 0)
TIMER = (nextDateTime - currentDT).total_seconds()
# Notify for the user of what was done
if len(ListEXERCISES) == 0:
notify("Workout Starter Pack", "Error: no exercises loaded. Please look at the configuration file. Quitting.", 5)
sys.exit(0)
# EXERCISES_MODE :
if EXERCISES_MODE == 1:
shuffle(ListEXERCISES)
ListEXERCISES = ListEXERCISES[:NUMBER_TO_PICK_PER_SESSION]
# Notify of the next session's content
names = [i[0] for i in ListEXERCISES]
notify("Workout Starter Pack", str(len(ListEXERCISES)) + " exercise(s) loaded!\nWaiting " + str(int(TIMER)) + " seconds. \nNext Session : " + " ".join(names) , 5)
# Starting the tray
tray.init_icon_tray()
# Sleep TIMER seconds
time.sleep(TIMER)
# Redirect the keys and mouse events
print("Redirecting keys events...")
inputs = config.getInputsConfig()
keyboard = Thread(target= lambda : init_keyboard(*inputs))
keyboard.start()
# Run the server
PORT = config.getServerConfig()
if(has_internet()):
notify("Workout Starter Pack", ((EXERCISES_MODE == 0) and "Ordered" or "Randomized") + " sequence :\n - " + "\n - ".join(names) , 5)
run(lastTrigger, ListEXERCISES, PORT, tray)
else:
notify("Workout Starter Pack", "No internet connection (needed for the webserver)! Stopping...", 5, threaded=False)
if __name__ == "__main__":
if(has_internet()):
start()
else:
notify("Workout Starter Pack", "No internet connection (needed for the webserver)! Stopping...", 5, threaded=False)