-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfitbit_client.go
More file actions
138 lines (128 loc) · 2.85 KB
/
Copy pathfitbit_client.go
File metadata and controls
138 lines (128 loc) · 2.85 KB
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
package main
import (
"encoding/base64"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
const (
CLIENTUUID = "2ea32002-a079-48f4-8020-0badd22939e3"
FITBITHOST = "https://client.fitbit.com"
STARTPATH = "/device/tracker/uploadData"
)
type FitbitConfig struct {
ResponseInfo Response `xml:"response"`
RemoteOps []RemoteOp `xml:"device>remoteOps>remoteOp"`
}
type FitbitClient struct {
*FitbitBase
}
type Response struct {
Body string `xml:",chardata"`
Host string `xml:"host,attr"`
Path string `xml:"path,attr"`
}
type RemoteOp struct {
OpCode string `xml:"opCode"`
PayloadData string `xml:"payloadData"`
}
func (c *FitbitClient) UploadData() error {
//init_tracker_for_transfer
v := url.Values{}
weburl := FITBITHOST + STARTPATH
err := c.InitTrackerForTransfer()
log.Println("end init----------")
if err != nil {
return err
}
defer c.CommandSleep()
client := http.Client{}
v.Set("beaconType", "standard")
v.Set("clientMode", "standard")
v.Set("clientVersion", "1.0")
v.Set("os", "fitbitd")
v.Set("clientId", CLIENTUUID)
for {
log.Println(weburl, v)
resp, err := client.PostForm(weburl, v)
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
config := FitbitConfig{}
if err == nil {
err = xml.Unmarshal(body, &config)
}
resp.Body.Close()
log.Println(string(body))
v, err = url.ParseQuery(config.ResponseInfo.Body)
if err != nil {
return err
}
for i, op := range config.RemoteOps {
opcode, err := base64.StdEncoding.DecodeString(op.OpCode)
payload, err := base64.StdEncoding.DecodeString(op.PayloadData)
code, err := c.RunOpcode(opcode, payload)
if err != nil {
return err
}
resp := base64.StdEncoding.EncodeToString(code)
log.Printf("opCode[%d]: %s, payload: %s, response: %s\n", i, op.OpCode, op.PayloadData, resp)
v.Set(fmt.Sprintf("opResponse[%d]", i), resp)
v.Set(fmt.Sprintf("opStatus[%d]", i), "success")
}
if config.ResponseInfo.Host == "" {
break
}
v.Set("beaconType", "standard")
v.Set("clientMode", "standard")
v.Set("clientVersion", "1.0")
v.Set("os", "fitbitd")
v.Set("clientId", CLIENTUUID)
weburl = "http://" + config.ResponseInfo.Host + config.ResponseInfo.Path
}
return err
}
func (f *FitbitClient) SetBase() error {
fb := &FitbitBase{}
err := fb.Open()
if err == nil {
f.FitbitBase = fb
err = f.SettingUp()
}
return err
}
type SyncTask struct {
exitChannel chan int
}
func (s *SyncTask) Run() {
ticker := time.Tick(time.Second * 600)
for {
c := FitbitClient{}
err := c.SetBase()
if err != nil {
continue
}
log.Println("start sync")
err = c.UploadData()
if err != nil {
log.Println("sync failed")
} else {
log.Println("sync OK")
}
c.Close()
select {
case <-ticker:
continue
case <-s.exitChannel:
return
}
}
}
func (s *SyncTask) Stop() {
close(s.exitChannel)
}