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={}

-- ---------------------------------
-- Réglage des consigne temperature
-- ---------------------------------

function callDomoticz(device, svalue)
	local idx = otherdevices_idx[device]
	if (svalue == nil) then
	print("CONSIGNE Call Domoticz Update canceled null value"..tostring(idx).." "..svalue)
	end
	print("CONSIGNE Call Domoticz IDx="..idx.." device="..device.." value="..tostring(svalue))

	--commandArray['SetSetpoint:'..idx]=svalue

	--local cmd ='curl -y 1 "http://localhost:81/json.htm?type=command&param=udevice&idx='..idx..'&nvalue=0&svalue='..svalue..'"'

  --local vac = os.capture(cmd, true)
	--os.execute(cmd)
	--debug("Domoticz called pour consige " .. cmd .. ": ") -- .. vac)
	print("CONSIGNE Consigne "..otherdevices_svalues[device].." svalue="..svalue.." idx="..tostring(idx).." "..device)
	if tonumber(otherdevices_svalues[device]) ~= tonumber(svalue) then
		update(idx,tonumber(svalue),tonumber(svalue))
		print("CONSIGNE Update "..tostring(idx).." "..svalue)
	else
		print("CONSIGNE Update inutile "..tostring(idx).." "..svalue)
	end
	--sleep(1)
end

--print("CONSIGNE ++++++++++++PRESENCE MANON "..otherdevices['Manon'])

if minutes%1 == 0 
	--and otherdevices['ChauffageGeneral'] == 'Off' 
