require "scripts/lua/functions"

--recupere les minutes
time=os.time()
seconds=tonumber(os.date('%S',time))
minutes=tonumber(os.date('%M',time))
heures=tonumber(os.date('%H',time))
jour=tonumber(os.date('%w',time))
mois=tonumber(os.date('%M',time))

--Initialise la commande de retour finale
commandArray={}

-- --------------------------------------------------------------------------------
-- --------------------------------------------------------------------------------
if (False and lastUpdateOfDevice('BatterieTension') > 60) then
	url = "http://192.168.1.18/getData"

	--result = os.execute(curl..'"'..url..'"')
   	local jsonContent = getJSONContent(url)
   	--print(cmd)

   	local statuses = decodeJSON(jsonContent)
   	--printTableAsTree(statuses)

    updatenum("BatterieCharge", 0)
    updatenum("BatterieInjection", 0)
	if isempty(statuses) then
       	print("BAT ERROR getData")        
		return commandArray
	end
    local tension = round(tonumber(statuses['V']) / 1000,2) -- tonumber(otherdevices['BatterieTension'])
    local temperature = round(tonumber(statuses['TEMPERATURE']),1) -- tonumber(otherdevices['BatterieTension'])

    updatenum("BatterieTension", tension)
    updatenum("BatterieCharge", tonumber(statuses['PPV']))
    updatenum("TemperatureBatterie", temperature)

	if tension > 20 and tension < 25.4 then
		switchOff("Batterie")
	      	-- switchOn("ChargeBatterie")
	end
end

	print('Capteur solaire redémarré'..tostring( lastUpdateOfDevice('SolaireProduction').. ' '..tostring(lastUpdateOfDevice('LUMIERE_PLANTE')) ))

if (lastUpdateOfDevice('Tuya_2') > 60 and lastUpdateOfDevice('LUMIERE_PLANTE') > 60)  then
	switchOff('LUMIERE_PLANTE')
	sleep(3)
	switchOn('LUMIERE_PLANTE')
	print('Capteur solaire redémarré')
end



return commandArray
