Folder reorganize 1
This commit is contained in:
92
data/tag-control-get.php
Normal file
92
data/tag-control-get.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php // phpcs:ignoreFile
|
||||
|
||||
require __DIR__ . '/../session.php';
|
||||
|
||||
if (($_SESSION['SESS_MEMBER_LEVEL'] ?? '') !== 'controls') {
|
||||
http_response_code(403);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Access denied.',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
require __DIR__ . '/../includes/control-tags.php';
|
||||
require __DIR__ . '/../includes/kepware-rest.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$idRaw = $_GET['id'] ?? '';
|
||||
$id = is_string($idRaw) ? trim($idRaw) : '';
|
||||
|
||||
if ($id === '') {
|
||||
http_response_code(400);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'A valid tag id is required.',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$tag = control_tag_find($id);
|
||||
|
||||
if ($tag === null) {
|
||||
http_response_code(404);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Tag not configured for overrides.',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$read = kepware_read([$tag['id']]);
|
||||
$result = $read[$tag['id']] ?? null;
|
||||
|
||||
if ($result === null) {
|
||||
http_response_code(502);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Kepware did not return data for the requested tag.',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$rawValue = $result['value'] ?? null;
|
||||
$timestamp = isset($result['timestamp']) && is_string($result['timestamp']) ? $result['timestamp'] : null;
|
||||
$status = isset($result['status']) ? $result['status'] : null;
|
||||
|
||||
$numericValue = null;
|
||||
if (is_numeric($rawValue)) {
|
||||
$numericValue = (float) $rawValue;
|
||||
}
|
||||
|
||||
$responseValue = $numericValue ?? $rawValue;
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'id' => $tag['id'],
|
||||
'name' => $tag['name'],
|
||||
'value' => $responseValue,
|
||||
'control' => 1,
|
||||
'type' => $tag['type_label'],
|
||||
'control_mode' => $tag['control_mode'],
|
||||
'source_idnumber' => $tag['config']['source'] ?? $tag['id'],
|
||||
'timestamp' => $timestamp,
|
||||
'displayTimestamp' => control_format_timestamp($timestamp),
|
||||
'status' => $status,
|
||||
'units' => $tag['units'] ?? null,
|
||||
'description' => $tag['description'] ?? null,
|
||||
],
|
||||
]);
|
||||
} catch (Throwable $exception) {
|
||||
error_log('Kepware read failed: ' . $exception->getMessage());
|
||||
http_response_code(502);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Failed to read tag value from Kepware.',
|
||||
'details' => $exception->getMessage(),
|
||||
]);
|
||||
}
|
||||
Reference in New Issue
Block a user