direct Enedis Api call support

Signed-off-by: Laurent ARNAL <laurent@clae.net>
This commit is contained in:
Laurent ARNAL 2024-06-03 16:34:31 +02:00
parent a6717ce69b
commit f95b6e5691
2 changed files with 60 additions and 24 deletions

View File

@ -31,6 +31,9 @@ public class LinkyBindingConstants {
public static final String BINDING_ID = "linky"; public static final String BINDING_ID = "linky";
// public final String clientId = "e551937c-5250-48bc-b4a6-2323af68db92";
// public final String clientSecret = "";
public static final String clientId = "_88uJnEjEs_IMf4bjGZJV6gGxYga"; public static final String clientId = "_88uJnEjEs_IMf4bjGZJV6gGxYga";
public static final String clientSecret = "6lsPfCmu0fEXuKYy3e0e6w8ydIca"; public static final String clientSecret = "6lsPfCmu0fEXuKYy3e0e6w8ydIca";
@ -72,6 +75,30 @@ public class LinkyBindingConstants {
public static final String TEST_SELECT = "main#linkyTestSelect"; public static final String TEST_SELECT = "main#linkyTestSelect";
public static final String MAIN_IDENTITY = "main#Identity";
public static final String MAIN_CONTRACT_SUBSCRIBED_POWER = "main#contractSubscribedPower";
public static final String MAIN_CONTRACT_LAST_ACTIVATION_DATE = "main#contractLastActivationDate";
public static final String MAIN_CONTRACT_DISTRIBUTION_TARIFF = "main#contractDistributionTariff";
public static final String MAIN_CONTRACT_OFF_PEAK_HOURS = "main#contractOffpeakHours";
public static final String MAIN_CONTRACT_CONTRACT_STATUS = "main#contractStatus";
public static final String MAIN_CONTRACT_CONTRACT_TYPE = "main#contractType";
public static final String MAIN_CONTRACT_LAST_DISTRIBUTION_TARIFF_CHANGE_DATE = "main#contractLastDistributionTariffChangeDate";
public static final String MAIN_CONTRACT_SEGMENT = "main#contractSegment";
public static final String MAIN_USAGEPOINT_ID = "main#usagePointId";
public static final String MAIN_USAGEPOINT_STATUS = "main#usagePointStatus";
public static final String MAIN_USAGEPOINT_METER_TYPE = "main#usagePointMeterType";
public static final String MAIN_USAGEPOINT_METER_ADDRESS_CITY = "main#usagePointAddressCity";
public static final String MAIN_USAGEPOINT_METER_ADDRESS_COUNTRY = "main#usagePointAddressCountry";
public static final String MAIN_USAGEPOINT_METER_ADDRESS_INSEE_CODE = "main#usagePointAddressInseeCode";
public static final String MAIN_USAGEPOINT_METER_ADDRESS_POSTAL_CODE = "main#usagePointAddressPostalCode";
public static final String MAIN_USAGEPOINT_METER_ADDRESS_STREET = "main#usagePointAddressStreet";
public static final String MAIN_CONTACT_MAIL = "main#contactMail";
public static final String MAIN_CONTACT_PHONE = "main#contactPhone";
// Authorization related Servlet and resources aliases. // Authorization related Servlet and resources aliases.
public static final String LINKY_ALIAS = "/connectlinky"; public static final String LINKY_ALIAS = "/connectlinky";
public static final String LINKY_IMG_ALIAS = "/img"; public static final String LINKY_IMG_ALIAS = "/img";
@ -86,13 +113,17 @@ public class LinkyBindingConstants {
// "r:installedapps", "w:installedapps" // "r:installedapps", "w:installedapps"
// List of Linky services related urls, information // List of Linky services related urls, information
public static final String LINKY_ACCOUNT_URL = "https://www.myelectricaldata.fr/"; public static final String LINKY_MYELECTRICALDATA_ACCOUNT_URL = "https://www.myelectricaldata.fr/";
public static final String LINKY_AUTHORIZE_URL = LINKY_ACCOUNT_URL + "v1/oauth2/authorize"; public static final String LINKY_MYELECTRICALDATA_AUTHORIZE_URL = LINKY_MYELECTRICALDATA_ACCOUNT_URL
public static final String LINKY_API_TOKEN_URL = LINKY_ACCOUNT_URL + "token"; + "v1/oauth2/authorize";
public static final String LINKY_MYELECTRICALDATA_API_TOKEN_URL = LINKY_MYELECTRICALDATA_ACCOUNT_URL + "token";
public static final String ENEDIS_API_ACCOUNT_URL = "https://ext.prod-sandbox.api.enedis.fr/"; public static final String ENEDIS_ACCOUNT_URL_PROD = "https://mon-compte-particulier.enedis.fr/";
public static final String ENEDIS_API_TOKEN_URL = ENEDIS_API_ACCOUNT_URL + "oauth2/v3/token"; public static final String ENEDIS_AUTHORIZE_URL_PROD = ENEDIS_ACCOUNT_URL_PROD + "dataconnect/v1/oauth2/authorize";
public static final String ENEDIS_API_TOKEN_URL_PROD = ENEDIS_ACCOUNT_URL_PROD + "oauth2/v3/token";
public static final String ENEDIS_AUTH_ACCOUNT_URL = "https://mon-compte-particulier.enedis.fr/"; public static final String ENEDIS_ACCOUNT_URL_PREPROD = "https://ext.prod-sandbox.api.enedis.fr/";
public static final String ENEDIS_AUTH_AUTHORIZE_URL = ENEDIS_AUTH_ACCOUNT_URL + "dataconnect/v1/oauth2/authorize"; public static final String ENEDIS_AUTHORIZE_URL_PREPROD = ENEDIS_ACCOUNT_URL_PREPROD
+ "dataconnect/v1/oauth2/authorize";
public static final String ENEDIS_API_TOKEN_URL_PREPROD = ENEDIS_ACCOUNT_URL_PREPROD + "oauth2/v3/token";
} }

View File

@ -58,16 +58,17 @@ public class EnedisHttpApi {
private static final DateTimeFormatter API_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd"); private static final DateTimeFormatter API_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// private static final String BASE_URL = "https://www.myelectricaldata.fr/";
private static final String BASE_URL = "https://ext.prod-sandbox.api.enedis.fr/"; private static final String BASE_URL = "https://ext.prod-sandbox.api.enedis.fr/";
// private static final String CONTRACT_URL = BASE_URL
// + "metering_data_dc/v5/daily_consumption?start=2024-04-01&end=2024-04-17";
private static final String CONTRACT_URL = BASE_URL + "customers_upc/v5/usage_points/contracts"; private static final String CONTRACT_URL = BASE_URL + "customers_upc/v5/usage_points/contracts";
private static final String IDENTITY_URL = BASE_URL + "identity"; private static final String IDENTITY_URL = BASE_URL + "customers_i/v5/identity";
private static final String CONTACT_URL = BASE_URL + "contact"; private static final String CONTACT_URL = BASE_URL + "customers_cd/v5/contact_data";
private static final String ADDRESS_URL = BASE_URL + "addresses"; private static final String ADDRESS_URL = BASE_URL + "customers_upa/v5/usage_points/addresses";
private static final String MEASURE_URL = BASE_URL + "%s/%s/start/%s/end/%s/cache"; private static final String MEASURE_DAILY_CONSUMPTION_URL = BASE_URL
+ "metering_data_dc/v5/daily_consumption?usage_point_id=%s&start=%s&end=%s";
private static final String MEASURE_MAX_POWER_URL = BASE_URL
+ "metering_data_dcmp/v5/daily_consumption_max_power?usage_point_id=%s&start=%s&end=%s";
private static final String TEMPO_URL = BASE_URL + "rte/tempo/%s/%s"; private static final String TEMPO_URL = BASE_URL + "rte/tempo/%s/%s";
private static final String TOKEN_URL = BASE_URL private static final String TOKEN_URL = BASE_URL
@ -114,7 +115,7 @@ public class EnedisHttpApi {
request = request.method(HttpMethod.GET); request = request.method(HttpMethod.GET);
if (!("".equals(token))) { if (!("".equals(token))) {
request = request.header("Authorization", "Bearer " + token); request = request.header("Authorization", "Bearer " + token);
request = request.header("accept", "application/json"); request = request.header("Accept", "application/json");
} }
ContentResponse result = request.send(); ContentResponse result = request.send();
@ -159,12 +160,16 @@ public class EnedisHttpApi {
return result; return result;
} }
public String formatUrl(String apiUrl, String prmId) {
// return "%s/%s/cache".formatted(apiUrl, config.prmId);
return "%s?usage_point_id=%s".formatted(apiUrl, prmId);
}
public Customer getCustomer(String prmId) throws LinkyException { public Customer getCustomer(String prmId) throws LinkyException {
if (!connected) { if (!connected) {
initialize(); initialize();
} }
String data = getData("%s?usage_point_id=%s".formatted(CONTRACT_URL, prmId)); String data = getData(formatUrl(CONTRACT_URL, prmId));
// String data = getData("%s&usage_point_id=%s".formatted(CONTRACT_URL, config.prmId));
if (data.isEmpty()) { if (data.isEmpty()) {
throw new LinkyException("Requesting '%s' returned an empty response", CONTRACT_URL); throw new LinkyException("Requesting '%s' returned an empty response", CONTRACT_URL);
} }
@ -184,7 +189,7 @@ public class EnedisHttpApi {
if (!connected) { if (!connected) {
initialize(); initialize();
} }
String data = getData("%s/%s/cache".formatted(ADDRESS_URL, prmId)); String data = getData(formatUrl(ADDRESS_URL, prmId));
if (data.isEmpty()) { if (data.isEmpty()) {
throw new LinkyException("Requesting '%s' returned an empty response", ADDRESS_URL); throw new LinkyException("Requesting '%s' returned an empty response", ADDRESS_URL);
} }
@ -204,7 +209,7 @@ public class EnedisHttpApi {
if (!connected) { if (!connected) {
initialize(); initialize();
} }
String data = getData("%s/%s/cache".formatted(IDENTITY_URL, prmId)); String data = getData(formatUrl(IDENTITY_URL, prmId));
if (data.isEmpty()) { if (data.isEmpty()) {
throw new LinkyException("Requesting '%s' returned an empty response", IDENTITY_URL); throw new LinkyException("Requesting '%s' returned an empty response", IDENTITY_URL);
} }
@ -224,7 +229,7 @@ public class EnedisHttpApi {
if (!connected) { if (!connected) {
initialize(); initialize();
} }
String data = getData("%s/%s/cache".formatted(CONTACT_URL, prmId)); String data = getData(formatUrl(CONTACT_URL, prmId));
if (data.isEmpty()) { if (data.isEmpty()) {
throw new LinkyException("Requesting '%s' returned an empty response", CONTACT_URL); throw new LinkyException("Requesting '%s' returned an empty response", CONTACT_URL);
@ -241,12 +246,12 @@ public class EnedisHttpApi {
} }
} }
private MeterReading getMeasures(String userId, String prmId, LocalDate from, LocalDate to, String request) private MeterReading getMeasures(String apiUrl, String userId, String prmId, LocalDate from, LocalDate to)
throws LinkyException { throws LinkyException {
String dtStart = from.format(API_DATE_FORMAT); String dtStart = from.format(API_DATE_FORMAT);
String dtEnd = to.format(API_DATE_FORMAT); String dtEnd = to.format(API_DATE_FORMAT);
String url = String.format(MEASURE_URL, request, prmId, dtStart, dtEnd); String url = String.format(apiUrl, prmId, dtStart, dtEnd);
if (!connected) { if (!connected) {
initialize(); initialize();
} }
@ -268,11 +273,11 @@ public class EnedisHttpApi {
} }
public MeterReading getEnergyData(String userId, String prmId, LocalDate from, LocalDate to) throws LinkyException { public MeterReading getEnergyData(String userId, String prmId, LocalDate from, LocalDate to) throws LinkyException {
return getMeasures(userId, prmId, from, to, "daily_consumption"); return getMeasures(MEASURE_DAILY_CONSUMPTION_URL, userId, prmId, from, to);
} }
public MeterReading getPowerData(String userId, String prmId, LocalDate from, LocalDate to) throws LinkyException { public MeterReading getPowerData(String userId, String prmId, LocalDate from, LocalDate to) throws LinkyException {
return getMeasures(userId, prmId, from, to, "daily_consumption_max_power"); return getMeasures(MEASURE_MAX_POWER_URL, userId, prmId, from, to);
} }
public String getTempoData() throws LinkyException { public String getTempoData() throws LinkyException {