- Introduced `/worker/debug`, `/worker/debug/on`, and `/worker/debug/off` routes for testing Shelly device states and switch operations. - Updated routes to handle device state retrieval and switching using the Shelly API with improved error messaging for disabled endpoints.
175 lines
7.7 KiB
PHP
175 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\economic;
|
|
use classes\router;
|
|
use classes\shelly;
|
|
use classes\virkdata;
|
|
use modules\shelly\helpers\shelly_device_switch;
|
|
use modules\shelly\helpers\shelly_request_body_get_states;
|
|
use modules\virkdata\helpers\virkdata_response;
|
|
use objects\users_o;
|
|
use traits\route_t;
|
|
|
|
class workerRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
$this->get('/worker/status', function () {
|
|
global /** @var router $router */
|
|
$response, $router;
|
|
$response->success(['message' => 'Worker is running', 'status' => 'OK', 'time' => date('Y-m-d H:i:s'), 'timezone' => date_default_timezone_get(), 'host' => gethostname(), 'version' => '1.0.1', 'routes' => $router->countRoutes()]);
|
|
});
|
|
$this->get('/worker/debug', function () {
|
|
global /** @var router $router */
|
|
$response, $router;
|
|
$response->error('This endpoint is disabled for security reasons.', 403);
|
|
$shelly = new shelly();
|
|
$shelly->requireModuleEnabled();
|
|
$shelly->requireValidSecretKey();
|
|
$parameters = new shelly_request_body_get_states();
|
|
$parameters->ids = ['e4b323243f90'];
|
|
$parameters->select = ['status'];
|
|
$result = $shelly->sendPostRequest('/v2/devices/api/get', (array)$parameters);
|
|
// Format the result
|
|
$result = array_map(function ($device) {
|
|
return (new shelly_device_switch())->populate($device);
|
|
}, $result);
|
|
//TODO: fetch statuses
|
|
$response->success($result);
|
|
});
|
|
$this->get('/worker/debug/on', function () {
|
|
global /** @var router $router */
|
|
$response, $router;
|
|
$response->error('This endpoint is disabled for security reasons.', 403);
|
|
$shelly = new shelly();
|
|
$shelly->requireModuleEnabled();
|
|
$shelly->requireValidSecretKey();
|
|
$parameters = new shelly_request_body_get_states();
|
|
$parameters->ids = ['e4b323243f90'];
|
|
$parameters->select = ['status'];
|
|
$result = $shelly->sendPostRequest('/v2/devices/api/get', (array)$parameters);
|
|
// Wait 1 second
|
|
sleep(1);
|
|
// Format the result
|
|
$result = array_map(function ($device) {
|
|
return (new shelly_device_switch())->populate($device);
|
|
}, $result);
|
|
// Open the switch
|
|
foreach ($result as $device) {
|
|
$device->switch(true);
|
|
}
|
|
//TODO: fetch statuses
|
|
$response->success($result);
|
|
});
|
|
$this->get('/worker/debug/off', function () {
|
|
global /** @var router $router */
|
|
$response, $router;
|
|
$response->error('This endpoint is disabled for security reasons.', 403);
|
|
$shelly = new shelly();
|
|
$shelly->requireModuleEnabled();
|
|
$shelly->requireValidSecretKey();
|
|
$parameters = new shelly_request_body_get_states();
|
|
$parameters->ids = ['e4b323243f90'];
|
|
$parameters->select = ['status'];
|
|
$result = $shelly->sendPostRequest('/v2/devices/api/get', (array)$parameters);
|
|
// Wait 1 second
|
|
sleep(1);
|
|
// Format the result
|
|
$result = array_map(function ($device) {
|
|
return (new shelly_device_switch())->populate($device);
|
|
}, $result);
|
|
// Open the switch
|
|
foreach ($result as $device) {
|
|
$device->switch(false);
|
|
}
|
|
//TODO: fetch statuses
|
|
$response->success($result);
|
|
});
|
|
$this->get('/worker/licenseplates', function () {
|
|
global /** @var router $router */
|
|
$response, $db;
|
|
$response->error('This endpoint is disabled for security reasons.', 403);
|
|
$counted_plates = []; // Array to hold counted license plates
|
|
// This is used to fetch all UNIQUE license plates from the database tables:
|
|
// 'customer_vehicles' -> 'reg' column
|
|
// 'orders' -> 'reg_1', 'reg_2', 'reg_3' columns
|
|
// 'bookings' -> 'regNrTraekker', 'regNrTrailer' columns
|
|
// 'plate_scans' -> 'plate' column
|
|
$tables_and_columns = [
|
|
'customer_vehicles' => ['reg'],
|
|
'orders' => ['reg_1', 'reg_2', 'reg_3'],
|
|
'bookings' => ['regNrTraekker', 'regNrTrailer'],
|
|
'plate_scans' => ['plate'],
|
|
];
|
|
foreach ($tables_and_columns as $table => $columns) {
|
|
foreach ($columns as $column) {
|
|
$results = $db->query("SELECT DISTINCT $column FROM $table WHERE $column IS NOT NULL AND $column != ''");
|
|
foreach ($results as $row) {
|
|
$formatted_plate = $this->FORMAT_LICENSE_PLATE($row[$column]);
|
|
if ($formatted_plate !== '') {
|
|
if (!isset($counted_plates[$formatted_plate])) {
|
|
$counted_plates[$formatted_plate] = 0;
|
|
}
|
|
$counted_plates[$formatted_plate]++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$response->success(['counted_license_plates' => $counted_plates, 'total_unique_plates' => count($counted_plates)]);
|
|
});
|
|
$this->get('/economic/doesCustomerExist', function () {
|
|
global $response;
|
|
self::requirePermission( 'economic_does_customer_exist'); // TODO: Remove this
|
|
self::requireParameters(['cvr']);
|
|
$cvr = self::getParameter('cvr');
|
|
// Check if the customer exists in E-conomic.
|
|
if (!is_numeric($cvr)) {
|
|
$response->error('Invalid CVR number', 400);
|
|
}
|
|
$economic = new economic();
|
|
$results = $economic->customers->customers->search([
|
|
'corporateIdentificationNumber' => (string)$cvr,
|
|
],[
|
|
'skipPages' => 0,
|
|
'pageSize' => 1000, // Since the limit is 1000, we need to set the page size to 1000.
|
|
])->collection;
|
|
if (count($results) === 0) {
|
|
$response->error('Customer not found', 404);
|
|
}
|
|
$response->success(['cvr' => $cvr, 'result' => $results], 200);
|
|
});
|
|
$this->get('/cvr/lookup', function () {
|
|
global $response;
|
|
self::requirePermission('cvr_lookup'); // TODO: Remove this
|
|
self::requireParameters(['cvr']);
|
|
$cvr = self::getParameter('cvr');
|
|
if (!is_numeric($cvr)) {
|
|
$response->error('Invalid CVR number', 400);
|
|
}
|
|
// Check if the result is cached
|
|
if (!empty(redis->get('cvr_lookup_' . $cvr))) {
|
|
$result = (new virkdata_response())->populate((array)json_decode(redis->get('cvr_lookup_' . $cvr)));
|
|
} else {
|
|
$virkdata = new virkdata();
|
|
$result = $virkdata->getCompanyInformation($cvr, '', []);
|
|
// Cache the result
|
|
redis->set('cvr_lookup_' . $cvr, json_encode($result->asArray()));
|
|
// Set the cache expiration time (3 days)
|
|
redis->expire('cvr_lookup_' . $cvr, (60 * 60 * 24 * 4));
|
|
}
|
|
$response->success(['cvr' => $cvr, 'result' => (object)$result->asArray(), 'phone' => $result->phone, 'email' => $result->email], 200);
|
|
});
|
|
}
|
|
|
|
protected function FORMAT_LICENSE_PLATE(string $plate): string
|
|
{
|
|
// Remove all non-alphanumeric characters
|
|
$cleaned = preg_replace('/[^A-Za-z0-9]/', '', $plate);
|
|
// Convert to uppercase
|
|
return strtoupper($cleaned);
|
|
}
|
|
} |