lost a écrit : 02 août 2023, 11:29
Il n'y a qu'un extrait de code, mais la déclaration "FichierArp" visible ici est après le test d'existence du fichier?
Oups !... Cette ligne de déclaration n'existe pas dans le script. Je l'avais mise pour la compréhension, mais elle aurait dû être au tout début de l'extrait.
Voici donc l'intégralité du code, mais je doute que ce soit vraiment utile pour résoudre le problème. La seule difficulté, c'est l'impossibilité d'accéder au partage réseau du routeur.
Code : Tout sélectionner
--[[
Prerequisits
==================================
Domoticz v3.8837 or later (dzVents version 2.4 or later)
Scan ARP + ping
-- Authors ----------------------------------------------------------------
V1.0 - Neutrino
V1.1 - Aile
]]--
local scriptName = 'ARPing'
local scriptVersion = '1.1'
local scriptTrigger = 'MAJ'
local PingTrigger = 'Ping'
local Presences = ''
local PresenceOk = false
-- Variables to customize --------------------------------------------------
local IntervalleMinutes = 1 -- Délai Dzvents
local IntervalleSecondes = 10 -- Délai entre interrogations
local nbPings = 1 -- Nombre de pings pour tester la connexion
local timoutPings = 50 -- Timeout entre pings
local tempo = 1 -- Temporisation en minutes avant détection d'absence (évite les faux positifs)
local ClasseC = '10.0.0.'
local Panneau = 'Panneau de sécurité'
local DevicePresence = 'Présence'
local VariablePresence = 'Présences'
local armAwayToggleBtn = 'Alarme en absence'
local FichierArp = '\\\\10.0.0.254\\arp\\IPs.txt'
--Tableau des périphériques à interroger
-- ScanARP['adresse IP']={'Interrupteur virtuel',0}
local ScanARP = {}
ScanARP[ClasseC..'2']={'Julien',false}
ScanARP[ClasseC..'4']={'Cathy',false}
ScanARP[ClasseC..'6']={'Cédric',false}
ScanARP[ClasseC..'7']={'Sylvie',false}
ScanARP[ClasseC..'3']={'Luc',false}
------------------------------------------------------------------------------
-------------------------------
function Ping(IP,Domoticz,Item)
-------------------------------
command = "cmd /K Ping -n "..nbPings.." -w "..timoutPings.." "..IP
command = "Ping -n "..nbPings.." -w "..timoutPings.." "..IP
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
return string.find(result, 'temps=') ~= nil
end
--------------------------------
function ScanPresences(domoticz)
--------------------------------
if not domoticz.utils.fileExists(FichierArp) then
domoticz.log('Erreur lecture fichier ARP',domoticz.LOG_ERROR)
else
domoticz.log('Fichier lu !',domoticz.LOG_ERROR)
end
arp = {} -- Table pour les lignes du fichier
local fileArp = io.open(FichierArp,'r') -- Ouvre le fichier en mode lecture
for line in fileArp:lines() do -- Pour chaque ligne
table.insert(arp, line) -- On insère les lignes dans la table
end
io.close(fileArp)
for Ligne in pairs(arp) do
IPutilisateur = arp[Ligne]:sub(1,arp[Ligne]:find(" ")-1) --Extraction des IP de la table ARP
for word in arp[Ligne]:gmatch("%a+") do
StatutUtilisateur = word -- (REACHABLE, STALE, FAILED, etc.)
end
if ScanARP[IPutilisateur] ~= nil then -- IP recherchée
domoticz.log(IPutilisateur.." ("..ScanARP[IPutilisateur][1]..") : "..StatutUtilisateur,domoticz.LOG_DEBUG)
if arp[Ligne]:find("REACHABLE")~=nil then -- IP connue sur le réseau, connectée
domoticz.log("ARP Ok sur "..arp[Ligne], domoticz.LOG_DEBUG)
ScanARP[IPutilisateur][2]=true -- flag pour mise à On
elseif arp[Ligne]:find("STALE")~=nil or arp[Ligne]:find("DELAY")~=nil then -- IP connue sur le réseau, état incertain
domoticz.log("ARP douteux sur "..arp[Ligne], domoticz.LOG_DEBUG)
ScanARP[IPutilisateur][2]=Ping(IPutilisateur,domoticz,item)
else -- Adresse IP connue mais déconnectée
domoticz.log("Echec ARP "..arp[Ligne], domoticz.LOG_DEBUG)
ScanARP[IPutilisateur][2]=Ping(IPutilisateur,domoticz,item)
end
domoticz.log("Résultat du ping sur "..ScanARP[IPutilisateur][1].." : "..tostring(ScanARP[IPutilisateur][2]),domoticz.LOG_DEBUG)
else
domoticz.log(IPutilisateur.." : injoignable",domoticz.LOG_DEBUG)
end
end
ScanARPtri = {}
for IP in pairs(ScanARP) do
if ScanARP[IP][2] then
PresenceOk = true
table.insert(ScanARPtri, ScanARP[IP][1])
end
end
table.sort(ScanARPtri)
for Ligne,Valeur in pairs(ScanARPtri) do Presences = Presences..'-'..Valeur end
domoticz.log("Présence : "..tostring(PresenceOk),domoticz.LOG_DEBUG)
local TelephonesActifs = string.sub(Presences,2,string.len(Presences))
domoticz.log("Telephones actifs : "..TelephonesActifs,domoticz.LOG_DEBUG)
domoticz.devices(VariablePresence).updateText(TelephonesActifs)
return TelephonesActifs
end
return {
active = true,
logging = {
-- level = domoticz.LOG_INFO, -- Uncomment to override the dzVents global logging setting
marker = scriptName..' '..scriptVersion
},
on = {
timer = { 'every '..tostring(IntervalleMinutes)..' minutes' },
httpResponses = { scriptTrigger },
shellCommandResponses = { pingTrigger }
},
execute = function(domoticz, item)
if item.isTimer then
url = domoticz.settings['Domoticz url']
for seconds = IntervalleSecondes, (IntervalleMinutes * 60) - IntervalleSecondes, IntervalleSecondes do -- start with 10 step 10 until 290
domoticz.openURL({url = url, callback = scriptTrigger }).afterSec(seconds)
end
end
TelephonesActifs = ScanPresences(domoticz)
if domoticz.devices(armAwayToggleBtn).state == "Off" and tonumber(os.date("%M")) % 5 == 0 and tonumber(os.date("%S")) < 2 then
domoticz.log('********* Attention ! Bouton Alarme désactivé *********',domoticz.LOG_ERROR)
if domoticz.devices(Panneau).state == "Armed Away" then domoticz.devices(Panneau).disarm() end
return
end
if PresenceOk then
domoticz.devices(DevicePresence).switchOn()
if domoticz.devices(Panneau).state == "Armed Away" then -- and domoticz.devices(armAwayToggleBtn).state == "Off"
domoticz.log('********* Présence détectée ('..TelephonesActifs..') ===> Désactivation alarme *********',domoticz.LOG_FORCE)
domoticz.devices(Panneau).disarm()
end
else
if tonumber(os.date("%M")) % 5 == 0 and tonumber(os.date("%S")) < 2 then
domoticz.log('Aucune présence détectée depuis '..domoticz.devices(Panneau).lastUpdate.minutesAgo.." minute(s)",domoticz.LOG_FORCE)
end
if domoticz.devices(DevicePresence).lastUpdate.minutesAgo > tempo-1 then
domoticz.devices(DevicePresence).switchOff()
if domoticz.devices(Panneau).state == "Disarmed" and domoticz.devices(armAwayToggleBtn).state == "On" then
domoticz.log('********* Aucune présence détectée depuis '..domoticz.devices(DevicePresence).lastUpdate.minutesAgo..' minutes ===> Activation alarme *********',domoticz.LOG_FORCE)
domoticz.devices(Panneau).armAway()
end
end
end
end
}
lost a écrit : 02 août 2023, 11:29
Aucune raison pour que les changements sécurité pour une connexion entrante à Domoticz aient en impact pour de telles connexions sortantes. Surtout que le io.open() va ici appeler directement des fonctions du système.
[...]
A priori, si io.open() retourne 'nil' c'est que vu du système le fichier n'est pas présent/accessible (montage réseau, droits...)
Et pourtant, lorsque je suis sur le PC qui héberge Domoticz, j'accède sans problème au partage :

- Capture.JPG (39.11 Kio) Consulté 876 fois
lost a écrit : 02 août 2023, 11:29
Bizarre de mélanger des fonctions de Domoticz et du système sur ce genre de besoin purement système.
Tu peux développer ? Quelle aurait été la méthode "orthodoxe" ?