def run(path, mapping_path, server, eplus_config, default):

    with open(mapping_path, 'r') as f:
        s = f.read()
        mapping = json.loads(s)

    opc = OpenOPC.open_client()
    opc.connect(server)
    print("Connected to: " + server)

    #Default EnergyPlus Version to 8-4-0
    pyEp.set_eplus_dir(default)
    path_to_buildings = path

    buildings = []

    command = ""
    prev_command = ""
    while True:
        command, _, _ = opc_read('EPSimServer.EnergyPlus.Status', opc)
        command = int(command)
        print(command)
        if command is not prev_command:
            if command is 0:  #setup
                print("Setup")
                builder = pyEp.socket_builder(path_to_buildings)
                configs = builder.build()  #[port, building_folder_path, idf]

                #Setup EnergyPlus instances for each building
                for config in configs:
                    building_path = config[1]

                    building_name = os.path.normpath(building_path).split(
                        os.sep)[-1]
                    print(building_name)

                    #Set weather file and EnergyPlus version
                    weather_file = eplus_config[building_name][0]
                    eplus_path = None
                    print(weather_file)
                    if len(eplus_config[building_name]
                           ) > 1 and eplus_config[building_name][1] is not '':
                        eplus_path = eplus_config[building_name][1]

                    print(eplus_path)
                    ep = pyEp.ep_process('localhost', config[0], building_path,
                                         weather_file, eplus_path)

                    output = find_output_group(building_name, mapping)
                    output_rdy = find_output_rdy(building_name, mapping)
                    input = find_input_group(building_name, mapping)
                    input_rdy = find_input_rdy(building_name, mapping)
                    ts = find_ts_tag(building_name, mapping)

                    buildings.append(
                        Building(building_name, ep, output, input, input_rdy,
                                 output_rdy, ts))
                    opc.write((ts, 0))
                    opc.write((input_rdy, 0))
                    opc.write((output_rdy, 0))

                print("E+ Iniitalized, Waiting for Start Signal")
            elif command is 2:
                print("EnergyPlus Simulation finished")
            elif command is 3:  #reset
                print("Reset")
                for building in buildings:
                    building.ep.close()
                    delay.sleep(1)
                buildings = []
                delay.sleep(1)
            elif command is 4:  #close
                print("Closing")
                for building in buildings:
                    building.ep.close()
                opc.remove(opc.groups())
                opc.close()
                break
                sys.exit(0)
        elif command is 1:
            for building in buildings:
                building_name = building.name
                ep = building.ep
                input_group = building.input_group  #OPC group for easy reading from tags
                output_rdy = building.output_rdy
                input_rdy = building.input_rdy
                timestep_tag = building.timestep
                print(building_name)
                outputs = ep.decode_packet_simple(ep.read())
                print("Writing Outputs")
                output_mapping = map_outputs(
                    building_name, outputs,
                    mapping)  # Mapping is in (k, v) of TagNames, Value
                for tag, value in output_mapping:
                    opc.write((tag, value))

                #Notify controller that outputs are ready
                opc.write((output_rdy, 1))

                print("Waiting for new inputs")
                #Wait for controller to write new inputs

                inputs_ready = int(opc_read(input_rdy, opc)[0])
                while inputs_ready is not 1:
                    inputs_ready = int(opc_read(input_rdy, opc)[0])
                    temp_command, _, _ = opc_read(
                        'EPSimServer.EnergyPlus.Status', opc)
                    temp_command = int(temp_command)
                    if temp_command is not command:
                        print("Got Interrupt")
                        break

                print("Sending inputs to EnergyPlus")
                inputs = opc_read(input_group,
                                  opc)  # [(name, value, status, time)]
                #print(inputs)
                setpoints = [sp for (_, sp, _, _) in inputs]
                input_mapping = map_inputs(
                    building_name, inputs, mapping
                )  #input_mapping is a list of the values of the tags, in order
                time, _, _ = opc_read(timestep_tag, opc)
                encoded_packet = ep.encode_packet_simple(input_mapping, time)
                ep.write(encoded_packet)
                inputs_ready = opc.write((input_rdy, 0))

        prev_command = command
# TabQ Learning Parameters
learning_rate = 1  # Structural parameter
gamma = 0.5  # Structural parameter

TQL = TabularQLearning(lr=learning_rate,
                       gamma=gamma,
                       action_space_shape=n_actions,
                       state_space_shape=n_states)

pyEp.set_eplus_dir("C:\\EnergyPlusV9-2-0")

directory = os.path.dirname(os.path.realpath(__file__))

path_to_buildings = os.path.join(directory)
# 'C:\\Users\\SilvioBrandi\\Desktop'
builder = pyEp.socket_builder(path_to_buildings)
configs = builder.build()  # Configs is [port, building_folder_path, idf]
weather_file = 'GBR_London.Gatwick.037760_IWEC'
ep = pyEp.ep_process('localhost', configs[0][0], configs[0][1], weather_file)

#	Cosimulation
outputs = []

EPTimeStep = 1
SimDays = 365
kStep = 0
MAXSTEPS = int(SimDays * 24 * EPTimeStep) + 1
deltaT = (60 / EPTimeStep) * 60

print("Running Cosimulation with Total Steps " + str(MAXSTEPS))