<?php

class ConfigRouterOSSignal extends NetworkConfigItem
{
  private int $Time;

  function ReadWirelessRegistration(): void
  {
    $this->Time = time();
    $Queries = array();
    
    $DbResult3 = $this->Database->query('SELECT `Id`, '.
      '(SELECT `LocalIP` FROM `NetworkInterface` WHERE `NetworkInterface`.`Device` = `NetworkDevice`.`Id` LIMIT 1) AS `LocalIP`, '.
      '`Name` '.
      'FROM `NetworkDevice` WHERE (`API` = 1) AND (`Used` = 1)');
    while ($Device = $DbResult3->fetch_assoc())
    {
      echo($Device['LocalIP'].' ('.$Device['Name']."): ");
      $Routerboard = new RouterosAPI();
      //$Routerboard->SSL = true;
      //$Routerboard->Port = 8729;
      $Routerboard->Connect($Device['LocalIP'], $this->System->Config['API']['UserName'], $this->System->Config['API']['Password']);
      if ($Routerboard->Connected) 
      {
        $Queries = array_merge($Queries, $this->ReadWireless($Device, $Routerboard));
        $Queries = array_merge($Queries, $this->ReadWireless60G($Device, $Routerboard));
      }
      echo("\n");
    }
    $this->Database->Transaction($Queries);
  }

  // Read normal wireless clients signal
  function ReadWireless(array $Device, RouterosAPI $Routerboard): array
  {
    $Queries = array();

    $Routerboard->Write('/interface/wireless/registration-table/getall', false);
    $Routerboard->Write('=.proplist=signal-strength,tx-signal-strength,mac-address,rx-rate,tx-rate', false);
    $Routerboard->Write('=stats=');
    $Read = $Routerboard->Read(false);
    $Array = $Routerboard->ParseResponse($Read);
    if (array_key_exists('!trap', $Array))
    {
      return array();
    }
    $NetworkMac = new NetworkMac($this->System);
    foreach ($Array as $Properties)
    {
      $DbResult = $this->Database->select('NetworkInterface', 'Id', 'MAC="'.$Properties['mac-address'].'"');
      if ($DbResult->num_rows > 0)
      {
        $DbRow = $DbResult->fetch_assoc();
        $Interface = $DbRow['Id'];
      } else $Interface = 'NULL';

      if (strpos($Properties['signal-strength'], '@') === false)
      {
        $Strength = $Properties['signal-strength'];
      } else
      {
        $Parts = explode('@', $Properties['signal-strength']);
        if (substr($Parts[0], -3) == 'dBm')
          $Strength = substr($Parts[0], 0, -3); // without dBm
          else $Strength = $Parts[0];
      }

      if (array_key_exists('tx-signal-strength', $Properties))
      {
        if (strpos($Properties['tx-signal-strength'], '@') === false)
        {
          $RemoteSignal = $Properties['tx-signal-strength'];
        } else
        {
          $Parts = explode('@', $Properties['tx-signal-strength']);
          if (substr($Parts[0], -3) == 'dBm')
            $RemoteSignal = substr($Parts[0], 0, -3); // without dBm
            else $RemoteSignal = $Parts[0];
        }
      } else $RemoteSignal = 0;

      $RateRx = $this->StripUnits($Properties['rx-rate']);
      $RateTx = $this->StripUnits($Properties['tx-rate']);
      $MacRef = $NetworkMac->GetIndex($Properties['mac-address']);
      $Queries[] = 'INSERT INTO `NetworkSignal` (`MAC`, `Value`, `Remote`, `RateRx`, `RateTx`, `Time`, `Interface`, `Device`) VALUES '.
        '('.$MacRef.', '.$Strength.', '.$RemoteSignal.', '.$RateRx.', '.$RateTx.', "'.
        TimeToMysqlDateTime($this->Time).'", '.$Interface.', '.$Device['Id'].')';
      echo('.');
    }
    return $Queries;
  }

  // Read 60 GHz wireless clients signal
  function ReadWireless60G(array $Device, RouterosAPI $Routerboard): array
  {
    $Queries = array();
    
    $Routerboard->Write('/interface/w60g/monitor', false);
    $Routerboard->Write('=.proplist=tx-phy-rate,rssi,remote-address', false);
    $Routerboard->Write('=numbers=wlan60-1', false);
    $Routerboard->Write('=once=');
    $Read = $Routerboard->Read(false);
    $Array = $Routerboard->ParseResponse($Read);
    if (array_key_exists('!trap', $Array))
    {
      return array();
    }
    $NetworkMac = new NetworkMac($this->System);
    foreach ($Array as $Properties)
    {
      $DbResult = $this->Database->select('NetworkInterface', 'Id', 'MAC="'.$Properties['remote-address'].'"');
      if ($DbResult->num_rows > 0)
      {
        $DbRow = $DbResult->fetch_assoc();
        $Interface = $DbRow['Id'];
      } else $Interface = 'NULL';

      if (strpos($Properties['rssi'], '@') === false)
      {
        $Strength = $Properties['rssi'];
      } else
      {
        $Parts = explode('@', $Properties['rssi']);
        if (substr($Parts[0], -3) == 'dBm')
          $Strength = substr($Parts[0], 0, -3); // without dBm
          else $Strength = $Parts[0];
      }

      $RateTx = $this->StripUnits($Properties['tx-phy-rate']);
      
      $MacRef = $NetworkMac->GetIndex($Properties['remote-address']);
      $Queries[] = 'INSERT INTO `NetworkSignal` (`MAC`, `Value`, `Remote`, `RateRx`, `RateTx`, `Time`, `Interface`, `Device`) VALUES '.
        '('.$MacRef.', '.$Strength.', 0, 0, '.round($RateTx / 1000000).', "'.
        TimeToMysqlDateTime($this->Time).'", '.$Interface.', '.$Device['Id'].')';
      echo('.');
    }
    return $Queries;
  }

  function StripUnits(string $Value): string
  {
    if (strpos($Value, '-') !== false) $Value = substr($Value, 0, strpos($Value, '-') - 1); // without channel info
    if (substr($Value, -3, 3) == "MHz") $Value = substr($Value, 0, -3); // without MHz unit
    else if (substr($Value, -4, 4) == "Mbps") $Value = substr($Value, 0, -4); // without Mbps unit
    else if (substr($Value, -3, 3) == "Mbp") $Value = substr($Value, 0, -3); // without Mbp unit
    else if (substr($Value, -1, 1) == "M") $Value = substr($Value, 0, -1); // without M unit
    else if (substr($Value, -4, 4) == "Gbps") $Value = substr($Value, 0, -4) * 1000; // without Gbps unit
    else if (substr($Value, -3, 3) == "Gbp") $Value = substr($Value, 0, -3) * 1000; // without Gbp unit
    else if (substr($Value, -1, 1) == "G") $Value = substr($Value, 0, -1) * 1000; // without G unit
    return $Value;
  }

  function Run(): void
  {
    RepeatFunction(60 * 60, array($this, 'ReadWirelessRegistration'));
  }
}
