Created
June 22, 2017 10:35
-
-
Save yoimbert/15184a3c675fcff2955f7a309e0a4336 to your computer and use it in GitHub Desktop.
Add VG Hc2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Global variables | |
if not VG then | |
VG = { | |
['p_freebox_appToken'] = {['default']='' , ['enum']={}}, | |
['p_freebox_lastcmd'] = {['default']='' , ['enum']={}}, | |
['p_freebox_trackId'] = {['default']='' , ['enum']={}}, | |
['FbxV6_IP'] = {['default']='' , ['enum']={}}, | |
['FbxV6_HddFree'] = {['default']='0' , ['enum']={}}, | |
['FbxV6_HddTemp'] = {['default']='0' , ['enum']={}}, | |
['FbxV6_Cnx'] = {['default']='down' , ['enum']={"down","going_down","going_up","up"}}, | |
['FbxV6_WiFi'] = {['default']='Inactif', ['enum']={"Actif","Inactif"}}, | |
['FbxV6_CPUbTemp'] = {['default']='0' , ['enum']={}}, | |
['FbxV6_CPUmTemp'] = {['default']='0' , ['enum']={}}, | |
['FbxV6_SwTemp'] = {['default']='0' , ['enum']={}}, | |
['FbxV6_FanRPM'] = {['default']='0' , ['enum']={}}, | |
['FbxV6_FwVersion'] = {['default']='0' , ['enum']={}}, | |
['FbxV6_WiFiDev1'] = {['default']='0' , ['enum']={}}, | |
['FbxV6_WiFiDev2'] = {['default']='0' , ['enum']={}} | |
} | |
-- Function : Create global variable | |
function CreateVG(varName, varValue, varEnum) | |
local isEnum = (#varEnum > 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 vg, param in pairs(VG) do | |
fibaro:debug("Check if global variable '"..vg.."' exists") | |
local response, status, errorCode = HC2:GET("/api/globalVariables/"..vg) | |
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 ~= vg then | |
fibaro:debug('Response OK but global variable "'..vg..'" does not exist...') | |
CreateVG(vg, param['default'], param['enum']) | |
end | |
else | |
fibaro:debug('Global variable "'..vg..'" does not exist...') | |
CreateVG(vg, param['default'], param['enum']) | |
end | |
end | |
end -- if not VG | |
-- Exemple de résultat attendu dans la fenêtre de debug : | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_WiFiDev2' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_WiFiDev2" does not exist... | |
--[DEBUG] 23:44:02: Global variable "FbxV6_WiFiDev2" created | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_Cnx' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_Cnx" does not exist... | |
--[DEBUG] 23:44:02: Global variable "FbxV6_Cnx" created | |
--[DEBUG] 23:44:02: Global variable "FbxV6_Cnx" modified with enum values | |
--[DEBUG] 23:44:02: Check if global variable 'p_freebox_appToken' exists | |
--[DEBUG] 23:44:02: Global variable "p_freebox_appToken" does not exist... | |
--[DEBUG] 23:44:02: Global variable "p_freebox_appToken" created | |
--[DEBUG] 23:44:02: Check if global variable 'p_freebox_lastcmd' exists | |
--[DEBUG] 23:44:02: Global variable "p_freebox_lastcmd" does not exist... | |
--[DEBUG] 23:44:02: Global variable "p_freebox_lastcmd" created | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_WiFiDev1' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_WiFiDev1" does not exist... | |
--[DEBUG] 23:44:02: Global variable "FbxV6_WiFiDev1" created | |
--[DEBUG] 23:44:02: Check if global variable 'p_freebox_trackId' exists | |
--[DEBUG] 23:44:02: Global variable "p_freebox_trackId" does not exist... | |
--[DEBUG] 23:44:02: Global variable "p_freebox_trackId" created | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_FwVersion' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_FwVersion" does not exist... | |
--[DEBUG] 23:44:02: Global variable "FbxV6_FwVersion" created | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_HddTemp' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_HddTemp" does not exist... | |
--[DEBUG] 23:44:02: Global variable "FbxV6_HddTemp" created | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_SwTemp' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_SwTemp" does not exist... | |
--[DEBUG] 23:44:02: Global variable "FbxV6_SwTemp" created | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_FanRPM' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_FanRPM" does not exist... | |
--[DEBUG] 23:44:02: Global variable "FbxV6_FanRPM" created | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_CPUbTemp' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_CPUbTemp" does not exist...-- | |
--[DEBUG] 23:44:02: Global variable "FbxV6_CPUbTemp" created | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_WiFi' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_WiFi" does not exist... | |
--[DEBUG] 23:44:02: Global variable "FbxV6_WiFi" created | |
--[DEBUG] 23:44:02: Global variable "FbxV6_WiFi" modified with enum values | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_IP' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_IP" does not exist... | |
--[DEBUG] 23:44:02: Global variable "FbxV6_IP" created | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_CPUmTemp' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_CPUmTemp" does not exist... | |
--[DEBUG] 23:44:02: Global variable "FbxV6_CPUmTemp" created | |
--[DEBUG] 23:44:02: Check if global variable 'FbxV6_HddFree' exists | |
--[DEBUG] 23:44:02: Global variable "FbxV6_HddFree" does not exist... | |
--[DEBUG] 23:44:03: Global variable "FbxV6_HddFree" created |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment