diff --git a/docs/Webhook.md b/docs/Webhook.md new file mode 100644 index 0000000..d077b48 --- /dev/null +++ b/docs/Webhook.md @@ -0,0 +1,86 @@ +# Webhook + +Comming soon. +See [https://github.com/jomjol/AI-on-the-edge-device/pull/3163](https://github.com/jomjol/AI-on-the-edge-device/pull/3163) and [https://github.com/jomjol/AI-on-the-edge-device/pull/3174](https://github.com/jomjol/AI-on-the-edge-device/pull/3174) + +@RaHehl Please add some explanations. +Also I think it would be great to add the PHP examples: +```PHP + 'error', 'message' => 'Invalid API key']); + exit; +} + +$method = $_SERVER['REQUEST_METHOD']; + +if ($method === 'POST') { + // Handle POST request: Write data to CSV + $csvFile = 'webhook_log.csv'; + + $jsonData = file_get_contents('php://input'); + + $dataArray = json_decode($jsonData, true); + if (!$jsonData || !is_array($dataArray)) { + http_response_code(400); // 400 Bad Request + echo json_encode(['status' => 'error', 'message' => 'Invalid JSON data']); + exit; + } + + $csvHandle = fopen($csvFile, 'a'); + if ($csvHandle === false) { + http_response_code(500); // 500 Internal Server Error + echo json_encode(['status' => 'error', 'message' => 'Unable to open CSV file']); + exit; + } + + foreach ($dataArray as $data) { + $csvRow = [ + $data['timestamp'], + $data['name'], + $data['rawValue'], + $data['value'], + $data['preValue'], + $data['rate'], + $data['changeAbsolute'], + $data['error'] + ]; + fputcsv($csvHandle, $csvRow); + } + + fclose($csvHandle); + + http_response_code(200); // 200 OK + echo json_encode(['status' => 'success', 'message' => 'Data written to CSV file']); +} elseif ($method === 'PUT') { + // Handle PUT request: Save image + $imageFilePath = 'uploaded_image.jpg'; + + $imageData = file_get_contents('php://input'); + + if (!$imageData) { + http_response_code(400); // 400 Bad Request + echo json_encode(['status' => 'error', 'message' => 'No image data received']); + exit; + } + + if (file_put_contents($imageFilePath, $imageData) === false) { + http_response_code(500); // 500 Internal Server Error + echo json_encode(['status' => 'error', 'message' => 'Unable to save the image']); + exit; + } + + http_response_code(200); // 200 OK + echo json_encode(['status' => 'success', 'message' => 'Image uploaded successfully']); +} else { + // Handle unsupported HTTP methods + http_response_code(405); // 405 Method Not Allowed + echo json_encode(['status' => 'error', 'message' => 'Method not allowed']); +} +?> +``` diff --git a/docs/nav.yml b/docs/nav.yml index 65cd76a..be589d2 100644 --- a/docs/nav.yml +++ b/docs/nav.yml @@ -52,6 +52,7 @@ nav: - REST-API.md - MQTT-API.md - Influx-DB.md + - Webhook.md # - ... - Development: