forked from davidkroell/bodycomposition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (88 loc) · 2.41 KB
/
main.go
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
package bodycomposition
import (
"encoding/binary"
"github.com/abrander/garmin-connect"
"github.com/tormoder/fit"
"io"
"time"
)
type BodyComposition struct {
TimeStamp time.Time
Weight float64
PercentFat float64
PercentHydration float64
PercentBone float64
PercentMuscle float64
}
func (bc BodyComposition) writeFitFile(writer io.Writer) error {
weightfile := fit.WeightFile{
UserProfile: nil,
WeightScales: []*fit.WeightScaleMsg{
{
Timestamp: bc.TimeStamp,
Weight: fit.Weight(bc.Weight * 100),
PercentFat: uint16(bc.PercentFat * 100),
PercentHydration: uint16(bc.PercentHydration * 100),
BoneMass: uint16(bc.Weight * bc.PercentBone),
MuscleMass: uint16(bc.Weight * bc.PercentMuscle),
},
},
}
fitfile := fit.File{
FileId: struct {
Type fit.FileType
Manufacturer fit.Manufacturer
Product uint16
SerialNumber uint32
TimeCreated time.Time
Number uint16
ProductName string
}{Type: fit.FileTypeWeight, Manufacturer: fit.ManufacturerTanita},
Header: struct {
Size byte
ProtocolVersion byte
ProfileVersion uint16
DataSize uint32
DataType [4]byte
CRC uint16
}{Size: 14, ProtocolVersion: 16, ProfileVersion: 2092, DataType: [4]byte{46, 70, 73, 84}},
}
err := fitfile.SetWeight(&weightfile)
if err != nil {
panic(err)
}
return fit.Encode(writer, &fitfile, binary.BigEndian)
}
func (bc BodyComposition) uploadFitFile(reader io.Reader, email string, password string) bool {
client := connect.NewClient(connect.Credentials(email, password))
_, err := client.ImportActivity(reader, connect.ActivityFormatFIT)
if err != nil {
panic(err)
}
return true
}
func (bc BodyComposition) UploadWeight(email, password string) {
reader, writer := io.Pipe()
go func() {
defer writer.Close()
err := bc.writeFitFile(writer)
if err != nil {
panic(err)
}
}()
bc.uploadFitFile(reader, email, password)
}
func NewBodyComposition(weight, percentFat, percentHydration, percentBone, percentMuscle float64, timestamp int64) BodyComposition {
ts := time.Now()
if timestamp != -1 {
ts = time.Unix(timestamp, 0)
}
return BodyComposition{
TimeStamp: ts,
Weight: weight,
PercentFat: percentFat,
PercentHydration: percentHydration,
PercentBone: percentBone,
PercentMuscle: percentMuscle,
}
}