From facb3d6ffccb171e23b8d2269e1abcb7c531c5d4 Mon Sep 17 00:00:00 2001 From: Thomas Bella Date: Sun, 13 Aug 2023 17:26:43 +0200 Subject: [PATCH] Add electrocardiogram support --- electrocardiogram.go | 70 ++++++++++++++++++++++++++++++++++++++++++++ fitbit.go | 1 + 2 files changed, 71 insertions(+) create mode 100644 electrocardiogram.go diff --git a/electrocardiogram.go b/electrocardiogram.go new file mode 100644 index 0000000..5009638 --- /dev/null +++ b/electrocardiogram.go @@ -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 +} diff --git a/fitbit.go b/fitbit.go index 95cbc6c..11bfb08 100644 --- a/fitbit.go +++ b/fitbit.go @@ -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"