Compare commits

...

1 Commits

Author SHA1 Message Date
Flatlogic Bot
c38ec08c48 1.00 2025-11-22 18:25:54 +00:00
9 changed files with 481 additions and 146 deletions

21
api/get_radar_data.php Normal file
View File

@ -0,0 +1,21 @@
<?php
session_start();
header('Content-Type: application/json');
require_once __DIR__ . '/../includes/thingspeak.php';
$channel_id = $_SESSION['channel_id'] ?? null;
$api_key = $_SESSION['api_key'] ?? null;
if (!$channel_id || !$api_key) {
echo json_encode(['error' => 'Not connected. Please set Channel ID and API Key.']);
exit;
}
$data = fetch_thingspeak_data($channel_id, $api_key, 50); // Fetch more points for better clustering
if ($data && !empty($data['feeds'])) {
echo json_encode($data['feeds']);
} else {
echo json_encode(['error' => 'Failed to fetch data from ThingSpeak.']);
}

34
assets/css/custom.css Normal file
View File

@ -0,0 +1,34 @@
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
background-color: #F8F9FA;
}
.navbar-brand {
font-weight: 600;
}
.card {
border-radius: 0.5rem;
box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
}
.form-control:focus {
box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
}
.btn-primary {
background-image: linear-gradient(to right, #0D6EFD, #0D47A1);
border: none;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}

174
assets/js/main.js Normal file
View File

@ -0,0 +1,174 @@
document.addEventListener('DOMContentLoaded', function() {
const radarCanvas = document.getElementById('radarCanvas');
if (radarCanvas) {
updateRadarData();
setInterval(updateRadarData, 5000); // Update every 5 seconds
}
});
function updateRadarData() {
fetch('api/get_radar_data.php')
.then(response => response.json())
.then(data => {
if (data && !data.error) {
const feeds = data;
checkProximity(feeds, 0.5);
drawRadar(feeds);
updateObjectsTable(feeds);
} else {
console.error('Error fetching radar data:', data ? data.error : 'No data received');
}
})
.catch(error => {
console.error('Failed to fetch or parse radar data:', error);
});
}
function toCartesian(angle, distance, canvas) {
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const maxCanvasDistance = Math.min(centerX, centerY) * 0.9;
const MAX_REAL_WORLD_METERS = 5;
const distanceInMeters = distance / 100;
const normalizedDistance = (distanceInMeters / MAX_REAL_WORLD_METERS) * maxCanvasDistance;
const angleRad = (angle - 90) * (Math.PI / 180);
const x = centerX + normalizedDistance * Math.cos(angleRad);
const y = centerY + normalizedDistance * Math.sin(angleRad);
return { x, y };
}
function updateObjectsTable(feeds) {
const tableBody = document.getElementById('objectsTableBody');
if (!tableBody) return;
tableBody.innerHTML = '';
feeds.forEach((feed, index) => {
const row = document.createElement('tr');
row.style.animation = `fadeIn 0.5s ease-in-out ${index * 0.1}s both`;
const idCell = document.createElement('td');
idCell.textContent = feed.entry_id;
row.appendChild(idCell);
const distanceCell = document.createElement('td');
const distanceInMeters = parseFloat(feed.field2) / 100;
distanceCell.textContent = distanceInMeters.toFixed(2);
row.appendChild(distanceCell);
const angleCell = document.createElement('td');
angleCell.textContent = parseFloat(feed.field1).toFixed(2);
row.appendChild(angleCell);
const timeCell = document.createElement('td');
timeCell.textContent = new Date(feed.created_at).toLocaleTimeString();
row.appendChild(timeCell);
tableBody.appendChild(row);
});
}
function checkProximity(feeds, distanceThreshold) {
const alertElement = document.getElementById('proximityAlert');
const distanceElement = document.getElementById('closestDistance');
if (!alertElement || !distanceElement) return;
let minDistance = Infinity;
let isClose = false;
feeds.forEach(feed => {
const distanceInMeters = parseFloat(feed.field2) / 100;
if (distanceInMeters < minDistance) {
minDistance = distanceInMeters;
}
if (distanceInMeters < distanceThreshold) {
isClose = true;
}
});
if (isClose) {
distanceElement.textContent = minDistance.toFixed(2);
alertElement.classList.remove('d-none');
} else {
alertElement.classList.add('d-none');
}
}
function drawRadar(feeds) {
const canvas = document.getElementById('radarCanvas');
if(!canvas) return;
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const centerX = width / 2;
const centerY = height / 2;
const maxDistance = Math.min(centerX, centerY) * 0.9;
ctx.fillStyle = '#0b1220';
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = 'rgba(0, 255, 213, 0.3)';
ctx.lineWidth = 1;
for (let i = 1; i <= 4; i++) {
ctx.beginPath();
ctx.arc(centerX, centerY, maxDistance * (i / 4), 0, 2 * Math.PI);
ctx.stroke();
}
ctx.beginPath();
ctx.moveTo(centerX, 0);
ctx.lineTo(centerX, height);
ctx.moveTo(0, centerY);
ctx.lineTo(width, centerY);
ctx.stroke();
feeds.forEach((feed, index) => {
const angle = parseFloat(feed.field1);
const distance = parseFloat(feed.field2);
if (!isNaN(angle) && !isNaN(distance)) {
const { x, y } = toCartesian(angle, distance, canvas);
// Animation
let radius = 0;
let alpha = 0;
const targetRadius = 5;
const animationDuration = 500;
const startTime = Date.now();
function animateDot() {
const elapsedTime = Date.now() - startTime;
const progress = Math.min(elapsedTime / animationDuration, 1);
radius = targetRadius * progress;
alpha = progress;
ctx.fillStyle = `rgba(33, 150, 243, ${alpha})`;
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.fill();
if (progress < 1) {
requestAnimationFrame(animateDot);
}
}
setTimeout(animateDot, index * 100);
}
});
}
(function () {
'use strict'
var forms = document.querySelectorAll('.needs-validation')
Array.prototype.slice.call(forms)
.forEach(function (form) {
form.addEventListener('submit', function (event) {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
})();

72
connect.php Normal file
View File

@ -0,0 +1,72 @@
<?php
session_start();
$page_title = "Connect to IoT Channel";
$page_description = "Connect your ThingSpeak-compatible IoT channel to start monitoring.";
$channel_id = $_SESSION['channel_id'] ?? '';
$api_key = $_SESSION['api_key'] ?? '';
$message = '';
$message_type = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$channel_id = trim($_POST['channel_id']);
$api_key = trim($_POST['api_key']);
if (!empty($channel_id) && !empty($api_key)) {
// For now, we just validate that they are not empty.
// In a real scenario, we would test the connection to ThingSpeak API.
$_SESSION['channel_id'] = $channel_id;
$_SESSION['api_key'] = $api_key;
$message = '<strong>Success!</strong> Your settings have been saved. You can now proceed to the dashboard.';
$message_type = 'success';
} else {
$message = '<strong>Error!</strong> Please provide a valid Channel ID and Read API Key.';
$message_type = 'danger';
}
}
include 'partials/header.php';
?>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-body p-5">
<h2 class="card-title text-center mb-4">Connect IoT Channel</h2>
<?php if ($message): ?>
<div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
<?php echo $message; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<form action="connect.php" method="POST" id="connectForm" novalidate>
<div class="mb-3">
<label for="channel_id" class="form-label">Channel ID</label>
<input type="text" class="form-control" id="channel_id" name="channel_id" value="<?php echo htmlspecialchars($channel_id); ?>" required>
<div class="invalid-feedback">
Please enter your ThingSpeak Channel ID.
</div>
</div>
<div class="mb-3">
<label for="api_key" class="form-label">Read API Key</label>
<input type="text" class="form-control" id="api_key" name="api_key" value="<?php echo htmlspecialchars($api_key); ?>" required>
<div class="invalid-feedback">
Please enter your Read API Key.
</div>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary btn-lg">Connect</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include 'partials/footer.php'; ?>

75
dashboard.php Normal file
View File

@ -0,0 +1,75 @@
<?php
session_start();
$page_title = "Dashboard";
$page_description = "Live and historical data visualization.";
include 'partials/header.php';
$channel_id = $_SESSION['channel_id'] ?? null;
$api_key = $_SESSION['api_key'] ?? null;
?>
<div class="container mt-5">
<div class="px-4 py-5 my-5 text-center">
<h1 class="display-5 fw-bold">Dashboard</h1>
<div class="col-lg-6 mx-auto">
<p class="lead mb-4">This is where the synchronized polar and Cartesian plots will be displayed.</p>
<?php if ($channel_id && $api_key): ?>
<div id="proximityAlert" class="alert alert-danger d-none" role="alert">
<strong>Warning!</strong> An object is detected at a distance of <span id="closestDistance"></span> units.
</div>
<div class="row">
<div class="col-md-7">
<div class="card mb-4">
<div class="card-header">
Radar View
</div>
<div class="card-body d-flex justify-content-center">
<canvas id="radarCanvas" width="500" height="500" style="background-color: #0b1220;"></canvas>
</div>
</div>
</div>
<div class="col-md-5">
<div class="card mb-4">
<div class="card-header">
Detected Objects
</div>
<div class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Distance (m)</th>
<th scope="col">Angle (°)</th>
<th scope="col">Timestamp</th>
</tr>
</thead>
<tbody id="objectsTableBody">
</tbody>
</table>
</div>
</div>
</div>
</div>
<details class="card">
<summary class="card-header">
Raw ThingSpeak Data
</summary>
<div class="card-body">
<pre class="text-start"><code id="rawDataContainer">Fetching data...</code></pre>
</div>
</details>
<?php else: ?>
<div class="alert alert-warning" role="alert">
Please <a href="connect.php" class="alert-link">connect to a channel</a> first to see live data.
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php include 'partials/footer.php'; ?>

18
includes/thingspeak.php Normal file
View File

@ -0,0 +1,18 @@
<?php
function fetch_thingspeak_data($channel_id, $api_key, $results = 10) {
$url = "https://api.thingspeak.com/channels/{$channel_id}/feeds.json?api_key={$api_key}&results={$results}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code == 200) {
return json_decode($output, true);
}
return null;
}
?>

167
index.php
View File

@ -1,150 +1,25 @@
<?php
declare(strict_types=1);
@ini_set('display_errors', '1');
@error_reporting(E_ALL);
@date_default_timezone_set('UTC');
session_start();
$page_title = "Welcome to IoT Radar Tracker";
$page_description = "A modern, professional web application for monitoring radar data from ThingSpeak-compatible IoT channels.";
$phpVersion = PHP_VERSION;
$now = date('Y-m-d H:i:s');
include 'partials/header.php';
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>New Style</title>
<?php
// Read project preview data from environment
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? '';
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
?>
<?php if ($projectDescription): ?>
<!-- Meta description -->
<meta name="description" content='<?= htmlspecialchars($projectDescription) ?>' />
<!-- Open Graph meta tags -->
<meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<!-- Twitter meta tags -->
<meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<?php endif; ?>
<?php if ($projectImageUrl): ?>
<!-- Open Graph image -->
<meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<!-- Twitter image -->
<meta property="twitter:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<?php endif; ?>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
<style>
:root {
--bg-color-start: #6a11cb;
--bg-color-end: #2575fc;
--text-color: #ffffff;
--card-bg-color: rgba(255, 255, 255, 0.01);
--card-border-color: rgba(255, 255, 255, 0.1);
}
body {
margin: 0;
font-family: 'Inter', sans-serif;
background: linear-gradient(45deg, var(--bg-color-start), var(--bg-color-end));
color: var(--text-color);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
text-align: center;
overflow: hidden;
position: relative;
}
body::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><path d="M-10 10L110 10M10 -10L10 110" stroke-width="1" stroke="rgba(255,255,255,0.05)"/></svg>');
animation: bg-pan 20s linear infinite;
z-index: -1;
}
@keyframes bg-pan {
0% { background-position: 0% 0%; }
100% { background-position: 100% 100%; }
}
main {
padding: 2rem;
}
.card {
background: var(--card-bg-color);
border: 1px solid var(--card-border-color);
border-radius: 16px;
padding: 2rem;
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.1);
}
.loader {
margin: 1.25rem auto 1.25rem;
width: 48px;
height: 48px;
border: 3px solid rgba(255, 255, 255, 0.25);
border-top-color: #fff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.hint {
opacity: 0.9;
}
.sr-only {
position: absolute;
width: 1px; height: 1px;
padding: 0; margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap; border: 0;
}
h1 {
font-size: 3rem;
font-weight: 700;
margin: 0 0 1rem;
letter-spacing: -1px;
}
p {
margin: 0.5rem 0;
font-size: 1.1rem;
}
code {
background: rgba(0,0,0,0.2);
padding: 2px 6px;
border-radius: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
footer {
position: absolute;
bottom: 1rem;
font-size: 0.8rem;
opacity: 0.7;
}
</style>
</head>
<body>
<main>
<div class="card">
<h1>Analyzing your requirements and generating your website…</h1>
<div class="loader" role="status" aria-live="polite" aria-label="Applying initial changes">
<span class="sr-only">Loading…</span>
</div>
<p class="hint"><?= ($_SERVER['HTTP_HOST'] ?? '') === 'appwizzy.com' ? 'AppWizzy' : 'Flatlogic' ?> AI is collecting your requirements and applying the first changes.</p>
<p class="hint">This page will update automatically as the plan is implemented.</p>
<p>Runtime: PHP <code><?= htmlspecialchars($phpVersion) ?></code> — UTC <code><?= htmlspecialchars($now) ?></code></p>
<div class="container col-xxl-8 px-4 py-5">
<div class="row flex-lg-row-reverse align-items-center g-5 py-5">
<div class="col-10 col-sm-8 col-lg-6">
<img src="https://images.pexels.com/photos/373543/pexels-photo-373543.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" class="d-block mx-lg-auto img-fluid" alt="Bootstrap Themes" width="700" height="500" loading="lazy">
</div>
<div class="col-lg-6">
<h1 class="display-5 fw-bold lh-1 mb-3">Monitor Real-Time IoT Data with Ease</h1>
<p class="lead"><?php echo htmlspecialchars($page_description); ?></p>
<div class="d-grid gap-2 d-md-flex justify-content-md-start">
<a href="connect.php" class="btn btn-primary btn-lg px-4 me-md-2">Get Started</a>
<a href="dashboard.php" class="btn btn-outline-secondary btn-lg px-4">View Dashboard</a>
</div>
</div>
</div>
</main>
<footer>
Page updated: <?= htmlspecialchars($now) ?> (UTC)
</footer>
</body>
</html>
</div>
<?php include 'partials/footer.php'; ?>

14
partials/footer.php Normal file
View File

@ -0,0 +1,14 @@
<footer class="footer mt-auto py-3 bg-light">
<div class="container text-center">
<span class="text-muted">© <?php echo date("Y"); ?> <?php echo htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'IoT Radar Tracker'); ?>. All rights reserved.</span>
</div>
</footer>
<!-- Bootstrap 5 JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<!-- Custom JS -->
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>

52
partials/header.php Normal file
View File

@ -0,0 +1,52 @@
<?php
// Fallback for project name if not set in environment
$project_name = $_SERVER['PROJECT_NAME'] ?? 'IoT Radar Tracker';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($page_title ?? $project_name); ?></title>
<meta name="description" content="<?php echo htmlspecialchars($page_description ?? 'A radar-based monitoring system.'); ?>">
<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap Icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<!-- Custom CSS -->
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
<!-- Google Fonts (Inter) -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="index.php">
<i class="bi bi-broadcast"></i>
<?php echo htmlspecialchars($project_name); ?>
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="connect.php">Connect</a>
</li>
<li class="nav-item">
<a class="nav-link" href="dashboard.php">Dashboard</a>
</li>
</ul>
</div>
</div>
</nav>