@Lazer
j'ai importé et paramètre le VD mais celui ci ne fonctionne pas.
Une idée de ce que j'ai fait comme erreur ?
--------------------------------------------------
-- Module : Network Monitor
-- Author : Lazer with contribution of JM13
-- Version : 1.20
-- Date : July 2016
--------------------------------------------------
-- User variables
local userID = {} -- Mail
local smartphoneID = {627,628, 629} -- Push
local sms = {
["VD_ID"] = 0, -- Virtual Device ID
["VD_Button"] = "1", -- Virtual Device Button
["VG_Name"] = "SMS" -- Global Variable Name
}
local icons = {
["OK"] = 1016, -- Normal Icon ID
["Error"] = 1017 -- Error Icon ID
}
local DEVICES = {
-- ["Service Name"] = { IP Address , Port, Protocol, Data to send, Expected return code, Expected data, Retry, {Notification methods} , Global Variable},
["Google"] = {"www.google.fr", 80, "http", "/", 200, "Recherche Google" , 1, { "smartphoneID"}, nil, {"InternetDown", "1"}}}
}
local debug = false
-- System variables
local erreur = 0
-- Notification function
function Notification(message, param, smartphoneID, userID, sms)
local message = message or "<vide>"
fibaro:debug("Notification : "..message)
if param then
for _, notif in ipairs(param) do
if debug then
fibaro:debug(notif)
end
-- Envoi Push
if notif == "push" and smartphoneID then
for _, id in ipairs(smartphoneID) do
if debug then
fibaro:debug("Send Push smartphone ID : "..id)
end
fibaro:call(id, "sendPush", message)
end
-- Envoi Email
elseif notif == "email" and userID then
for _, id in ipairs(userID) do
if debug then
fibaro:debug("Send Email user ID : "..id)
end
fibaro:call(id, "sendEmail", "HC2 Network Monitor", message)
end
-- Envoi SMS
elseif notif == "sms" and sms then
if debug then
fibaro:debug("Send SMS : VD_ID="..(sms["VD_ID"] or 0).." VD_Button="..(sms["VD_Button"] or "0").." VG_Name="..(sms["VG_Name"] or ""))
end
fibaro:setGlobal(sms["VG_Name"], message)
if sms["VD_ID"] and tonumber(sms["VD_ID"])>0 and sms["VD_Button"] and tonumber(sms["VD_Button"])>0 then
fibaro:call(sms["VD_ID"], "pressButton", sms["VD_Button"])
end
end
end
else
fibaro:debug('<span style="color:orange;">Warning : no notification options given</span>')
end
end
-- Function : Create global variable
function CreateVG(varName, varValue, varEnum)
local isEnum = (#(varEnum or {}) > 0) and 1 or 0
local HC2 = Net.FHttp("127.0.0.1", 11111)
local payload = '{"name":"'..varName..'","isEnum":'..isEnum..',"value":"'..(varValue or "")..'"}'
local response, status, errorCode = HC2:POST("/api/globalVariables/"..varName, payload)
if tonumber(errorCode) == 0 and (tonumber(status) == 200 or tonumber(status) == 201) and response ~= nil and response ~= "" then
fibaro:debug('Global variable "'..varName..'" created')
if isEnum > 0 then
local payload = '{"name":"'..varName..'","isEnum":true,"enumValues":'..json.encode(varEnum)..'}'
local response, status, errorCode = HC2:PUT("/api/globalVariables/"..varName, payload)
if tonumber(errorCode) == 0 and (tonumber(status) == 200 or tonumber(status) == 201) and response ~= nil and response ~= "" then
fibaro:debug('Global variable "'..varName..'" modified with enum values')
else
fibaro:debug('<span style="display:inline;color:red;">Error : Can not modify enum global variable, errorCode='..errorCode..', status='..status..', payload='..payload..', response='..(response or "")..'</span>')
end
end
else
fibaro:debug('<span style="display:inline;color:red;">Error : Can not create global variable, errorCode='..errorCode..', status='..status..', payload='..payload..', response='..(response or "")..'</span>')
end
HC2 = nil
end
-- Check if global variables exist
local HC2 = Net.FHttp("127.0.0.1", 11111)
for _, param in pairs(DEVICES) do
if param[9] and param[9] ~= "" then
if debug then
fibaro:debug("Check if global variable '"..param[9].."' exists")
end
local response, status, errorCode = HC2:GET("/api/globalVariables/"..param[9])
if tonumber(errorCode) == 0 and tonumber(status) == 200 and response ~= nil and response ~= "" then
local jsonTable = json.decode(response)
if not jsonTable.name or jsonTable.name ~= param[9] then
fibaro:debug('Response OK but global variable "'..param[9]..'" does not exist...')
CreateVG(param[9], "0", nil)
end
else
fibaro:debug('Global variable "'..param[9]..'" does not exist...')
CreateVG(param[9], "0", nil)
end
end
end
HC2 = nil
-- Main
fibaro:debug(os.date('%d/%m/%Y'))
local selfID = fibaro:getSelfId()
fibaro:call(selfID, "setProperty", "ui.LabelCheck.value", os.date('%d/%m/%Y %H:%M:%S'))
local j = 1
for device, param in pairs(DEVICES) do
if debug then
fibaro:debug(device.." => "..param[1])
end
local message = ""
for i=1, param[7] do
-- Check exclusion global variable
local exclude = false
if param[10] and type(param[10]) == "table" and param[10][1] and type(param[10][1]) == "string" and param[10][1] ~= "" and param[10][2] and type(param[10][2]) == "string" and param[10][2] ~= "" then
local value = fibaro:getGlobalValue(param[10][1])
if value == param[10][2] then
if debug then
fibaro:debug("Found VG="..param[10][1].." => value = "..param[10][2].." == "..value.." => Exclude")
end
exclude = true
elseif debug then
fibaro:debug("Found VG="..param[10][1].." => value = "..param[10][2].." <> "..value.." => Keep")
end
end
-- Debug
if debug then
fibaro:debug("Try #"..i)
end
-- Wait 1s between 2 connect attempts
if i > 1 then
fibaro:sleep(1*1000)
end
if not exclude then
-- TCP
if param[3] == "tcp" then
local tcpSocket = Net.FTcpSocket(param[1], param[2])
tcpSocket:setReadTimeout(1000)
if param[4] ~= nil then
local response, errorCode = tcpSocket:write("\n")
fibaro:sleep(1000)
end
local response, errorCode = tcpSocket:read()
if debug then
fibaro:debug("errorCode=" .. errorCode .. " - response=" .. response)
end
if tonumber(errorCode) == 0 then -- Socket opened
if (response ~= nil) then
if response:match(param[6]) then
if debug then
fibaro:debug("OK")
end
message = ""
break
else
fibaro:debug('<span style="color:red;">Error : '..param[1]..' => '..device..' => unattended response, errorCode='..errorCode..'</span>')
message = "Réponse inattendue pour "..device
erreur = erreur + 1
end
else
fibaro:debug('<span style="color:red;">Error : '..param[1]..' => '..device..' => empty response, errorCode='..errorCode..'</span>')
message = "Réponse vide reçue pour "..device
erreur = erreur + 1
end
elseif tonumber(errorCode) == 1 then -- Socket opened, but no data received
if debug then
fibaro:debug("OK")
end
message = ""
break
else -- Socket not opened
fibaro:debug('<span style="color:red;">Error : '..param[1]..' => '..device..' => can not connect to server, errorCode='..errorCode..'</span>')
message = "Impossible de se connecter à "..device
erreur = erreur + 1
end
tcpSocket:disconnect()
-- HTTP
elseif param[3] == "http" then
local server = Net.FHttp(param[1], param[2])
local response, status, errorCode = server:GET(param[4])
if debug then
fibaro:debug("errorCode=" .. errorCode .. " - status=" .. status .. " response length = " .. string.len(response))
--fibaro:debug("response=" .. response)
end
if tonumber(errorCode) == 0 then
if tonumber(status) == param[5] then
if (response ~= nil) then
if response:match(param[6]) then
if debug then
fibaro:debug("OK")
end
message = ""
break
else
fibaro:debug('<span style="color:red;">Error : '..param[1]..' => '..device..' => unattended response, errorCode='..errorCode..', status='..status..'</span>')
message = "Réponse inattendue pour "..device
erreur = erreur + 1
end
else
fibaro:debug('<span style="color:red;">Error : '..param[1]..' => '..device..' => empty response, errorCode='..errorCode..', status='..status..'</span>')
message = "Réponse vide reçue pour "..device
erreur = erreur + 1
end
else
fibaro:debug('<span style="color:red;">Error : '..param[1]..' => '..device..' => invalid http return code, errorCode='..errorCode..', status='..status..'</span>')
message = "Code de retour http invalide pour "..device
erreur = erreur + 1
end
else
fibaro:debug('<span style="color:red;">Error : '..param[1]..' => '..device..' => can not connect to server, errorCode='..errorCode..', status='..status..'</span>')
message = "Impossible de se connecter à "..device
erreur = erreur + 1
end
server = nil
end -- if param[3] == "tcp" or "http"
end -- if exclude
end -- for i=1, param[7]
if message ~= "" then
-- Send notification
Notification("Attention : "..message, param[8], smartphoneID, userID, sms)
-- Increment global variable
if param[9] and param[9] ~= "" then
fibaro:debug('Increment global variable "'..param[9]..'" value')
fibaro:setGlobal(param[9], tostring((tonumber(fibaro:getGlobalValue(param[9]) or "0") or 0) + 1))
end
-- Update labels
fibaro:call(selfID, "setProperty", "ui.LabelError.value", os.date('%d/%m/%Y %H:%M:%S'))
if j <= 5 then
fibaro:call(selfID, "setProperty", "ui.LabelMessage"..j..".value", message)
j = j + 1
end
elseif param[9] and param[9] ~= "" then
if fibaro:getGlobalValue(param[9]) ~= "0" then
fibaro:debug('Reset global variable "'..param[9]..'" to "0" value')
fibaro:setGlobal(param[9], "0")
end
end
end -- for device, param in pairs(DEVICES)
-- Clean labels
for k=j, 5 do
local label = fibaro:get(selfID, "ui.LabelMessage"..k..".value")
if label ~= "" then
fibaro:call(selfID, "setProperty", "ui.LabelMessage"..k..".value", "")
end
end
-- Update label, log, icon
if erreur > 0 then
fibaro:log("Erreur")
if icons["Error"] and tonumber(icons["Error"])>0 then
local currentIcon = fibaro:getValue(selfID, "currentIcon")
if tonumber(currentIcon) ~= tonumber(icons["Error"]) then
fibaro:call(selfID, "setProperty", "currentIcon", icons["Error"])
end
end
else
fibaro:log("Network OK")
if icons["OK"] and tonumber(icons["OK"])>0 then
local currentIcon = fibaro:getValue(selfID, "currentIcon")
if tonumber(currentIcon) ~= tonumber(icons["OK"]) then
fibaro:call(selfID, "setProperty", "currentIcon", icons["OK"])
end
end
end
Concernant GEA, Tu n'aurais pas la ligne sous le coude . ?