Skip to content

Commit aaa3fde

Browse files
committed
Improved Growatt authentication procedure.
1 parent 7c9e74f commit aaa3fde

4 files changed

Lines changed: 17 additions & 3 deletions

File tree

ChargeHQSender.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public string UserId
2222
{
2323
get
2424
{
25-
if (ChargeHQSettings.ApiKey != null && ChargeHQSettings.ApiKey != default(Guid))
25+
if (ChargeHQSettings != null && ChargeHQSettings.ApiKey != null && ChargeHQSettings.ApiKey != default(Guid))
2626
{
2727
return ChargeHQSettings.ApiKey.Value.ToString();
2828
}
@@ -134,7 +134,7 @@ public async Task<bool> SendErrorData(string errorMessage)
134134
/// </returns>
135135
public async Task<bool> SendData(SiteMeterPush data)
136136
{
137-
if (ChargeHQSettings.ApiKey != null && ChargeHQSettings.ApiKey != default(Guid))
137+
if (ChargeHQSettings != null && ChargeHQSettings.ApiKey != null && ChargeHQSettings.ApiKey != default(Guid))
138138
{
139139
// Send the SiteMeterPush data model to ChargeHQ Push API
140140
logger.LogDebug("ChargeHQ Site Meter Push: {0}", JsonConvert.SerializeObject(data, Formatting.None));

GrowattProducer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ private async Task<bool> GetCookies(CancellationToken cancellationToken)
139139
string url = Utility.GetUrl(GrowattSettings.BaseURI, Constants.Growatt.LOGIN_URI);
140140
var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) };
141141
var res = await _client.SendAsync(req);
142-
if (res.StatusCode == HttpStatusCode.OK)
142+
var jsonResponse = Utility.GetJsonResponse(res, cancellationToken);
143+
var loginResponse = JsonConvert.DeserializeObject<LogonResponse>(jsonResponse);
144+
if (res.StatusCode == HttpStatusCode.OK && loginResponse.result > 0)
143145
{
144146
return true;
145147
}

Models/Growatt/LogonResponse.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ namespace SolarUseOptimiser.Models.Growatt
33
public class LogonResponse
44
{
55
public int result { get; set; }
6+
7+
public string msg { get; set; }
68
}
79
}

PeriodicPoller.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,19 @@ private void PollGenerationStatistics_Elapsed(object sender, ElapsedEventArgs e)
106106
{
107107
if (!CancellationTokenSource.IsCancellationRequested)
108108
{
109+
int retryCount = 3;
109110
while (!DataSource.IsInitialised)
110111
{
111112
DataSource.Authenticate(CancellationTokenSource).GetAwaiter().GetResult();
113+
if (retryCount > 0)
114+
{
115+
Thread.Sleep(5000);
116+
retryCount--;
117+
}
118+
else
119+
{
120+
return;
121+
}
112122
}
113123

114124
SiteMeterPush pushData = DataSource.GetSiteMeterData(DataTarget.UserId, CancellationTokenSource);

0 commit comments

Comments
 (0)