
###########
### CONNECTING TO unCoVer OPAL SERVERS ###
### Last edit: 20230120/JP
###########

#RStudio 2022.12.0+353 [Mac OS Monterey 12.6.3]

.rs.restartR()

###########
###1. install required packages [You only need to install this the first time]
###2. make sure you are connected to Open VPN ONLY and check which hospitals are unavailable from daily email
###3. prep login to the servers (# servers that are down, or those that doesn't interest)
###4. login
###5. check available project/resources per partner (and logout) 
###6. printing connection's details
###7. specify paths to resources (comment out those that are not available or not interested)
###8. resolve object to load datasets
###9. describe uploads
###10. logout from all servers 
###########

###1. install required packages [You only need to install this the first time]

# install.packages("devtools")

# devtools::install_version("opalr", version = "3.1.0", repos = 'https://cran.r-project.org/')
# devtools::install_version("DSI", version = "1.4.0", repos = 'https://cran.r-project.org/')
# devtools::install_version("DSOpal", version = "1.3.0", repos = 'https://cran.r-project.org/')
# devtools::install_github("datashield/dsBaseClient", "6.1.1")
# devtools::install_github("lifecycle-project/ds-helper", "0.4.12")
# devtools::install_github("neelsoumya/dsSurvivalClient")
# devtools::install_github("transbioZI/dsMTLClient")
# devtools::install_github("isglobal-brge/dsDatesClient")

# load the required libraries

library(DSI)
library(DSOpal)
library(dsBaseClient)
library(httr)

###2. make sure you are connected to Open VPN ONLY and check which hospitals are unavailable from daily email

# not available on 20230120:

# Hospital_Princesa
# Hospital_CIPH
# Hospital_TU
# Hospital_SMUC
# Hospital_ASPEUR
# Hospital_Oporto
# Hospital_UDEA

###3. prep login to the servers (# servers that are down, or those that doesnt interest)

builder <- DSI::newDSLoginBuilder()

#### BELGIUM ####
builder$append(server = "UZA", url = "https://uncover.itg.be", user = "emertens", password = "3^z4AV.)hG5~PT/]")

#### BRAZIL ##
builder$append(server = "ASPEUR", url = "https://uncover.itg.be", user = "emertens", password = "3^z4AV.)hG5~PT/]")

#### BOSNIA ####
#builder$append(server = "UNSA_Inpatient", url = "https://192.168.1.200:8008", user = "user_analisis", password = "Ekfl07UUgz")

#### COLOMBIA ####
#builder$append(server = "UDEA", url = "https://fenfisdi.udea.edu.co/opal", user = "user_analisis", password = "Ekfl07UUgz")

#### CROATIA ####
#builder$append(server = "CIPH", url = "https://192.168.1.200:8002", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "INANTRO", url = "https://192.168.1.200:8007", user = "user_analisis", password = "Ekfl07UUgz")

#### ITALY ####
#builder$append(server = "Sacrocuore_emergency", url = "https://192.168.1.50:8890", user = "user_analisis", password = "Ekfl07UUgz")
#builder$append(server = "Sacrocuore_employees", url = "https://192.168.1.50:8890", user = "user_analisis", password = "Ekfl07UUgz")
#builder$append(server = "Sacrocuore_Verona", url = "https://192.168.1.50:8890", user = "user_analisis", password = "Ekfl07UUgz")
#builder$append(server = "Sacrocuore_ISARIC", url = "https://192.168.1.50:8890", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "ULSS6", url = "https://opal.aulss6.veneto.it", user = "user_analisis", password = "Ekfl07UUgz")

#### IRELAND ####
builder$append(server = "TUDublin", url = "https://uncover.itg.be", user = "emertens", password = "3^z4AV.)hG5~PT/]")

#### LUXEMBOURG ###


#### PORTUGAL ####
#builder$append(server = "Porto", url = "https://192.168.1.102", user = "user_analisis", password = "Ekfl07UUgz")
#builder$append(server = "Coimbra", url = "https://uncover.itg.be", user = "emertens", password = "3^z4AV.)hG5~PT/]")

