require "scripts/lua/functions"

-- URL du fichier JSON
local weather_url = "http://api.openweathermap.org/data/2.5/weather?q=La%20gacilly,fr&APPID=feba3f4d926db3b358a25ec782bd1c8b&lang=FR&units=metric"
local forecast_url = "http://api.openweathermap.org/data/2.5/forecast?q=La%20gacilly,fr&APPID=feba3f4d926db3b358a25ec782bd1c8b&lang=FR&units=metric"

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))

local function forecast(pressure)
	-- pressureForecast = tonumber(forecast)
	 
	if (pressure == 0) then
	    pressureForecast = 0
	elseif (pressure > 1030) then
	  	pressureForecast = 1
	elseif ((pressure > 1010) and (pressure <= 1030)) then
		pressureForecast = 2
	elseif ((pressure > 990) and (pressure <= 1010)) then
		pressureForecast = 3
	elseif ((pressure > 970) and (pressure <= 990)) then
		pressureForecast = 4
	end
	return pressureForecast
end

local function updateBarometerDevice(deviceId, temperature, humidity, pressure, cloud)
    local svalue = string.format("%.1f;%d;%d;%d;%d;", temperature, humidity, humidity, pressure, cloud)
    local url = domoticzURL..'/json.htm?type=command&param=udevice&idx='..deviceId..'&svalue='..svalue
    print(url)
    result = os.execute(curl..'"'..url..'" &')
    print(result)
end

commandArray = {}

if minutes == 0 and heures == 8 then
    if mois == 2 and jour == 1 then
        sendNotification('Changement d\'angle#Passer l\'angle des panneaux à 60°!')
    end
    if mois == 3 and jour == 1 then
        sendNotification('Changement d\'angle#Passer l\'angle des panneaux à 45°!')
    end
    if mois == 4 and jour == 24 then
        sendNotification('Changement d\'angle#Passer l\'angle des panneaux à 30°!')
    end
    if mois == 8 and jour == 19 then
        sendNotification('Changement d\'angle#Passer l\'angle des panneaux à 45°!')
    end
    if mois == 10 and jour == 13 then
        sendNotification('Changement d\'angle#Passer l\'angle des panneaux à 60°!')
    end
end

if minutes % 10 == 0 then
    local jsonContent = getJSONContent(weather_url)

    local devices = decodeJSON(jsonContent)
    printTableAsTree(devices)

    local nuage = 0
    local pluie_1h = 0
    local main = searchKeyInTable("main", devices['weather'])
    print('main='..main)
    local nuage = searchKeyInTable("all", devices['clouds'])

    print("pluie " .. pluie_1h)
    print("Nuage " .. nuage)

    updateDeviceValue(1159, ';'..pluie_1h)
    updateDeviceValue(1160, nuage)

    local temp_min = searchKeyInTable("temp", devices)
    local humidity = searchKeyInTable("humidity", devices)
    local pressure = searchKeyInTable("pressure", devices)
    local cloud = searchKeyInTable("all", devices)
 	local forecast = forecast(pressure)

    updateBarometerDevice(1157, temp_min, humidity, pressure, forecast)

    local temp = searchKeyInTable("temp", devices)
    local feels_like = searchKeyInTable("feels_like", devices)
    local deg = searchKeyInTable("deg", devices)
    local speed = searchKeyInTable("speed", devices) * 10
    local gust = searchKeyInTable("gust", devices) * 10
    local angle = searchKeyInTable("deg", devices['wind'])
    local compass = convertirAngle(angle) 

--    print("deg"..tostring(deg))
    updateDeviceValue(1158, string.format("%d;%s;%.1f;%.1f;%.1f;%.1f", deg, compass, speed, gust, temp, feels_like))
end

if heures % 3 == 0 and minutes == 26 then
    local jsonContent = getJSONContent(forecast_url)
    --print(jsonContent)
    local data = decodeJSON(jsonContent)

    -- Extraire les 6 premières valeurs du champ "all"
    local cloud_values = {}
    for i = 1, math.min(6, #data.list) do
        table.insert(cloud_values, data.list[i].clouds.all)
    end

    -- Afficher les valeurs extraites
    local somme = 0
    for i, value in ipairs(cloud_values) do
        print("Cloud coverage for point " .. i .. ": " .. value)
	somme = somme + tonumber(value)
    end
    local moyenne = somme / 6
    print("Cloud coverage average".. tostring(moyenne))
    updateDeviceValue(1199, tostring(moyenne))

end

return commandArray
