Skip to content

Commit e36dffa

Browse files
committed
Added sending error data to the DataTarget
1 parent dc5685c commit e36dffa

6 files changed

Lines changed: 119 additions & 35 deletions

File tree

GrowattProducer.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Newtonsoft.Json;
77

8+
using SolarUseOptimiser.Models;
89
using SolarUseOptimiser.Models.ChargeHQ;
910
using SolarUseOptimiser.Models.Configuration;
1011
using SolarUseOptimiser.Models.Growatt;
@@ -100,15 +101,16 @@ public async Task<IDataSource> InitialiseAsync(CancellationTokenSource cancellat
100101
return true;
101102
};
102103
_client = new HttpClient(_handler);
103-
IsInitialised = await Authenticate(cancellationTokenSource);
104+
var authResponse = await Authenticate(cancellationTokenSource);
105+
IsInitialised = authResponse.Success;
104106
}
105107
return this;
106108
}
107109

108-
public async Task<bool> Authenticate(CancellationTokenSource cancellationTokenSource)
110+
public async Task<CommandResponse> Authenticate(CancellationTokenSource cancellationTokenSource)
109111
{
110-
var success = await GetCookies(CancellationTokenSource.Token);
111-
if (success)
112+
var cookieResponse = await GetCookies(CancellationTokenSource.Token);
113+
if (cookieResponse.Success)
112114
{
113115
logger.LogInformation("Successfully authenticated the user against the Growatt API");
114116

@@ -118,22 +120,34 @@ public async Task<bool> Authenticate(CancellationTokenSource cancellationTokenSo
118120
var deviceListSuccess = await GetDevices(cancellationTokenSource.Token);
119121
if (deviceListSuccess)
120122
{
121-
return true;
123+
return new CommandResponse { Success = true };
122124
}
123125
else
124126
{
125-
return false;
127+
return new CommandResponse
128+
{
129+
Success = false,
130+
Message = "Get device list failed"
131+
};
126132
}
127133
}
128134
else
129135
{
130-
return false;
136+
return new CommandResponse
137+
{
138+
Success = false,
139+
Message = "Get plant list failed"
140+
};;
131141
}
132142
}
133-
return false;
143+
return new CommandResponse
144+
{
145+
Success = false,
146+
Message = cookieResponse.Message
147+
};
134148
}
135149

136-
private async Task<bool> GetCookies(CancellationToken cancellationToken)
150+
private async Task<CommandResponse> GetCookies(CancellationToken cancellationToken)
137151
{
138152
if (initialised)
139153
{
@@ -149,15 +163,26 @@ private async Task<bool> GetCookies(CancellationToken cancellationToken)
149163
var loginResponse = JsonConvert.DeserializeObject<LogonResponse>(jsonResponse);
150164
if (res.StatusCode == HttpStatusCode.OK && loginResponse.result > 0)
151165
{
152-
return true;
166+
return new CommandResponse
167+
{
168+
Success = true
169+
};
153170
}
154171
else
155172
{
156173
logger.LogWarning($"Failed to authenticate with Growatt server returning the message: {loginResponse.msg}");
157-
return false;
174+
return new CommandResponse
175+
{
176+
Success = false,
177+
Message = loginResponse.msg
178+
};
158179
}
159180
}
160-
return false;
181+
return new CommandResponse
182+
{
183+
Success = false,
184+
Message = "Not initialised"
185+
};
161186
}
162187

163188
private async Task<bool> GetPlantList( CancellationToken cancellationToken)

HuaweiProducer.cs

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Newtonsoft.Json;
77

8+
using SolarUseOptimiser.Models;
89
using SolarUseOptimiser.Models.ChargeHQ;
910
using SolarUseOptimiser.Models.Configuration;
1011
using SolarUseOptimiser.Models.Huawei;
@@ -100,12 +101,13 @@ public async Task<IDataSource> InitialiseAsync(CancellationTokenSource cancellat
100101
return true;
101102
};
102103
_client = new HttpClient(_handler);
103-
IsInitialised = await Authenticate(cancellationTokenSource);
104+
var authResposne = await Authenticate(cancellationTokenSource);
105+
IsInitialised = authResposne.Success;
104106
}
105107
return this;
106108
}
107109

