Integrate Shelly module with new routes, classes, helpers, and configurations
- Added `shelly` class to manage API operations, handle requests, and validate configurations. - Introduced `shelly_i` interface and supporting helpers: `shelly_device_state`, `shelly_request_body_get_states`, and `shelly_response_body_get_states`. - Created new routes for fetching and updating Shelly configurations (`/shelly/config`). - Added Shelly module configurations: `enabled`, `secret_key`, and `server_url`. - Implemented `shelly_c` for module setup and configuration handling. - Integrated Shelly initialization in `index.php`.
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace classes;
|
||||
|
||||
require_once WD . '/modules/shelly/shelly_c.php';
|
||||
/** Actions */
|
||||
require_once WD . '/modules/shelly/actions/shelly_search_a.php';
|
||||
|
||||
use Exception;
|
||||
use interfaces\shelly_i;
|
||||
use shelly\actions\shelly_search_a;
|
||||
use shelly\shelly_c;
|
||||
|
||||
class shelly implements shelly_i
|
||||
{
|
||||
/**
|
||||
* Configuration of the shelly module
|
||||
* @var shelly_c
|
||||
*/
|
||||
public shelly_c $config;
|
||||
|
||||
/**
|
||||
* API URL
|
||||
* @var string
|
||||
*/
|
||||
private string $api_url = 'https://api.shelly.com/';
|
||||
|
||||
/**
|
||||
* ACTION: shelly_SEARCH
|
||||
* @see shelly_search_a
|
||||
* @notation This action is when a company search request is made, and logs the request in the database
|
||||
* @var shelly_search_a $shelly_search
|
||||
*/
|
||||
private shelly_search_a $shelly_search;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = new shelly_c();
|
||||
/** Actions */
|
||||
$this->shelly_search = new shelly_search_a();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws Exception If the module is not enabled, the license plate is invalid, the daily limit is exceeded, or the secret key is invalid
|
||||
*/
|
||||
function sendRequest(
|
||||
string $endpoint,
|
||||
array $data = [],
|
||||
string $method = 'GET'
|
||||
): object
|
||||
{
|
||||
// Validate the module is enabled
|
||||
self::requireModuleEnabled();
|
||||
// Validate the secret key
|
||||
self::requireValidSecretKey();
|
||||
// Send the request
|
||||
$response = match ($method) {
|
||||
//'GET' => self::sendGetRequest($endpoint, $data),
|
||||
//'POST' => self::sendPostRequest($search, $endpoint, $data),
|
||||
//'PUT' => self::sendPutRequest($search, $endpoint, $data),
|
||||
//'DELETE' => self::sendDeleteRequest($search, $endpoint, $data),
|
||||
default => self::exception(
|
||||
[
|
||||
'method' => $method,
|
||||
'endpoint' => $endpoint,
|
||||
'data' => $data,
|
||||
'response' => null,
|
||||
'status_code' => 400,
|
||||
'error' => 'Invalid request method',
|
||||
],
|
||||
500
|
||||
),
|
||||
};
|
||||
// Add the usage to the request log
|
||||
$this->shelly_search->shelly_search($response, 200);
|
||||
// Return the response
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
function requireModuleEnabled(): void
|
||||
{
|
||||
// Check if the module is enabled
|
||||
if (!$this->config->enabled->isTrue()) {
|
||||
throw new Exception('The shelly module is not enabled');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
function requireValidSecretKey(): void
|
||||
{
|
||||
// Check if the secret key is valid
|
||||
if ($this->config->secret_key->getVariableValue() === null) {
|
||||
self::exception(
|
||||
[
|
||||
'status_code' => 500,
|
||||
'error' => 'Invalid secret key defined in the config (shelly_secret_key_c)',
|
||||
],
|
||||
500
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
function exception(array $data, int $status_code = 500): object
|
||||
{
|
||||
// Check if the response is valid JSON
|
||||
if (!json_decode($data['response'])) {
|
||||
throw new Exception('Invalid response');
|
||||
}
|
||||
// Add the request to the log
|
||||
$this->shelly_search->shelly_search($data, $status_code);;
|
||||
return throw new Exception($data['error'] ?? 'An error occurred while processing the request, in ' . $this->config->getModuleName() . ' module');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user