-
Notifications
You must be signed in to change notification settings - Fork 2
/
sleep.go
60 lines (50 loc) · 1.54 KB
/
sleep.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
package fitbit
import (
"fmt"
"time"
)
// SleepPhase represents a sleep phase
type SleepPhase = string
// Enum of all sleep phases
const (
Awake SleepPhase = "wake"
Deep SleepPhase = "deep"
Light SleepPhase = "light"
REM SleepPhase = "rem"
)
// SleepSummary is the summary of a sleep phase
type SleepSummary struct {
Count int `json:"count"`
Duration MinuteDuration `json:"minutes"`
}
// SleepEvent is a sleep phase in the sleep timeline
type SleepEvent struct {
Date LocalDate `json:"datetime"`
Phase SleepPhase `json:"level"`
Duration SecondDuration `json:"seconds"`
}
// SleepData contains the summary and details for a sleep
type SleepData struct {
Summary map[SleepPhase]SleepSummary `json:"summary"`
Timeline []SleepEvent `json:"data"`
}
// SleepLogEntry represents a single sleep
type SleepLogEntry struct {
ID int64 `json:"logId"`
Duration MillisecondDuration `json:"duration"`
StartDate LocalDate `json:"startTime"`
EndDate LocalDate `json:"endTime"`
Data SleepData `json:"levels"`
}
// GetSleepLogs returns all sleep logs for a specific date
func (client Client) GetSleepLogs(date time.Time) ([]SleepLogEntry, error) {
var result struct {
Logs []SleepLogEntry `json:"sleep"`
}
path := fmt.Sprintf("/1.2/user/-/sleep/date/%s", date.Format(dateLayout))
err := client.getResource(path, nil, &result)
if err != nil {
return nil, err
}
return result.Logs, nil
}