forked from hagronnestad/nissan-connect-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
138 lines (125 loc) · 5.37 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using NissanConnectLib.Api;
using NissanConnectLib.Models;
using System.Text.Json;
namespace NissanConnectLib.Example
{
internal class Program
{
private const string TOKEN_CACHE_FILE = "token.cache";
static async Task Main(string[] args)
{
// Instantiate client
var ncc = new NissanConnectClient(Configuration.Region.EU);
var loggedIn = false;
// Try to use token cache file
if (File.Exists(TOKEN_CACHE_FILE))
{
var cachedToken = JsonSerializer.Deserialize<OAuthAccessTokenResult>(File.ReadAllText(TOKEN_CACHE_FILE));
ncc.AccessToken = cachedToken;
if (await ncc.GetUserId() is null)
{
Console.WriteLine("Could not get user ID using cached token, deleting cache file...");
File.Delete(TOKEN_CACHE_FILE);
}
else
{
Console.WriteLine("Cached token is valid!");
loggedIn = true;
}
}
// Log in using username and password
if (!loggedIn)
{
// Are we missing arguments?
if (args.Length == 0)
{
Console.WriteLine("Command line arguments are missing. Specify username and password");
return;
}
// Log in using a username and password
if (args.Length == 2)
{
loggedIn = await ncc.LogIn(args[0], args[1]);
if (loggedIn)
{
Console.WriteLine("Logged in using username and password. Writing token to cache file...");
File.WriteAllText(TOKEN_CACHE_FILE, JsonSerializer.Serialize(ncc.AccessToken));
}
else
{
Console.WriteLine("Login failed!");
Console.ReadLine();
return;
}
}
}
// Get the user id
var userId = await ncc.GetUserId();
if (userId == null)
{
Console.WriteLine("Couldn't get user!");
Console.ReadLine();
return;
}
Console.WriteLine($"Logged in as: {userId}");
Console.WriteLine($"Access Token: {ncc.AccessToken?.AccessToken ?? "null"}");
// Get all cars
var cars = await ncc.GetCars(userId);
if (cars == null)
{
Console.WriteLine("Couldn't get cars!");
Console.ReadLine();
return;
}
Console.WriteLine($"Found {cars.Count} car(s)!");
// List all cars and their battery status
foreach (var car in cars)
{
if (car.Vin is null) continue;
Console.WriteLine("\nCars:");
Console.WriteLine($" Nickname: {car.NickName}");
Console.WriteLine($" ModelName: {car.ModelName}");
Console.WriteLine($" ModelCode: {car.ModelCode}");
Console.WriteLine($" ModelYear: {car.ModelYear}");
Console.WriteLine($" VIN: {car.Vin}");
// Get battery status for car
var bs = await ncc.GetBatteryStatus(car.Vin);
if (bs == null)
{
Console.WriteLine(" Couldn't get battery status!");
continue;
}
Console.WriteLine($" BatteryStatus");
Console.WriteLine($" BatteryLevel: {bs.BatteryLevel}%");
Console.WriteLine($" RangeHvacOff: {bs.RangeHvacOff} km");
Console.WriteLine($" RangeHvacOn: {bs.RangeHvacOn} km");
Console.WriteLine($" LastUpdateTime: {bs.LastUpdateTime}");
Console.WriteLine($" BatteryStatusAge: {bs.BatteryStatusAge}");
Console.WriteLine($" PlugStatus: {bs.PlugStatus}");
Console.WriteLine($" PlugStatusDetail: {bs.PlugStatusDetail}");
Console.WriteLine($" ChargeStatus: {bs.ChargeStatus}");
Console.WriteLine($" ChargePower: {bs.ChargePower}");
// Get HVAC status for car
var hvacs = await ncc.GetHvacStatus(car.Vin);
if (hvacs == null)
{
Console.WriteLine(" Couldn't get HVAC status!");
continue;
}
Console.WriteLine($" HvacStatus");
Console.WriteLine($" SocThreshold: {hvacs.SocThreshold}%");
Console.WriteLine($" LastUpdateTime: {hvacs.LastUpdateTime}");
Console.WriteLine($" HvacStatus: {hvacs.HvacStatus}");
// Get cockpit status for car
var cs = await ncc.GetCockpitStatus(car.Vin);
if (cs == null)
{
Console.WriteLine(" Couldn't get cockpit status!");
continue;
}
Console.WriteLine($" Cockpit");
Console.WriteLine($" TotalMileage: {cs.TotalMileage} km");
}
}
}
}