Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 77 additions & 36 deletions bundles/org.openhab.binding.sunsynk/README.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.sunsynk-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>

<feature name="openhab-binding-sunsynk" description="SunSynk Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.sunsynk/${project.version}</bundle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,30 @@ public class SunSynkBindingConstants {
public static final String CHANNEL_INVERTER_GRID_POWER = "inverter-grid-power";
public static final String CHANNEL_INVERTER_GRID_VOLTAGE = "inverter-grid-voltage";
public static final String CHANNEL_INVERTER_GRID_CURRENT = "inverter-grid-current";
public static final String CHANNEL_INVERTER_GRID_FREQUENCY = "inverter-grid-frequency";
public static final String CHANNEL_INVERTER_RATED_AC_OUTPUT = "inverter-rated-ac-output";

public static final String CHANNEL_INVERTER_AC_TEMPERATURE = "inverter-ac-temperature";
public static final String CHANNEL_INVERTER_DC_TEMPERATURE = "inverter-dc-temperature";

public static final String CHANNEL_BATTERY_VOLTAGE = "battery-grid-voltage";
public static final String CHANNEL_BATTERY_CURRENT = "battery-grid-current";
public static final String CHANNEL_BATTERY_POWER = "battery-grid-power";
public static final String CHANNEL_BATTERY_VOLTAGE = "battery-dc-voltage";
public static final String CHANNEL_BATTERY_CURRENT = "battery-dc-current";
public static final String CHANNEL_BATTERY_POWER = "battery-dc-power";
public static final String CHANNEL_BATTERY_SOC = "battery-soc";
public static final String CHANNEL_BATTERY_TEMPERATURE = "battery-temperature";

public static final String CHANNEL_INVERTER_SOLAR_ENERGY_TODAY = "inverter-solar-energy-today";
public static final String CHANNEL_INVERTER_SOLAR_ENERGY_MONTH = "inverter-solar-energy-month";
public static final String CHANNEL_INVERTER_SOLAR_ENERGY_YEAR = "inverter-solar-energy-year";
public static final String CHANNEL_INVERTER_SOLAR_ENERGY_TOTAL = "inverter-solar-energy-total";
public static final String CHANNEL_INVERTER_SOLAR_POWER_NOW = "inverter-solar-power-now";
public static final String CHANNEL_INVERTER_SOLAR_EFFICIENCY = "inverter-solar-efficiency";
public static final String CHANNEL_INVERTER_SOLAR_POWER = "inverter-solar-ac-power";
public static final String CHANNEL_INVERTER_SOLAR_STRING_VOLTAGE_1 = "inverter-solar-string-voltage-1";
public static final String CHANNEL_INVERTER_SOLAR_STRING_VOLTAGE_2 = "inverter-solar-string-voltage-2";
public static final String CHANNEL_INVERTER_SOLAR_STRING_CURRENT_1 = "inverter-solar-string-current-1";
public static final String CHANNEL_INVERTER_SOLAR_STRING_CURRENT_2 = "inverter-solar-string-current-2";
public static final String CHANNEL_INVERTER_SOLAR_STRING_POWER_1 = "inverter-solar-string-power-1";
public static final String CHANNEL_INVERTER_SOLAR_STRING_POWER_2 = "inverter-solar-string-power-2";

