-
Notifications
You must be signed in to change notification settings - Fork 3
/
routes.php
91 lines (62 loc) · 2.78 KB
/
routes.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
use RouterOsStumbler\Entity\Site;
use RouterOsStumbler\Entity\Survey;
use RouterOsStumbler\Services\UbiquitiScanResultParser;
use RouterOsStumbler\Services\UbiquitiScanResultReader;
$app->get('/', function() use ($app)
{
return $app->redirect('/surveys');
});
$app->get('/surveys', function () use ($app, $entityManager, $devices) {
$surveys = $entityManager->getRepository(Survey::class)->findAll();
$app->render('surveys/index.html.twig', ['surveys' => $surveys, 'devices' => array_keys($devices)]);
});
$app->get('/surveys/create', function () use ($app) {
$app->render('surveys/create.html.twig');
});
$app->post('/surveys', function () use ($app, $entityManager) {
$request = $app->request();
$surveyName = $request->post('survey_name');
$survey = new Survey($surveyName);
$entityManager->persist($survey);
$entityManager->flush();
return $app->redirect('/surveys/' . $survey->getId());
});
$app->get('/surveys/:surveyId', function ($surveyId) use ($app, $entityManager) {
$survey = $entityManager->getRepository(Survey::class)->find($surveyId);
$devices = $app->request()->get('devices');
$devices = (array) $devices;
return $app->render('surveys/scan.html.twig', ['survey' => $survey, 'devices' => $devices]);
});
$app->get('/surveys/:surveyId/scan/:deviceName', function ($surveyId, $deviceName) use ($app, $entityManager, $devices) {
$device = $devices[$deviceName];
if ( ! $device) {
throw new Exception('Device not found');
} else {
if ($device instanceof \RouterOsStumbler\Entity\Routerboard) {
$reader = new \RouterOsStumbler\Services\RouterBoardScanResultReader($device);
}
if($device instanceof \RouterOsStumbler\Entity\Ubiquiti) {
$reader = new UbiquitiScanResultReader(new UbiquitiScanResultParser());
}
$scanResults = $reader->read($device);
$survey = $entityManager->getRepository(Survey::class)->find($surveyId);
foreach ($scanResults as $scanResult) $survey->addResult($scanResult);
$entityManager->persist($survey);
$entityManager->flush();
$response = $app->response();
$response->setBody(json_encode($scanResults));
return $response;
}
});
$app->get('/surveys/:surveyId/results', function ($surveyId) use ($app, $entityManager) {
$survey = $entityManager->getRepository(Survey::class)->find($surveyId);
$ssids = $survey->getSsidsScanned();
$bestSignals = [];
foreach ($ssids as $ssid) {
$bestSignals[$ssid] = $survey->getBestScanResultForSsid($ssid)->getSignalStrength();
}
asort($bestSignals);
$bestSignals = array_reverse($bestSignals);
return $app->render('surveys/results.html.twig', ['survey' => $survey, 'bestSignals' => $bestSignals]);
});