then

	print('CONSIGNE ####### Lancement du calcul consigne ' ..heures..'h'..minutes)

	local temp_ext       = getExternalTempRef("BarometreLaGacilly")

	 local heureCreuse = (uservariables['HeuresCreuses'] == 'On')
	 local delta = -0.5 
	 if (heureCreuse) then
	 	delta = 0.5
	 end
	 -- Pas d'heure creuse
	 delta = 0

	 local jourS=josdGetJourSemaine()
	 local consigneConfortGrenier = 18
	 local consigneAbsence 		 = otherdevices['ConsigneAbsence'] + delta
	 local consigneConfort 		 = otherdevices['ConsigneChauffageGeneral'] + delta
	 local consigneConfortSalon  = uservariables['ConfortSalon'] + delta
	 local consigneConfortCuisine = uservariables['ConfortCuisine'] + delta
	 local consigneConfortBureau = uservariables["ConfortBureau"] + delta 
	 local consigneConfortManon  = uservariables["ConfortManon"] + delta
	 local consigneConfortTheo   = uservariables["ConfortTheo"] + delta
	 local consigneConfortChambre = uservariables["ConfortChambre"] + delta
	 --local consigneConfortSalon  = 17.5 + delta
	 local consigneHorsGel  = 14

	 -- Cheminee allumée 
	 local currentTempCheminee = tonumber(otherdevices_svalues["Cheminee"])
   	 local variation = variationTemp2("Cheminee", 120) -- variation des 120 dernières minutes

	 local coucheManon      = 17
	 local coucheTheo       = 17
	 local reveilManon      = uservariables["HeureLeveManon"] 
	 local reveilTheo       = uservariables["HeureLeveTheo"] 
	 local reveilManonEcole = uservariables["HeureLeveEcoleManon"] 
	 local reveilTheoEcole  = uservariables["HeureLeveEcoleTheo"] 
	 local presenceManon    = ( 
	 		otherdevices['Manon'] == "On" or uservariables['Detection_Manon_20H'] == 1
	 		or otherdevices["Zone B"] == 'On'
	 		or (((jourS == "vendredi" and heures >= 18) or jourS == "samedi" or 
	 			(jourS == "dimanche" and heures <= 18)
	 			)
	 		--	or otherdevices["Manon"] == 'On'
	 		--	)
	 			-- and uservariables['Detection_Manon_20H'] == "1"
	 			)
	 	) 
	 	and otherdevices['AbsenceFamille'] == 'Off'
	 local presenceTheo    = (
	 		otherdevices['Theo'] == "On" or uservariables['Detection_Theo_20H'] == 1
	 		or otherdevices["Zone B"] == 'On'
	 		or (
				--(jourS == "jeudi" and heures >= 18) or 
				(jourS == "vendredi" and heures >= 18) or 
				jourS == "samedi" or 
	 			(jourS == "dimanche" and uservariables['Detection_Theo_20H'] == 1) )
	 		--and uservariables['Detection_Theo_20H'] == "1")
	 		--	or otherdevices["Theo"] == 'On'
	 		--	)
	 		--	)
	 	) 
	 	and otherdevices['AbsenceFamille'] == 'Off' 
	 local presenceMoi    = (otherdevices['Moi'] == "On" ) 
	    or (
		(jourS == "lundi" or jourS == "mardi" or jourS == "mercredi" or jourS == "jeudi" or jourS == "vendredi") and (heures >= 8 and heures <= 18) and otherdevices['jerome'] == "On"
		)
	 local presenceDomi   = (otherdevices['Domi'] == "On" or jourS == "samedi" or jourS == "dimanche") 
	 				and otherdevices['AbsenceFamille'] == 'Off'
	 local yaPersonne = not (presenceManon or presenceTheo or presenceMoi or presenceDomi or otherdevices['PresenceForcee'] == 'On') 
	 if not yaPersonne then
	 	if (heures >= 7 and heures <= 9) or (heures >= 11 and heures <= 14) or (heures >= 19 and heures <= 21) then
	  		consigneConfortCuisine = uservariables['ConfortCuisine'] + delta
	 	else
	  		consigneConfortCuisine = uservariables['ConfortCuisine'] - 1
	 	end 
	 end
	 print("CONSIGNE Cheminee variation"..tostring(variation))
	 local cheminee_allumee = false
	 if (currentTempCheminee > 21.5 or (variation > 0 and currentTempCheminee >= 20)) then
		 cheminee_allumee = true;
	 	print('CONSIGNE Cheminee fonctionne : arrêt radiateurs')
	 	if (heures > 11) then
	 		consigneConfortSalon = consigneHorsGel
	 	end

	 	if (heures > 11 and heures < 20) then
		 	consigneConfortManon  = consigneAbsence
			consigneConfortTheo   = consigneAbsence
		 	consigneConfortChambre = consigneAbsence
		 end
		if ((not presenceMoi or otherdevices['jerome'] == 'Off') and otherdevices['Akhesa'] == 'Off') then
			consigneConfortBureau = consigneHorsGel
		end
	 end
     	 --if ((not presenceMoi or otherdevices['jerome'] == 'Off' or josdJourChome())) then
	 --	consigneConfortBureau = consigneHorsGel
	 --end

	 print("CONSIGNE Presence Manon "..tostring(presenceManon)); 
	 print("CONSIGNE Presence Theo "..tostring(presenceTheo)); 
	 print("CONSIGNE Presence moi "..tostring(presenceMoi)); 
	 if (otherdevices['Manon'] == 'On' and heures >= 17) then
	 	coucheManon = heures
	 end

	if (otherdevices['Vacances'] == 'On') then
			 print("CONSIGNE Vacances"); 

		callDomoticz('ConsigneConfortCuisine',consigneHorsGel)  -- Chambre
		callDomoticz('ConsigneConfortChambre',consigneHorsGel)  -- Chambre
		callDomoticz('ConsigneConfortTheo',consigneHorsGel)	 -- ChconsigneConfortambreTheo
		callDomoticz('ConsigneConfortManon', consigneHorsGel) -- ChambreManon
		callDomoticz('ConsigneConfortBureau',consigneHorsGel) -- Bureau
		callDomoticz('ConsigneConfortSalon',consigneHorsGel) -- Salon
		callDomoticz('ConsigneConfortGrenier',consigneHorsGel) -- Salon

	else

	   	if otherdevices['Force_Grenier'] == "On" then   
		   callDomoticz('ConsigneConfortGrenier',consigneConfortGrenier) 
	   	elseif heures > 18 or heures < 8 or josdJourChome() or otherdevices['Domi_Work'] == "Off" then
		   callDomoticz('ConsigneConfortGrenier', 6) --consigneHorsGel) 
	   	elseif otherdevices['Domi_Work'] == "On" then   
		   callDomoticz('ConsigneConfortGrenier',consigneConfortGrenier) 
		end
		 --commandArray['Presence']='Off
		 -- Presence à la maison
		 ------------------------------------------------------------------------
		 if (josdJourChome() or otherdevices['PresenceForcee'] == 'On') then
			 print('CONSIGNE        Jour Chômé / consigne temperature présence')
			 --  callDomoticz('ConsigneConfortBureau',consigneConfortBureau) -- Bureau
			 if (heures >= 21 or heures < 11 or yaPersonne) then
				 callDomoticz('ConsigneConfortCuisine',consigneAbsence) -- Salon
			 else
				 callDomoticz('ConsigneConfortCuisine',consigneConfortCuisine) -- Salon
			 end
			 if (heures >= 22 or heures < 8 or yaPersonne --or temp_ext < 3 
			 	or cheminee_allumee) 
			 then
				 callDomoticz('ConsigneConfortSalon',consigneAbsence) -- Salon
			 else
				 callDomoticz('ConsigneConfortSalon',consigneConfortSalon) -- Salon
			 end
			 if (heures >= 20 or heures < 8) then
				 callDomoticz('ConsigneConfortChambre',consigneConfortChambre)  -- Chambre
			 else
				 callDomoticz('ConsigneConfortChambre',consigneAbsence)  -- Chambre
			 end
			 if ( 
			 	(otherdevices['Akhesa'] == 'On' or otherdevices['jerome'] == 'On')
			 	and (heures > 7 and heures < 21)
				
			 ) then
				 callDomoticz('ConsigneConfortBureau',consigneConfortBureau) -- Bureau
			 else
				 callDomoticz('ConsigneConfortBureau',consigneHorsGel) -- Bureau
			 end
			 -- Heure Manon en vacances
			 if --(heures >= coucheManon or heures <= reveilManon) and 
			 	presenceManon then
				 callDomoticz('ConsigneConfortManon', consigneConfortManon) -- ChambreManon
			 else
				 callDomoticz('ConsigneConfortManon', consigneAbsence) -- ChambreManon
			 end
			 -- Heure Théo en vacances
			 if (heures >= coucheTheo or heures <= reveilTheo) and presenceTheo then
				 callDomoticz('ConsigneConfortTheo',consigneConfortTheo)	 -- ChambreTheo
			 else
				 callDomoticz('ConsigneConfortTheo',consigneAbsence)	 -- ChambreTheo
			 end
		 else
			-- 
			if otherdevices['AbsenceFamille'] == 'Off' then
				if otherdevices["Zone B"] == 'On' or otherdevices["PresenceForcee"] == 'On' then
					print('CONSIGNE        Jour vacances zone B ')
					if (heures >= 17 or heures <= 8) then
						 callDomoticz('ConsigneConfortChambre',consigneConfortChambre)  -- Chambre
					else
						 callDomoticz('ConsigneConfortChambre',consigneAbsence)  -- Chambre
					end

					if (heures >= 21 or heures < 11 or (yaPersonne and heures < 16)) then
						 callDomoticz('ConsigneConfortCuisine',consigneAbsence) -- Salon
					else
						 callDomoticz('ConsigneConfortCuisine',consigneConfortCuisine) -- Salon
					end
					
					if (heures >= 22 or heures < 16 or (yaPersonne and heures < 16)) then
						callDomoticz('ConsigneConfortSalon',consigneAbsence) -- Salon
					else
						callDomoticz('ConsigneConfortSalon',consigneConfortSalon) -- Salon
					end
					if (
						--heures >= 20 or heures < 9  or*/ 
						(otherdevices['Akhesa'] == 'Off' and otherdevices['jerome'] == 'Off')
						or (heures > 21 and heures < 7)
					) then
						callDomoticz('ConsigneConfortBureau',consigneHorsGel) -- Bureau
					else
						callDomoticz('ConsigneConfortBureau',consigneConfortBureau) -- Bureau
					end
					-- Heure Manon en vacances
					if (heures >= coucheManon or heures <= reveilManon) and presenceManon then
						callDomoticz('ConsigneConfortManon', consigneConfortManon) -- ChambreManon
					else
						callDomoticz('ConsigneConfortManon', consigneAbsence) -- ChambreManon
					end
					-- Heure Théo en vacances
					if (heures >= coucheTheo or heures <= reveilTheo) and presenceTheo then
						callDomoticz('ConsigneConfortTheo',consigneConfortTheo)	 -- ChambreTheo
					else
						callDomoticz('ConsigneConfortTheo',consigneAbsence)	 -- ChambreTheo
					end
					-- Heures hors vacances
				-- Ecole 
				else --if (heures >= 17 or heures < 8) then
					 print('CONSIGNE        Jour non Chômé pas de vacances '..tostring(heures).."h"..tostring(minutes))
					 if (heures >= 20 or heures <= 6) then
					    callDomoticz('ConsigneConfortChambre',consigneConfortChambre)  -- Chambre
					 else
					   callDomoticz('ConsigneConfortChambre',consigneAbsence)  -- Chambre
					 end

					 if (heures >= 21 or heures < 18 or (yaPersonne and heures < 18) --or temp_ext < 3
					 	) then
						 callDomoticz('ConsigneConfortCuisine',consigneAbsence) -- Salon
					 else
						 callDomoticz('ConsigneConfortCuisine',consigneConfortCuisine) -- Salon
					 end
					 if (heures <= 16 or heures >= 22 or (yaPersonne and heures < 16) --or temp_ext < 3
					 	) then
					  callDomoticz('ConsigneConfortSalon',consigneAbsence) -- Salon
				 	 else
  				      	  callDomoticz('ConsigneConfortSalon',consigneConfortSalon) -- Salon
					 end
					 
					 if (
					 	(otherdevices['Akhesa'] == 'On' or otherdevices['jerome'] == 'On' or presenceMoi) 
					 	and (heures >= 7 and heures < 18) 
					 ) then
					    callDomoticz('ConsigneConfortBureau',consigneConfortBureau) -- Bureau
				 	 else
					    callDomoticz('ConsigneConfortBureau',consigneHorsGel) -- Bureau
					 end

					-- Heure Manon école
					if (presenceManon) then
						callDomoticz('ConsigneConfortManon', consigneConfortManon) -- ChambreManon
					else 
						callDomoticz('ConsigneConfortManon', consigneAbsence) -- ChambreManon
					end
					if (presenceTheo and (otherdevices['Akhesa'] == 'Off' or heures >= 21)) then
						callDomoticz('ConsigneConfortTheo',consigneConfortTheo)	 -- ChambreTheo
					else 
						callDomoticz('ConsigneConfortTheo',consigneAbsence)	 -- ChambreTheo
					end
				 end

			 else
			 	 callDomoticz('ConsigneConfortCuisine',consigneAbsence)
				 callDomoticz('ConsigneConfortTheo',consigneHorsGel)	 -- ChambreTheo
				 callDomoticz('ConsigneConfortManon', consigneHorsGel) -- ChambreManon
				 -- Absence famille
				 if (
				 	((heures == 8 and minutes <= 15) or heures < 8) or heures >= 20) or 
				 	(otherdevices['Moi'] == 'On'
				 		or otherdevices['Akhesa'] == 'On' or otherdevices['Presence'] == 'On'
				 		or otherdevices['Nefertiti'] == 'On' or otherdevices['jerome'] == 'On'
					)
				 then
					callDomoticz('ConsigneConfortChambre',consigneConfortChambre)  -- Chambre
					if (heures >= 22 or heures < 19) then
						callDomoticz('ConsigneConfortSalon',consigneAbsence) -- Salon
					else
						callDomoticz('ConsigneConfortSalon',consigneConfortSalon) -- Salon
					end
					if (otherdevices['Akhesa'] == 'Off' and otherdevices['jerome'] == 'Off') 
						or ((heures > 21 and heures < 7) or cheminee_allumee)
					then
						 callDomoticz('ConsigneConfortBureau',consigneHorsGel) -- Bureau
				 	 else
						 callDomoticz('ConsigneConfortBureau',consigneConfortBureau) -- Bureau
					 end
				 else
					 callDomoticz('ConsigneConfortChambre',consigneAbsence)  -- Chambre
					 callDomoticz('ConsigneConfortBureau',consigneAbsence) -- Bureau
					 callDomoticz('ConsigneConfortSalon',consigneAbsence) -- Salon
					 callDomoticz('ConsigneConfortTheo',consigneAbsence)	 -- ChambreTheo
					 callDomoticz('ConsigneConfortManon', consigneAbsence) -- ChambreManon
					 callDomoticz('ConsigneConfortGrenier',consigneHorsGel) -- Salon

				 end
			 end
		 end
	end
end

return commandArray
