-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemperature_tracker.py
358 lines (257 loc) · 10.3 KB
/
temperature_tracker.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from gpiozero import CPUTemperature
import time
class TemperatureTracker:
MAX_TEMP_RASP4 = 82.0
def __init__(self, temp_time_list=[]):
self.temp_time_list = temp_time_list
self.start_timestamp = None
self.stop_timestamp = None
def minimum(self):
"""
Returns the minimum temperature reading in the instance list
Calls the minimum_from() function, using the instance list as an input parameter
Returns
-------
float
The temperature from the input list with the lowest temperature value
"""
return self.minimum_from(self.temp_time_list)[0]
def minimum_from(self, temp_time_list):
"""
Returns the minimum temperature reading in the specified list
Get the minimum temperature listed from an input list of tuples that contain
temperature floats and time data
Parameters
-------
temp_list : [(float, time)]
A list of (temperature, time) tuples that each specify a temperature
readings and the time the reading was taken
Returns
-------
(float, time)
The tuple from the input list with the lowest temperature value
"""
result = temp_time_list[0]
for temp, time in temp_time_list:
if temp < result[0]:
result = (temp, time)
return result
def maximum(self):
"""
Returns the maximum temperature reading in the instance list
Calls the maximum_from() function, using the instance list as an input parameter
Returns
-------
float
The temperature from the input list with the highest temperature value
"""
return self.maximum_from(self.temp_time_list)[0]
def maximum_from(self, temp_time_list):
"""
Returns the maximum temperature reading in the specified list
Get the maximum temperature listed from an input list of tuples that contain
temperature floats and time data
Parameters
-------
temp_list : [(float, time)]
A list of (temperature, time) tuples that each specify a temperature
readings and the time the reading was taken
Returns
-------
(float, time)
The tuple from the input list with the highest temperature value
"""
result = temp_time_list[0]
for temp, time in temp_time_list:
if temp > result[0]:
result = (temp, time)
return result
def update(self):
"""
Updates the temperature readings
Calculates the current temperature reading, calls the update_from() function,
using the instance list as an input parameter, as well as the new temperature reading.
"""
cpu = CPUTemperature()
new_reading = (cpu.temperature, time.localtime())
self.update_from(new_reading, self.temp_time_list)
def update_from(self, new_reading, temp_time_list):
"""
Takes a new temperature reading and appends this to
the input list.
Updates the instance list with the appended list.
Parameters
-------
new_reading : (float, time)
temp_time_list : [(float, time)]
"""
temp_time_list.append(new_reading)
# update the instance list with the new set of readings
self.temp_time_list = temp_time_list
def now(self):
"""
Calculates and returns the current temperature and timestamp
Returns
-------
new_reading : (float, time)
The current temperature and time
"""
cpu = CPUTemperature()
new_reading = (cpu.temperature, time.localtime())
return new_reading
def average(self):
"""
Returns the average temperature reading in the instance list
Calls the average_from() function, using the instance list as an input parameter
Returns
-------
float
The temperature from the input list with the average temperature value
"""
return self.average_from(self.temp_time_list)
def average_from(self, temp_time_list):
"""
Returns the average temperature reading in the specified list
Calculates the average temperature from an input list of tuples that contain
temperature floats and time data
Parameters
-------
temp_list : [(float, time)]
A list of (temperature, time) tuples that each specify a temperature
readings and the time the reading was taken
Returns
-------
float
The average temperature reading from the input list
"""
total_temp = 0.0
count = 0
for temp, time in temp_time_list:
total_temp += temp
count += 1
return total_temp / count
def count(self):
"""
Returns the number of temperature readings in the specified list
Calls count_from() with the instance list as input
Returns
-------
int
The number of temperature readings from the input list
"""
return self.count_from(self.temp_time_list)
def count_from(self, temp_time_list):
"""
Returns the number of temperature readings in the specified list
Parameters
-------
temp_list : [(float, time)]
A list of (temperature, time) tuples that each specify a temperature
readings and the time the reading was taken
Returns
-------
int
The number of temperature readings from the input list
"""
count = len(temp_time_list)
return count
def temperatures(self):
"""
Returns the temperature readings for the instance
Returns
-------
temp_list : [(float, time)]
A list of (temperature, time) tuples that each specify a temperature
readings and the time the reading was taken
"""
return self.temp_time_list
def start(self):
"""
Marks the start time of the temperature tracker
Sets the instance time of start_timestamp to current time
"""
self.start_timestamp = time.localtime()
def get_start(self):
"""
Returns the start time of the temperature tracker
Returns
-------
time
The time the tracker started
"""
return self.start_timestamp
def stop(self):
"""
Marks the stop time of the temperature tracker
Sets the instance time of stop_timestamp to current time
"""
self.stop_timestamp = time.localtime()
def get_stop(self):
"""
Returns the stop time of the temperature tracker
Returns
-------
time
The time the tracker stopped
"""
return self.stop_timestamp
def summary_from(self, temp_time_list):
"""
Prints out a summary of the temperature readings in easy to read format.
Includes average temperature, the number of readings, each reading in
human-readable format, and the the maximum, minimum, and average temperatures.
Parameters
-------
temp_list : [(float, time)]
A list of (temperature, time) tuples that each specify a temperature
readings and the time the reading was taken
Returns
-------
string
A string of summary details on the temperature readings
"""
temp_dict = {}
# first make sure all readings are up to date
temp_dict['average'] = self.average()
temp_dict['minimum'] = self.minimum()
temp_dict['maximum'] = self.maximum()
temp_dict['count'] = self.count()
temp_dict['start_timestamp'] = self.start_timestamp
temp_dict['stop_timestamp'] = self.start_timestamp
if temp_dict['start_timestamp'] is None:
return "The temperature tracker has not yet been started"
else:
summary_string = "The temperature tracker was started at: " + \
time.strftime('%Y-%m-%d %H:%M:%S',
temp_dict['start_timestamp']) + "\n"
summary_string += "A total of " + \
str(temp_dict['count']) + " have been gathered\n"
summary_string += "The readings so far are: \n"
for i in range(0, len(temp_time_list)):
summary_string += "\t\t" + "{:1.2f}C/{:1.2f}F at time {}\n".format(temp_time_list[i][0],
(((temp_time_list[i][0] * (9 / 5)) + 32)),
time.strftime('%Y-%m-%d %H:%M:%S', temp_time_list[i][1]))
summary_string += "The average temperature was: {:1.2f}C/{:1.2f}F".format(
temp_dict['average'], (((temp_dict['average'] * (9 / 5)) + 32))) + "\n"
summary_string += "The maximum temperature was: {:1.2f}C/{:1.2f}F".format(
temp_dict['maximum'], (((temp_dict['maximum'] * (9 / 5)) + 32))) + "\n"
summary_string += "The minimum temperature was: {:1.2f}C/{:1.2f}F".format(
temp_dict['minimum'], (((temp_dict['minimum'] * (9 / 5)) + 32))) + "\n"
if temp_dict['stop_timestamp'] is not None:
summary_string += "The temperature tracker was stopped at time: " + \
time.strftime('%Y-%m-%d %H:%M:%S',temp_dict['stop_timestamp']) + "\n"
else:
summary_string += "The temperature tracker is still on.\n"
return summary_string
def summary(self):
"""
Prints out a summary of the temperature readings in easy to read format.
Includes average temperature, the number of readings,
the maximum temperature and the minimum temperature.
Calls summary_from() with the instance list as input to gather summary data
Returns
-------
string
A string of summary details on the temperature readings
"""
return self.summary_from(self.temp_time_list)