-
Notifications
You must be signed in to change notification settings - Fork 17
/
wifi_controller.php
106 lines (88 loc) · 3.28 KB
/
wifi_controller.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
function wifi_controller()
{
global $settings, $session, $route, $redis;
$route->format = "json";
require "Modules/wifi/wifi.php";
$wifi = new Wifi();
$result = false;
// Special setup access to WIFI function scan and setconfig
$setup_access = false;
if (isset($_SESSION['setup_access']) && $_SESSION['setup_access']) {
$setup_access = true;
}
// ------------------------------------------------------------
// Write level access
// ------------------------------------------------------------
if ($session["write"]) {
if ($route->action=="start") {
$result = $wifi->start();
} elseif ($route->action=="stop") {
$result = $wifi->stop();
} elseif ($route->action=="restart") {
$result = $wifi->restart();
} elseif ($route->action=="") {
$route->format = "html";
$result = view("Modules/wifi/view.html",array());
}
}
// ------------------------------------------------------------
// Read level access
// ------------------------------------------------------------
if ($session["read"] || $setup_access) {
if ($route->action=="info") {
$result = $wifi->info();
} elseif ($route->action=="getconfig") {
$result = $wifi->getconfig();
} elseif ($route->action=="log") {
$route->format = "text";
$result = $wifi->wifilog();
} elseif ($route->action=="scan") {
if (file_exists($settings['openenergymonitor_dir']."/emonpi/emoncms_wifiscan.php")) {
return cmd("wifi/scan",array());
} else {
$result = $wifi->scan();
}
}
}
if ($session["write"] || $setup_access) {
if ($route->action=="setconfig") {
$networks = urldecode(post('networks'));
$country = "GB";
if (isset($_POST['country'])) {
$country = $_POST['country'];
}
$result = $wifi->setconfig(json_decode($networks),$country);
if ($setup_access) shell_exec('sudo shutdown -r now 2>&1');
}
}
return array('content' => $result);
}
function cmd($classmethod,$properties) {
global $settings, $redis;
if ($redis) {
$redis->del($classmethod); // 1. remove last result
$update_script = $settings['openenergymonitor_dir']."/emonpi/emoncms-wifiscan.sh";
$update_logfile = $settings['log']['location']."/wifiscan.log";
$redis->rpush("service-runner","$update_script>$update_logfile");
$start = time(); // 3. wait for result
while((time()-$start)<5.0) {
$result = $redis->get($classmethod);
if ($result) {
return json_decode($result);
}
usleep(100000); // check every 100ms
}
}
return false;
}