-
Notifications
You must be signed in to change notification settings - Fork 0
/
thingdomAPI.php
208 lines (167 loc) · 5.45 KB
/
thingdomAPI.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
206
207
208
<?php
/**
* Thingdom API PHP Wrapper
*
* @author Andrew Frenz <[email protected]>
* @author Nicholas Kreidberg <[email protected]>
* @copyright 2014-2015 MTS Systems
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 1.1b (Modified specifically for integration with WordPress)
* @link https://github.com/thingdomio/php-thingdom
* @website https://thingdom.io
*/
class Thingdom
{
const API_URL = 'https://api.thingdom.io/1.1/';
const API_ERROR = 'error';
const API_SUCCESS = 'success';
private $apiSecret = '2VTUBkdUDGEc3ektwAeFrNbrWDXgExtS7u6JnBkw59JDU8YANEGrNJntCE77tzSSnVnN8btEhZbqkXs49ZEc75dFspbEuR2Mn5v5';
public $token;
public $lastError = null;
//
// Constructor / Public Methods
//
public function __construct()
{
if(!$this->checkCurl()) {
throw new Exception('cURL is not enabled on this server');
}
}
public function authenticate($deviceSecret = '')
{
$data = array(
'api_secret' => $this->apiSecret,
'device_secret' => $deviceSecret
);
$response = $this->postToThingdom($endpoint = "token", $data);
$this->token = $response['application_token'];
if(empty($deviceSecret)) {
return $response['device_secret'];
}
return $deviceSecret;
}
public function getThing($name, $product_type = '')
{
// if we don't have a token we can't look up a thing
if(empty($this->token)) {
$this->lastError = "Invalid API key.";
}
$data = array(
'token' => $this->token,
'product_type' => $product_type,
'name' => $name
);
$response = $this->postToThingdom("thing", $data);
if(empty($response['thing_id'])) {
// failed to look up thing, set instance flag and
// return empty Thing object
$thing = (object) array(
'id' => -1,
'code' => '',
'token' => '',
'lastError' => '',
'name' => ''
);
} else {
// Instantiate new Thing object and return it back to the caller
$thing = new Thing($response['thing_id'], $response['code'], $this->token);
}
return $thing;
}
public function postToThingdom($endpoint, $data)
{
$ch = curl_init();
$curl_options = array(
CURLOPT_URL => self::API_URL.$endpoint,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => array('Content-Type: application/json', '', 'Accept: application/json'),
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
);
curl_setopt_array($ch, $curl_options);
$result = curl_exec ($ch);
$errMsg = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
if($errMsg) {
$response = array('response' => 'error', 'msg' => $errMsg);
} else if($httpCode != 200) {
$response = array('response' => 'error', 'msg' => $result);
} else {
$response = json_decode($result, true);
}
return $response;
}
//
// Private Methods
//
private function checkCurl()
{
return function_exists('curl_version');
}
}
class Thing extends Thingdom {
public $id;
public $code;
public $token;
public $lastError = null;
protected $name;
protected $display_name;
//
// Constructor / Public Methods
//
public function __construct($thingId, $code, $token)
{
$this->id = $thingId;
$this->token = $token;
$this->code = $code;
}
public function feed($category, $message)
{
$data = array(
'token' => $this->token,
'thing_id' => $this->id,
'feed_category' => $category,
'message' => $message,
'options' => null
);
$response = parent::postToThingdom("feed", $data);
if($response['response'] != parent::API_SUCCESS) {
$this->lastError = $response['msg'];
}
return $response;
}
public function status($key, $value, $unit = '')
{
$data = array(
'token' => $this->token,
'thing_id' => $this->id,
'status_array' => array(
array(
'name' => $key,
'value'=> $value,
'unit' => $unit
)
)
);
$response = parent::postToThingdom("status", $data);
if($response['response'] != parent::API_SUCCESS) {
$this->lastError = $response['msg'];
}
return $response;
}
public function statusArray($statusArray)
{
$data = array(
'token' => $this->token,
'thing_id' => $this->id,
'status_array' => $statusArray
);
$response = parent::postToThingdom('status', $data);
if($response['response'] != parent::API_SUCCESS) {
$this->lastError = $response['msg'];
}
return $response;
}
}