Comme inscrit dans le titre, je souhaiterais que l'on analyse un code lua que j'ai créé a taton avec mes très maigres connaissances avec ces langages
J'ai demandé a un ami d’essayer de m'améliorer ce code disons le très basique,
Avant de l'utiliser j'aimerais en discuter avec les spécialistes présents ici
Voici mon code d'origine
Code : Tout sélectionner
return { active = true, logging = { level = domoticz.LOG_DEBUG,marker = 'Mini_Inter_LED_Lidl'},on = { devices = { 'Mini_Inter', 'LED_Lidl',},}, -- {validation du script {le script écrit dans les LOG_DEBUG de DomoticZ , -- nom du script='Mini_Inter_LED_Lidl'} {liste des devices{nom des devices='Mini_Inter', 'LED_Lidl'}}
execute = function ( domoticz, item) domoticz.log ( "Mini_Inter : "..item.state) -- EXECUTER la fonction DomoticZ , -- VERIFIER l'état de Mini_Inter dans les logs DomoticZ
if item.state == 'simple_pression' then -- SI l'état de Mini_Inter passe a simple_pression ALORS
domoticz.log ( "simple_pression sur Mini_Inter > LED_Lidl = On") -- VALIDATION de simple_pression sur Mini_Inter dans les logs PASSE a la suite
domoticz.devices ( 'LED_Lidl').switchOn() -- ALLUMER device LED_Lidl
elseif item.state == 'double_pression' then -- SINON SI l'état de Mini_Inter passe a double_pression ALORS
domoticz.log ( "double_pression sur Mini_Inter > LED_Lidl =") -- VALIDATION de double_pression sur Mini_Inter dans les logs PASSE a la suite
domoticz.devices ( 'LED_Lidl') -- REGLER device LED_Lidl (a définir)
elseif item.state == 'longue_pression' then -- SINON SI l'état de Mini_Inter passe a longue_pression ALORS
domoticz.log ( "longue_pression sur Mini_Inter > LED_Lidl = Off") -- VALIDATION de longue_pression sur Mini_Inter dans les logs PASSE a la suite
domoticz.devices ( 'LED_Lidl').switchOff() -- ETEINDRE device LED_Lidl
end end}Voici le code améliorer de mon ami
Code : Tout sélectionner
return {
active = true,
logging = {
level = domoticz.LOG_DEBUG,
marker = 'Mini_Inter_LED_Lidl'
},
on = {
devices = { 'Mini_Inter', 'LED_Lidl' },
},
execute = function (domoticz, item)
-- Si l'appareil Mini_Inter n'est pas présent, sortir de la fonction
if (item.isDevice == false) then
domoticz.log("Device Mini_Inter not found", domoticz.LOG_ERROR)
return
end
-- Si l'appareil LED_Lidl n'est pas présent, sortir de la fonction
if (domoticz.devices('LED_Lidl').isDevice == false) then
domoticz.log("Device LED_Lidl not found", domoticz.LOG_ERROR)
return
end
-- Si l'appareil LED_Lidl n'est pas un switch, sortir de la fonction
if (domoticz.devices('LED_Lidl').switchType == nil) then
domoticz.log("Device LED_Lidl is not a switch", domoticz.LOG_ERROR)
return
end
-- Log l'état de Mini_Inter
domoticz.log("Mini_Inter : " .. item.state)
-- Si l'état de Mini_Inter est "simple_pression"
if (item.state == 'simple_pression') then
domoticz.log("Simple pression on Mini_Inter > LED_Lidl = On")
domoticz.devices('LED_Lidl').switchOn()
-- Définir un minuteur pour éteindre l'ampoule après LIGHT_DELAY secondes
domoticz.openURL("http://127.0.0.1:8080/json.htm?type=command¶m=switchlight&idx=".. LIGHT_SWITCH_IDX .. "&switchcmd=Off&level=0&passcode=1234&timer="..LIGHT_DELAY)
-- Si l'état de Mini_Inter est "double_pression"
elseif (item.state == 'double_pression') then
domoticz.log("Double pression on Mini_Inter > LED_Lidl = ?")
-- TODO: Ajouter le comportement souhaité pour l'état "double_pression"
-- Si l'état de Mini_Inter est "longue_pression"
elseif (item.state == 'longue_pression') then
domoticz.log("Long pression on Mini_Inter > LED_Lidl = Off")
domoticz.devices('LED_Lidl').switchOff()
end
end
}