Re: LUA : récupération informations Livebox TV
Publié : 10 mars 2018, 17:47
il va falloir mettre un peu les mains dans le camboui
j'ai adapté un fichier bash pour récupérer les devices connectés
et j'ai ensuite adapté le script lua précédent pour lancer le script bash et extraire les données.
attention à personnaliser le mdp dans le bash ainsi que les chemins d'accès aux fichiers
j'ai adapté un fichier bash pour récupérer les devices connectés
Code : Tout sélectionner
#!/opt/bin/bash
###########################################
# Firmware Livebox 4 = 2.22.8 g0-f-sip-fr #
# Script mis a jour le 03/06/2017 #
###########################################
#script source https://blog.tetsumaki.net/articles/2015/10/recuperation-dinformations-livebox-play.html
#############################
# Déclaration des variables #
#############################
myLivebox=192.168.1.1
myPassword=
#myBashDir=/home/pi/domoticz/scripts/sh/livebox/
myBashDir=/home/pi/domoticz/Trend/
myOutput=$myBashDir/myOutput.txt
myCookies=$myBashDir/myCookies.txt
########################################
# Connexion et recuperation du cookies #
########################################
curl -s -o "$myOutput" -X POST -c "$myCookies" -H 'Content-Type: application/x-sah-ws-4-call+json' -H 'Authorization: X-Sah-Login' -d "{\"service\":\"sah.Device.Information\",\"method\":\"createContext\",\"parameters\":{\"applicationName\":\"so_sdkut\",\"username\":\"admin\",\"password\":\"$myPassword\"}}" http://$myLivebox/ws > /dev/null
##################################################
# Lecture du cookies pour utilisation ultérieure #
##################################################
myContextID=$(tail -n1 "$myOutput" | sed 's/{"status":0,"data":{"contextID":"//1'| sed 's/",//1' | sed 's/"groups":"http,admin//1' | sed 's/"}}//1')
###############################################################################################
# Envoi des commandes pour récupérer les informations et écriture dans un fichier TXT séparé #
###############################################################################################
getDSLStats=`curl -s -b "$myCookies" -X POST -H 'Content-Type: application/x-sah-ws-4-call+json' -H "X-Context: $myContextID" -d "{\"service\":\"NeMo.Intf.dsl0\",\"method\":\"getDSLStats\",\"parameters\":{}}" http://$myLivebox/ws`
getMIBs=`curl -s -b "$myCookies" -X POST -H 'Content-Type: application/x-sah-ws-4-call+json' -H "X-Context: $myContextID" -d "{\"service\":\"NeMo.Intf.data\",\"method\":\"getMIBs\",\"parameters\":{}}" http://$myLivebox/ws`
getWanStatus=`curl -s -b "$myCookies" -X POST -H 'Content-Type: application/x-sah-ws-4-call+json' -H "X-Context: $myContextID" -d "{\"service\":\"NMC\",\"method\":\"getWANStatus\",\"parameters\":{}}" http://$myLivebox/ws`
getDeviceInfo=`curl -s -b "$myCookies" -X POST -H 'Content-Type: application/x-sah-ws-4-call+json' -H "X-Context: $myContextID" -d "{\"service\":\"DeviceInfo\",\"method\":\"get\",\"parameters\":{}}" http://$myLivebox/ws`
getDevices=`curl -s -b "$myCookies" -X POST -H 'Content-Type: application/x-sah-ws-4-call+json' -H "X-Context: $myContextID" -d "{\"service\":\"Devices\",\"method\":\"get\",\"parameters\":{}}" http://$myLivebox/ws`
#getDevices=`curl -s -b "$myCookies" -X POST -H 'Content-Type: application/json\' -H "X-Context: $myContextID" -d "{\"service\":\"Devices\",\"method\":\"get\",\"parameters\":{}}" http://$myLivebox/ws`
echo $getDSLStats > $myBashDir/DSLStats.txt
echo $getMIBs > $myBashDir/MIBs.txt
echo $getWanStatus > $myBashDir/WanStatus.txt
echo $getDeviceInfo > $myBashDir/DeviceInfo.txt
echo $getDevices > $myBashDir/Devices.txt
#######################################################
# Deconnexion et suppression des fichiers temporaires #
#######################################################
curl -s -b "$myCookies" -X POST http://$myLivebox/logout
rm "$myCookies" "$myOutput"
attention à personnaliser le mdp dans le bash ainsi que les chemins d'accès aux fichiers
Code : Tout sélectionner
--[[
source : https://github.com/vzwingma/domotique/blob/master/domoticz/scripts/lua/script_time_network_status.lua
https://blog.tetsumaki.net/articles/2015/10/recuperation-dinformations-livebox-play.html
https://easydomoticz.com/forum/viewtopic.php?f=10&t=3825&start=10#p35974
http://pon.fr/network-status-via-Livebox-en-lua/
MAJ : 23/12/2017
]]--
--------------------------------------------
------------ Variables à éditer ------------
--------------------------------------------
local nom_script = "Livebox Network Status"
local version = "1.4"
local debugging = false -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
local script_actif = true -- active (true) ou désactive (false) ce script simplement
-- Créer une variable "Livebox_mac_adress_smartphones" avec les MAC ADDRESS des smartphones. Le séparateur est ";"
livebox_mac_adress_smartphones = uservariables["livebox_mac_adress_smartphones"]
livebox_mac_adress_surveillance = uservariables["livebox_mac_adress_surveillance"]
livebox_mac_adress_smartphones = livebox_mac_adress_smartphones .. livebox_mac_adress_surveillance
-- Créer une variable "Livebox_mac_adress_surveillance" avec les MAC ADDRESS des Smartphones N' entrant PAS en compte pour l'alarme. Le séparateur est ";"
--------------------------------------------
----------- Fin variables à éditer ---------
--------------------------------------------
local patternMacAdresses = string.format("([^%s]+)", ";")
local chemin_tmp = "/home/pi/domoticz/Trend/"
--------------------------------------------
---------------- Fonctions -----------------
--------------------------------------------
package.path = package.path..";/home/pi/domoticz/scripts/lua/fonctions/?.lua"
require('fonctions_perso')
function readAll(file)
local f = io.open(file, "rb")
if(f == nil) then
return ""
else
local content = f:read("*all")
f:close()
return content
end
end
function getPeripheriquesConnectes() -- liste les périphériques utilisés pour l'activation/désactivation automatique de l'alarme
--local TMP_PERIPHERIQUES = "/home/pi/domoticz/Trend/peripheriques.tmp"
--local TMP_PERIPHERIQUES = "/home/pi/domoticz/scripts/sh/livebox/Devices.txt"
local TMP_PERIPHERIQUES = chemin_tmp .."Devices.txt"
-- Appel sur la liste des périphériques
voir_les_logs("Recherche des peripheriques connus de la Livebox",debugging)
local json_peripheriques = JSON:decode(readAll(TMP_PERIPHERIQUES))
local etatSmartphone = false
-- Liste des périphériques
for index, peripherique in pairs(json_peripheriques.status) do
--voir_les_logs("--- --- --- [Livebox] Statut du peripherique surveillance " .. index .. " :: " .. peripherique.Key,debugging)
for mac in string.gmatch(livebox_mac_adress_smartphones, patternMacAdresses) do
if(mac == peripherique.Key) then
if(peripherique.Active) then
-- local lastChanged = os.time() - convertStringUTCTimeToSeconds(peripherique.LastChanged)
-- local lastConnect = os.time() - convertStringUTCTimeToSeconds(peripherique.LastConnection)
-- if(lastChanged < 0) then
-- lastChanged = 0
-- end
voir_les_logs("Statut du peripherique en surveillance ".. peripherique.Name.." [" .. mac .. "] => actif:" .. tostring((peripherique.Active)),debugging)
--voir_les_logs("--- --- --- [" .. peripherique.Name .. "] actif; Dernière activite = " .. peripherique.LastChanged .. " :: " .. lastChanged .. "s / Dernière connexion = " .. peripherique.LastConnection .. " :: " .. lastConnect .. "s",debugging)
etatSmartphone = true
if otherdevices[peripherique.Name] == 'Off' then
commandArray [peripherique.Name]='On'
voir_les_logs("--- --- --- [Livebox] Activation de : " .. peripherique.Name .." --- --- --- ",debugging)
end
else
if otherdevices[peripherique.Name] == 'On' then
commandArray [peripherique.Name]='Off'
voir_les_logs("--- --- --- [Livebox] DesActivation de : " .. peripherique.Name .." --- --- --- ",debugging)
end
end
end
end
end
return etatSmartphone
end
function getPeripheriquesConnectes2() -- liste les périphériques utilisés pour
--local TMP_PERIPHERIQUESHORSALARME = "/home/pi/domoticz/Trend/peripheriques_hors_alarme.tmp"
-- Appel sur la liste des périphériques
voir_les_logs("Recherche des peripheriques connus de la Livebox (hors alarme)",debugging)
-- local commandeurl="curl -s -H \"Content-Type: application/json\" -H \"X-Fbx-App-Auth: " .. session_token .. "\" -X GET " .. apiLiveboxv4 .. "/lan/browser/pub/"
-- os.execute(commandeurl .. " > " .. TMP_PERIPHERIQUESHORSALARME)
local TMP_PERIPHERIQUESHORSALARME = chemin_tmp .."Devices.txt"
local other_json_peripheriques = JSON:decode(readAll(TMP_PERIPHERIQUESHORSALARME))
other_etatPeripheriques = false
-- Liste des périphériques HORS ALARME
for index, peripherique in pairs(other_json_peripheriques.status) do
voir_les_logs("--- --- --- [Livebox] Statut du peripherique hors surveillance " .. index .. " :: " .. peripherique.Key,debugging)
for mac in string.gmatch(livebox_mac_adress_smartphones, patternMacAdresses) do
if(mac == peripherique.Key)
then
voir_les_logs("Statut du peripherique hors surveillance".. peripherique.Name.." [" .. mac .. "] => actif:" .. tostring((peripherique.Active)),debugging)
if(peripherique.Active) then
other_etatPeripheriques = true
if otherdevices[peripherique.Name] == 'Off' then
commandArray [peripherique.Name]='On'
voir_les_logs("--- --- --- [Livebox] Activation de : " .. peripherique.Name .." --- --- --- ",debugging)
end
else
if otherdevices[peripherique.Name] == 'On' then
commandArray [peripherique.Name]='Off'
voir_les_logs("--- --- --- [Livebox] DesActivation de : " .. peripherique.Name .." --- --- --- ",debugging)
end
end
end
end
end
return other_etatPeripheriques
end
-- Mise à jour de l'alarme suivant le statut des périphériques
-- @param : état des périphériques
function updateAlarmeStatus(etat_peripheriques)
local etatActuelAlarme=otherdevices['Security Panel']
voir_les_logs(" > Etat du panneau de securite = " .. etatActuelAlarme ,debugging)
local SEUIL_ALARME = 1 -- temps en minute avant activation de l'alarme
local TMPDIR_COMPTEUR_OUT = "/home/pi/domoticz/Trend/compteur_smartphone_out.tmp"
-- Activation de l'alarme au bout de X min
if(not etat_peripheriques and etatActuelAlarme == "Normal") then
--if(not etat_peripheriques) then
compteurOff=readAll(TMPDIR_COMPTEUR_OUT)
if(compteurOff == "") then
compteurOff = 0
end
compteurOff = compteurOff + 1
voir_les_logs(" > Compteur de mise en alarme = " .. compteurOff .. " / " .. SEUIL_ALARME,debugging)
if(compteurOff >= SEUIL_ALARME) then
voir_les_logs("Activation de l'alarme",debugging)
commandArray[#commandArray+1] = {['Alarme Out']="On"}
commandArray[#commandArray+1] = {['test presences']="Off"}
compteurOff = 0
end
os.execute("echo " .. compteurOff .. " > " .. TMPDIR_COMPTEUR_OUT)
elseif(etat_peripheriques and etatActuelAlarme == "Arm Away") then
-- Désactivation immédiate
commandArray[#commandArray+1] = {['Alarme Out']="On"}
commandArray[#commandArray+1] = {['test presences']="On"}
voir_les_logs("Desactivation de l'alarme",debugging)
os.execute("echo 0 > " .. TMPDIR_COMPTEUR_OUT)
elseif(etat_peripheriques) then
os.execute("echo 0 > " .. TMPDIR_COMPTEUR_OUT)
commandArray[#commandArray+1] = {['test presences']="On"}
voir_les_logs("Remise a zero du compte de l'alarme",debugging)
end
end
--------------------------------------------
-------------- Fin Fonctions ---------------
--------------------------------------------
commandArray = {}
time=os.date("*t")
if time.min ~= 0 and script_actif == true then -- execution toutes les minutes sauf à xx:00
voir_les_logs("[Livebox] Statuts des peripheriques reseau Livebox",debugging)
-- Boucle principale
if ( livebox_mac_adress_surveillance == nil or livebox_mac_adress_surveillance == nil ) then
--error("[Livebox] {livebox_mac_adress_smartphones}, {livebox_mac_adress_surveillance} ne sont pas definies dans Domoticz")
--return 512
print("erreur")
else
voir_les_logs("Test de presence des appareils d'adresses MAC (" .. livebox_mac_adress_smartphones .. ")",debugging)
voir_les_logs("Test de presence des appareils d'adresses MAC (" .. livebox_mac_adress_surveillance .. ")",debugging)
JSON = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- one-time load of the routines
-- Connexion à la Livebox
--connectToLivebox()
-- Recherche des périphériques connectés
os.execute('sudo bash /home/pi/domoticz/scripts/sh/livebox/livebox.sh')
peripheriques_up = getPeripheriquesConnectes()
-- Recherche des périphériques connectés (HORS ALARME)
--getPeripheriquesConnectes2()
--updateAlarmeStatus(peripheriques_up)
-- Déconnexion à la Livebox
--disconnectToLivebox()
end
end --if time
return commandArray