|
|
@@ -37,7 +37,7 @@ class FindBoxCoreSystem
|
|
|
$this->Pass = $Password;
|
|
|
|
|
|
$URI = '/1.0.0/index.php/getstatus/1.0.0/';
|
|
|
- $ret = $this->CallAPI("GET", $URI, "");
|
|
|
+ $ret = $this->CallAPI($URI, "");
|
|
|
|
|
|
if ($ret['err'] == 0 && $ret['value']['code'] == 0) {
|
|
|
$details = $ret['value']['value'];
|
|
|
@@ -54,26 +54,26 @@ class FindBoxCoreSystem
|
|
|
*/
|
|
|
private function returnCode(int $errorCode, string $value): array
|
|
|
{
|
|
|
+ $val = json_decode($value, true) ?? $value;
|
|
|
return array(
|
|
|
"err" => $errorCode,
|
|
|
- "value" => json_decode($value, true));
|
|
|
+ "value" => $val);
|
|
|
}
|
|
|
|
|
|
public function BlinkUnassigned()
|
|
|
{
|
|
|
$URI = "/1.0.0/index.php/smartesl.blink_unassigned/2.0.0/";
|
|
|
- $this->CallAPI("GET", $URI, "", true);
|
|
|
+ $this->CallAPI($URI, "", true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* CallGetAPI make HTTP GET requests
|
|
|
- * @param string $method GET | POST
|
|
|
* @param string $URI full path of api url, for example "/1.0.0/index.php/smartesl.blink_special/1.0.0/"
|
|
|
* @param string $Parameters
|
|
|
* @param bool $skipResponse for faster communications, if response not need, you can skip this step
|
|
|
* @return array converted from JSON response of request if present
|
|
|
*/
|
|
|
- public function CallAPI(string $method, string $URI, string $Parameters, bool $skipResponse = false): array
|
|
|
+ public function CallAPI(string $URI, string $Parameters, bool $skipResponse = false): array
|
|
|
{
|
|
|
$bodyJSON = "{}";
|
|
|
$auth = "Authorization: Basic " . base64_encode("{$this->Key}:{$this->Pass}") . "\r\n";
|
|
|
@@ -83,7 +83,11 @@ class FindBoxCoreSystem
|
|
|
fclose($streamfbOS);
|
|
|
return $this->returnCode($errno, $errstr);
|
|
|
} else {
|
|
|
- fwrite($streamfbOS, "{$method} {$URI}{$Parameters} HTTP/1.0\r\nHost: {$this->Host}\r\n{$auth}Accept: */*\r\nConnection: close\r\n\r\n");
|
|
|
+ //$requestText = "{$method} {$URI}{$Parameters} HTTP/1.0\r\nHost: {$this->Host}\r\n{$auth}Accept: */*\r\nConnection: close\r\n\r\n";
|
|
|
+ $requestBodyLength = strlen($Parameters);
|
|
|
+ $requestText = "POST {$URI} HTTP/1.0\r\nHost: {$this->Host}\r\n{$auth}Accept: */*\r\nContent-Type: application/json\r\nContent-Length: {$requestBodyLength}\r\n\r\n{$Parameters}\r\n";
|
|
|
+ //file_put_contents("request.txt", $requestText);
|
|
|
+ fwrite($streamfbOS, $requestText);
|
|
|
if (!$skipResponse) {
|
|
|
$r = stream_get_contents($streamfbOS);
|
|
|
list(, $bodyJSON) = explode("\r\n\r\n", $r, 2);
|
|
|
@@ -102,16 +106,15 @@ class FindBoxCoreSystem
|
|
|
*/
|
|
|
public function SaveProducts(string $barcode, string $name, array $dataFields): array
|
|
|
{
|
|
|
- $encodeStringsForFindBoxsystem = array_map('rawurlencode', $dataFields);
|
|
|
$product = json_encode([[[
|
|
|
"barcode" => $barcode,
|
|
|
- "name1" => rawurlencode($name),
|
|
|
- "datafields" => $encodeStringsForFindBoxsystem
|
|
|
+ "name1" => urlencode($name),
|
|
|
+ "datafields" => $dataFields
|
|
|
]]]);
|
|
|
|
|
|
$URI = "/1.0.0/index.php/saveproducts/1.2.0/";
|
|
|
|
|
|
- return $this->CallAPI("GET", $URI, $product);
|
|
|
+ return $this->CallAPI($URI, $product);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -125,20 +128,35 @@ class FindBoxCoreSystem
|
|
|
{
|
|
|
$URI = "/1.0.0/index.php/smartesl.bind/1.1.0/";
|
|
|
|
|
|
- return $this->CallAPI("GET", $URI, "[\"{$labelId}\",\"{$barcode}\"]");
|
|
|
+ return $this->CallAPI($URI, "[\"{$labelId}\",\"{$barcode}\"]");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Remove label from findbox system
|
|
|
*
|
|
|
* @param string $labelId
|
|
|
- * @return array
|
|
|
+ * @return bool true if successfull else no barcode exist in findbox articles
|
|
|
*/
|
|
|
public function UnBind(string $labelId): array
|
|
|
{
|
|
|
- $URI = "/1.0.0/index.php/smartesl.unbind/1.0.0/";
|
|
|
-
|
|
|
- return $this->CallAPI("GET", $URI, "[\"{$labelId}\"]");
|
|
|
+ // Retrieve barcode associated to the label
|
|
|
+ $labelDetails = $this->CallAPI("/1.0.0/index.php/smartesl.getstatus/3.1.0/", "[[\"{$labelId}\"]]");
|
|
|
+ // If RSSI exist, label it is binded to system
|
|
|
+ if (isset($labelDetails['value']['value'][$labelId]['rssi'])) {
|
|
|
+ $barcode = $labelDetails['value']['value'][$labelId]['data']['barcode'];
|
|
|
+
|
|
|
+ $ret = $this->CallAPI("/1.0.0/index.php/smartesl.unbind/1.0.0/", "[\"{$labelId}\"]");
|
|
|
+ if ($ret['err'] != 0)
|
|
|
+ return $this->returnCode(1, 'Error on unbind');
|
|
|
+
|
|
|
+ $ret = $this->CallAPI("/1.0.0/index.php/deleteproducts/1.0.0/", "[\"{$barcode}\"]");
|
|
|
+ if ($ret['err'] != 0)
|
|
|
+ return $this->returnCode(1, 'Error on deleteproducts');
|
|
|
+
|
|
|
+ // All ok
|
|
|
+ return $this->returnCode(0, '');
|
|
|
+ } else
|
|
|
+ return $this->returnCode(1, 'No label exist on Findbox');
|
|
|
}
|
|
|
|
|
|
}
|