Connecting same type of connection between an equipement and a pipe

Hello , i am a newbie in Dynamo Revit ( i guess i am not first or the las to say that ^^ )

I am working in a MEP project , i tried to create a dynamo program to auto connect pipes to an equipement .
Here the variables :
i have x Spaces , y Equipement , z Pipes .
in each space i can have 1 or multiple Equipement , and 4 or more pipes
the equipement has 4 connectors of 4 different types
each pipe has a connector of a type from the 4 types

I am thinkin about an “algorythm” to resolve that :

select all : spaces , equipements with the type i want , the pipe then

For Each space in spaces
# For each equipement in equipements
## Check if the equipement is in the space
## For each connector in the equipement
### Test if the connector is connected
### For each pipe in Pipes
#### check if the pipe is in the space
##### Test if the pipe is connected
####### Test if the type of the pipe = type of the connector of the equipement
########## Calculate distance between the 2 connectors and save this distance
####Choose the smallest distance ( min of a list ) (#this follow "For each equipement’s connector
#### create a list of pair of conncetor

then finaly a python scrip to connect the pairs of connectior (i found this script)

Parameters :
each Space Has a location ( need it to locate the connector.location in the space with "IsInSpace"à
each equipement has a location , 4 connector , each connector has a type of parameter

I am just asking if the “algorithm” is correct( in logic) , and advices or link to the way how i can do it

I tried to do it for the moment i am stucked
Here my work :


and here the python script :

i am stucked in the comparaison between types of PipeSystemType