-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
255 lines (218 loc) · 6.09 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"sort"
"strings"
"time"
"sync"
"image/color"
"github.com/PerformLine/go-stockutil/colorutil"
"net/http/httputil"
"github.com/cenkalti/dominantcolor"
"github.com/kbinani/screenshot"
)
var (
Token string
Host = "https://openapi.tuyaeu.com"
ClientID = "__"
Secret = "___"
//PUT your devices in this array
// EXAMPLE:
// (one dev) DeviceID = [...]string{"bf21c1b7582f28e0bf8hgz"}
// (two dev) DeviceID = [...]string{"bfb9f75c7fcccaa01elmox", "bf21c1b7582f28e0bf8hgz"}
// (n dev) DeviceID = [...]string{"bfb9f75c7fcccaa01elmox", "bf21c1b7582f28e0bf8hgz", ..(n - 3).., "aaaaaaaaaaaaaaaaaa"}
DeviceID = [...] string { "bfb9f75c7fcccaa01elmox", "bf21c1b7582f28e0bf8hgz"}
)
type TokenResponse struct {
Result struct {
AccessToken string `json:"access_token"`
ExpireTime int `json:"expire_time"`
RefreshToken string `json:"refresh_token"`
UID string `json:"uid"`
} `json:"result"`
Success bool `json:"success"`
T int64 `json:"t"`
}
func main() {
GetToken()
inputControl()
}
func GetToken() {
method := "GET"
body := []byte(``)
req, _ := http.NewRequest(method, Host+"/v1.0/token?grant_type=1", bytes.NewReader(body))
buildHeader(req, body)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
bs, _ := ioutil.ReadAll(resp.Body)
ret := TokenResponse{}
json.Unmarshal(bs, &ret)
log.Println("resp:", string(bs))
if v := ret.Result.AccessToken; v != "" {
Token = v
}
}
// formatRequest generates ascii representation of a request
func formatRequest(r *http.Request) string {
// Create return string
var request []string // Add the request string
url := fmt.Sprintf("%v %v %v", r.Method, r.URL, r.Proto)
request = append(request, url) // Add the host
request = append(request, fmt.Sprintf("Host: %v", r.Host)) // Loop through headers
for name, headers := range r.Header {
name = strings.ToLower(name)
for _, h := range headers {
request = append(request, fmt.Sprintf("%v: %v", name, h))
}
}
// If this is a POST, add post data
if r.Method == "POST" {
r.ParseForm()
request = append(request, "\n")
request = append(request, r.Form.Encode())
} // Return the request as a string
return strings.Join(request, "\n")
}
// Function to translate named color RGBA to RGB, preserving some A
func rgba2rgb(incolor color.RGBA)(float64, float64, float64) {
var alpha = incolor.A
return float64(255 / alpha * incolor.R),
float64(255 / alpha * incolor.G),
float64(255 / alpha * incolor.B)
}
func inputControl() {
for {
bounds := screenshot.GetDisplayBounds(0)
img, err := screenshot.CaptureRect(bounds)
if err != nil {
panic(err)
}
color := color.RGBA(dominantcolor.Find(img))
hslH, hslS, hslV := colorutil.RgbToHsl(rgba2rgb(color))
c := make(chan string)
var wg sync.WaitGroup
wg.Add(len(DeviceID))
for ii := 0; ii < len(DeviceID); ii++ {
go func(c chan string) {
for {
device, more := <-c
if more == false {
wg.Done()
return
}
sendChangeColorRequest(device, hslH, hslS, hslV)
}
}(c)
}
for _, a := range DeviceID {
c <- a
}
close(c)
wg.Wait()
time.Sleep(500 * time.Millisecond)
}
}
func sendChangeColorRequest(deviceId string, H float64, S float64, V float64) {
hslS := S * 1000
hslV := V * 1000
body := [] byte(fmt.Sprintf(`{
"commands": [
{
"code": "colour_data_v2",
"value": {"h":%d,"s":%d,"v":%d}
}
]
}`, int(H), int(hslS), int(hslV)))
req, _ := http.NewRequest(http.MethodPost, Host + "/v1.0/iot-03/devices/" + deviceId + "/commands", bytes.NewReader(body))
buildHeader(req, body)
log.Println("req:", formatRequest(req))
reqDump, err := httputil.DumpRequestOut(req, true)
if err != nil {
log.Fatal(err)
}
log.Println("REQUEST:\n%s", string(reqDump))
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
bs, _ := ioutil.ReadAll(resp.Body)
log.Println("resp:", string(bs))
}
func buildHeader(req * http.Request, body[] byte) {
req.Header.Set("client_id", ClientID)
req.Header.Set("sign_method", "HMAC-SHA256")
ts := fmt.Sprint(time.Now().UnixNano() / 1e6)
req.Header.Set("t", ts)
if Token != "" {
req.Header.Set("access_token", Token)
}
sign := buildSign(req, body, ts)
req.Header.Set("sign", sign)
}
func buildSign(req * http.Request, body[] byte, t string) string {
headers := getHeaderStr(req)
urlStr := getUrlStr(req)
contentSha256 := Sha256(body)
stringToSign := req.Method + "\n" + contentSha256 + "\n" + headers + "\n" + urlStr
signStr := ClientID + Token + t + stringToSign
sign := strings.ToUpper(HmacSha256(signStr, Secret))
return sign
}
func Sha256(data[] byte) string {
sha256Contain := sha256.New()
sha256Contain.Write(data)
return hex.EncodeToString(sha256Contain.Sum(nil))
}
func getUrlStr(req *http.Request) string {
url := req.URL.Path
keys := make([]string, 0, 10)
query := req.URL.Query()
for key, _ := range query {
keys = append(keys, key)
}
if len(keys) > 0 {
url += "?"
sort.Strings(keys)
for _, keyName := range keys {
value := query.Get(keyName)
url += keyName + "=" + value + "&"
}
}
if url[len(url)-1] == '&' {
url = url[:len(url)-1]
}
return url
}
func getHeaderStr(req *http.Request) string {
signHeaderKeys := req.Header.Get("Signature-Headers")
if signHeaderKeys == "" {
return ""
}
keys := strings.Split(signHeaderKeys, ":")
headers := ""
for _, key := range keys {
headers += key + ":" + req.Header.Get(key) + "\n"
}
return headers
}
func HmacSha256(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
sha := hex.EncodeToString(h.Sum(nil))
return sha
}