Newer
Older
<?php
namespace SmartData\SmartAPI
{
require_once( __DIR__ . '/Logger.php');
require_once( __DIR__ . '/Backend.php');
require_once( __DIR__ . '/Packer.php');
require_once( __DIR__ . '/MultiSmartData.php');
use SmartData\SmartAPI\Internals\{JsonAPI, BinaryAPI};
use SmartData\Exception\{BadRequestException, RequestFailedException, InsertionFailedException, CreationFailedException};
use SmartData\{Series, SmartData, Backend, Logger, MultiSmartData, Credentials};
use SmartData\Utility\{Pack,Unpack};
function health_check()
{
return Backend::health_check();
}
function search()
{
$json = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);
if (json_last_error() === JSON_ERROR_NONE) {
list($credentials,$series,$parameter) = JsonAPI::parse_search($json);
if ($series instanceof Series) { // is this verification required?
$backend = new Backend($credentials);
switch($series->version) {
case SmartData::STATIC_VERSION:
case SmartData::MOBILE_VERSION:
$return = $backend->search($series,$parameter);
break;
default:
throw new \Exception("Unsupported SmartData version [{$ver}]");
return null;
}
if($return != NULL){
$return = json_encode($return);
$return = preg_replace('/:"([0-9]*)"/',':$1',$return);
} else {
throw new RequestFailedException("Error processing request: null return");
}
}
} else {
throw new BadRequestException("Error parsing content request: invalid series");
}
return $return;
}
function get($content)
{
$json = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);
if (json_last_error() === JSON_ERROR_NONE)
list($credentials,$series,$aggregator,$options) = JsonAPI::parse_get($json);
else
list($credentials,$series,$aggregator,$options) = BinaryAPI::parse_get($content);
if ($series instanceof Series) {
$backend = new Backend($credentials);
switch($series->version) {
case SmartData::STATIC_VERSION:
if ($series->dev== -999999)
$return = $backend->get_activity($series,$aggregator,$options);
else
$return = $backend->query($series,$aggregator,$options);
break;
case SmartData::MOBILE_VERSION:
$return = $backend->track($series,$aggregator,$options);
break;
default:
throw new \Exception("Unsupported SmartData version [{$ver}]");
return null;
}
if($return != NULL){
$return = json_encode($return);
$return = preg_replace('/:"([0-9]*)"/',':$1',$return);
} else {
throw new RequestFailedException("Error processing request: null return");
}
} else {
throw new BadRequestException("Error parsing content request: invalid series");
}
return $return;
}
/*********************
function put_old($content)
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
{
$json = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);
if (json_last_error() === JSON_ERROR_NONE)
list($credentials,$smartdata_array,$params) = JsonAPI::parse_put($json);
else
list($credentials,$smartdata_array,$params) = BinaryAPI::parse_put($content);
$backend = new Backend($credentials);
foreach($smartdata_array as $smartdata) {
//if(REQUEST_CERT == 'A7B64D415BD3E97B' && $smartdata->unit()->is_digital())
//Logger::debug('smartlisha: '.$smartdata, true);
//if($smartdata->unit == 2224466212)
// Logger::debug('vibration: '.$smartdata, true);
//if($smartdata->unit == 2224199972)
// Logger::debug('sound: '.$smartdata, true);
if($smartdata->unit == 6488072){
//Logger::debug('mmmmmm: '.$smartdata, true);
//Logger::var_dump($smartdata->value, true);
//$value = $json->smartdata[0]->value;
//Logger::debug('json: '.$value, true);
//$value = base64_decode($value);
//Logger::debug('decoded: '.$value, true);
//Logger::debug('hex: '.bin2hex($value), true);
//Logger::debug('uint: '.Unpack::d64($value), true);
//$v = Unpack::d64($smartdata->value);
//Logger::debug('::'.$v, true);
}
//if($smartdata->unit == 196612)
// Logger::debug('CARD_READER::: '.$smartdata, true);
//else if($smartdata->unit == 2224180004 && false)
// Logger::debug('POWER::: '.$smartdata, true);
//else if($smartdata->unit == 262148)
// Logger::debug('DOOR::: '.$smartdata, true);
//else if(false)
// Logger::debug('SmartData: '.$smartdata, true);
//Logger::time_elapsed(true);
if(!$backend->insert($smartdata, ...$params)) {
throw new InsertionFailedException("Insertion Failed");
}
//Logger::time_elapsed();
}
}
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
function put($content)
{
$values = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);
if (json_last_error() === JSON_ERROR_NONE) {
if (property_exists( $values, "credentials") ) {
$credentials = Credentials::fromJson( $values->credentials );
$backend = new Backend($credentials);
} else {
$backend = new Backend();
}
if (property_exists( $values, "MultiValueSmartData")) {
$data = MultiSmartData::multi_values($values);
if (!$backend->insert_multiple(...$data)) {
throw new InsertionFailedException("JSON MultiValue SmartData Insertion Failed");
}
} else if (property_exists( $values, "MultiDeviceSmartData")) {
$data = MultiSmartData::multi_devices($values);
if (!$backend->insert_multiple(...$data)) {
throw new InsertionFailedException("JSON MultiValue SmartData Insertion Failed");
}
} else if (property_exists( $values, "MultiUnitSmartData")) {
$data = MultiSmartData::multi_units($values);
$keys = array_keys($data);
foreach ($keys as $k) {
$lst = $data[$k];
if (!$backend->insert_multiple(...$lst)) {
throw new InsertionFailedException("MultiValue SmartData Insertion Failed");
}
}
} else if ( property_exists($values,"smartdata" ) ) {
list($credentials,$smartdata_array,$params) = JsonAPI::parse_put($values);
foreach($smartdata_array as $smartdata) {
if(!$backend->insert($smartdata, ...$params)) {
throw new InsertionFailedException("Insertion Failed");
}
}
} else {
throw new InsertionFailedException("No a found...");
}
} else {
// Is Binary
list($credentials,$smartdata_array,$params) = BinaryAPI::parse_put($content);
if(!$backend->insert($smartdata, ...$params)) {
throw new InsertionFailedException("Insertion Failed");
}
}
}
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
function create($content)
{
$json = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);
if (json_last_error() === JSON_ERROR_NONE)
list($credentials,$series,$params) = JsonAPI::parse_create($json);
else
list($credentials,$series,$params) = BinaryAPI::parse_create($content);
if ($series instanceof Series) {
Logger::debug("series ".$series, true);
$backend = new Backend($credentials);
if(!$backend->create($series, ...$params)){
throw new CreationFailedException("Creation Failed");
}
} else {
throw new BadRequestException("Error parsing content request: invalid series");
}
}
function attach($content)
{
$json = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);
if (json_last_error() === JSON_ERROR_NONE)
list($credentials,$series,$params) = JsonAPI::parse_attach($json);
else
list($credentials,$series,$params) = BinaryAPI::parse_attach($content);
if ($series instanceof Series) {
$backend = new Backend($credentials);
if(!$backend->attach($series, ...$params)){
throw new CreationFailedException("Attachment Failed");
}
} else {
throw new BadRequestException("Error parsing content request: invalid series");
}
}
function batch($content)
{
$json = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);
if (json_last_error() === JSON_ERROR_NONE)
throw new Exception\NotImplementedException("JSON-based import in batch has not been implemented yet");
else{
//list($credentials,$smartdata_array,$params) = BinaryAPI::parse_put($content);
$binary = $content;
$credentials = null;
if(!REQUEST_CERT){
$credentials = Credentials::unpack($binary);
if(!$credentials){
throw new BadRequestException("!CERT > Credentials cannot be null");
}
Logger::debug("sem CRT", true);
}else{
Logger::debug("com CRT", true);
}
$series = Series::unpack($binary);
$csdf = array();
$smartdata_array = array();
while($binary) {
$smartdata = SmartData::unpack($binary, $csdf);
//Logger::debug($smartdata, true);
if($smartdata instanceof SmartData)
array_push($smartdata_array, $smartdata);
}
$params = array();
$backend = new Backend($credentials);
if(!$backend->insertBatch($series, ...$smartdata_array)) {
Logger::debug("failure", true);
throw new Exception\InsertionFailedException("Insertion Failed");
}
}
}
}
namespace SmartData\SmartAPI\Internals
{
use SmartData\{Series,SmartData,Credentials,Logger};
trait JsonAPI
{
public function parse_get($json)
{
$credentials = null;
if(isset($json->credentials))
$credentials = Credentials::fromJson($json->credentials);
$series = Series::fromJson($json->series);
$aggregator = $json->aggregator ?? null;
$options = $json->options?? null;
return array($credentials, $series, $aggregator, $options);
}
public function parse_put($json)
{
////Logger::debug('JsonAPI', true);
$credentials = null;
$params = $smartdata_array = array();
if(isset($json->credentials))
$credentials = Credentials::fromJson($json->credentials);
$smartdata_set = $json->smartdata;
foreach($smartdata_set as $smartdata_fields) {
$smartdata = SmartData::fromJson($smartdata_fields);
if($smartdata instanceof SmartData)
array_push($smartdata_array, $smartdata);
}
return array($credentials, $smartdata_array, $params);
}
public function parse_create($json)
{
////Logger::debug('JsonAPI', true);
$credentials = null;
$params = array();
if(isset($json->credentials))
$credentials = Credentials::fromJson($json->credentials);
$series = Series::fromJson($json->series);
$aggregator = array($json->aggregator??null);
return array($credentials, $series, $params);
}
public function parse_attach($json)
{
////Logger::debug('JsonAPI', true);
$credentials = null;
$params = array();
if(isset($json->credentials))
$credentials = Credentials::fromJson($json->credentials);
$series = Series::fromJson($json->series);
$aggregator = array($json->aggregator??null);
return array($credentials, $series, $params);
}
public function parse_search($json)
{
$credentials = null;
if(isset($json->credentials))
$credentials = Credentials::fromJson($json->credentials);
$series = Series::fromJson($json->series);
$parameter = $json->search->parameter??null;
return array($credentials, $series, $parameter);
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
}
}
namespace SmartData\SmartAPI\Internals
{
use SmartData\Exception\{BadRequestException};
use SmartData\{Series,SmartData,Credentials,Logger};
use SmartData\Utility\{Unpack};
trait BinaryAPI
{
public function parse_get($binary)
{
//Logger::debug('BinaryAPI', true);
$credentials = null;
$series = Series::unpack($binary);
$params = array();
$options = array();
return array($credentials, $series, $params, $options);
}
public function parse_put($binary)
{
//Logger::debug('BinaryAPI', true);
$credentials = null;
if(!REQUEST_CERT){
$credentials = Credentials::unpack($binary);
if(!$credentials){
throw new BadRequestException("!CERT > Credentials cannot be null");
}
}
// Common SmartData Fields Bitmap
// ------7-----------6-----------5-----------4-----------3-----------2-----------1-----------0-------
// | version | unit | error | confidence | x | y | z | dev |
// --< 1 bit >---< 1 bit >---< 1 bit >---< 1 bit >---< 1 bit >---< 1 bit >---< 1 bit >---< 1 bit >---
$csdf = array();
if(isset($_GET['csdf'])){
$csdf_bitmap = intval($_GET['csdf']);
if(($csdf_bitmap & (0b1 << 7)) != 0) $csdf = array_merge($csdf, array('version' => Unpack::uInt8($binary, true)));
if(($csdf_bitmap & (0b1 << 6)) != 0) $csdf = array_merge($csdf, array('unit' => Unpack::uInt32($binary, true)));
if(($csdf_bitmap & (0b1 << 5)) != 0) $csdf = array_merge($csdf, array('error' => Unpack::uInt8($binary, true)));
if(($csdf_bitmap & (0b1 << 4)) != 0) $csdf = array_merge($csdf, array('confidence' => Unpack::uInt8($binary, true)));
if(($csdf_bitmap & (0b1 << 3)) != 0) $csdf = array_merge($csdf, array('x' => Unpack::int32($binary, true)));
if(($csdf_bitmap & (0b1 << 2)) != 0) $csdf = array_merge($csdf, array('y' => Unpack::int32($binary, true)));
if(($csdf_bitmap & (0b1 << 1)) != 0) $csdf = array_merge($csdf, array('z' => Unpack::int32($binary, true)));
if(($csdf_bitmap & (0b1 << 0)) != 0) $csdf = array_merge($csdf, array('dev' => Unpack::uint32($binary, true)));
}
$smartdata_array = array();
while($binary) {
$smartdata = SmartData::unpack($binary, $csdf);
if($smartdata instanceof SmartData)
array_push($smartdata_array, $smartdata);
}
$params = array();
return array($credentials, $smartdata_array, $params);
}
public function parse_create($binary)
{
//Logger::debug('BinaryAPI', true);
$credentials = null;
if(!REQUEST_CERT){
$credentials = Credentials::unpack($binary);
if(!$credentials){
throw new BadRequestException("!CERT > Credentials cannot be null");
}
}
$series = Series::unpack($binary);
$params = array();
return array($credentials, $series, $params);
}
public function parse_attach($binary)
{
//Logger::debug('BinaryAPI', true);
$credentials = null;
if(!REQUEST_CERT){
$credentials = Credentials::unpack($binary);
if(!$credentials){
throw new BadRequestException("!CERT > Credentials cannot be null");
}
}
$series = Series::unpack($binary);
$params = array();
return array($credentials, $series, $params);
}
}
}