-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
93 lines (81 loc) · 2.36 KB
/
login.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
package tplinkcloudapi
import (
"encoding/json"
"fmt"
"github.com/nu7hatch/gouuid"
)
var Token, DeviceId string
var Deices map[string]string
type LoginRequest struct {
Method string `json:"method"`
Params `json:"params"`
}
type Params struct {
AppType string `json:"appType"`
CloudUserName string `json:"cloudUserName"`
CloudPassword string `json:"cloudPassword"`
TerminalUUID string `json:"terminalUUID"`
}
type DeviceListRequest struct {
Method string `json:"method"`
ParamsDevice `json:"params"`
}
type ParamsDevice struct {
AppName string `json:"appName"`
TerminalUUID string `json:"termID"`
AppVer string `json:"appVer"`
Ospf string `json:"ospf"`
NetType string `json:"netType"`
Locale string `json:"locale"`
Token string `json:"token"`
}
func Login(cloudUserName string, cloudPassword string) {
u, _ := uuid.NewV4()
loginJSON := &LoginRequest{
Method: "login",
Params: Params{
AppType: "Kasa_Android",
CloudUserName: cloudUserName,
CloudPassword: cloudPassword,
TerminalUUID: u.String(),
},
}
body, _ := json.Marshal(loginJSON)
respBody := Request(body)
var dat map[string]interface{}
if err := json.Unmarshal(respBody, &dat); err != nil {
panic(err)
}
Token = dat["result"].(map[string]interface{})["token"].(string)
deviceList := &DeviceListRequest{
Method: "getDeviceList",
ParamsDevice: ParamsDevice{
AppName: "Kasa_Android",
TerminalUUID: u.String(),
AppVer: "1.4.4.607",
Ospf: "Android+6.0.1",
NetType: "wifi",
Locale: "es_ES",
Token: Token,
},
}
body, _ = json.Marshal(deviceList)
respBody = Request(body)
if err := json.Unmarshal(respBody, &dat); err != nil {
panic(err)
}
respDevMap := dat["result"].(map[string]interface{})["deviceList"].([]interface{})
if len(respDevMap) == 1 {
alias, _ := respDevMap[0].(map[string]interface{})["alias"].(string)
deviceModel, _ := respDevMap[0].(map[string]interface{})["deviceModel"].(string)
fmt.Println("You have 1 device:", alias, deviceModel)
} else {
for idx := range respDevMap {
if alias, ok := respDevMap[idx].(map[string]interface{})["alias"].(string); ok {
deviceId, _ := respDevMap[idx].(map[string]interface{})["deviceId"].(string)
Deices[alias] = deviceId
}
}
}
DeviceId = respDevMap[0].(map[string]interface{})["deviceId"].(string)
}