Skip to content

Commit

Permalink
Update Program.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Blueion76 authored Nov 16, 2024
1 parent d426173 commit d65992f
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions FCAAddon/FCAClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ await app.RunAsync(async (CoconaAppContext ctx) =>
Log.Information("{0}", appConfig.ToStringWithoutSecrets());
Log.Debug("{0}", appConfig.Dump());

FCAClient FcaClient = new FCAClient(appConfig.FCAUser, appConfig.FCAPw, appConfig.Brand, appConfig.Region);
FCAClient FCAClient = new FCAClient(appConfig.FCAUser, appConfig.FCAPw, appConfig.Brand, appConfig.Region);

var mqttClient = new SimpleMqttClient(appConfig.MqttServer, appConfig.MqttPort, appConfig.MqttUser, appConfig.MqttPw, "FCAUconnect");

Expand All @@ -60,7 +60,7 @@ await app.RunAsync(async (CoconaAppContext ctx) =>
if (!ctx.CancellationToken.IsCancellationRequested && appConfig.AutoDeepRefresh && vinCharging.Any())
{
Log.Information("AutoDeepRefresh");
foreach(string vin in vinCharging) { await TrySendCommand(FcaClient, FCACommand.DEEPREFRESH, vin); }
foreach(string vin in vinCharging) { await TrySendCommand(FCAClient, FCACommand.DEEPREFRESH, vin); }
await Task.Delay(TimeSpan.FromSeconds(6), ctx.CancellationToken);
Log.Information("AutoDeepRefresh COMPLETED. Next update in {0} minutes.", appConfig.AutoDeepInterval);
forceLoopResetEvent.Set();
Expand All @@ -78,9 +78,9 @@ await app.RunAsync(async (CoconaAppContext ctx) =>

try
{
await FcaClient.LoginAndKeepSessionAlive();
await FCAClient.LoginAndKeepSessionAlive();

foreach (var vehicle in await FcaClient.Fetch())
foreach (var vehicle in await FCAClient.Fetch())
{
Log.Information($"Found : {vehicle.Nickname} {vehicle.Vin} {vehicle.ModelDescription}");

Expand All @@ -101,7 +101,7 @@ await app.RunAsync(async (CoconaAppContext ctx) =>
await Parallel.ForEachAsync(haEntities, async (sensor, token) => { await sensor.Announce(); });

Log.Information("Pushing new buttons to Home Assistant");
var haInteractiveEntities = CreateInteractiveEntities(ctx, FcaClient, mqttClient, vehicle, haDevice);
var haInteractiveEntities = CreateInteractiveEntities(ctx, FCAClient, mqttClient, vehicle, haDevice);
await Parallel.ForEachAsync(haInteractiveEntities, async (button, token) => { await button.Announce(); });
}

Expand Down Expand Up @@ -139,7 +139,7 @@ await app.RunAsync(async (CoconaAppContext ctx) =>
}
});

async Task<bool> TrySendCommand(FCAClient FcaClient, FCACommand command, string vin)
async Task<bool> TrySendCommand(FCAClient FCAClient, FCACommand command, string vin)
{
Log.Information("SEND COMMAND {0}: ", command.Message);

Expand All @@ -152,7 +152,7 @@ async Task<bool> TrySendCommand(FCAClient FcaClient, FCACommand command, string

try
{
await FcaClient.SendCommand(vin, command.Message, pin, command.Action);
await FCAClient.SendCommand(vin, command.Message, pin, command.Action);
await Task.Delay(TimeSpan.FromSeconds(5));
Log.Information("Command: {0} SUCCESSFUL", command.Message);
}
Expand All @@ -168,12 +168,12 @@ async Task<bool> TrySendCommand(FCAClient FcaClient, FCACommand command, string



IEnumerable<HaEntity> CreateInteractiveEntities(CoconaAppContext ctx, FCAClient FcaClient, SimpleMqttClient mqttClient, Vehicle vehicle,
IEnumerable<HaEntity> CreateInteractiveEntities(CoconaAppContext ctx, FCAClient FCAClient, SimpleMqttClient mqttClient, Vehicle vehicle,
HaDevice haDevice)
{
var updateLocationButton = new HaButton(mqttClient, "UpdateLocation", haDevice, async button =>
{
if (await TrySendCommand(FcaClient, FCACommand.VF, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.VF, vehicle.Vin))
{
await Task.Delay(TimeSpan.FromSeconds(6), ctx.CancellationToken);
forceLoopResetEvent.Set();
Expand All @@ -184,7 +184,7 @@ IEnumerable<HaEntity> CreateInteractiveEntities(CoconaAppContext ctx, FCAClient
{
if (vinPlugged.Contains(vehicle.Vin))
{
if (await TrySendCommand(FcaClient, FCACommand.DEEPREFRESH, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.DEEPREFRESH, vehicle.Vin))
{
await Task.Delay(TimeSpan.FromSeconds(6), ctx.CancellationToken);
forceLoopResetEvent.Set();
Expand All @@ -194,7 +194,7 @@ IEnumerable<HaEntity> CreateInteractiveEntities(CoconaAppContext ctx, FCAClient

var lightsButton = new HaButton(mqttClient, "Light", haDevice, async button =>
{
if (await TrySendCommand(FcaClient, FCACommand.HBLF, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.HBLF, vehicle.Vin))
{
forceLoopResetEvent.Set();
}
Expand All @@ -203,23 +203,23 @@ IEnumerable<HaEntity> CreateInteractiveEntities(CoconaAppContext ctx, FCAClient

var hvacButton = new HaButton(mqttClient, "HVAC", haDevice, async button =>
{
if (await TrySendCommand(FcaClient, FCACommand.ROPRECOND, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.ROPRECOND, vehicle.Vin))
{
forceLoopResetEvent.Set();
}
});

var startengineButton = new HaButton(mqttClient, "StartEngine", haDevice, async button =>
{
if (await TrySendCommand(FcaClient, FCACommand.REON, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.REON, vehicle.Vin))
{
forceLoopResetEvent.Set();
}
});

var stopengineButton = new HaButton(mqttClient, "StopEngine", haDevice, async button =>
{
if (await TrySendCommand(FcaClient, FCACommand.REOFF, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.REOFF, vehicle.Vin))
{
forceLoopResetEvent.Set();
}
Expand All @@ -228,15 +228,15 @@ IEnumerable<HaEntity> CreateInteractiveEntities(CoconaAppContext ctx, FCAClient

var lockButton = new HaButton(mqttClient, "DoorLock", haDevice, async button =>
{
if (await TrySendCommand(FcaClient, FCACommand.RDL, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.RDL, vehicle.Vin))
{
forceLoopResetEvent.Set();
}
});

var unLockButton = new HaButton(mqttClient, "DoorUnLock", haDevice, async button =>
{
if (await TrySendCommand(FcaClient, FCACommand.RDU, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.RDU, vehicle.Vin))
{
forceLoopResetEvent.Set();
}
Expand All @@ -250,23 +250,23 @@ IEnumerable<HaEntity> CreateInteractiveEntities(CoconaAppContext ctx, FCAClient

var suppressalarmButton = new HaButton(mqttClient, "SuppressAlarm", haDevice, async button =>
{
if (await TrySendCommand(FcaClient, FCACommand.TA, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.TA, vehicle.Vin))
{
forceLoopResetEvent.Set();
}
});

var locktrunkButton = new HaButton(mqttClient, "LockTrunk", haDevice, async button =>
{
if (await TrySendCommand(FcaClient, FCACommand.ROTRUNKLOCK, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.ROTRUNKLOCK, vehicle.Vin))
{
forceLoopResetEvent.Set();
}
});

var unlocktrunkButton = new HaButton(mqttClient, "UnLockTrunk", haDevice, async button =>
var unlocktrunkButton = new HaButton(mqttClient, "UnlockTrunk", haDevice, async button =>
{
if (await TrySendCommand(FcaClient, FCACommand.ROTRUNKUNLOCK, vehicle.Vin))
if (await TrySendCommand(FCAClient, FCACommand.ROTRUNKUNLOCK, vehicle.Vin))
{
forceLoopResetEvent.Set();
}
Expand All @@ -286,7 +286,6 @@ async Task<IEnumerable<HaEntity>> GetHaEntities(HaRestApi haClient, SimpleMqttCl
bool charging = false;
string charginglevel = "battery_timetofullychargel2";
string batteryPluginstatus = "battery_pluginstatus";
string ignitionStatus = "ignitionstatus";

DateTime refChargeEndTime = DateTime.Now;

Expand All @@ -304,7 +303,6 @@ async Task<IEnumerable<HaEntity>> GetHaEntities(HaRestApi haClient, SimpleMqttCl
|| detail.Key.Contains("chargetofull", StringComparison.InvariantCultureIgnoreCase)
|| detail.Key.Contains("enablescheduletype", StringComparison.InvariantCultureIgnoreCase)
|| detail.Key.Contains("repeatschedule", StringComparison.InvariantCultureIgnoreCase)
|| detail.Key.Contains("ignitionstatus", StringComparison.InvariantCultureIgnoreCase)
)
{
binary = true;
Expand Down

0 comments on commit d65992f

Please sign in to comment.