-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdump_roasts.py
executable file
·286 lines (226 loc) · 8.54 KB
/
dump_roasts.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
#! /usr/bin/env python3
import argparse
import csv
import datetime
import json
import os
import sys
#
# Roastime control codes.
#
codes_by_control = {
'power': 0,
'fan': 1,
'drum': 2,
}
controls_by_code = {v:k for k,v in codes_by_control.items()}
#
# Roast fields for sample arrays.
#
roast_sample_fields = [
'beanDerivative',
'beanTemperature',
'drumTemperature',
]
#
# Functions for computing event fields.
#
def make_get_sample(index_field):
'''
Get a sampled field at an index.
'''
def get_sample(roast_json, source_field):
try:
index = min(roast_json[index_field], len(roast_json[source_field]) - 1)
return roast_json[source_field][index]
except:
raise
sys.stderr.write(f'failed to get sample {source_field} using index field {index_field}\n')
return None
return get_sample
def make_get_control(control):
'''
Get a control value at an index.
'''
control_code = codes_by_control[control]
def get_control(roast_json, source_field):
current_value = None
index_value = roast_json[source_field]
try:
action_times = roast_json['actions']['actionTimeList']
for action in action_times:
if action['ctrlType'] != control_code:
continue
if action['index'] > index_value:
return current_value
current_value = action['value']
if current_value is not None:
return current_value
sys.stderr.write(f'index value {index_value} not within control range\n')
return None
except:
sys.stderr.write(f'failed to get control {control} using index field {source_field}\n')
raise
return None
return get_control
def make_conversion(conversion_type):
'''
Coerce a roast field to a specific type.
'''
def conversion(roast_json, source_field):
value = roast_json[source_field]
return None if value is None else conversion_type(value)
return conversion
def seconds_from_index(roast_json, source_field):
'''
Convert an index field to a time value in seconds.
'''
try:
return roast_json[source_field] / roast_json['sampleRate']
except:
sys.stderr.write(f'failed to get convert {source_field} to time value\n')
return None
#
# Set up basic fields.
#
roast_fields = [
{'fields': ['dateTime'], 'mapped_field': ('date', lambda roast_json, source_field: datetime.datetime.fromtimestamp(roast_json[source_field] / 1000).strftime('%Y-%m-%d')) },
{'fields': ['dateTime'], 'mapped_field': ('time', lambda roast_json, source_field: datetime.datetime.fromtimestamp(roast_json[source_field] / 1000).strftime('%H:%M:%S')) },
'dateTime',
'uid',
'roastNumber',
'roastName',
'beanId',
'rating',
'serialNumber',
'firmware',
'hardware',
{'fields': ['ambient', 'ambientTemp'], 'mapped_field': ('ambient', make_conversion(float))},
{'fields': ['humidity', 'roomHumidity'], 'mapped_field': ('humidity', make_conversion(float))},
{'fields': ['weightGreen'], 'mapped_field': ('weightGreen', make_conversion(float))},
{'fields': ['weightRoasted'], 'mapped_field': ('weightRoasted', make_conversion(float))},
'preheatTemperature',
'beanChargeTemperature',
'beanDropTemperature',
'drumChargeTemperature',
'drumDropTemperature',
'totalRoastTime',
'sampleRate',
'roastStartIndex',
'indexYellowingStart',
'indexFirstCrackStart',
'indexFirstCrackEnd',
'indexSecondCrackStart',
'indexSecondCrackEnd',
'roastEndIndex',
]
#
# Work around event naming inconsistencies.
#
events = [
('roastStart', True),
('roastEnd', True),
('YellowingStart', False),
('FirstCrackStart', False),
('FirstCrackEnd', False),
('SecondCrackStart', False),
('SecondCrackEnd', False),
]
def get_event_field(event_name, prepend, field):
return f'{event_name}{field.capitalize()}' if prepend else f'{field}{event_name}'
#
# Add computed event fields.
#
for event_name, prepend in events:
#
# Fields for event times.
#
source_field = get_event_field(event_name, prepend, 'index')
destination_field = get_event_field(event_name, prepend, 'seconds')
roast_fields.append({'fields': [source_field], 'mapped_field': (destination_field, seconds_from_index) })
#
# Fields for event control values.
#
for control in codes_by_control.keys():
source_field = get_event_field(event_name, prepend, 'index')
destination_field = get_event_field(event_name, prepend, control)
roast_fields.append({'fields': [source_field], 'mapped_field': (destination_field, make_get_control(control)) })
#
# Fields for event sample values.
#
for roast_sample_field in roast_sample_fields:
source_field = get_event_field(event_name, prepend, 'index')
destination_field = get_event_field(event_name, prepend, roast_sample_field)
roast_fields.append({'fields': [roast_sample_field], 'mapped_field': (destination_field, make_get_sample(source_field)) })
def set_roast_column(roast_json, roast_columns, roast_field):
if 'mapped_field' in roast_field:
mapped_field, mapping_fn = roast_field['mapped_field']
if 'fields' in roast_field:
#
# Map a source field (optionally involving arbitrary
# data as specified in the mapping function).
#
for field in roast_field['fields']:
if field in roast_json:
roast_columns[mapped_field] = mapping_fn(roast_json, field)
return
else:
#
# Compute a value from arbitrary roast fields.
#
roast_columns[mapped_field] = mapping_fn(roast_json, None)
sys.stderr.write(f'failed to retrieve data for {mapped_field}\n')
roast_columns[mapped_field] = None
return
roast_columns[roast_field] = roast_json.get(roast_field, None)
def create_roast(roast_json):
roast = {}
for roast_field in roast_fields:
try:
set_roast_column(roast_json, roast, roast_field)
except:
sys.stderr.write(f'an exception occurred while processing field {roast_field}')
raise
return roast
def load_roasts(roast_dirname):
roasts = []
for roast_filename in os.listdir(roast_dirname):
roast_pathname = os.path.join(roast_dirname, roast_filename)
sys.stderr.write(f'loading {roast_pathname}\n')
with open(roast_pathname, 'r', encoding='utf-8') as roast_file:
roast_json = json.load(roast_file)
roast = create_roast(roast_json)
roasts.append(roast)
return roasts
def get_fields():
return [f if 'mapped_field' not in f else f['mapped_field'][0] for f in roast_fields]
def write_roasts(csv_file, roasts, fields):
writer = csv.writer(csv_file)
writer.writerow(fields)
for roast in roasts:
writer.writerow([roast[field] for field in fields])
def main():
default_fields = ['date', 'time', 'beanId', 'weightGreen']
valid_fields = ', '.join(get_fields())
epilog = f'Valid field names are: {valid_fields}'
parser = argparse.ArgumentParser(description='Convert RoasTime roast data to CSV.', epilog=epilog)
parser.add_argument('-f', '--fields', help=f'comma-separated list of fields (default is {",".join(default_fields)})')
parser.add_argument('output_file', metavar='PATH', help='CSV file name (default is stdout)', nargs='?')
if sys.platform.startswith('linux'):
config_path = os.environ.get('XDG_CONFIG_HOME', os.path.join(os.path.expanduser('~'), '.config'))
elif sys.platform == 'darwin':
config_path = os.path.join(os.path.expanduser('~'), 'Library', 'Application Support')
elif sys.platform in ['win32', 'cygwin']:
config_path = os.path.join(os.path.expanduser('~'), 'AppData', 'Roaming')
else:
raise NotImplementedError(f'platform {sys.platform} is not supported')
args = parser.parse_args()
roast_path = os.path.join(config_path, 'roast-time', 'roasts')
roasts = load_roasts(roast_path)
fields = default_fields if args.fields is None else args.fields.split(',')
file_id = sys.stdout.fileno() if args.output_file is None else args.output_file
with open(file_id, 'w', newline='') as csv_file:
write_roasts(csv_file, roasts, fields)
if __name__ == '__main__':
rv = main()
sys.exit(rv)