Skip to content

Commit f28d83c

Browse files
authored
Merge pull request #9 from JSkimming/handle-nulls
Handle Null Values in Charge State
2 parents ea1485c + b13fa06 commit f28d83c

File tree

5 files changed

+206
-187
lines changed

5 files changed

+206
-187
lines changed

src/Tesla.NET/Models/ChargeState.cs

Lines changed: 82 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Tesla.NET.Models
55
{
66
using System;
7+
using System.ComponentModel;
78
using System.Diagnostics;
89
using System.Globalization;
910
using Newtonsoft.Json;
@@ -59,51 +60,51 @@ public class ChargeState
5960
/// <param name="notEnoughPowerToHeat">The <see cref="NotEnoughPowerToHeat"/>.</param>
6061
/// <param name="timestamp">The <see cref="Timestamp"/>.</param>
6162
public ChargeState(
62-
string chargingState = default,
63-
string fastChargerType = default,
64-
string fastChargerBrand = default,
65-
long chargeLimitSoc = default,
66-
long chargeLimitSocStd = default,
67-
long chargeLimitSocMin = default,
68-
long chargeLimitSocMax = default,
69-
bool chargeToMaxRange = default,
70-
long maxRangeChargeCounter = default,
71-
bool fastChargerPresent = default,
72-
double batteryRange = default,
73-
double estBatteryRange = default,
74-
double idealBatteryRange = default,
75-
long batteryLevel = default,
76-
long usableBatteryLevel = default,
77-
double chargeEnergyAdded = default,
78-
double chargeMilesAddedRated = default,
79-
long chargeMilesAddedIdeal = default,
80-
long chargerVoltage = default,
81-
long chargerPilotCurrent = default,
82-
long chargerActualCurrent = default,
83-
long chargerPower = default,
84-
long timeToFullCharge = default,
85-
bool tripCharging = default,
86-
long chargeRate = default,
87-
bool chargePortDoorOpen = default,
88-
string connChargeCable = default,
89-
string scheduledChargingStartTime = default,
90-
bool scheduledChargingPending = default,
91-
string userChargeEnableRequest = default,
92-
bool chargeEnableRequest = default,
93-
int? chargerPhases = default,
94-
string chargePortLatch = default,
95-
long chargeCurrentRequest = default,
96-
long chargeCurrentRequestMax = default,
97-
bool managedChargingActive = default,
98-
bool managedChargingUserCanceled = default,
99-
string managedChargingStartTime = default,
100-
bool batteryHeaterOn = default,
101-
bool notEnoughPowerToHeat = default,
102-
long timestamp = default)
63+
string chargingState = null,
64+
string fastChargerType = null,
65+
string fastChargerBrand = null,
66+
long? chargeLimitSoc = null,
67+
long? chargeLimitSocStd = null,
68+
long? chargeLimitSocMin = null,
69+
long? chargeLimitSocMax = null,
70+
bool? chargeToMaxRange = null,
71+
long? maxRangeChargeCounter = null,
72+
bool? fastChargerPresent = null,
73+
double? batteryRange = null,
74+
double? estBatteryRange = null,
75+
double? idealBatteryRange = null,
76+
long? batteryLevel = null,
77+
long? usableBatteryLevel = null,
78+
double? chargeEnergyAdded = null,
79+
double? chargeMilesAddedRated = null,
80+
long? chargeMilesAddedIdeal = null,
81+
long? chargerVoltage = null,
82+
long? chargerPilotCurrent = null,
83+
long? chargerActualCurrent = null,
84+
long? chargerPower = null,
85+
long? timeToFullCharge = null,
86+
bool? tripCharging = null,
87+
long? chargeRate = null,
88+
bool? chargePortDoorOpen = null,
89+
string connChargeCable = null,
90+
string scheduledChargingStartTime = null,
91+
bool? scheduledChargingPending = null,
92+
string userChargeEnableRequest = null,
93+
bool? chargeEnableRequest = null,
94+
int? chargerPhases = null,
95+
string chargePortLatch = null,
96+
long? chargeCurrentRequest = null,
97+
long? chargeCurrentRequestMax = null,
98+
bool? managedChargingActive = null,
99+
bool? managedChargingUserCanceled = null,
100+
string managedChargingStartTime = null,
101+
bool? batteryHeaterOn = null,
102+
bool? notEnoughPowerToHeat = null,
103+
long timestamp = 0)
103104
{
104-
ChargingState = chargingState ?? string.Empty;
105-
FastChargerType = fastChargerType ?? string.Empty;
106-
FastChargerBrand = fastChargerBrand ?? string.Empty;
105+
ChargingState = chargingState;
106+
FastChargerType = fastChargerType;
107+
FastChargerBrand = fastChargerBrand;
107108
ChargeLimitSoc = chargeLimitSoc;
108109
ChargeLimitSocStd = chargeLimitSocStd;
109110
ChargeLimitSocMin = chargeLimitSocMin;
@@ -127,18 +128,18 @@ public ChargeState(
127128
TripCharging = tripCharging;
128129
ChargeRate = chargeRate;
129130
ChargePortDoorOpen = chargePortDoorOpen;
130-
ConnChargeCable = connChargeCable ?? string.Empty;
131-
ScheduledChargingStartTime = scheduledChargingStartTime ?? string.Empty;
131+
ConnChargeCable = connChargeCable;
132+
ScheduledChargingStartTime = scheduledChargingStartTime;
132133
ScheduledChargingPending = scheduledChargingPending;
133-
UserChargeEnableRequest = userChargeEnableRequest ?? string.Empty;
134+
UserChargeEnableRequest = userChargeEnableRequest;
134135
ChargeEnableRequest = chargeEnableRequest;
135136
ChargerPhases = chargerPhases;
136-
ChargePortLatch = chargePortLatch ?? string.Empty;
137+
ChargePortLatch = chargePortLatch;
137138
ChargeCurrentRequest = chargeCurrentRequest;
138139
ChargeCurrentRequestMax = chargeCurrentRequestMax;
139140
ManagedChargingActive = managedChargingActive;
140141
ManagedChargingUserCanceled = managedChargingUserCanceled;
141-
ManagedChargingStartTime = managedChargingStartTime ?? string.Empty;
142+
ManagedChargingStartTime = managedChargingStartTime;
142143
BatteryHeaterOn = batteryHeaterOn;
143144
NotEnoughPowerToHeat = notEnoughPowerToHeat;
144145
Timestamp = timestamp;
@@ -166,139 +167,139 @@ public ChargeState(
166167
/// Gets the charge limit <see cref="Vehicle"/>.
167168
/// </summary>
168169
[JsonProperty("charge_limit_soc")]
169-
public long ChargeLimitSoc { get; }
170+
public long? ChargeLimitSoc { get; }
170171

171172
/// <summary>
172173
/// Gets the standard charge limit of a <see cref="Vehicle"/>.
173174
/// </summary>
174175
[JsonProperty("charge_limit_soc_std")]
175-
public long ChargeLimitSocStd { get; }
176+
public long? ChargeLimitSocStd { get; }
176177

177178
/// <summary>
178179
/// Gets the minimum change limit of a <see cref="Vehicle"/>.
179180
/// </summary>
180181
[JsonProperty("charge_limit_soc_min")]
181-
public long ChargeLimitSocMin { get; }
182+
public long? ChargeLimitSocMin { get; }
182183

183184
/// <summary>
184185
/// Gets the maximum change limit of a <see cref="Vehicle"/>.
185186
/// </summary>
186187
[JsonProperty("charge_limit_soc_max")]
187-
public long ChargeLimitSocMax { get; }
188+
public long? ChargeLimitSocMax { get; }
188189

189190
/// <summary>
190191
/// Gets a value indicating whether a <see cref="Vehicle"/> will change to max range.
191192
/// </summary>
192193
[JsonProperty("charge_to_max_range")]
193-
public bool ChargeToMaxRange { get; }
194+
public bool? ChargeToMaxRange { get; }
194195

195196
/// <summary>
196197
/// Gets the counter of haw many times a <see cref="Vehicle"/> has been changed to maximum range.
197198
/// </summary>
198199
[JsonProperty("max_range_charge_counter")]
199-
public long MaxRangeChargeCounter { get; }
200+
public long? MaxRangeChargeCounter { get; }
200201

201202
/// <summary>
202203
/// Gets a value indicating whether a <see cref="Vehicle"/> is supercharging.
203204
/// </summary>
204205
[JsonProperty("fast_charger_present")]
205-
public bool FastChargerPresent { get; }
206+
public bool? FastChargerPresent { get; }
206207

207208
/// <summary>
208209
/// Gets the batter range of a <see cref="Vehicle"/>.
209210
/// </summary>
210211
[JsonProperty("battery_range")]
211-
public double BatteryRange { get; }
212+
public double? BatteryRange { get; }
212213

213214
/// <summary>
214215
/// Gets the estimated battery range of a <see cref="Vehicle"/>.
215216
/// </summary>
216217
[JsonProperty("est_battery_range")]
217-
public double EstBatteryRange { get; }
218+
public double? EstBatteryRange { get; }
218219

219220
/// <summary>
220221
/// Gets the ideal battery range of a <see cref="Vehicle"/>.
221222
/// </summary>
222223
[JsonProperty("ideal_battery_range")]
223-
public double IdealBatteryRange { get; }
224+
public double? IdealBatteryRange { get; }
224225

225226
/// <summary>
226227
/// Gets the current battery level of a <see cref="Vehicle"/>.
227228
/// </summary>
228229
[JsonProperty("battery_level")]
229-
public long BatteryLevel { get; }
230+
public long? BatteryLevel { get; }
230231

231232
/// <summary>
232233
/// Gets the usable battery level of a <see cref="Vehicle"/>.
233234
/// </summary>
234235
[JsonProperty("usable_battery_level")]
235-
public long UsableBatteryLevel { get; }
236+
public long? UsableBatteryLevel { get; }
236237

237238
/// <summary>
238239
/// Gets the energy added to a <see cref="Vehicle"/> at the last charge.
239240
/// </summary>
240241
[JsonProperty("charge_energy_added")]
241-
public double ChargeEnergyAdded { get; }
242+
public double? ChargeEnergyAdded { get; }
242243

243244
/// <summary>
244245
/// Gets the rated miles added to a <see cref="Vehicle"/> at the last charge.
245246
/// </summary>
246247
[JsonProperty("charge_miles_added_rated")]
247-
public double ChargeMilesAddedRated { get; }
248+
public double? ChargeMilesAddedRated { get; }
248249

249250
/// <summary>
250251
/// Gets the ideal miles added to a <see cref="Vehicle"/> at the last charge.
251252
/// </summary>
252253
[JsonProperty("charge_miles_added_ideal")]
253-
public long ChargeMilesAddedIdeal { get; }
254+
public long? ChargeMilesAddedIdeal { get; }
254255

255256
/// <summary>
256257
/// Gets the charge voltage of a <see cref="Vehicle"/> when charging.
257258
/// </summary>
258259
[JsonProperty("charger_voltage")]
259-
public long ChargerVoltage { get; }
260+
public long? ChargerVoltage { get; }
260261

261262
/// <summary>
262263
/// Gets the charge pilot current of a <see cref="Vehicle"/> when charging.
263264
/// </summary>
264265
[JsonProperty("charger_pilot_current")]
265-
public long ChargerPilotCurrent { get; }
266+
public long? ChargerPilotCurrent { get; }
266267

267268
/// <summary>
268269
/// Gets the charge actual current of a <see cref="Vehicle"/> when charging.
269270
/// </summary>
270271
[JsonProperty("charger_actual_current")]
271-
public long ChargerActualCurrent { get; }
272+
public long? ChargerActualCurrent { get; }
272273

273274
/// <summary>
274275
/// Gets the charge power of a <see cref="Vehicle"/> when charging.
275276
/// </summary>
276277
[JsonProperty("charger_power")]
277-
public long ChargerPower { get; }
278+
public long? ChargerPower { get; }
278279

279280
/// <summary>
280281
/// Gets the time in minutes to a full charge of a <see cref="Vehicle"/> when charging.
281282
/// </summary>
282283
[JsonProperty("time_to_full_charge")]
283-
public long TimeToFullCharge { get; }
284+
public long? TimeToFullCharge { get; }
284285

285286
/// <summary>
286287
/// Gets a value indicating whether a <see cref="Vehicle"/> is trip charging.
287288
/// </summary>
288289
[JsonProperty("trip_charging")]
289-
public bool TripCharging { get; }
290+
public bool? TripCharging { get; }
290291

291292
/// <summary>
292293
/// Gets the charge rate of a <see cref="Vehicle"/> when charging.
293294
/// </summary>
294295
[JsonProperty("charge_rate")]
295-
public long ChargeRate { get; }
296+
public long? ChargeRate { get; }
296297

297298
/// <summary>
298299
/// Gets a value indicating whether the charge port of a <see cref="Vehicle"/> is open.
299300
/// </summary>
300301
[JsonProperty("charge_port_door_open")]
301-
public bool ChargePortDoorOpen { get; }
302+
public bool? ChargePortDoorOpen { get; }
302303

303304
/// <summary>
304305
/// Gets the type of the charge cable connected to a <see cref="Vehicle"/>.
@@ -316,7 +317,7 @@ public ChargeState(
316317
/// Gets a value indicating whether scheduled charging is pending for a <see cref="Vehicle"/>.
317318
/// </summary>
318319
[JsonProperty("scheduled_charging_pending")]
319-
public bool ScheduledChargingPending { get; }
320+
public bool? ScheduledChargingPending { get; }
320321

321322
/// <summary>
322323
/// Gets a value indicating whether a user charge enable request has been made for a <see cref="Vehicle"/>.
@@ -328,7 +329,7 @@ public ChargeState(
328329
/// Gets a value indicating whether a charge enable request has been made for a <see cref="Vehicle"/>.
329330
/// </summary>
330331
[JsonProperty("charge_enable_request")]
331-
public bool ChargeEnableRequest { get; }
332+
public bool? ChargeEnableRequest { get; }
332333

333334
/// <summary>
334335
/// Gets the charger phases of a <see cref="Vehicle"/>.
@@ -346,25 +347,25 @@ public ChargeState(
346347
/// Gets the charge current request of a <see cref="Vehicle"/>.
347348
/// </summary>
348349
[JsonProperty("charge_current_request")]
349-
public long ChargeCurrentRequest { get; }
350+
public long? ChargeCurrentRequest { get; }
350351

351352
/// <summary>
352353
/// Gets the maximum charge current request of a <see cref="Vehicle"/>.
353354
/// </summary>
354355
[JsonProperty("charge_current_request_max")]
355-
public long ChargeCurrentRequestMax { get; }
356+
public long? ChargeCurrentRequestMax { get; }
356357

357358
/// <summary>
358359
/// Gets a value indicating whether managed charging is active for a <see cref="Vehicle"/>.
359360
/// </summary>
360361
[JsonProperty("managed_charging_active")]
361-
public bool ManagedChargingActive { get; }
362+
public bool? ManagedChargingActive { get; }
362363

363364
/// <summary>
364365
/// Gets a value indicating whether managed charging for <see cref="Vehicle"/> has been canceled by a user.
365366
/// </summary>
366367
[JsonProperty("managed_charging_user_canceled")]
367-
public bool ManagedChargingUserCanceled { get; }
368+
public bool? ManagedChargingUserCanceled { get; }
368369

369370
/// <summary>
370371
/// Gets the managed charging start time of a <see cref="Vehicle"/>.
@@ -376,13 +377,13 @@ public ChargeState(
376377
/// Gets a value indicating whether battery heating is on for a <see cref="Vehicle"/>.
377378
/// </summary>
378379
[JsonProperty("battery_heater_on")]
379-
public bool BatteryHeaterOn { get; }
380+
public bool? BatteryHeaterOn { get; }
380381

381382
/// <summary>
382383
/// Gets a value indicating whether there is not enough power to heat a <see cref="Vehicle"/>.
383384
/// </summary>
384385
[JsonProperty("not_enough_power_to_heat")]
385-
public bool NotEnoughPowerToHeat { get; }
386+
public bool? NotEnoughPowerToHeat { get; }
386387

387388
/// <summary>
388389
/// Gets the millisecond Epoch timestamp when the <see cref="ChargeState"/> was captured.
@@ -398,6 +399,6 @@ public ChargeState(
398399

399400
private string DebuggerDisplay =>
400401
$"{GetType().Name}: {ChargingState} @ {BatteryLevel}% giving " +
401-
$"{EstBatteryRange.ToString(CultureInfo.InvariantCulture)}";
402+
$"{EstBatteryRange.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)}";
402403
}
403404
}

0 commit comments

Comments
 (0)