-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
149 lines (126 loc) · 3.1 KB
/
client.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 miio
import (
"encoding/json"
"fmt"
"runtime"
"sync"
"github.com/ofen/miio-go/proto"
)
// Client is device client that extends protocol connection.
type Client struct {
sync.Mutex
proto.Conn
requestID int
}
// New creates new device client.
//
// Example:
// New("192.168.0.3:54321")
func New(addr string) *Client {
conn, err := proto.Dial(addr, nil)
if err != nil {
panic(err)
}
client := &Client{sync.Mutex{}, conn, 1}
runtime.SetFinalizer(client, (*client).Close)
return client
}
// Send sends request to device.
func (c *Client) Send(method string, params interface{}) ([]byte, error) {
req := struct {
RequestID int `json:"id"`
Method string `json:"method"`
Params interface{} `json:"params"`
}{
RequestID: c.requestID,
Method: method,
Params: params,
}
payload, err := json.Marshal(req)
if err != nil {
return nil, err
}
if _, err := c.Write(payload); err != nil {
return nil, err
}
resp := make([]byte, 4096)
n, err := c.Read(resp)
if err != nil {
return nil, err
}
if err == nil {
c.Lock()
c.requestID++
c.Unlock()
}
return resp[:n], nil
// // trim non-printable characters
// return bytes.TrimFunc(resp[:n], func(r rune) bool {
// return !unicode.IsGraphic(r)
// }), err
}
// ConfigRouter configures wifi network on device.
func (c *Client) ConfigRouter(ssid string, passwd string, uid string) ([]byte, error) {
v := struct {
SSID string `json:"ssid"`
Passwd string `json:"passwd"`
UID string `json:"uid"`
}{
SSID: ssid,
Passwd: passwd,
UID: uid,
}
return c.Send("miIO.config_router", v)
}
// Info requests device info.
func (c *Client) Info() ([]byte, error) {
return c.Send("miIO.info", nil)
}
// OTAProgress requests OTA update progress.
func (c *Client) OTAProgress() ([]byte, error) {
return c.Send("miIO.get_ota_progress", nil)
}
// OTAState requests available update for device.
func (c *Client) OTAState() ([]byte, error) {
return c.Send("miIO.get_ota_state", nil)
}
// OTA updates the device.
func (c *Client) OTA(url string, fileMD5 string) ([]byte, error) {
v := struct {
Mode string `json:"mode"`
Install string `json:"install"`
AppURL string `json:"app_url"`
FileMD5 string `json:"file_md5"`
Proc string `json:"proc"`
}{
Mode: "normal",
Install: "1",
AppURL: url,
FileMD5: fileMD5,
Proc: "dnld install",
}
return c.Send("miIO.ota", v)
}
// GetProperties gets device propetriest.
func (c *Client) GetProperties(params []map[string]interface{}) ([]byte, error) {
return c.Send("get_properties", params)
}
// SetProperties sets device propetriest.
func (c *Client) SetProperties(params []map[string]interface{}) ([]byte, error) {
return c.Send("set_properties", params)
}
// Action execute device action.
func (c *Client) Action(siid int, aiid int, params []interface{}) ([]byte, error) {
v := struct {
DID string `json:"did"`
SIID int `json:"siid"`
AIID int `json:"aiid"`
In []interface{} `json:"in"`
}{
DID: fmt.Sprintf("%d-%d", siid, aiid),
SIID: siid,
AIID: aiid,
In: params,
}
return c.Send("action", v)
}