-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataManager.py
193 lines (162 loc) · 6.38 KB
/
dataManager.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import MySQLdb
from datetime import datetime
STEP = 4 #метров
POWER_IMPULSE = STEP
RESET_SPEED_TIME = 3
class DataManager:
def __init__(self):
self.time = time
self.currentTimeForLastRace = datetime.now()
self.currentTime = self.time.time()
self.speed = 0
self.db = MySQLdb.connect(host="localhost", user="root", passwd="root", db="skirunner", charset='utf8')
self.cursor = self.db.cursor()
self.isGetLastRaceSpeeds = False
self.dataLastRace = []
self.lastRaceMinDate = datetime.now()
self.value = 0
self.lastValue = 0
self.impulse = 0
self.isRaceStart = False
self.currentRaceId = -1
self.currentDistanceId = -1
self.currentProfileId = -1
def getImpulse(self, value):
self.impulse = 0
if self.time.time() - self.currentTime > RESET_SPEED_TIME:
self.speed = 0
self.value = value
if self.value != self.lastValue:
time = self.time.time() - self.currentTime
self.impulse = POWER_IMPULSE
self.isRaceStart = True
self.speed = STEP / time # метры в секунду
self.currentTime = self.time.time()
self.lastValue = self.value
return self.impulse
def getLastRaceDistanceAtCurrentTime(self, raceId,currentTime):
lastRaceDistance = 0
dateFormat = "%Y-%m-%d %H:%M:%S.%f"
if not self.isGetLastRaceSpeeds:
sql = """SELECT min(date) FROM runLog WHERE race_id = %s""" % raceId
self.cursor.execute(sql)
data = self.cursor.fetchall()
for rec in data:
self.lastRaceMinDate = datetime.strptime(rec[0],dateFormat)
sql = """SELECT distance,date FROM runLog WHERE race_id = %s ORDER BY date DESC""" % raceId
self.cursor.execute(sql)
self.dataLastRace = self.cursor.fetchall()
self.isGetLastRaceSpeeds = True
if self.isRaceStart:
time = datetime.now() - datetime.fromtimestamp(currentTime)
for rec in self.dataLastRace:
distance, date = rec
if time <= (datetime.strptime(date,dateFormat) - self.lastRaceMinDate):
lastRaceDistance = distance
return lastRaceDistance
def getRaceDistance(self, raceId):
sql = """SELECT distance FROM distance WHERE id =
(SELECT id_distance FROM race WHERE id = %s)""" % raceId
self.cursor.execute(sql)
try:
data = self.cursor.fetchone()[0]
except:
data = 0
return data
def getLastRaceId(self, profileId):
sql = """SELECT max(id) FROM race
WHERE id_user = %s
AND id != (SELECT max(id) FROM race
WHERE id_user = %s)""" % (profileId, profileId)
self.cursor.execute(sql)
data = self.cursor.fetchone()[0]
return data
def getCurrentRaceId(self):
if self.currentRaceId != -1:
return self.currentRaceId
sql = "SELECT max(id) FROM race"
self.currentRaceId = self.cursor.execute(sql)[0]
return self.currentRaceId
def newRace(self,profileId,distanceId):
sql = """INSERT INTO race(id_user,id_distance)
VALUES ('%s', '%s')
"""%(profileId,distanceId)
self.cursor.execute(sql)
self.db.commit()
sql = "SELECT max(id) FROM race"
self.cursor.execute(sql)
self.currentRaceId = self.cursor.fetchone()[0]
return self.currentRaceId
def getProfilesAndIds(self):
sql = "SELECT * FROM user"
self.cursor.execute(sql)
data = self.cursor.fetchall()
return data
def getDistancesAndIds(self):
sql = "SELECT * FROM distance"
self.cursor.execute(sql)
data = self.cursor.fetchall()
return data
def newProfile(self,name):
sql = """INSERT INTO user(name)
VALUES ('%s')
"""%(name)
self.cursor.execute(sql)
self.db.commit()
def newDistance(self,distance):
sql = """INSERT INTO distance(distance)
VALUES ('%s')
"""%(distance)
self.cursor.execute(sql)
self.db.commit()
def closeDB(self):
self.db.close()
def commitDB(self):
self.db.commit()
#TODO Если будет тормозить, комитить после окончания бега
def logSpeed(self,distance, raceId):
sql = """INSERT INTO runLog(race_id,distance,date)
VALUES ('%s', '%s', '%s')
"""%(raceId,distance,datetime.now())
self.cursor.execute(sql)
self.db.commit()
def getAverageSpeedByRace(self, raceId):
sql = """SELECT (SELECT distance FROM distance
WHERE id = (SELECT id_distance FROM race WHERE id = %s))
/ TIME_TO_SEC(TIMEDIFF(max(date), min(date))) AS race_time FROM runLog
where race_id = %s""" % (raceId,raceId)
self.cursor.execute(sql)
averageSpeedByRace = self.cursor.fetchone()[0]
return averageSpeedByRace
def getProfileNameById(self,profileId):
sql = "SELECT name FROM user WHERE id=%s" % profileId
self.cursor.execute(sql)
data = self.cursor.fetchone()[0]
return data
def getDistanceNameById(self,distanceId):
sql = "SELECT distance FROM distance WHERE id=%s" % distanceId
self.cursor.execute(sql)
data = self.cursor.fetchone()[0]
return data
def getDatesAndRaceTimesAndRaceIdsByProfileId(self,profileId):
sql = """SELECT
(SELECT max(date) FROM runLog WHERE race_id = race.id),
(SELECT TIME_TO_SEC(TIMEDIFF(max(date), min(date))) AS race_time FROM runLog WHERE race_id = race.id),
race.id
FROM race
WHERE race.id_user = %s""" % profileId
self.cursor.execute(sql)
data = self.cursor.fetchall()
return data
def getRaceIdByDateAndProfileId(self, date, profileId):
dateformat = '%Y-%m-%d'
sql = """SELECT id FROM race
WHERE id_user = %s
AND id IN (SELECT DISTINCT race_id FROM runLog
WHERE DATE(date) = STR_TO_DATE('%s', '%s'))""" % (profileId,date,dateformat)
self.cursor.execute(sql)
data = self.cursor.fetchall()
return data