Re: Récupération donnée JSON
Publié : 15 mars 2018, 13:22
Pour le site web, tu dois t'authentifier avec le nom et pas avec l'adresse mail.
Reprenez le contrôle de votre domotique
https://easydomoticz.com/forum/
Code : Tout sélectionner
--[[
/home/pi/domoticz/scripts/lua/script_time_pellets.lua
auteur : papoo
date de création : 14/03/2018
Date de mise à jour : 15/03/2018
Principe :
/!\attention/!\
si vous souhaitez utiliser ce script dans l'éditeur interne, pour indiquer le chemin complet vers le fichier JSON.lua, il vous faudra changer la ligne
json = assert(loadfile(luaDir..'JSON.lua'))()
par
json = assert(loadfile('/le/chemin/vers/le/fichier/lua/JSON.lua'))()
exemple :
json = assert(loadfile('/home/pi/domoticz/scripts/lua/JSON.lua'))()
la reconnaissance automatique du chemin d'exécution de ce script ne fonctionnant pas dans l'éditeur interne
--]]
--------------------------------------------
------------ Variables à éditer ------------
--------------------------------------------
local debugging = true -- 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
local delai = 1 -- délai d'exécution de ce script en minutes de 1 à 59 (délai entre deux appels à l'API)
local url_info_pellets = "https://www.ecopellets.fr/appjson.php?uniqid=" -- Adresse de l'API ecopellets
local uniqid = "c49f8579b9aec326eac372e8a70a9f" -- votre uniqid
local dev_qtemois = "Consommation mois en cours"
local dev_prixmois = "Coût mensuel"
local dev_tendance = "Tendance"
local dev_stock0 = "Stock zéro"
local dev_datestock0 = "Date stock zéro"
local dev_qtelastmonth = "Quantité mois passé"
local dev_prixlastmonth = "Coût mois passé"
local dev_qtesept = "Quantité depuis septembre"
local dev_coutsept = "Coût depuis septembre"
local dev_qtestock = "État du stock"
local dev_prixstock = "Coût du stock"
local dev_coutotal = "Coût total"
local dev_coutreparation = "Coût réparation"
local dev_coutentretien = "Coût entretien"
--------------------------------------------
----------- Fin variables à éditer ---------
--------------------------------------------
--------------------------------------------
------------- Autres Variables -------------
--------------------------------------------
local nom_script = 'Infos ecopellets.fr'
local version = '0.2'
curl = '/usr/bin/curl -m 5 ' -- ne pas oublier l'espace à la fin
-- chemin vers le dossier lua
if (package.config:sub(1,1) == '/') then
luaDir = debug.getinfo(1).source:match("@?(.*/)")
else
luaDir = string.gsub(debug.getinfo(1).source:match("@?(.*\\)"),'\\','\\\\')
end
json = assert(loadfile(luaDir..'JSON.lua'))() -- chargement du fichier JSON.lua
--------------------------------------------
----------- Fin Autres Variables -----------
--------------------------------------------
--------------------------------------------
---------------- Fonctions -----------------
--------------------------------------------
function voir_les_logs (s, debugging)
if (debugging) then
if s ~= nil then
print ("<font color='#f3031d'>".. s .."</font>")
else
print ("<font color='#f3031d'>aucune valeur affichable</font>")
end
end
end
--------------------------------------------
-------------- Fin Fonctions ---------------
--------------------------------------------
commandArray = {}
time = os.date("*t")
if script_actif == true then
if ((time.min-1) % delai) == 0 then -- toutes les xx minutes en commençant par xx:01
voir_les_logs("=========== ".. nom_script .." (v".. version ..") ===========",debugging)
if otherdevices[dev_qtemois] == nil then creaDev(dev_qtemois, 5) end
--=========== Lecture json ===============--
local config = assert(io.popen(curl..' "'.. url_info_pellets .. uniqid ..'"'))
--end
local blocjson = config:read('*all')
config:close()
local jsonValeur = json:decode(blocjson)
if jsonValeur then
local qtemois = jsonValeur.qtemois
local prixmois = jsonValeur.prixmois
local tendance = jsonValeur.tendance
local stock0 = jsonValeur.stock0
local datestock0 = jsonValeur.datestock0
local qtelastmonth = jsonValeur.qtelastmonth
local prixlastmonth = jsonValeur.prixlastmonth
local qtesept = jsonValeur.qtesept
local coutsept = jsonValeur.coutsept
local qtestock = jsonValeur.qtestock
local prixstock = jsonValeur.prixstock
local coutotal = jsonValeur.coutotal
local coutreparation = jsonValeur.coutreparation
local coutentretien = jsonValeur.coutentretien
if qtemois ~= nil then voir_les_logs('--- --- --- qtemois : '..qtemois,debugging) end
if prixmois ~= nil then voir_les_logs('--- --- --- prixmois : '..prixmois,debugging) end
if tendance ~= nil then voir_les_logs('--- --- --- tendance : '..tendance,debugging) end
if stock0 ~= nil then voir_les_logs('--- --- --- stock0 : '..stock0,debugging) end
if datestock0 ~= nil then voir_les_logs('--- --- --- datestock0 : '..datestock0,debugging) end
if qtelastmonth ~= nil then voir_les_logs('--- --- --- qtelastmonth : '..qtelastmonth,debugging) end
if prixlastmonth ~= nil then voir_les_logs('--- --- --- prixlastmonth : '..prixlastmonth,debugging) end
if qtesept ~= nil then voir_les_logs('--- --- --- qtesept : '..qtesept,debugging) end
if coutsept ~= nil then voir_les_logs('--- --- --- coutsept : '..coutsept,debugging) end
if qtestock ~= nil then voir_les_logs('--- --- --- qtestock : '..qtestock,debugging) end
if prixstock ~= nil then voir_les_logs('--- --- --- prixstock : '..prixstock,debugging) end
if coutotal ~= nil then voir_les_logs('--- --- --- coutotal : '..coutotal,debugging) end
if coutreparation ~= nil then voir_les_logs('--- --- --- coutreparation : '..coutreparation,debugging)
else
coutreparation = 0
voir_les_logs('--- --- --- coutreparation : '..coutreparation,debugging)
end
if coutentretien ~= nil then
voir_les_logs('--- --- --- coutentretien : '..coutentretien,debugging)
else
coutentretien = 0
voir_les_logs('--- --- --- coutentretien : '..coutentretien,debugging)
end
else
voir_les_logs('--- --- --- aucun résultat à décoder',debugging)
end --if jsonValeur
-- ====================================================================================================================
voir_les_logs("======== Fin ".. nom_script .." (v".. version ..") ==========",debugging)
end --if time
end -- if script_actif
return commandArrayCode : Tout sélectionner
if otherdevices[dev_qtemois] == nil then creaDev(dev_qtemois, 5) end