Skip to content

Commit

Permalink
Refactor to bone mass and bone percent as base units
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkroell committed Feb 6, 2022
1 parent 7558e35 commit 89c6d7a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
24 changes: 5 additions & 19 deletions bodycomposition.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (bc BodyComposition) writeFitFile(writer io.Writer) error {
Weight: fit.Weight(bc.Weight * 100),
PercentFat: uint16(bc.PercentFat * 100),
PercentHydration: uint16(bc.PercentHydration * 100),
BoneMass: uint16(bc.BoneMass),
MuscleMass: uint16(bc.MuscleMass),
BoneMass: uint16(bc.BoneMass * 100),
MuscleMass: uint16(bc.MuscleMass * 100),
VisceralFatRating: uint8(bc.VisceralFatRating),
PhysiqueRating: uint8(bc.PhysiqueRating),
MetabolicAge: uint8(bc.MetabolicAge),
Expand All @@ -54,33 +54,19 @@ func (bc BodyComposition) writeFitFile(writer io.Writer) error {
}

// NewBodyComposition creates a new BodyComposition instance
func NewBodyComposition(weight, percentFat, percentHydration, percentBone, boneMass, percentMuscle, muscleMass ,visceralFatRating, physiqueRating, metabolicAge, caloriesActiveMet, bmi float64, timestamp int64) BodyComposition {
func NewBodyComposition(weight, percentFat, percentHydration, boneMass, muscleMass, visceralFatRating, physiqueRating, metabolicAge, caloriesActiveMet, bmi float64, timestamp int64) BodyComposition {
ts := time.Now()
if timestamp != -1 {
ts = time.Unix(timestamp, 0)
}
var bm float64
var mm float64

if(percentBone != 0 ) {
bm = weight * percentBone
} else {
bm = boneMass * 100
}

if(percentMuscle != 0 ) {
mm = weight * percentMuscle
} else {
mm = muscleMass * 100
}

return BodyComposition{
TimeStamp: ts,
Weight: weight,
PercentFat: percentFat,
PercentHydration: percentHydration,
BoneMass: bm,
MuscleMass: mm,
BoneMass: boneMass,
MuscleMass: muscleMass,
VisceralFatRating: visceralFatRating,
PhysiqueRating: physiqueRating,
MetabolicAge: metabolicAge,
Expand Down
4 changes: 2 additions & 2 deletions bodycomposition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func TestNewBodyCompositionWithDefault(t *testing.T) {

bc := NewBodyComposition(80, 14.4, 55.2, 37, 2.98, 45.5, 55, 21, 5, 23, 2250, 12.6, -1)
bc := NewBodyComposition(80, 14.4, 55.2, 37, 45.5, 21, 5, 23, 2250, 12.6, -1)

now := time.Now()

Expand All @@ -26,7 +26,7 @@ func TestNewBodyComposition(t *testing.T) {
// set local time to UTC, no matter where executed
time.Local = time.UTC

bc := NewBodyComposition(80, 14.4, 55.2, 37, 2.98, 45.5, 55, 21, 5, 23, 2250, 12.6, timeStamp)
bc := NewBodyComposition(80, 14.4, 55.2, 37, 45.5, 21, 5, 23, 2250, 12.6, timeStamp)

year, month, date := bc.TimeStamp.Date()
hour, min, sec := bc.TimeStamp.Clock()
Expand Down
35 changes: 30 additions & 5 deletions cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,32 @@ var uploadCmd = &cobra.Command{
calories, _ := flags.GetFloat64("calories")
bmi, _ := flags.GetFloat64("bmi")

bc := bodycomposition.NewBodyComposition(weight, fat, hydration, bone, boneKg, muscle, muscleKg, visceralFat, physiqueRating, metabolicAge, calories, bmi, ts)
var boneMass float64
var muscleMass float64

if bone != -1 && boneKg != -1 {
cmd.PrintErrf("Cannot provide bone weight in percent and bone mass in kg! Use either of both!")
os.Exit(1)
}

if bone != -1 {
boneMass = weight * bone / 100
} else {
boneMass = boneKg
}

if muscle != -1 && muscleKg != -1 {
cmd.PrintErrf("Cannot provide muscle weight in percent and muscle mass in kg! Use either of both!")
os.Exit(1)
}

if muscle != -1 {
muscleMass = weight * muscle / 100
} else {
muscleMass = muscleKg
}

bc := bodycomposition.NewBodyComposition(weight, fat, hydration, boneMass, muscleMass, visceralFat, physiqueRating, metabolicAge, calories, bmi, ts)

email, _ := cmd.Flags().GetString("email")
password, _ := cmd.Flags().GetString("password")
Expand Down Expand Up @@ -71,10 +96,10 @@ func init() {
flags.Float64P("weight", "w", -1, "Set your weight in kilograms")
flags.Float64P("fat", "f", 0, "Set your fat in percent")
flags.Float64("hydration", 0, "Set your hydration in percent")
flags.Float64P("bone", "b", 0, "Set your bone mass in percent")
flags.Float64("bone-mass", 0, "Set your bone mass in kilograms")
flags.Float64P("muscle", "m", 0, "Set your muscle mass in percent")
flags.Float64("muscle-mass", 0, "Set your muscle mass in kilograms")
flags.Float64P("bone", "b", -1, "Set your bone mass in percent")
flags.Float64("bone-mass", -1, "Set your bone mass in kilograms")
flags.Float64P("muscle", "m", -1, "Set your muscle mass in percent")
flags.Float64("muscle-mass", -1, "Set your muscle mass in kilograms")
flags.Float64P("calories", "c", 0, "Set your caloric intake")
flags.Float64("visceral-fat", 0, "Set your visceral fat rating (valid values: 1-60)")
flags.Float64("metabolic-age", 0, "Set your metabolic age")
Expand Down
2 changes: 1 addition & 1 deletion uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestUploadWeightToGarminIntegration(t *testing.T) {
t.Fatalf("Environment variable %s was not set. This is required.", garminPasswordEnvKey)
}

var bc = NewBodyComposition(80, 14.4, 55.2, 37, 2.98, 45.5, 55, 21, 5, 23, 2250, 12.6, -1)
bc := NewBodyComposition(80, 14.4, 55.2, 37, 45.5, 21, 5, 23, 2250, 12.6, -1)

err := Upload(email, passWord, bc)

Expand Down

0 comments on commit 89c6d7a

Please sign in to comment.