108-
public async Task<bool> Authenticate(CancellationTokenSource cancellationTokenSource)
110+
public async Task<CommandResponse> Authenticate(CancellationTokenSource cancellationTokenSource)
109111
{
110112
var success = await GetXsrfToken(CancellationTokenSource.Token);
111113
if (success)
@@ -137,22 +139,39 @@ public async Task<bool> Authenticate(CancellationTokenSource cancellationTokenSo
137139
bool foundInverter = SetDeviceInfos(deviceInfoResponse.data);
138140
if (!foundInverter)
139141
{
140-
return false;
142+
return new CommandResponse
143+
{
144+
Success = false,
145+
Message = "inverter not found"
146+
};
141147
}
142148
}
143149
}
144-
return true;
150+
return new CommandResponse
151+
{
152+
Success = true
153+
};
145154
}
146155
else
147156
{
148-
logger.LogError("The station with the name '{0}' could not be found when listing the stations:", HuaweiSettings.StationName);
149-
return false;
157+
string message = string.Format("The station with the name '{0}' could not be found when listing the stations.", HuaweiSettings.StationName);
158+
logger.LogError(message);
159+
return new CommandResponse
160+
{
161+
Success = false,
162+
Message = message
163+
};
150164
}
151165
}
152166
else
153167
{
154-
logger.LogError("There were no plants/stations associated with this login.");
155-
return false;
168+
string message = "There were no plants/stations associated with this login.";
169+
logger.LogError(message);
170+
return new CommandResponse
171+
{
172+
Success = false,
173+
Message = message
174+
};
156175
}
157176
}
158177
else
@@ -180,29 +199,51 @@ public async Task<bool> Authenticate(CancellationTokenSource cancellationTokenSo
180199
bool foundInverter = SetDeviceInfos(deviceInfoResponse.data);
181200
if (!foundInverter)
182201
{
183-
return false;
202+
return new CommandResponse
203+
{
204+
Success = false,
205+
Message = "inverter not found"
206+
};
184207
}
185208
}
186209
}
187-
return true;
210+
return new CommandResponse
211+
{
212+
Success = true
213+
};
188214
}
189215
else
190216
{
191-
logger.LogError("The station with the name '{0}' could not be found when listing the stations.", HuaweiSettings.StationName);
192-
return false;
217+
string message = string.Format("The station with the name '{0}' could not be found when listing the stations.", HuaweiSettings.StationName);
218+
logger.LogError(message);
219+
return new CommandResponse
220+
{
221+
Success = false,
222+
Message = message
223+
};
193224
}
194225
}
195226
else
196227
{
197-
logger.LogError("The reason for the failure of the original list stations interface was not 401, it was {0}", stationListResponse.failCode);
198-
return false;
228+
string message = string.Format("The reason for the failure of the original list stations interface was not 401, it was {0}", stationListResponse.failCode);
229+
logger.LogError(message);
230+
return new CommandResponse
231+
{
232+
Success = false,
233+
Message = message
234+
};
199235
}
200236
}
201237
}
202238
else
203239
{
204-
logger.LogError("Failed to authenticate the user during initialisation.");
205-
return false;
240+
string message = "Failed to authenticate the user during initialisation.";
241+
logger.LogError(message);
242+
return new CommandResponse
243+
{
244+
Success = false,
245+
Message = message
246+
};
206247
}
207248
}
208249

IDataSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Threading;
22

3-
3+
using SolarUseOptimiser.Models;
44
using SolarUseOptimiser.Models.ChargeHQ;
55

66
namespace SolarUseOptimiser
@@ -24,7 +24,7 @@ bool IsInitialised
2424

2525
Task<IDataSource> InitialiseAsync(CancellationTokenSource cancellationTokenSource);
2626

27-
Task<bool> Authenticate(CancellationTokenSource cancellationTokenSource);
27+
Task<CommandResponse> Authenticate(CancellationTokenSource cancellationTokenSource);
2828

2929
SiteMeterPush GetSiteMeterData(string userId, CancellationTokenSource cancellationTokenSource);
3030

IoTaWattProducer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Newtonsoft.Json;
77

8+
using SolarUseOptimiser.Models;
89
using SolarUseOptimiser.Models.ChargeHQ;
910
using SolarUseOptimiser.Models.Configuration;
1011
using SolarUseOptimiser.Models.IoTaWatt;
@@ -67,9 +68,9 @@ public Task<IDataSource> InitialiseAsync(CancellationTokenSource cancellationTok
6768
return Task.FromResult<IDataSource>(this);
6869
}
6970

70-
public Task<bool> Authenticate(CancellationTokenSource cancellationTokenSource)
71+
public Task<CommandResponse> Authenticate(CancellationTokenSource cancellationTokenSource)
7172
{
72-
return Task.FromResult<bool>(true);
73+
return Task.FromResult<CommandResponse>(new CommandResponse{Success = true});
7374
}
7475

7576
public SiteMeterPush GetSiteMeterData(string userId, CancellationTokenSource cancellationTokenSource)

Models/CommandResponse.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SolarUseOptimiser.Models
2+
{
3+
public class CommandResponse
4+
{
5+
public bool Success { get; set; } = false;
6+
public string Message { get; set; } = string.Empty;
7+
}
8+
}

PeriodicPoller.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,31 @@ private void PollGenerationStatistics_Elapsed(object sender, ElapsedEventArgs e)
128128
{
129129
if (!CancellationTokenSource.IsCancellationRequested)
130130
{
131+
SiteMeterPush pushData = null;
131132
int retryCount = 3;
132133
while (!DataSource.IsInitialised)
133134
{
134-
DataSource.Authenticate(CancellationTokenSource).GetAwaiter().GetResult();
135+
var response = DataSource.Authenticate(CancellationTokenSource).GetAwaiter().GetResult();
135136
if (retryCount > 0)
136137
{
137138
Thread.Sleep(5000);
138139
retryCount--;
139140
}
140141
else
141142
{
142-
return;
143+
pushData = new SiteMeterPush
144+
{
145+
apiKey = DataTarget.UserId,
146+
error = string.Format($"{DataSource.Name} responded with {response.Message}")
147+
};
148+
break;
143149
}
144150
}
145151

146-
SiteMeterPush pushData = DataSource.GetSiteMeterData(DataTarget.UserId, CancellationTokenSource);
152+
if (string.IsNullOrWhiteSpace(pushData.error))
153+
{
154+
pushData = DataSource.GetSiteMeterData(DataTarget.UserId, CancellationTokenSource);
155+
}
147156

148157
if (string.IsNullOrWhiteSpace(pushData.error))
149158
{

0 commit comments

Comments
 (0)