#### ROMANIA ####
builder$append(server = "UMF_Iasi", url = "https://192.168.1.200:8003", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "UMF_Cluj", url = "https://192.168.1.200:8005", user = "user_analisis", password = "Ekfl07UUgz")

### SLOVAKIA ###
builder$append(server = "TU", url = "https://192.168.1.200:8004", user = "user_analisis", password = "Ekfl07UUgz")

#### SPAIN ####
builder$append(server = "hm_hospitales", url = "https://192.168.1.50:9002", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "FJD", url = "https://uncover.itg.be", user = "emertens", password = "3^z4AV.)hG5~PT/]")
builder$append(server = "UNAV", url = "https://192.168.1.50:9001", user = "user_analisis", password = "Ekfl07UUgz")
#builder$append(server = "princesa", url = "https://192.168.1.200:8001", user = "user_analisis", password = "Ekfl07UUgz")

#### TURKEY ####
builder$append(server = "Ankara_inpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Ankara_outpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Ankara_icu_inpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Ankara_icu_outpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Konya_inpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Instanbul_inpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Instanbul_outpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Izmir_inpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Alanya_inpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Alanya_outpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Adana_inpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")
builder$append(server = "Adana_outpatient", url = "https://192.168.1.101:8443", user = "user_analisis", password = "Ekfl07UUgz")

#### UK ####
#builder$append(server = "SMUC", url = "https://192.168.1.200:8006", user = "user_analisis", password = "Ekfl07UUgz")

###4.login

logindata <- builder$build()
set_config(config(ssl_verifypeer = 0L, ssl_verifyhost = 0L))
connections <- DSI::datashield.login(logins = logindata, failSafe = T)

###6. printing connection's details
#connections 

###7. specify paths to resources (comment out those that are not available or not interested)

### BELGIUM ###
DSI::datashield.assign.resource(connections$UZA, "resource", "UZA.20220919_UZA")

### BOSNIA ###
#DSI::datashield.assign.resource(connections$UNSA_Inpatient, "resource", "UnCoVer-BiH-Final.20220919_UNSAInpatient")

### BRAZIL ###
DSI::datashield.assign.resource(connections$ASPEUR, "resource", "ASPEUR.20220919_ASPEUR")

### COLOMBIA ###
# DSI::datashield.assign.resource(connections$UDEA, "resource", "INS_Data.colombia_all")

### CROATIA ###
DSI::datashield.assign.resource(connections$INANTRO, "resource", "INANTRO.20220919_INANTRO")

### ITALY ###
#DSI::datashield.assign.resource(connections$Sacrocuore_Verona, "resource", "S_uncover.20220919_Verona")
#DSI::datashield.assign.resource(connections$Sacrocuore_emergency, "resource", "S_uncover.20220919_SacroCuoreEmergency")
#DSI::datashield.assign.resource(connections$Sacrocuore_employees, "resource", "S_uncover.employees")
#DSI::datashield.assign.resource(connections$Sacrocuore_ISARIC, "resource", "S_uncover.20220919_ISARIC")
#DSI::datashield.assign.resource(connections$ULSS6, "resource", "ULSS6.20220919_ULSS6")

### IRELAND ###
DSI::datashield.assign.resource(connections$TUDublin, "resource", "TUDublin.20220919_TUDublin")

### LUXEMBOURG ###

### PORTUGAL ###
# DSI::datashield.assign.resource(connections$Porto, "resource", "uncover-up.Resource_derived")
# DSI::datashield.assign.resource(connections$Coimbra, "resource", "IPC.IPC_Harmonised")

### ROMANIA ###
DSI::datashield.assign.resource(connections$UMF_Iasi, "resource", "umfiasi.20220919_UMFIasi")
DSI::datashield.assign.resource(connections$UMF_Cluj, "resource", "UMF_Cluj.Romania")

