-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnectTuya.php
205 lines (180 loc) · 5.32 KB
/
ConnectTuya.php
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
<?php
/**
* Php connect tuya sample
* Contact me [email protected]
* @version 1.0
*/
class ConnectTuya
{
private $_baseUrl;
private $_deviceId;
private $_clientId;
private $_secret_key;
private $_getToken;
private $_fDevices;
private $_t;
public function __construct()
{
// code...
$this->_baseUrl = ''; # url of your data center Ex: https://openapi.tuyaus.com/
$this->_clientId = '';
$this->_secret_key = '';
$this->_getToken = "v1.0/token?grant_type=1";
$this->_t = round(microtime(true) * 1000);
$this->_fDevices = 'v1.0/iot-03/devices/';
}
/**
* Get access token
*
* @author hao.nguyen <[email protected]>
* @return Array response access_token of request
*/
public function get_token()
{
$headers = array(
'sign_method: HMAC-SHA256',
'client_id: '.$this->_clientId,
't: '.$this->_t,
'Content-Type: application/json',
'access_token:'
);
$url = $this->_baseUrl . $this->_getToken;
$sign = $this->build_sign('GET', $url, [], []);
$headers[] = "sign: " . $sign;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}
/**
* Get device specification
*
* @param String $device_id device id
* @param String $access_token Access_token from get_token function
*
* @author hao.nguyen <[email protected]>
* @return Array response specification of request
*/
public function get_specification($device_id, $access_token)
{
$headers = array(
'sign_method: HMAC-SHA256',
'client_id: '.$this->_clientId,
't: '.$this->_t,
'Content-Type: application/json',
'access_token: ' . $access_token
);
$method = 'GET';
$payload = array(
'device_id' => $device_id
);
$url = $this->_baseUrl . $this->_fDevices . $device_id . '/specification';
$sign = $this->build_sign($method, $url, [], [], $access_token);
$headers[] = "sign: " . $sign;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}
/**
* Send command by device
*
* @param String $device_id device id
* @param Array $commands array of commands
* @param String $access_token Access_token from get_token function
*
* @author hao.nguyen <[email protected]>
* @return Array response specification of request
*/
public function send_commands($device_id, $commands ,$access_token)
{
$headers = array(
'sign_method: HMAC-SHA256',
'client_id: '.$this->_clientId,
't: '.$this->_t,
'Content-Type: application/json',
'access_token: ' . $access_token
);
$method = 'POST';
if (empty($commands)) {
// code...
return false;
}
$payload = array(
"commands" => $commands
);
$url = $this->_baseUrl . $this->_fDevices . $device_id . '/commands';
$sign = $this->build_sign($method, $url, $payload, [], $access_token);
$headers[] = "sign: " . $sign;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS =>json_encode($payload),
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}
/**
* Genarator sign for request
*
* @param String $method POST or GET
* @param String $url url of request
* @param Array $payload body of request
* @param Array $headers headers of request, !!! if error put it empty array
* @param String $token access_token
*
* @author hao.nguyen <[email protected]>
* @return hash sign
*/
private function build_sign($method, $url ,$payload, $headers, $token = "")
{
$str_header = "";
if ($headers) {
// code...
$str_header = implode("\n", $headers);
}
$str_payload = "";
if ($payload) {
// code...
$str_payload = json_encode($payload);
}
$content_SHA256 = hash('sha256', $str_payload);
$parse_url = parse_url($url);
$part_url = empty($parse_url['path']) ? "" : $parse_url['path'];
$query_url = empty($parse_url['query']) ? "" : "?" . $parse_url['query'];
$str_to_sign = $method . "\n" . $content_SHA256 . "\n" . $str_header . "\n" . $part_url . $query_url;
$sign = strtoupper(hash_hmac('SHA256', $this->_clientId . $token . $this->_t . $str_to_sign, $this->_secret_key));
return $sign;
}
}