
dofile(LockOn_Options.script_path.."devices.lua")
dofile(LockOn_Options.script_path.."command_defs.lua")
dofile(LockOn_Options.script_path.."utils.lua")


local update_time_step = 0.02 --update will be called 50 times per second
make_default_activity(update_time_step) 
sensor_data                     =   get_base_data()
local   dev                     =   GetSelf()
local   aircraft                =   get_aircraft_type()
--------------------------------------------------------------------
--  Variables Declaration
local   GND_EPS                     
local   GND_PWR_2,GND_PWR_3,GND_PWR_4,GND_PWR_CC
local   SYSTEM_STAT,SYSTEM_TARGET  
local   AC_BUS_1                =   get_param_handle("AC_BUS_1")
local   AC_BUS_2                =   get_param_handle("AC_BUS_2")
local   DC_BUS_1                =   get_param_handle("DC_BUS_1")
local   DC_BUS_2                =   get_param_handle("DC_BUS_2")
local   ECS                     =   get_param_handle("ECS_READY") 
local   TMB_GND_PWR_1           =   get_param_handle("TMB_GND_PWR_1")

--[[ show_param_handles_list()   
    TMB_GND_PWR_1,SYSTEM_STAT_TEST,SYSTEM_TARGET_TEST]]

local   birth                   =   LockOn_Options.init_conditions.birth_place            
--------------------------------------------------------------------
local   function    elec_hot_start()
    GND_EPS                     =   1 
    dev:performClickableAction(device_commands.GND_PWR_2,  -1, false)
    dev:performClickableAction(device_commands.GND_PWR_3,  -1, false)    
    dev:performClickableAction(device_commands.GND_PWR_4,  -1, false)
    dev:performClickableAction(device_commands.GND_PWR_CC, -1, false)
end    
local   function    elec_cold_start()
    GND_EPS                     =   0
    dev:performClickableAction(device_commands.GND_PWR_2,  -1, false)
    dev:performClickableAction(device_commands.GND_PWR_3,  -1, false)    
    dev:performClickableAction(device_commands.GND_PWR_4,  -1, false)
    dev:performClickableAction(device_commands.GND_PWR_CC, -1, false)
end
function post_initialize()
    --print_message_to_user("DEV.ELEC_CONTROL_SYSTEM",0.2)                          debug
    if              birth           ==      "GROUND_HOT"            or 
                    birth           ==      "AIR_HOT"               then
                    elec_hot_start()

        
    elseif          birth           ==      "GROUND_COLD"           then
                    elec_cold_start()
     

    end

	
    dev:listen_command(Keys.iCommandPowerOnOff)

end
function SetCommand(command,value)
    if          command          ==  device_commands.GND_EPS                then
                dispatch_action(nil,Keys.iCommandPowerOnOff) 
    end
    if          command         ==  Keys.iCommandPowerOnOff                 then        
                GND_EPS         =   GND_EPS +   1
        if      GND_EPS         >   1                                       then
                GND_EPS         =   0
        end                  
    end 
    if          command         ==  device_commands.GND_PWR_2               then
                GND_PWR_2       =   value
    end
    if          command         ==  device_commands.GND_PWR_3               then
                GND_PWR_3       =   value
    end
    if          command         ==  device_commands.GND_PWR_4               then
                GND_PWR_4       =   value
    end     
    if          command         ==  device_commands.GND_PWR_CC              then
                GND_PWR_CC      =   value
    end

    
    if          GND_EPS      ==  1                                       then
                dev:AC_Generator_1_on(true)   
                dev:AC_Generator_2_on(true)   
                dev:DC_Battery_on(true)       
    else     
                dev:AC_Generator_1_on(false)   
                dev:AC_Generator_2_on(false)   
                dev:DC_Battery_on(false)
    end
     
end


function update()  
  
                AC_BUS_1:set(dev:get_AC_Bus_1_voltage())
                AC_BUS_2:set(dev:get_AC_Bus_2_voltage())
                DC_BUS_1:set(dev:get_DC_Bus_1_voltage())
                DC_BUS_2:set(dev:get_DC_Bus_2_voltage())
                ECS:set(GND_EPS) 
                TMB_GND_PWR_1:set(GND_EPS)
end

need_to_be_closed = false -- close lua state after initialization