### SPAIN ###
DSI::datashield.assign.resource(connections$hm_hospitales, "resource", "FiHM.20220919_FiHM")
DSI::datashield.assign.resource(connections$UNAV, "resource", "unCOVer-UNAV.20230411_UNAV")
DSI::datashield.assign.resource(connections$FJD, "resource", "IISFJD.20220919_IISFJD")

### SLOVAKIA ###
DSI::datashield.assign.resource(connections$TU, "resource", "TU_Uncover.20220919_TU")

### TURKEY ###
DSI::datashield.assign.resource(connections$Ankara_inpatient, "resource", "BU.20230423_ankara_inpatient")
DSI::datashield.assign.resource(connections$Ankara_outpatient, "resource", "BU.20230423_ankara_outpatient")
DSI::datashield.assign.resource(connections$Ankara_icu_inpatient, "resource", "BU.20230423_ankara_icu_inpatient")
DSI::datashield.assign.resource(connections$Ankara_icu_outpatient, "resource", "BU.20230423_ankara_icu_outpatient")
DSI::datashield.assign.resource(connections$Konya_inpatient, "resource", "BU.20230423_konya_inpatient")
DSI::datashield.assign.resource(connections$Instanbul_inpatient, "resource", "BU.20230423_istanbul_inpatient")
DSI::datashield.assign.resource(connections$Instanbul_outpatient, "resource", "BU.20230423_istanbul_outpatient")
DSI::datashield.assign.resource(connections$Izmir_inpatient, "resource", "BU.20230423_izmir_inpatient")
DSI::datashield.assign.resource(connections$Alanya_inpatient, "resource", "BU.20230423_alanya_inpatient")
DSI::datashield.assign.resource(connections$Alanya_outpatient, "resource", "BU.20230423_alanya_outpatient")
DSI::datashield.assign.resource(connections$Adana_inpatient, "resource", "BU.20230423_adana_inpatient")
DSI::datashield.assign.resource(connections$Adana_outpatient, "resource", "BU.20230423_adana_outpatient")

### UK ###
# DSI::datashield.assign.resource(connections$SMUC, "resource", "SMUC.20220919_SMUC")


###8. resolve object to load datasets

DSI::datashield.assign.expr(conns = connections, symbol = "data", 
                            expr = "as.resource.data.frame(resource)")

###9. describe uploads

ds.class("data")
ds.dim("data")

###10. logout from all servers [after done with analysis, this is NEEDED]

datashield.logout(connections)

# Log out Hospital by hospital
# datashield.logout(connections$hm_hospitales)
# datashield.logout(connections$princesa)
# datashield.logout(connections$CIPH)
# datashield.logout(connections$Porto)
# datashield.logout(connections$Ankara_inpatient)
# datashield.logout(connections$Ankara_outpatient)
# datashield.logout(connections$Konya_inpatient)
# datashield.logout(connections$Konya_outpatient)
# datashield.logout(connections$Instanbul_outpatient)
# datashield.logout(connections$Instanbul_inpatient)
# datashield.logout(connections$Izmir_inpatient)
# datashield.logout(connections$Izmir_outpatient)
# datashield.logout(connections$Alanya_inpatient)
# datashield.logout(connections$Alanya_outpatient)
# datashield.logout(connections$Adana_inpatient)
# datashield.logout(connections$UMF_Iasi)
# datashield.logout(connections$Sacrocuore_Verona)
# datashield.logout(connections$Sacrocuore_emergency)
# datashield.logout(connections$Sacrocuore_employees)
# datashield.logout(connections$Sacrocuore_ISARIC)
# datashield.logout(connections$UDEA)
# datashield.logout(connections$FJD)
# datashield.logout(connections$Coimbra)
# datashield.logout(connections$UNAV)
# datashield.logout(connections$SMUC)
# datashield.logout(connections$TU)
# datashield.logout(connections$TUDublin)
# datashield.logout(connections$UNSA_Inpatient)
# datashield.logout(connections$UNSA_Outpatient)
# datashield.logout(connections$UZA)
# datashield.logout(connections$INANTRO)
# datashield.logout(connections$UMF_Cluj)
# datashield.logout(connections$ULSS6)
