forked from sahadev381/ISP-PLATFORM-AS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_api_temp.php
More file actions
64 lines (49 loc) · 2.27 KB
/
map_api_temp.php
File metadata and controls
64 lines (49 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
include 'config.php';
include 'includes/auth.php';
header('Content-Type: application/json');
$action = $_GET['action'] ?? '';
// ... Keep existing actions (add_node, update_port, get_data, etc.) ...
if ($action == 'predict_fault') {
$route_id = $_POST['route_id'];
$distance = (float)$_POST['distance']; // in meters
$route = $conn->query("SELECT * FROM fiber_routes WHERE id = $route_id")->fetch_assoc();
if (!$route) die(json_encode(['status' => 'error']));
$path = json_decode($route['path_data'], true);
// Logic: Iterate through points, calculate distance between them, find where $distance falls.
$current_dist = 0;
$predicted_point = null;
for ($i = 0; $i < count($path) - 1; $i++) {
$p1 = $path[$i];
$p2 = $path[$i+1];
$d = haversineDistance($p1['lat'], $p1['lng'], $p2['lat'], $p2['lng']);
if ($current_dist + $d >= $distance) {
// The break is between p1 and p2
$ratio = ($distance - $current_dist) / $d;
$predicted_point = [
'lat' => $p1['lat'] + ($p2['lat'] - $p1['lat']) * $ratio,
'lng' => $p1['lng'] + ($p2['lng'] - $p1['lng']) * $ratio
];
break;
}
$current_dist += $d;
}
if (!$predicted_point) $predicted_point = end($path); // Fallback to end of line
// Log the fault
$stmt = $conn->prepare("INSERT INTO network_faults (fault_type, predicted_lat, predicted_lng, severity, description) VALUES ('FIBER_BREAK', ?, ?, 'CRITICAL', ?)");
$desc = "Predicted break at {$distance}m on route: " . $route['name'];
$stmt->bind_param("dds", $predicted_point['lat'], $predicted_point['lng'], $desc);
$stmt->execute();
echo json_encode(['status' => 'success', 'point' => $predicted_point, 'id' => $conn->insert_id]);
}
function haversineDistance($lat1, $lon1, $lat2, $lon2) {
$earth_radius = 6371000; // in meters
$dLat = deg2rad($lat2 - $lat1);
$dLon = deg2rad($lon2 - $lon1);
$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin($dLon/2) * sin($dLon/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
return $earth_radius * $c;
}
// ... Rest of map_api.php ...
// (I will merge this into the existing file)
?>