Skip to content

Commit

Permalink
.fast method work with compoennt fields #3
Browse files Browse the repository at this point in the history
  • Loading branch information
roznet committed Mar 21, 2022
1 parent f970427 commit a30459d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
26 changes: 20 additions & 6 deletions Sources/FitFileParser/rzfit_swift_map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9949,7 +9949,11 @@ fileprivate func rzfit_swift_value_dict_for_event( ptr : UnsafePointer<FIT_EVENT
sourceData = sourceData >> bits
rv[name] = Double(val) / Double(scale)
}
}else{
}else if x.event != 0 /* timer */ &&
x.event != 10 /* course_point */ &&
x.event != 27 /* fitness_equipment */ &&
x.event != 44 /* rider_position_change */ &&
x.event != 47 /* comm_timeout */ {
let val : Double = Double(x.data)
rv[ "data" ] = val
}
Expand Down Expand Up @@ -10049,7 +10053,8 @@ fileprivate func rzfit_swift_value_dict_for_device_info( ptr : UnsafePointer<FIT
if x.source_type == 0 { // ant
let val : Double = Double(x.device_type)
rv[ "ant_device_type" ] = val
}else{
}else if x.source_type != 1 /* antplus */ &&
x.source_type != 1 /* antplus */ {
let val : Double = Double(x.device_type)
rv[ "device_type" ] = val
}
Expand Down Expand Up @@ -11249,7 +11254,10 @@ fileprivate func rzfit_swift_value_dict_for_workout_step( ptr : UnsafePointer<FI
}else if x.duration_type == 29 { // reps
let val : Double = Double(x.duration_value)
rv[ "duration_reps" ] = val
}else{
}else if x.duration_type != 2 /* hr_less_than */ &&
x.duration_type != 3 /* hr_greater_than */ &&
x.duration_type != 14 /* power_less_than */ &&
x.duration_type != 15 /* power_greater_than */ {
let val : Double = Double(x.duration_value)
rv[ "duration_value" ] = val
}
Expand Down Expand Up @@ -11279,7 +11287,11 @@ fileprivate func rzfit_swift_value_dict_for_workout_step( ptr : UnsafePointer<FI
}else if x.duration_type == 9 { // repeat_until_calories
let val : Double = Double(x.target_value)
rv[ "repeat_calories" ] = val
}else{
}else if x.duration_type != 10 /* repeat_until_hr_less_than */ &&
x.duration_type != 11 /* repeat_until_hr_greater_than */ &&
x.duration_type != 12 /* repeat_until_power_less_than */ &&
x.duration_type != 13 /* repeat_until_power_greater_than */ &&
x.target_type != 11 /* swim_stroke */ {
let val : Double = Double(x.target_value)
rv[ "target_value" ] = val
}
Expand All @@ -11291,7 +11303,8 @@ fileprivate func rzfit_swift_value_dict_for_workout_step( ptr : UnsafePointer<FI
}else if x.target_type == 3 { // cadence
let val : Double = Double(x.custom_target_value_low)
rv[ "custom_target_cadence_low" ] = val
}else{
}else if x.target_type != 1 /* heart_rate */ &&
x.target_type != 4 /* power */ {
let val : Double = Double(x.custom_target_value_low)
rv[ "custom_target_value_low" ] = val
}
Expand All @@ -11303,7 +11316,8 @@ fileprivate func rzfit_swift_value_dict_for_workout_step( ptr : UnsafePointer<FI
}else if x.target_type == 3 { // cadence
let val : Double = Double(x.custom_target_value_high)
rv[ "custom_target_cadence_high" ] = val
}else{
}else if x.target_type != 1 /* heart_rate */ &&
x.target_type != 4 /* power */ {
let val : Double = Double(x.custom_target_value_high)
rv[ "custom_target_value_high" ] = val
}
Expand Down
1 change: 1 addition & 0 deletions Sources/FitFileParserObjc/rzfit_objc_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef FIT_UINT8 FIT_UNIT;
#define FIT_FLAG_NONE 0b0000
#define FIT_FLAG_DATE 0b0001
#define FIT_FLAG_REL 0b0010
#define FIT_FLAG_PENDING 0b0100

typedef struct {
FIT_SINT16 scale;
Expand Down
7 changes: 7 additions & 0 deletions Tests/FitFileParserSwiftTests/FitFileParserSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ final class FitFileParserSwiftTests: XCTestCase {
// skip empty string, not interpreted the same
continue
}
// skip mixed switched type that is not fully handled by generic
if message.messageType.name() == "device_info" && fast_key == "device_type" {
continue
}
if message.messageType.name() == "event" && fast_key == "data" {
continue
}
XCTAssertNotNil(generic_message.interpretedField(key: fast_key), "\(message.messageType.name()).\(fast_key): missing")
// Know fields where array skipped in fast mode
if generic_message.messageType == .developer_data_id && fast_key == "application_id" {
Expand Down
55 changes: 40 additions & 15 deletions python/fitsdkparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
TYP_COL_VALUE = 3
TYP_COL_COMMENT = 4

# internal
FIT_FLAG_NONE = 0b0000
FIT_FLAG_DATE = 0b0001
FIT_FLAG_REL = 0b0010
FIT_FLAG_PENDING = 0b0100


def fix_variable_name( var_name ):
'''
fix for reserved names
Expand Down Expand Up @@ -74,7 +81,14 @@ def first_line_with_annotate_comment(prefix = '', annotate = True):
return []
previous_frame = currentframe().f_back
(filename, line_number,function_name, lines, index) = getframeinfo(previous_frame)
return [ '{}//Generated by {} at {}:{}'.format( prefix, function_name, os.path.basename(filename), line_number ) ]
return [ '{}//Generated by {}:{}:{}'.format( prefix, function_name, os.path.basename(filename), line_number ) ]

def annotate_comment(comment, prefix = '', annotate = True):
if not annotate:
return []
previous_frame = currentframe().f_back
(filename, line_number,function_name, lines, index) = getframeinfo(previous_frame)
return [ '{}//{} [{}:{}:{}]'.format( prefix, comment, function_name, os.path.basename(filename), line_number ) ]

class Type :
'''
Expand Down Expand Up @@ -603,6 +617,7 @@ def swift_stmt_case_convert_to_value(self,ctx,message):
rv = first_line_with_annotate_comment(' ',ctx.annotate)
something_done = False
if_statement = 'if'
else_conditions = []
for r in self.references:
if not r.reference_field:
logging.error( 'bug missing field {}.{}'.format( self.name, r.name ))
Expand All @@ -620,21 +635,29 @@ def swift_stmt_case_convert_to_value(self,ctx,message):
] )
if_statement = '}else if'
else:
if ctx.annotate:
rv.extend( [ prefix + '// Skipped {} that is not a value'.format( r.name ) ] )
else_conditions.append( 'x.{} != {} /* {} */'.format( onefield,ref_type_obj.value_for_string(oneval), oneval) )

if if_statement != 'if':
# unclear we should have an else here
# it will create a field with the value of the field, but given it's switch
if True:
rv.append( ' }else{' )
if else_conditions:
rv.extend( annotate_comment('not covered by string switch'.format(r.name), prefix, ctx.annotate) )
rv.append( ' }}else if {} &&'.format(else_conditions[0] ) )
rv.extend( [' ' + x + ' &&' for x in else_conditions[1:-1]] )
rv.append( ' ' + else_conditions[-1] + ' {' )

formula = self.swift_expr_formula(ctx)
rv.extend( [ ' let val : Double = {}'.format( formula ),
' rv[ "{}" ] = val'.format( self.name ),
' }',
] )
else:
rv.append( ' }' )
rv.append( ' }else{' )
formula = self.swift_expr_formula(ctx)
rv.extend( [ ' let val : Double = {}'.format( formula ),
' rv[ "{}" ] = val'.format( self.name ),
' }',
] )

if not something_done:
return []
Expand All @@ -648,7 +671,7 @@ def swift_stmt_case_convert_to_string(self,ctx,message):

for r in self.references:
if not r.reference_field:
print( 'bug', self.name, r.name )
logging.error( 'bug', self.name, r.name )
if r.name in ctx.types:
r_type_obj = ctx.types[r.name]
for (onefield, oneval) in zip( r.reference_field, r.reference_field_value ):
Expand All @@ -670,7 +693,7 @@ def swift_stmt_case_to_string(self,ctx,message):

for r in self.references:
if not r.reference_field:
print( 'bug', self.name, r.name )
logging.error( 'bug', self.name, r.name )
for (onefield, oneval) in zip( r.reference_field, r.reference_field_value ):
rv.extend( [ ' {} strings["{}"] == "{}" {{'.format( if_statement, onefield, oneval ) ,
' return "{}"'.format( r.name ) ] )
Expand Down Expand Up @@ -720,7 +743,7 @@ def objc_stmt_build_references_variables(self,ctx,message):
all_var = dict()
for r in self.references:
if not r.reference_field:
print( 'bug', self.name, r.name )
logging.error( 'bug', self.name, r.name )
for one in r.reference_field:
all_var[ one ] = message.fields_map[one].field_num
rv = first_line_with_annotate_comment(' ',ctx.annotate)
Expand All @@ -739,7 +762,7 @@ def objc_stmt_case_to_string(self,ctx,message):

for r in self.references:
if not r.reference_field:
print( 'bug', self.name, r.name )
logging.error( 'bug', self.name, r.name )
for (onefield, oneval) in zip( r.reference_field, r.reference_field_value ):
rv.extend( [ ' {}( {} == {} ){{ // {} '.format( if_statement, onefield, message.type_for_field(ctx,onefield).value_for_string(oneval),oneval ),
' return @"{}";'.format( r.name ),
Expand All @@ -757,13 +780,13 @@ def objc_stmt_case_to_string(self,ctx,message):
else:
return [ ' case {}: return @"{}";'.format( self.field_num, self.name ) ]

def objc_expr_fit_field_info(self,ctx):
def objc_expr_fit_field_info(self,ctx,pending=False):
rv = None
scale = 0
offset = 0
unit = 0
fit_type = 0
flags = 0
flags = FIT_FLAG_NONE
report = False
if self.scale and isinstance(self.scale, int ):
scale = self.scale
Expand All @@ -774,11 +797,13 @@ def objc_expr_fit_field_info(self,ctx):
if self.type_name and self.type_name in ctx.types:
fit_type = ctx.types[ self.type_name ].type_num
if self.type_name == 'date_time' or self.type_name == 'local_date_time':
flags = 1
flags = FIT_FLAG_DATE
report = True
if self.unit and self.unit in ctx.units:
unit = ctx.units[ self.unit ]
report = True
if pending:
flags |= FIT_FLAG_PENDING
if report:
rv = '(FIT_FIELD_INFO){{.scale = {}, .offset = {}, .fit_type = {}, .fit_unit = {}, .fit_flag = {} }}'.format( scale,offset,fit_type,unit,flags )

Expand All @@ -795,7 +820,7 @@ def objc_stmt_case_to_field_info(self,ctx,message):

for r in self.references:
if not r.reference_field:
print( 'bug', self.name, r.name )
logging.error( 'bug', self.name, r.name )
fit_field_info = r.objc_expr_fit_field_info(ctx)
if fit_field_info:
for (onefield, oneval) in zip( r.reference_field, r.reference_field_value ):
Expand Down Expand Up @@ -1194,7 +1219,7 @@ def parse_profile_excel(self):
for idx,col in enumerate(list(self.types_columns)):
varname = 'TYP_COL_{}'.format(col.upper().replace(' ', '_').replace('#','').replace(':',''))
if eval(varname) != idx:
print( '{} = {} but in spreadsheet is col {}'.format(varname, eval(varname), idx))
logging.error( '{} = {} but in spreadsheet is col {}'.format(varname, eval(varname), idx))

for row in ws_types[1:]:
if len(row)>0 and row[TYP_COL_TYPE_NAME] and row[TYP_COL_BASE_TYPE]:
Expand Down

0 comments on commit a30459d

Please sign in to comment.