-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUser.py
More file actions
339 lines (269 loc) · 10.8 KB
/
Copy pathUser.py
File metadata and controls
339 lines (269 loc) · 10.8 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# PiCycle - Python Exersize Bike Trainer
# Copyright (C) 2019 Jason Birch
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#/****************************************************************************/
#/* PiCycle - Python Exersize Bike Trainer. */
#/* ------------------------------------------------------------------------ */
#/* V1.00 - 2019-02-14 - Jason Birch */
#/* ------------------------------------------------------------------------ */
#/* User class. */
#/****************************************************************************/
import os
import datetime
class User:
#/***************************/
#/* Define class constants. */
#/***************************/
INCHES_TO_CM = 2.54
INCHES_PER_MILE = 63360
CM_PER_KM = 100000
def __init__(self, NewID):
# /**********************************/
# /* Intialise with default values. */
#/**********************************/
self.ID = NewID
self.Name = "CU" + str(self.ID)
self.PersonalBestAvgSpeed = 0.0
self.PersonalBestLapTime = datetime.timedelta(1, -1)
self.LoadUser(self.Name + ".USR")
#/****************************************/
#/* Load user data from the user's file. */
#/****************************************/
def LoadUser(self, FileName):
# /******************************************/
# /* Load the last active user from a file. */
#/******************************************/
if FileName == "" and os.path.isfile("USERS/DEFAULT.LD"):
File = open("USERS/DEFAULT.LD", 'r', 0)
FileName = File.readline()
File.close()
# /************************/
# /* Default user values. */
#/************************/
self.Level = 99
self.PlayMusic = "OFF"
self.Units = "M"
self.UnitsFine = "\""
self.SpeedUnits = "MPH"
self.UnitScale = self.INCHES_PER_MILE
self.TotalPulseCount = 0
self.NetworkStatus = "OFF"
self.NetworkIP = "192.168.001.100"
self.NetworkPort = "1024"
# /**************************************/
# /* Load user values from user's file. */
#/**************************************/
if os.path.isfile("USERS/" + FileName):
File = open("USERS/" + FileName, 'r', 0)
TextLine = "."
while TextLine != "":
TextLine = File.readline()
TextLine = TextLine.replace("\n", "")
if TextLine[:5] == "NAME=":
self.Name = TextLine[5:]
elif TextLine[:6] == "LEVEL=":
self.Level = int(TextLine[6:])
elif TextLine[:11] == "PLAY_MUSIC=":
self.PlayMusic = TextLine[11:]
elif TextLine[:6] == "UNITS=":
self.Units = TextLine[6:]
self.SwitchUnits(self.Units)
elif TextLine[:18] == "TOTAL_PULSE_COUNT=":
self.TotalPulseCount = int(TextLine[18:])
elif TextLine[:14] == "NETWORK_STATUS=":
self.NetworkStatus = TextLine[14:]
elif TextLine[:11] == "NETWORK_IP=":
self.NetworkIP = TextLine[11:]
elif TextLine[:13] == "NETWORK_PORT=":
self.NetworkPort = str(TextLine[13:])
File.close()
#/**************************************/
#/* Save user data to the user's file. */
#/**************************************/
def SaveUser(self, FileName):
File = open("USERS/DEFAULT.LD", 'w', 0)
File.write(FileName)
File.close()
File = open("USERS/" + FileName, 'w', 0)
File.write("NAME=" + self.Name + "\n")
File.write("LEVEL=" + str(self.Level) + "\n")
File.write("UNITS=" + self.Units + "\n")
File.write("PLAY_MUSIC=" + self.PlayMusic + "\n")
File.write("TOTAL_PULSE_COUNT=" + str(self.TotalPulseCount) + "\n")
File.write("NETWORK_STATUS=" + self.NetworkStatus + "\n")
File.write("NETWORK_IP=" + self.NetworkIP + "\n")
File.write("NETWORK_PORT=" + self.NetworkPort + "\n")
File.close()
#/******************************************************/
#/* Load user's event data from the user's event file. */
#/******************************************************/
def LoadUserEvent(self, FileName):
self.PersonalBestAvgSpeed = 0.0
self.PersonalBestLapTime = datetime.timedelta(1, -1)
if not os.path.isdir("USER_EVENT/" + self.Name):
os.mkdir("USER_EVENT/" + self.Name)
if os.path.isfile("USER_EVENT/" + self.Name + "/" + FileName):
File = open("USER_EVENT/" + self.Name + "/" + FileName, 'r', 0)
TextLine = "."
while TextLine != "":
TextLine = File.readline()
if TextLine[:23] == "PERSONAL_BEST_LAP_TIME=":
self.PersonalBestLapTime = datetime.timedelta(0, int(TextLine[23:]))
elif TextLine[:24] == "PERSONAL_BEST_AVG_SPEED=":
self.PersonalBestAvgSpeed = float(TextLine[24:])
#/****************************************************/
#/* Save user's event data to the user's event file. */
#/****************************************************/
def SaveUserEvent(self, FileName):
File = open("USER_EVENT/" + self.Name + "/" + FileName, 'w', 0)
File.write("PERSONAL_BEST_LAP_TIME=" + str(self.PersonalBestLapTime.seconds) + "\n")
File.write("PERSONAL_BEST_AVG_SPEED=" + str(self.PersonalBestAvgSpeed) + "\n")
File.close()
#/******************/
#/* Get user name. */
#/******************/
def GetName(self):
return self.Name
#/******************/
#/* Set user name. */
#/******************/
def SetName(self, NewName):
self.Name = NewName
#/*******************/
#/* Get user level. */
#/*******************/
def GetLevel(self):
return self.Level
#/*******************/
#/* Set user level. */
#/*******************/
def SetLevel(self, NewLevel):
self.Level = int(NewLevel)
#/***************************/
#/* Get user best lap time. */
#/***************************/
def GetPersonalBestLapTime(self):
return self.PersonalBestLapTime
#/***************************/
#/* Set user best lap time. */
#/***************************/
def SetPersonalBestLapTime(self, NewLapTime):
self.PersonalBestLapTime = NewLapTime
#/********************************/
#/* Get user best average speed. */
#/********************************/
def GetPersonalBestAvgSpeed(self):
return self.PersonalBestAvgSpeed
#/********************************/
#/* Set user best average speed. */
#/********************************/
def SetPersonalBestAvgSpeed(self, NewPersonalBestAvgSpeed):
self.PersonalBestAvgSpeed = NewPersonalBestAvgSpeed
#/***************************************************/
#/* Get appropreate scaling value to convert units. */
#/***************************************************/
def GetUnitScale(self):
return self.UnitScale
#/***************************************************/
#/* Set appropreate scaling value to convert units. */
#/***************************************************/
def GetUnitScale(self):
return self.UnitScale
#/*******************************************/
#/* Get user preference for distance units. */
#/*******************************************/
def GetUnits(self):
return self.Units
#/************************************************/
#/* Get user preference for fine distance units. */
#/************************************************/
def GetUnitsFine(self):
return self.UnitsFine
#/****************************************/
#/* Get user preference for speed units. */
#/****************************************/
def GetSpeedUnits(self):
return self.SpeedUnits
#/*************************************************************/
#/* Change user units preference between metric and imperial. */
#/*************************************************************/
def SwitchUnits(self, NewUnits = ""):
if NewUnits == "Km" or (NewUnits == "" and self.Units == "M"):
self.UnitScale = self.CM_PER_KM
self.Units = "Km"
self.UnitsFine = "cm"
self.SpeedUnits = "Km/H"
else:
self.UnitScale = self.INCHES_PER_MILE
self.Units = "M"
self.UnitsFine = "\""
self.SpeedUnits = "MPH"
return self.Units
#/***********************************/
#/* Get the current network status. */
#/***********************************/
def GetNetworkStatus(self):
return self.NetworkStatus
#/***********************************/
#/* Set the current network status. */
#/***********************************/
def SwitchNetworkStatus(self):
if self.NetworkStatus == "OFF":
self.NetworkStatus = "ON"
else:
self.NetworkStatus = "OFF"
#/**************************************/
#/* Get the current play music status. */
#/**************************************/
def GetPlayMusic(self):
return self.PlayMusic
#/**************************************/
#/* Set the current play music status. */
#/**************************************/
def SwitchPlayMusic(self):
if self.PlayMusic == "OFF":
self.PlayMusic = "ON"
else:
self.PlayMusic = "OFF"
#/*******************************/
#/* Get the current network IP. */
#/*******************************/
def GetNetworkIP(self):
return self.NetworkIP
#/*******************************/
#/* Set the current network IP. */
#/*******************************/
def SetNetworkIP(self, NewNetworkIP):
self.NetworkIP = NewNetworkIP
#/*********************************/
#/* Get the current network Port. */
#/*********************************/
def GetNetworkPort(self):
return self.NetworkPort
#/*********************************/
#/* Set the current network Port. */
#/*********************************/
def SetNetworkPort(self, NewNetworkPort):
self.NetworkPort = NewNetworkPort
#/***********************************************/
#/* Get the users total distance sensor pulses. */
#/***********************************************/
def GetTotalPulseCount(self):
return self.TotalPulseCount
#/***********************************************/
#/* Set the users total distance sensor pulses. */
#/***********************************************/
def SetTotalPulseCount(self, NewTotalPulseCount):
self.TotalPulseCount = NewTotalPulseCount