Skip to content

Commit

Permalink
Add electrocardiogram support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas2500 committed Aug 13, 2023
1 parent f40363b commit facb3d6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
70 changes: 70 additions & 0 deletions electrocardiogram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package fitbit

import (
"encoding/json"
"errors"
"net/url"
"strconv"
)

// ECG data
type ECGLogList struct {
EcgReadings []struct {
StartTime string `json:"startTime"`
AverageHeartRate int `json:"averageHeartRate"`
ResultClassification string `json:"resultClassification"`
WaveformSamples []int `json:"waveformSamples"`
SamplingFrequencyHz int `json:"samplingFrequencyHz"`
ScalingFactor int `json:"scalingFactor"`
NumberOfWaveformSamples int `json:"numberOfWaveformSamples"`
LeadNumber int `json:"leadNumber"`
FeatureVersion string `json:"featureVersion"`
DeviceName string `json:"deviceName"`
FirmwareVersion string `json:"firmwareVersion"`
} `json:"ecgReadings"`
Pagination struct {
AfterDate string `json:"afterDate"`
Limit int `json:"limit"`
Next string `json:"next"`
Offset int `json:"offset"`
Previous string `json:"previous"`
Sort string `json:"sort"`
} `json:"pagination"`
}

// ECGLog returns the ECG log list
func (m *Session) ECGLog(params LogListParameters) (ECGLogList, error) {
parameterList := url.Values{}

//nolint:gocritic
if params.BeforeDate != "" {
parameterList.Add("beforeDate", params.BeforeDate)
parameterList.Add("sort", "desc")
} else if params.AfterDate != "" {
parameterList.Add("afterDate", params.BeforeDate)
parameterList.Add("sort", "asc")
} else {
return ECGLogList{}, errors.New("beforeDate or afterDate must be given")
}

if params.Limit > 0 {
if params.Limit > 10 {
return ECGLogList{}, errors.New("limit must be 10 or less")
}
parameterList.Add("limit", strconv.Itoa(params.Limit))
}

parameterList.Add("offset", strconv.Itoa(params.Offset))

contents, err := m.makeRequest("https://api.fitbit.com/1/user/-/ecg/list.json?" + parameterList.Encode())
if err != nil {
return ECGLogList{}, err
}

ecgResponse := ECGLogList{}
if err := json.Unmarshal(contents, &ecgResponse); err != nil {
return ECGLogList{}, err
}

return ecgResponse, nil
}
1 change: 1 addition & 0 deletions fitbit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
ScopeActivity Scope = "activity"
ScopeCardioFitness Scope = "cardio_fitness"
ScopeBreathingRate Scope = "respiratory_rate"
ScopeECG Scope = "electrocardiogram"
ScopeHeartrate Scope = "heartrate"
ScopeLocation Scope = "location"
ScopeNutrition Scope = "nutrition"
Expand Down

0 comments on commit facb3d6

Please sign in to comment.