(Jag kör UniFi controller 5.6.19 som Docker-container samt vanliga Apache och PHP5 i en Synology NAS)
Redigera övre raderna och spara ner som index.php under i /unifi/ på en lämplig maskin som pratar PHP, hämta även UniFi-API-client och lägg den i /UniFi-API-client/ på samma maskin.
index.php
Code: Select all
<?php
header('Content-Type: application/json; charset=utf-8');
$controlleruser = "root"; // the user name for access to the UniFi Controller
$controllerpassword = "SuperSecret :)"; // the password for access to the UniFi Controller
$controllerurl = "https://22.22.11.11:8443"; // full url to the UniFi Controller, eg. "https://22.22.11.11:8443"
$site_id = "";
$controllerversion = "5.5.20"; // the version of the Controller software, eg. "4.6.6" (must be at least 4.0.0)
$debug = false;
require_once("../UniFi-API-client/src/Client.php");
$unifiConnection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
//$set_debug_mode = $unifi_connection->set_debug(true);
$loginresults = $unifiConnection->login();
$unifiClients = $unifiConnection->list_clients();
$result = array();
foreach ($unifiClients as $unifiClient) {
$c = array();
$c["mac"] = $unifiClient->mac;
$c["name"] = $unifiClient->name;
$c["vlan"] = $unifiClient->vlan;
$c["ip"] = $unifiClient->ip;
if ($unifiClient->is_wired == 1) {
$c["network"] = "LAN";
$c["ssid"] = null;
} else {
$c["network"] = "WiFi";
$c["ssid"] = $unifiClient->essid;
}
$result[] = $c;
}
//echo json_encode($result, JSON_PRETTY_PRINT);
echo json_encode($result);
?>
(Ett tips är att även ta hem UniFi-API-browser om man vill utforska APIet)
Nu ska du få ett JSON-svar om du surfar till /unifi/ ... om inte så är det något galet.
Skapa sedan upp en VD där "IP" pekar på din webbserver och port på webbporten.
Lägg sedan till en eller flera labels som du namnsätter enligt: "lblMAC", alltså lbl följt av mac-adressen till enheten utan :
Exempelvis: lbl223344556677
Som mainloop lägger du in följande kod:
Code: Select all
local doDebug = true
local sStatusOnline = "Online"
local sStatusOffline = "Offline"
local iSleepSeconds = 182
msgDebug = function (color, message)
if doDebug then
fibaro:debug("<span style='color: " .. color .. "'>" .. tostring(message) .. "</span>")
end
end
tableLength = function (T)
local c = 0
for _ in pairs(T) do
c = c + 1
end
return c
end
-- ------------------------------------------------------------------------------------------------------------------------
labels = {}
msgDebug("Orange", "Calling HC2 API for labels on device " .. tostring(fibaro:getSelfId()) )
local http = Net.FHttp("127.0.0.1", 11111)
local sResponse, status, err = http:GET("/api/devices/" .. tostring(fibaro:getSelfId()))
local jsonResponse = json.decode(sResponse)
for rowIndex, rowData in pairs(jsonResponse.properties.rows) do
for buttonIndex, buttonData in pairs(rowData.elements) do
msgDebug("Yellow", "Found label: " .. jsonResponse.properties.rows[rowIndex].elements[buttonIndex].name .. " (" .. jsonResponse.properties.rows[rowIndex].elements[buttonIndex].caption .. ")")
labels[#labels+1] = jsonResponse.properties.rows[rowIndex].elements[buttonIndex].name
end
end
msgDebug("Orange", "Calling UniFi API on " .. fibaro:getValue(fibaro:getSelfId(), 'IPAddress') )
local http = Net.FHttp(fibaro:getValue(fibaro:getSelfId(), 'IPAddress'), fibaro:getValue(fibaro:getSelfId(), 'TCPPort'))
local sResponse = http:GET("/unifi/")
local jsonResponse = json.decode(sResponse)
local jsonLength = tableLength(jsonResponse)
for p in pairs(labels) do
msgDebug("Black", "")
local label = labels[p]
sLabel = "ui." .. label .. ".value"
msgDebug("White", "Check device with label: " .. label)
sValue = os.date("%Y-%m-%d %H:%M:%S") .. " / " .. sStatusOffline
bOnline = false
for r = 1, jsonLength do
if (label == string.gsub("lbl" .. jsonResponse[r].mac, ":", "") ) then
msgDebug("Green", "Online: " .. jsonResponse[r].mac .. " / " .. jsonResponse[r].name )
sValue = tostring(os.date("%Y-%m-%d %H:%M:%S")) .. " / " .. sStatusOnline
bOnline = true
end
end
sCurrentValue = fibaro:getValue(fibaro:getSelfId(), sLabel)
msgDebug("Orange", "Label, current value = " .. sCurrentValue)
if bOnline then
if string.find(sCurrentValue, sStatusOnline) then
msgDebug("Gray", "Allready " .. sStatusOnline .. " status... no update!")
else
msgDebug("Yellow", sLabel .. " = " .. sValue)
fibaro:call(fibaro:getSelfId(), "setProperty", sLabel, sValue)
end
else
if string.find(sCurrentValue, sStatusOffline) then
msgDebug("Gray", "Allready " .. sStatusOffline .. " status... no update!")
else
msgDebug("Yellow", sLabel .. " = " .. sValue)
fibaro:call(fibaro:getSelfId(), "setProperty", sLabel, sValue)
end
end
end
-- ------------------------------------------------------------------------------------------------------------------------
msgDebug("Orange", "Done! Sleeping " .. tostring(iSleepSeconds) .. " seconds")
fibaro:sleep(iSleepSeconds * 1000)