Ce bout de code LUA permet de faire un Ping sur un équipement de votre réseau.
Première publication sur le Forum Fibaro ici http://forum.fibaro.com/viewtopic.php?t=1927
Les variables sont les suivantes:
deviceIp : Adresse IP du périphérique réseau à contacter.
devicePort : Port
maxRetryProcess : Nombre de tentatives pour contacter le périphérique.
--[[
%% properties
%% globals
--]]
-- Ping v 1.0.1 [05-2013]
-- Copyright © 2013 Jean-christophe Vermandé
fibaro:log("Start process");
local _deviceIp = "192.168.1.250";
local _devicePort = 80;
local _maxRetryProcess = 5;
-- recursive function to ping device
local function _ping(retry)
retry = retry or 0;
--open the socket
local tcpSocket = Net.FTcpSocket(_deviceIp, _devicePort);
--set the read timeout
tcpSocket:setReadTimeout(250);
--notify user
fibaro:log("Search on the local network, try #" .. retry .. " please wait...");
fibaro:sleep(250);
--send packet
local bytes, errorCode = tcpSocket:write("test");
--check for error
if errorCode == 0 then
return true;
else
if retry < _maxRetryProcess then
fibaro:log("Retry process, please wait...");
fibaro:sleep(1000);
return _ping(retry + 1);
end
return false;
end
end
--ping device, secure with pcall to catch errors.
local f, result = pcall(_ping);
if (f) then
if (result == true) then
fibaro:log("Device has been found, is awake and listening.");
else
fibaro:log("Device was not found!");
end
else
fibaro:log("Error: " .. f);
end
Notez l'utilisation de la fonction LUA pcall qui permet l’exécution du code en mode "protégé" ou "encapsulé", c'est à dire qu' il ne lèvera pas d' erreur dans le processus de votre box si jamais le code provoquait une erreur. La box est protégée, vos scènes aussi .