public static final String CHANNEL_INVERTER_CONTROL_TIMER = "inverter-control-timer";
public static final String CHANNEL_INVERTER_CONTROL_ENERGY_PATTERN = "inverter-control-energy-pattern";
Expand All @@ -92,4 +103,6 @@ public class SunSynkBindingConstants {
public static final String CONFIG_GATE_SERIAL = "gsn";
public static final String CONFIG_SERIAL = "serialnumber";
public static final String CONFIG_NAME = "alias";
public static final String CONFIG_PLANT_ID = "plantId";
public static final String CONFIG_PLANT_NAME = "plantName";
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class AccountController {
private static final int TIMEOUT_IN_MS = 4000;
private final Logger logger = LoggerFactory.getLogger(AccountController.class);
private static final String BEARER_TYPE = "Bearer ";
private static final long EXPIRYSECONDS = 100L; // 100 seconds before expiry
private Client sunAccount = new Client();

public AccountController() {
Expand Down Expand Up @@ -84,10 +85,13 @@ public void authenticate(String username, String password)
public void refreshAccount(String username) throws SunSynkAuthenticateException, SunSynkTokenException {
Long expiresIn = this.sunAccount.getExpiresIn();
Long issuedAt = this.sunAccount.getIssuedAt();
if ((issuedAt + expiresIn) - Instant.now().getEpochSecond() > 30) { // > 30 seconds
if ((issuedAt + expiresIn) - Instant.now().getEpochSecond() > EXPIRYSECONDS) {
logger.debug("Account configuration token not expired.");
return;
}
if (this.sunAccount.getRefreshTokenString().isEmpty()) {
throw new SunSynkTokenException("No refresh token available, re-authentication required.");
}
logger.debug("Account configuration token expired : {}", this.sunAccount.getData().toString());
String payload = makeRefreshBody(username, this.sunAccount.getRefreshTokenString());
sendHttp(payload);
Expand All @@ -110,21 +114,21 @@ private void sendHttp(String payload) throws SunSynkAuthenticateException, SunSy
Client client = gson.fromJson(response, Client.class);
if (client == null) {
throw new SunSynkAuthenticateException(
"Sun Synk Account could not be authenticated: Try re-enabling account");
"Sun Synk account could not be authenticated: Try re-enabling account");
}
this.sunAccount = client;
} catch (IOException | JsonSyntaxException e) {
throw new SunSynkAuthenticateException("Sun Synk Account could not be authenticated", e);
throw new SunSynkAuthenticateException("Sun Synk account could not be authenticated", e);
}
if (this.sunAccount.getCode() == 102) {
logger.debug("Sun Synk Account could not be authenticated: {}.", this.sunAccount.getMsg());
logger.debug("Sun Synk account could not be authenticated: {}.", this.sunAccount.getMsg());
throw new SunSynkAuthenticateException(
"Sun Synk Accountfailed to authenticate: Check your password or email.");
"Sun Synk account failed to authenticate: Check your password or email.");
}
if (this.sunAccount.getStatus() == 404) {
logger.debug("Sun Synk Account could not be authenticated: 404 {} {}.", this.sunAccount.getError(),
logger.debug("Sun Synk account could not be authenticated: 404 {} {}.", this.sunAccount.getError(),
this.sunAccount.getPath());
throw new SunSynkAuthenticateException("Sun Synk Accountfailed to authenticate: 404 Not Found.");
throw new SunSynkAuthenticateException("Sun Synk account failed to authenticate: 404 Not Found.");
}
getToken();
}
Expand All @@ -148,13 +152,13 @@ public ArrayList<Inverter> getDetails() throws SunSynkInverterDiscoveryException
Gson gson = new Gson();
Properties headers = new Properties();
String response = "";

String httpsURL = makeLoginURL(
"api/v1/inverters?page=1&limit=10&total=0&status=-1&sn=&plantId=&type=-2&softVer=&hmiVer=&agentCompanyId=-1&gsn=");
headers.setProperty(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
headers.setProperty(HttpHeaders.AUTHORIZATION, BEARER_TYPE + APIdata.staticAccessToken);
response = HttpUtil.executeUrl(HttpMethod.GET.asString(), httpsURL, headers, null,
MediaType.APPLICATION_JSON, TIMEOUT_IN_MS);
logger.trace("Account Details Response: {}", response);
@Nullable
Details maybeDeats = gson.fromJson(response, Details.class);
if (maybeDeats == null) {
Expand All @@ -163,10 +167,10 @@ public ArrayList<Inverter> getDetails() throws SunSynkInverterDiscoveryException
output = maybeDeats;
} catch (IOException | JsonSyntaxException e) {
if (logger.isDebugEnabled()) {
String message = Objects.requireNonNullElse(e.getMessage(), "unkown error message");
String message = Objects.requireNonNullElse(e.getMessage(), "unknown error message");
Throwable cause = e.getCause();
String causeMessage = cause != null ? Objects.requireNonNullElse(cause.getMessage(), "unkown cause")
: "unkown cause";
String causeMessage = cause != null ? Objects.requireNonNullElse(cause.getMessage(), "unknown cause")
: "unknown cause";
logger.debug("Error attempting to find inverters registered to account: Msg = {}. Cause = {}.", message,
causeMessage);
}
Expand Down
Loading
Loading