source: trunk/Modules/NetworkConfigRouterOS/Generators/Signal.php

Last change on this file was 929, checked in by chronos, 3 years ago
  • Modified: Removed commended out print_r and echo commands used only for debugging.
File size: 5.9 KB
Line 
1<?php
2
3class ConfigRouterOSSignal extends NetworkConfigItem
4{
5 private int $Time;
6
7 function ReadWirelessRegistration(): void
8 {
9 $this->Time = time();
10 $Queries = array();
11
12 $DbResult3 = $this->Database->query('SELECT `Id`, '.
13 '(SELECT `LocalIP` FROM `NetworkInterface` WHERE `NetworkInterface`.`Device` = `NetworkDevice`.`Id` LIMIT 1) AS `LocalIP`, '.
14 '`Name` '.
15 'FROM `NetworkDevice` WHERE (`API` = 1) AND (`Used` = 1)');
16 while ($Device = $DbResult3->fetch_assoc())
17 {
18 echo($Device['LocalIP'].' ('.$Device['Name'].")\n");
19 $Routerboard = new RouterosAPI();
20 //$Routerboard->SSL = true;
21 //$Routerboard->Port = 8729;
22 $Routerboard->Connect($Device['LocalIP'], $this->System->Config['API']['UserName'], $this->System->Config['API']['Password']);
23 if (!$Routerboard->Connected) continue;
24
25 $Queries = array_merge($Queries, $this->ReadWireless($Device, $Routerboard));
26 $Queries = array_merge($Queries, $this->ReadWireless60G($Device, $Routerboard));
27 }
28 $this->Database->Transaction($Queries);
29 }
30
31 // Read normal wireless clients signal
32 function ReadWireless(array $Device, RouterosAPI $Routerboard): array
33 {
34 $Queries = array();
35
36 $Routerboard->Write('/interface/wireless/registration-table/getall', false);
37 $Routerboard->Write('=.proplist=signal-strength,tx-signal-strength,mac-address,rx-rate,tx-rate', false);
38 $Routerboard->Write('=stats=');
39 $Read = $Routerboard->Read(false);
40 $Array = $Routerboard->ParseResponse($Read);
41 $NetworkMac = new NetworkMac($this->System);
42 foreach ($Array as $Properties)
43 {
44 $DbResult = $this->Database->select('NetworkInterface', 'Id', 'MAC="'.$Properties['mac-address'].'"');
45 if ($DbResult->num_rows > 0)
46 {
47 $DbRow = $DbResult->fetch_assoc();
48 $Interface = $DbRow['Id'];
49 } else $Interface = 'NULL';
50
51 if (strpos($Properties['signal-strength'], '@') === false)
52 {
53 $Strength = $Properties['signal-strength'];
54 } else
55 {
56 $Parts = explode('@', $Properties['signal-strength']);
57 if (substr($Parts[0], -3) == 'dBm')
58 $Strength = substr($Parts[0], 0, -3); // without dBm
59 else $Strength = $Parts[0];
60 }
61
62 if (array_key_exists('tx-signal-strength', $Properties))
63 {
64 if (strpos($Properties['tx-signal-strength'], '@') === false)
65 {
66 $RemoteSignal = $Properties['tx-signal-strength'];
67 } else
68 {
69 $Parts = explode('@', $Properties['tx-signal-strength']);
70 if (substr($Parts[0], -3) == 'dBm')
71 $RemoteSignal = substr($Parts[0], 0, -3); // without dBm
72 else $RemoteSignal = $Parts[0];
73 }
74 } else $RemoteSignal = 0;
75
76 $RateRx = $this->StripUnits($Properties['rx-rate']);
77 $RateTx = $this->StripUnits($Properties['tx-rate']);
78 $MacRef = $NetworkMac->GetIndex($Properties['mac-address']);
79 $Queries[] = 'INSERT INTO `NetworkSignal` (`MAC`, `Value`, `Remote`, `RateRx`, `RateTx`, `Time`, `Interface`, `Device`) VALUES '.
80 '('.$MacRef.', '.$Strength.', '.$RemoteSignal.', '.$RateRx.', '.$RateTx.', "'.
81 TimeToMysqlDateTime($this->Time).'", '.$Interface.', '.$Device['Id'].')';
82 }
83 return $Queries;
84 }
85
86 // Read 60 GHz wireless clients signal
87 function ReadWireless60G(array $Device, RouterosAPI $Routerboard): array
88 {
89 $Queries = array();
90
91 $Routerboard->Write('/interface/w60g/monitor', false);
92 $Routerboard->Write('=.proplist=tx-phy-rate,rssi,remote-address', false);
93 $Routerboard->Write('=numbers=wlan60-1', false);
94 $Routerboard->Write('=once=');
95 $Read = $Routerboard->Read(false);
96 $Array = $Routerboard->ParseResponse($Read);
97 if (array_key_exists('!trap', $Array))
98 {
99 return array();
100 }
101 $NetworkMac = new NetworkMac($this->System);
102 foreach ($Array as $Properties)
103 {
104 $DbResult = $this->Database->select('NetworkInterface', 'Id', 'MAC="'.$Properties['remote-address'].'"');
105 if ($DbResult->num_rows > 0)
106 {
107 $DbRow = $DbResult->fetch_assoc();
108 $Interface = $DbRow['Id'];
109 } else $Interface = 'NULL';
110
111 if (strpos($Properties['rssi'], '@') === false)
112 {
113 $Strength = $Properties['rssi'];
114 } else
115 {
116 $Parts = explode('@', $Properties['rssi']);
117 if (substr($Parts[0], -3) == 'dBm')
118 $Strength = substr($Parts[0], 0, -3); // without dBm
119 else $Strength = $Parts[0];
120 }
121
122 $RateTx = $this->StripUnits($Properties['tx-phy-rate']);
123
124 $MacRef = $NetworkMac->GetIndex($Properties['remote-address']);
125 $Queries[] = 'INSERT INTO `NetworkSignal` (`MAC`, `Value`, `Remote`, `RateRx`, `RateTx`, `Time`, `Interface`, `Device`) VALUES '.
126 '('.$MacRef.', '.$Strength.', 0, 0, '.round($RateTx / 1000000).', "'.
127 TimeToMysqlDateTime($this->Time).'", '.$Interface.', '.$Device['Id'].')';
128 }
129 return $Queries;
130 }
131
132 function StripUnits(string $Value): string
133 {
134 if (strpos($Value, '-') !== false) $Value = substr($Value, 0, strpos($Value, '-') - 1); // without channel info
135 if (substr($Value, -3, 3) == "MHz") $Value = substr($Value, 0, -3); // without MHz unit
136 else if (substr($Value, -4, 4) == "Mbps") $Value = substr($Value, 0, -4); // without Mbps unit
137 else if (substr($Value, -3, 3) == "Mbp") $Value = substr($Value, 0, -3); // without Mbp unit
138 else if (substr($Value, -1, 1) == "M") $Value = substr($Value, 0, -1); // without M unit
139 else if (substr($Value, -4, 4) == "Gbps") $Value = substr($Value, 0, -4) * 1000; // without Gbps unit
140 else if (substr($Value, -3, 3) == "Gbp") $Value = substr($Value, 0, -3) * 1000; // without Gbp unit
141 else if (substr($Value, -1, 1) == "G") $Value = substr($Value, 0, -1) * 1000; // without G unit
142 return $Value;
143 }
144
145 function Run(): void
146 {
147 RepeatFunction(60 * 60, array($this, 'ReadWirelessRegistration'));
148 }
149}
Note: See TracBrowser for help on using the repository browser.