-
Notifications
You must be signed in to change notification settings - Fork 1
/
Recorders.py
53 lines (40 loc) · 1.5 KB
/
Recorders.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
import os
from Measurement import Measurement
import datetime
class Recorder(object):
def __init__(self, recorderType):
self.recorderType = recorderType
def record(self, measure: Measurement):
None
class PrintRecorder(Recorder):
def __init__(self, config):
Recorder.__init__(self, 'file')
self.format = config['format']
def record(self, measure: Measurement):
line = self.format.format(
device_id=measure.device_id,
celsius=measure.get_celsius(),
fahrenheit=measure.get_fahrenheit(),
timestamp=measure.timestamp)
print(line, end='\n')
class FileRecorder(Recorder):
def __init__(self, config):
Recorder.__init__(self, 'file')
self.format = config['format']
self.container = config['container']
self.extension = config['extension']
def get_filename(self):
return "%s%s"%(datetime.date.today().strftime("%Y-%m-%d"), self.extension)
def record(self, measure: Measurement):
log_entry = self.format.format(
device_id=measure.device_id,
celsius=measure.get_celsius(),
fahrenheit=measure.get_fahrenheit(),
timestamp=measure.timestamp)
directory = self.container + measure.device_id
file_path = directory + "/" + self.get_filename()
if not os.path.exists(directory):
os.makedirs(directory)
f = open(file_path, 'a')
f.write(log_entry)
f.write('\n')