
dr_i("rel_pitot", "sim/operation/failures/rel_pitot")
dr_i("rel_pitot2", "sim/operation/failures/rel_pitot2")
dr_i("heat_on_1", "sim/cockpit/switches/pitot_heat_on")
dr_i("heat_on_2", "sim/cockpit/switches/pitot_heat_on2")
dr_f("airspeed_kts_pilot", "sim/cockpit2/gauges/indicators/airspeed_kts_pilot")

-- /*
-- *
-- * Cycle rate: DONE
-- * 	SLOW: 3 min
-- * 	FAST: 1 min
-- *

--  DONE Minimum heating time of front windshield in I. position before switching to position II: 7 minutes

--  The operation of the Pitot-static system heads and the stallprobe heating when the airplane is on the ground is limited as follows:
-- 	 - atambienttemperaturesaboveO°C (32OF): 1 minute maximum
-- 	 - atambienttemperaturesbelowO°C (32OF): 3 minutes maximum

-- wing ice
-- sim/flightmodel/failures/frm_ice
-- sim/flightmodel/failures/frm_ice2

-- prop ice
-- sim/flightmodel/failures/prop_ice_per_engine[0]

-- inlet
-- sim/flightmodel/failures/inlet_ice_per_engine[0]
-- * */

function bootIceLeftWing()
	-- console.log('bootIceLeftWing');
end

function bootIceRightWing()
	-- console.log('bootIceRightWing');
end

local windowTemperature = get(DR_OAT);
function checkWindowTemperature()
	if (get(DR_WINDOW_HEAT_SWITCH) == 1) then
		windowTemperature = windowTemperature + 0.5
	end
	if (get(DR_WINDOW_HEAT_SWITCH) == 2) then
		windowTemperature = windowTemperature + 1
	end
	if (get(DR_WINDOW_HEAT_SWITCH) == 0) then
		-- get closer to OAT
		if (windowTemperature > get(DR_OAT)) then
			windowTemperature = windowTemperature - get(DR_OAT) / 8;
		else
			windowTemperature = get(DR_OAT) - windowTemperature / 8;
		end
	end
	if (get(DR_WINDOW_HEAT_SWITCH) == 2 && windowTemperature < get(DR_OAT) + 4) then
		set(DR_WINDOW_HEAT_FAIL, 1)
	end

	-- cool down by IAS
	windowTemperature = windowTemperature - ((DR_IAS+0.001)/220);

	if (windowTemperature > 0) then
		set(DR_WINDOW_HEAT_FAIL, 0)
	else
		set(DR_WINDOW_HEAT_FAIL, 1)
	end
	-- 	console.log('windowTemperature', windowTemperature, DR_OAT);

end

local pitotTemperature = get(DR_OAT)
function checkPitotTemperature()
	if (get(DR_PITOT_HEAT_SWITCH) == 1) then
		pitotTemperature = pitotTemperature + 0.5
	end
	if (get(DR_PITOT_HEAT_SWITCH) == 0) then
		-- get closer to OAT
		if (pitotTemperature > get(DR_OAT)) then
			pitotTemperature = pitotTemperature - get(DR_OAT) / 4;
		else
			pitotTemperature = get(DR_OAT) - pitotTemperature / 4;
		end
	end

	-- cool down by IAS
	pitotTemperature = pitotTemperature - ((DR_IAS+0.001)/220);

	if (pitotTemperature > 120) then
		set(DR_PITOT_FAIL, 1)
	end
	-- console.log('pitotTemperature', pitotTemperature, DR_OAT);

end

local staticTemperature = get(DR_OAT)
function checkstaticTemperature()
	if (get(DR_STATIC_HEAT_SWITCH) == 1) then
		staticTemperature = staticTemperature + 0.5
	end
	if (get(DR_STATIC_HEAT_SWITCH) == 0) then
		-- get closer to OAT
		if (staticTemperature > get(DR_OAT)) then
			staticTemperature = staticTemperature - get(DR_OAT) / 4;
		else
			staticTemperature = get(DR_OAT) - staticTemperature / 4;
		end
	end

	-- cool down by IAS
	staticTemperature = staticTemperature - ((DR_IAS+0.001)/220);

	if (staticTemperature > 120) then
		set(DR_STATIC_FAIL, 1)
	end
	-- console.log('staticTemperature', staticTemperature, DR_OAT);

end

-- sim/flightmodel/failures/prop_ice_per_engine[]
-- sim/flightmodel/failures/inlet_ice_per_engine[]

function update()
	-- if (get(DR_FRAME_HEAT_SWITCH) == 1) then
	-- 	timeout(bootIceLeftWing, SLOW_RATE);
	-- 	timeout(bootIceRightWing, SLOW_RATE);
	-- end

	-- if (get(DR_FRAME_HEAT_SWITCH) == 2) then
	-- 	timeout(bootIceLeftWing, FAST_RATE);
	-- 	timeout(bootIceRightWing, FAST_RATE);
	-- end
	timeout(checkWindowTemperature, 1000);
	timeout(checkPitotTemperature, 1000);
end
