From 352827e075e2d92ad6f06c1f7042e207b4949e8c Mon Sep 17 00:00:00 2001
From: root <root@sv3.lisha.ufsc.br>
Date: Fri, 24 Nov 2023 00:37:22 -0300
Subject: [PATCH] Optimizing memory use - fix #3

---
 api/describe.php           | 24 ++++++++++++++++++++++++
 api/finish.php             | 23 +++++++++++++++++++++++
 api/list.php               | 25 +++++++++++++++++++++++++
 api/login.html             | 22 ++++++++++++++++++++++
 api/nop.php                | 28 ++++++++++++++++++++++++++++
 bin/smartdata/SmartAPI.php |  6 ++----
 6 files changed, 124 insertions(+), 4 deletions(-)
 create mode 100644 api/describe.php
 create mode 100644 api/finish.php
 create mode 100644 api/list.php
 create mode 100644 api/login.html
 create mode 100644 api/nop.php

diff --git a/api/describe.php b/api/describe.php
new file mode 100644
index 0000000..b65095c
--- /dev/null
+++ b/api/describe.php
@@ -0,0 +1,24 @@
+<?php
+namespace SmartData;
+
+require_once('../bin/smartdata/SmartAPI.php');
+use function SmartData\SmartAPI\describe;
+use SmartData\Exception\CustomException;
+use SmartData\Logger;
+
+http_response_code(HttpStatusCode::BAD_REQUEST);
+
+try {
+    $content = file_get_contents('php://input');
+    $response = describe($content);
+}catch(CustomException $e){
+    http_response_code($e->getHTTPCodeError());
+    Logger::exception($e);
+    return false;
+}catch(\Exception $e){
+    http_response_code(HttpStatusCode::BAD_REQUEST);
+    Logger::exception($e);
+    return false;
+}
+http_response_code(HttpStatusCode::NO_CONTENT);
+echo $response;
diff --git a/api/finish.php b/api/finish.php
new file mode 100644
index 0000000..895d5f4
--- /dev/null
+++ b/api/finish.php
@@ -0,0 +1,23 @@
+<?php
+namespace SmartData;
+
+require_once('../bin/smartdata/SmartAPI.php');
+use function SmartData\SmartAPI\finish;
+use SmartData\Exception\CustomException;
+use SmartData\Logger;
+
+http_response_code(HttpStatusCode::BAD_REQUEST);
+
+try {
+    $content = file_get_contents('php://input');
+    $response = finish($content);
+}catch(CustomException $e){
+    http_response_code($e->getHTTPCodeError());
+    header('X-Message: '. $e->getMessage(), false);
+    return false;
+}catch(\Exception $e){
+    http_response_code(HttpStatusCode::BAD_REQUEST);
+    return false;
+}
+http_response_code(HttpStatusCode::NO_CONTENT);
+echo $response;
diff --git a/api/list.php b/api/list.php
new file mode 100644
index 0000000..f6d3aea
--- /dev/null
+++ b/api/list.php
@@ -0,0 +1,25 @@
+<?php
+namespace SmartData;
+
+require_once('../bin/smartdata/SmartAPI.php');
+use function SmartData\SmartAPI\list_devices;
+use SmartData\Exception\CustomException;
+use SmartData\Logger;
+
+http_response_code(HttpStatusCode::BAD_REQUEST);
+
+try {
+    $content = file_get_contents('php://input');
+    $response = list_devices($content);
+    
+}catch(CustomException $e){
+    http_response_code($e->getHTTPCodeError());
+    Logger::exception($e);
+    return false;
+}catch(\Exception $e){
+    http_response_code(HttpStatusCode::BAD_REQUEST);
+    Logger::exception($e);
+    return false;
+}
+http_response_code(HttpStatusCode::OK);
+echo $response;
diff --git a/api/login.html b/api/login.html
new file mode 100644
index 0000000..6ce006c
--- /dev/null
+++ b/api/login.html
@@ -0,0 +1,22 @@
+<html>
+<head>
+<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
+<script>
+   function doLogin() {
+        console.log("iniciando...");
+        let xhr = new XMLHttpRequest();
+        xhr.open("POST", "https://iot.lisha.ufsc.br/grafana/login");
+        xhr.setRequestHeader("Accept", "application/json");
+        xhr.setRequestHeader("Content-Type", "application/json");
+        xhr.onload = () => { document.getElementById("grafana_frame").src = "https://iot.lisha.ufsc.br:3000/"; }
+       	let data = '{ "user": "aqtech@lisha.ufsc.br", "password": "jP#55Mh@" } ';
+        xhr.send(data);
+        console.log('Enviou...');
+   }
+</script>
+</head>
+<body onload="doLogin();">
+<iframe src=""  style="min-width:800px;  min-height:900px;" id="grafana_frame" name="grafana_frame"></iframe>
+</iframe>
+</body>
+</html>
diff --git a/api/nop.php b/api/nop.php
new file mode 100644
index 0000000..6f97c53
--- /dev/null
+++ b/api/nop.php
@@ -0,0 +1,28 @@
+<?php
+namespace SmartData;
+
+require_once('../bin/smartdata/SmartAPI.php');
+require_once('../bin/smartdata/Logger.php');
+require_once('../bin/smartdata/SmartData.php');
+require_once('../bin/smartdata/MultiSmartData.php');
+
+
+use SmartData\Exception\{AuthenticationException,BadRequestException,InternalException};
+
+http_response_code(HttpStatusCode::BAD_REQUEST);
+
+try {
+}catch(Exception\CustomException $e){
+    http_response_code($e->getHTTPCodeError());
+    error_log( $e );
+    header('X-Message: '.$e->getMessage(), false);
+    return false;
+}catch(\Exception $e){
+    http_response_code(HttpStatusCode::BAD_REQUEST);
+    error_log( $e );
+    header('X-Message: '.$e->getMessage(), false);
+    return false;
+}
+
+http_response_code(HttpStatusCode::NO_CONTENT);
+
diff --git a/bin/smartdata/SmartAPI.php b/bin/smartdata/SmartAPI.php
index 9e37fa2..d882470 100644
--- a/bin/smartdata/SmartAPI.php
+++ b/bin/smartdata/SmartAPI.php
@@ -40,8 +40,7 @@ namespace SmartData\SmartAPI
                 }
 
                 if($return != NULL){
-                    $return = json_encode($return);
-                    $return = preg_replace('/:"([0-9]*)"/',':$1',$return);
+                    $return = json_encode( $return, JSON_NUMERIC_CHECK );
                 } else {
                     throw new RequestFailedException("Error processing request: null return");
                 }
@@ -94,8 +93,7 @@ namespace SmartData\SmartAPI
                     }
                     $pos = $pos + 1;
                 }
-                $return = json_encode($return);
-                $return = preg_replace('/:"([0-9]*)"/',':$1',$return);
+                $return = json_encode( $return, JSON_NUMERIC_CHECK );
             } else {
                 throw new RequestFailedException("Error processing request: null return");
             }
-- 
GitLab