-
Notifications
You must be signed in to change notification settings - Fork 0
/
dexcom.go
149 lines (133 loc) · 3.46 KB
/
dexcom.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package dexgo
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"time"
)
const dexcomBaseUrl = "https://share2.dexcom.com/ShareWebServices/Services"
const dexcomLoginEndpoint = "General/LoginPublisherAccountById"
const dexcomAuthEndpoint = "General/AuthenticatePublisherAccount"
const dexcomGetLatestEndpoint = "Publisher/ReadPublisherLatestGlucoseValues"
const dexcomApplicationId = "d89443d2-327c-4a6f-89e5-496bbb0317db"
var timestampRegex = regexp.MustCompile(`Date\((\d*)\)`)
type GlucoseReading struct {
Time time.Time
Value int
Trend string
}
type VerifyPayload struct {
AccountName string `json:"accountName"`
Password string `json:"password"`
ApplicationId string `json:"applicationId"`
}
type AuthPayload struct {
AccountId string `json:"accountId"`
Password string `json:"password"`
ApplicationId string `json:"applicationId"`
}
type GetReadingsPayload struct {
SessionId string `json:"sessionId"`
Minutes int `json:"minutes"`
MaxCount int `json:"maxCount"`
}
type RawReading struct {
Time string `json:"WT"`
Trend string `json:"Trend"`
Value int `json:"Value"`
}
type Dexcom struct {
username string
password string
accountId *string
sessionId *string
}
func New(username string, password string) Dexcom {
return Dexcom{username: username, password: password}
}
func request(endPoint string, payload any, result interface{}) error {
payloadJSON, err := json.Marshal(payload)
if err != nil {
return err
}
authUrl, _ := url.JoinPath(dexcomBaseUrl, endPoint)
resp, err := http.Post(authUrl, "application/json", bytes.NewBuffer(payloadJSON))
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, result)
return err
}
func (d *Dexcom) fetchAccountId() error {
payload := VerifyPayload{AccountName: d.username, Password: d.password, ApplicationId: dexcomApplicationId}
var accountId string
err := request(dexcomAuthEndpoint, payload, &accountId)
if err != nil {
return err
}
d.accountId = &accountId
return nil
}
func (d *Dexcom) auth() error {
payload := AuthPayload{AccountId: *d.accountId, Password: d.password, ApplicationId: dexcomApplicationId}
var sessionId string
err := request(dexcomLoginEndpoint, payload, &sessionId)
if err != nil {
return err
}
d.sessionId = &sessionId
return nil
}
func convertTimestamp(wt string) (time.Time, error) {
matches := timestampRegex.FindStringSubmatch(wt)
if len(matches) != 2 {
return time.Time{}, fmt.Errorf("failed to parse timestamp: %s", wt)
}
timeMillis, err := strconv.Atoi(matches[1])
if err != nil {
return time.Time{}, fmt.Errorf("invalid timestamp: %s", matches[1])
}
return time.UnixMilli(int64(timeMillis)), nil
}
func (d *Dexcom) GetReadings(minutes int, numReadings int) ([]GlucoseReading, error) {
if d.sessionId == nil {
if d.accountId == nil {
d.fetchAccountId()
}
d.auth()
}
payload := GetReadingsPayload{
SessionId: *d.sessionId,
Minutes: minutes,
MaxCount: numReadings,
}
var rawValues []RawReading
err := request(dexcomGetLatestEndpoint, payload, &rawValues)
if err != nil {
return nil, err
}
var readings []GlucoseReading
for _, rv := range rawValues {
timestamp, err := convertTimestamp(rv.Time)
if err != nil {
return nil, err
}
r := GlucoseReading{
Trend: rv.Trend,
Value: rv.Value,
Time: timestamp,
}
readings = append(readings, r)
}
return readings, nil
}