|
5 | 5 |
|
6 | 6 | using Newtonsoft.Json; |
7 | 7 |
|
| 8 | +using SolarUseOptimiser.Models; |
8 | 9 | using SolarUseOptimiser.Models.ChargeHQ; |
9 | 10 | using SolarUseOptimiser.Models.Configuration; |
10 | 11 | using SolarUseOptimiser.Models.Huawei; |
@@ -100,12 +101,13 @@ public async Task<IDataSource> InitialiseAsync(CancellationTokenSource cancellat |
100 | 101 | return true; |
101 | 102 | }; |
102 | 103 | _client = new HttpClient(_handler); |
103 | | - IsInitialised = await Authenticate(cancellationTokenSource); |
| 104 | + var authResposne = await Authenticate(cancellationTokenSource); |
| 105 | + IsInitialised = authResposne.Success; |
104 | 106 | } |
105 | 107 | return this; |
106 | 108 | } |
107 | 109 |
|
108 | | - public async Task<bool> Authenticate(CancellationTokenSource cancellationTokenSource) |
| 110 | + public async Task<CommandResponse> Authenticate(CancellationTokenSource cancellationTokenSource) |
109 | 111 | { |
110 | 112 | var success = await GetXsrfToken(CancellationTokenSource.Token); |
111 | 113 | if (success) |
@@ -137,22 +139,39 @@ public async Task<bool> Authenticate(CancellationTokenSource cancellationTokenSo |
137 | 139 | bool foundInverter = SetDeviceInfos(deviceInfoResponse.data); |
138 | 140 | if (!foundInverter) |
139 | 141 | { |
140 | | - return false; |
| 142 | + return new CommandResponse |
| 143 | + { |
| 144 | + Success = false, |
| 145 | + Message = "inverter not found" |
| 146 | + }; |
141 | 147 | } |
142 | 148 | } |
143 | 149 | } |
144 | | - return true; |
| 150 | + return new CommandResponse |
| 151 | + { |
| 152 | + Success = true |
| 153 | + }; |
145 | 154 | } |
146 | 155 | else |
147 | 156 | { |
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 | + }; |
150 | 164 | } |
151 | 165 | } |
152 | 166 | else |
153 | 167 | { |
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 | + }; |
156 | 175 | } |
157 | 176 | } |
158 | 177 | else |
@@ -180,29 +199,51 @@ public async Task<bool> Authenticate(CancellationTokenSource cancellationTokenSo |
180 | 199 | bool foundInverter = SetDeviceInfos(deviceInfoResponse.data); |
181 | 200 | if (!foundInverter) |
182 | 201 | { |
183 | | - return false; |
| 202 | + return new CommandResponse |
| 203 | + { |
| 204 | + Success = false, |
| 205 | + Message = "inverter not found" |
| 206 | + }; |
184 | 207 | } |
185 | 208 | } |
186 | 209 | } |
187 | | - return true; |
| 210 | + return new CommandResponse |
| 211 | + { |
| 212 | + Success = true |
| 213 | + }; |
188 | 214 | } |
189 | 215 | else |
190 | 216 | { |
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 | + }; |
193 | 224 | } |
194 | 225 | } |
195 | 226 | else |
196 | 227 | { |
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 | + }; |
199 | 235 | } |
200 | 236 | } |
201 | 237 | } |
202 | 238 | else |
203 | 239 | { |
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 | + }; |
206 | 247 | } |
207 | 248 | } |
208 | 249 |
|
|
0 commit comments