@@ -55,6 +55,7 @@ public class AccountController {
5555 private static final int TIMEOUT_IN_MS = 4000 ;
5656 private final Logger logger = LoggerFactory .getLogger (AccountController .class );
5757 private static final String BEARER_TYPE = "Bearer " ;
58+ private static final long EXPIRYSECONDS = 100L ; // 100 seconds before expiry
5859 private Client sunAccount = new Client ();
5960
6061 public AccountController () {
@@ -84,10 +85,13 @@ public void authenticate(String username, String password)
8485 public void refreshAccount (String username ) throws SunSynkAuthenticateException , SunSynkTokenException {
8586 Long expiresIn = this .sunAccount .getExpiresIn ();
8687 Long issuedAt = this .sunAccount .getIssuedAt ();
87- if ((issuedAt + expiresIn ) - Instant .now ().getEpochSecond () > 30 ) { // > 30 seconds
88+ if ((issuedAt + expiresIn ) - Instant .now ().getEpochSecond () > EXPIRYSECONDS ) {
8889 logger .debug ("Account configuration token not expired." );
8990 return ;
9091 }
92+ if (this .sunAccount .getRefreshTokenString ().isEmpty ()) {
93+ throw new SunSynkTokenException ("No refresh token available, re-authentication required." );
94+ }
9195 logger .debug ("Account configuration token expired : {}" , this .sunAccount .getData ().toString ());
9296 String payload = makeRefreshBody (username , this .sunAccount .getRefreshTokenString ());
9397 sendHttp (payload );
@@ -110,21 +114,21 @@ private void sendHttp(String payload) throws SunSynkAuthenticateException, SunSy
110114 Client client = gson .fromJson (response , Client .class );
111115 if (client == null ) {
112116 throw new SunSynkAuthenticateException (
113- "Sun Synk Account could not be authenticated: Try re-enabling account" );
117+ "Sun Synk account could not be authenticated: Try re-enabling account" );
114118 }
115119 this .sunAccount = client ;
116120 } catch (IOException | JsonSyntaxException e ) {
117- throw new SunSynkAuthenticateException ("Sun Synk Account could not be authenticated" , e );
121+ throw new SunSynkAuthenticateException ("Sun Synk account could not be authenticated" , e );
118122 }
119123 if (this .sunAccount .getCode () == 102 ) {
120- logger .debug ("Sun Synk Account could not be authenticated: {}." , this .sunAccount .getMsg ());
124+ logger .debug ("Sun Synk account could not be authenticated: {}." , this .sunAccount .getMsg ());
121125 throw new SunSynkAuthenticateException (
122- "Sun Synk Accountfailed to authenticate: Check your password or email." );
126+ "Sun Synk account failed to authenticate: Check your password or email." );
123127 }
124128 if (this .sunAccount .getStatus () == 404 ) {
125- logger .debug ("Sun Synk Account could not be authenticated: 404 {} {}." , this .sunAccount .getError (),
129+ logger .debug ("Sun Synk account could not be authenticated: 404 {} {}." , this .sunAccount .getError (),
126130 this .sunAccount .getPath ());
127- throw new SunSynkAuthenticateException ("Sun Synk Accountfailed to authenticate: 404 Not Found." );
131+ throw new SunSynkAuthenticateException ("Sun Synk account failed to authenticate: 404 Not Found." );
128132 }
129133 getToken ();
130134 }
@@ -148,13 +152,13 @@ public ArrayList<Inverter> getDetails() throws SunSynkInverterDiscoveryException
148152 Gson gson = new Gson ();
149153 Properties headers = new Properties ();
150154 String response = "" ;
151-
152155 String httpsURL = makeLoginURL (
153156 "api/v1/inverters?page=1&limit=10&total=0&status=-1&sn=&plantId=&type=-2&softVer=&hmiVer=&agentCompanyId=-1&gsn=" );
154157 headers .setProperty (HttpHeaders .ACCEPT , MediaType .APPLICATION_JSON );
155158 headers .setProperty (HttpHeaders .AUTHORIZATION , BEARER_TYPE + APIdata .staticAccessToken );
156159 response = HttpUtil .executeUrl (HttpMethod .GET .asString (), httpsURL , headers , null ,
157160 MediaType .APPLICATION_JSON , TIMEOUT_IN_MS );
161+ logger .trace ("Account Details Response: {}" , response );
158162 @ Nullable
159163 Details maybeDeats = gson .fromJson (response , Details .class );
160164 if (maybeDeats == null ) {
@@ -163,10 +167,10 @@ public ArrayList<Inverter> getDetails() throws SunSynkInverterDiscoveryException
163167 output = maybeDeats ;
164168 } catch (IOException | JsonSyntaxException e ) {
165169 if (logger .isDebugEnabled ()) {
166- String message = Objects .requireNonNullElse (e .getMessage (), "unkown error message" );
170+ String message = Objects .requireNonNullElse (e .getMessage (), "unknown error message" );
167171 Throwable cause = e .getCause ();
168- String causeMessage = cause != null ? Objects .requireNonNullElse (cause .getMessage (), "unkown cause" )
169- : "unkown cause" ;
172+ String causeMessage = cause != null ? Objects .requireNonNullElse (cause .getMessage (), "unknown cause" )
173+ : "unknown cause" ;
170174 logger .debug ("Error attempting to find inverters registered to account: Msg = {}. Cause = {}." , message ,
171175 causeMessage );
172176 }
0 commit comments