Skip to content

Commit

Permalink
add wireless infos (#41)
Browse files Browse the repository at this point in the history
* add wireless infos

* add frequency and rx,tx for WirelessInfos

* run black

* Update unifi_client.py

* Update respondd_client.py

* run black

* Update respondd_client.py
  • Loading branch information
T0biii committed May 13, 2024
1 parent f10710e commit d743841
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
59 changes: 58 additions & 1 deletion unifi_respondd/respondd_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ class ClientInfo:
wifi5: int


@dataclasses.dataclass
class WirelessInfo:
"""This class contains the Wireless information of an AP.
Attributes:
frequency:
noise:
active:
busy:
rx:
tx:"""

frequency: int
# noise: int
# active: int
# busy: int
rx: int
tx: int


@dataclasses.dataclass
class MemoryInfo:
"""This class contains the memory information of an AP.
Expand Down Expand Up @@ -178,7 +197,11 @@ class StatisticsInfo:
node_id: The node id of the AP. This is the same as the MAC address (without :).
loadavg: The load average of the AP.
memory: The memory information of the AP.
traffic: The traffic information of the AP."""
traffic: The traffic information of the AP.
gateway: The MAC of the IPv4 Gateway
gateway6: The MAC of the IPv6 Gateway
gateway_nexthop: The MAC of the nexthop Gateway
wireless: The WirelessInfos of the AP"""

clients: ClientInfo
uptime: int
Expand All @@ -189,6 +212,7 @@ class StatisticsInfo:
gateway: str
gateway6: str
gateway_nexthop: str
wireless: List[WirelessInfo]


@dataclasses.dataclass
Expand Down Expand Up @@ -268,11 +292,43 @@ def getNodeInfos(self):
)
return nodes

@staticmethod
def frequency_from_channel(channel):
if channel >= 36:
return 5000 + (channel) * 5
else:
if channel == 14:
return 2484
elif channel < 14:
return 2407 + (channel) * 5

def getStatistics(self):
"""This method returns the statistics information of all APs."""
aps = self._aps
statistics = []
for ap in aps.accesspoints:
wirelessinfos = []

if ap.channel5:
frequency5 = self.frequency_from_channel(ap.channel5)
wirelessinfos.append(
WirelessInfo(
frequency=frequency5,
rx=ap.rx_bytes5,
tx=ap.tx_bytes5,
)
)

if ap.channel24:
frequency24 = self.frequency_from_channel(ap.channel24)
wirelessinfos.append(
WirelessInfo(
frequency=frequency24,
rx=ap.rx_bytes5,
tx=ap.tx_bytes5,
)
)

statistics.append(
StatisticsInfo(
clients=ClientInfo(
Expand All @@ -296,6 +352,7 @@ def getStatistics(self):
gateway=ap.gateway,
gateway6=ap.gateway6,
gateway_nexthop=ap.gateway_nexthop,
wireless=wirelessinfos,
)
)
return statistics
Expand Down
47 changes: 47 additions & 0 deletions unifi_respondd/unifi_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class Accesspoint:
client_count: int
client_count24: int
client_count5: int
channel5: int
rx_bytes5: bytes
tx_bytes5: bytes
channel24: int
rx_bytes24: bytes
tx_bytes24: bytes
latitude: float
longitude: float
model: str
Expand Down Expand Up @@ -86,6 +92,31 @@ def get_client_count_for_ap(ap_mac, clients, cfg):
return client24_count + client5_count, client24_count, client5_count


def get_ap_channel_usage(ssids, cfg):
"""This function returns the channels used for the Freifunk SSIDs"""
channel5 = None
rx_bytes5 = None
tx_bytes5 = None
channel24 = None
rx_bytes24 = None
tx_bytes24 = None
for ssid in ssids:
if re.search(cfg.ssid_regex, ssid.get("essid", "")):
channel = ssid.get("channel", 0)
rx_bytes = ssid.get("rx_bytes", 0)
tx_bytes = ssid.get("tx_bytes", 0)
if channel > 14:
channel5 = channel
rx_bytes5 = rx_bytes
tx_bytes5 = tx_bytes
else:
channel24 = channel
rx_bytes24 = tx_bytes
tx_bytes24 = tx_bytes

return channel5, rx_bytes5, tx_bytes5, channel24, rx_bytes24, tx_bytes24


def get_location_by_address(address, app):
"""This function returns latitude and longitude of a given address."""
time.sleep(1)
Expand Down Expand Up @@ -162,6 +193,16 @@ def get_infos():
client_count24,
client_count5,
) = get_client_count_for_ap(ap.get("mac", None), clients, cfg)

(
channel5,
rx_bytes5,
tx_bytes5,
channel24,
rx_bytes24,
tx_bytes24,
) = get_ap_channel_usage(ssids, cfg)

lat, lon = 0, 0
neighbour_macs = []
if ap.get("snmp_location", None) is not None:
Expand Down Expand Up @@ -203,6 +244,12 @@ def get_infos():
client_count=client_count,
client_count24=client_count24,
client_count5=client_count5,
channel5=channel5,
rx_bytes5=rx_bytes5,
tx_bytes5=tx_bytes5,
channel24=channel24,
rx_bytes24=rx_bytes24,
tx_bytes24=tx_bytes24,
latitude=float(lat),
longitude=float(lon),
model=ap.get("model", None),
Expand Down

0 comments on commit d743841

Please sign in to comment.