Skip to content

Commit 4ed55bb

Browse files
authored
Fix appointment reminders (#103)
1 parent c13c6aa commit 4ed55bb

File tree

8 files changed

+38
-31
lines changed

8 files changed

+38
-31
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,4 @@ appsettings.Development.json
366366
data/*
367367
/src/NuttyTree.NetDaemon.Infrastructure/HomeAssistant/EntitiesAndServices.cs
368368
/src/NuttyTree.NetDaemon/NetDaemonCodegen
369-
/src/NuttyTree.NetDaemon/Config/ToDoListItems.yaml
369+
/src/NuttyTree.NetDaemon/Config

src/NuttyTree.NetDaemon.Application/AppointmentReminders/AppointmentConstants.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace NuttyTree.NetDaemon.Application.AppointmentReminders;
44

55
internal static class AppointmentConstants
66
{
7-
public const string FamilyCalendar = "family";
7+
public const string FamilyCalendarEntityId = "calendar.family";
88

9-
public const string ScoutsCalendar = "troop_479";
9+
public const string ScoutsCalendarEntityId = "calendar.troop_479";
1010

1111
public const string Chris = nameof(Chris);
1212

src/NuttyTree.NetDaemon.Application/AppointmentReminders/AppointmentRemindersApp.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ public AppointmentRemindersApp(
6969
applicationStopping = applicationLifetime.ApplicationStopping;
7070
this.logger = logger;
7171

72+
// Temp code to cleanup appointments with old calendar id
73+
using var scope = this.serviceScopeFactory.CreateScope();
74+
using var dbContext = scope.ServiceProvider.GetRequiredService<NuttyDbContext>();
75+
var oldAppointments = dbContext.Appointments
76+
.Where(a => a.Calendar != FamilyCalendarEntityId && a.Calendar != ScoutsCalendarEntityId)
77+
.ToListAsync()
78+
.GetAwaiter()
79+
.GetResult();
80+
if (oldAppointments.Count > 0)
81+
{
82+
dbContext.RemoveRange(oldAppointments);
83+
dbContext.SaveChanges();
84+
}
85+
7286
appointmentUpdatesTask = taskScheduler.CreatePeriodicTask(TimeSpan.FromSeconds(options.Value.AppointmentUpdatesSchedulePeriod), UpdateAppointmentsFromHomeAssistantAsync);
7387
travelTimeUpdatesTask = taskScheduler.CreateTriggerableSelfSchedulingTask(UpdateAppointmentReminderTravelTimesAsync, TimeSpan.FromSeconds(30));
7488
announcementsTask = taskScheduler.CreateTriggerableSelfSchedulingTask(AnnounceAppointmentRemindersAsync, TimeSpan.FromSeconds(30));
@@ -187,7 +201,7 @@ private async Task<TimeSpan> UpdateAppointmentReminderTravelTimesAsync(Cancellat
187201
: await wazeTravelTimes.GetTravelTimeAsync(options.HomeLocation, reminderToUpdate.GetLocationCoordinates(), reminderToUpdate.GetArriveDateTime());
188202

189203
// If a Scouts appointment is more than 25 miles away it is pretty sure bet we are meeting at the Church so update the location and re-calculate the travel time
190-
if (reminderToUpdate.Appointment.Calendar == ScoutsCalendar && travelTime?.Miles > 25)
204+
if (reminderToUpdate.Appointment.Calendar == ScoutsCalendarEntityId && travelTime?.Miles > 25)
191205
{
192206
reminderToUpdate.Appointment.SetLocationCoordinates(DefaultScoutsLocation);
193207
travelTime = await wazeTravelTimes.GetTravelTimeAsync(options.HomeLocation, DefaultScoutsLocation, reminderToUpdate.GetArriveDateTime());

src/NuttyTree.NetDaemon.Application/AppointmentReminders/Extensions/AppointmentEntityExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static AppointmentEntity Update(this AppointmentEntity existing, Appointm
8181

8282
public static void SetAppointmentPerson(this AppointmentEntity appointment)
8383
{
84-
if (appointment.Calendar == ScoutsCalendar)
84+
if (appointment.Calendar == ScoutsCalendarEntityId)
8585
{
8686
appointment.Person = Mayson;
8787
}
@@ -108,28 +108,28 @@ public static void SetAppointmentPerson(this AppointmentEntity appointment)
108108

109109
public static LocationCoordinates? GetKnownLocationCoordinates(this AppointmentEntity appointment, AppointmentRemindersOptions options)
110110
{
111-
if (appointment.Calendar == FamilyCalendar &&
111+
if (appointment.Calendar == FamilyCalendarEntityId &&
112112
(appointment.Location!.Replace(" ", string.Empty, StringComparison.Ordinal)
113113
!.Contains(options.HomeAddress!.Replace(" ", string.Empty, StringComparison.Ordinal), StringComparison.OrdinalIgnoreCase)
114114
|| string.Equals(appointment.Location, "Home", StringComparison.OrdinalIgnoreCase)))
115115
{
116116
return options.HomeLocation;
117117
}
118-
else if (appointment.Calendar == FamilyCalendar &&
118+
else if (appointment.Calendar == FamilyCalendarEntityId &&
119119
appointment.Location!.Replace(" ", string.Empty, StringComparison.Ordinal)
120120
!.Contains("RidgewoodChurch", StringComparison.OrdinalIgnoreCase))
121121
{
122122
return RidgewoodChurchLocation;
123123
}
124-
else if (appointment.Calendar == ScoutsCalendar &&
124+
else if (appointment.Calendar == ScoutsCalendarEntityId &&
125125
(appointment.Location!.Contains("Various Sites in EP", StringComparison.OrdinalIgnoreCase)
126126
|| appointment.Location.Contains("Other or TBD", StringComparison.OrdinalIgnoreCase)
127127
|| appointment.Location.Contains("Pax Christi", StringComparison.OrdinalIgnoreCase)))
128128
{
129129
// If any these values are in the location odds are it will be at the Church or somewhere close so drive time will be similar
130130
return DefaultScoutsLocation;
131131
}
132-
else if (appointment.Calendar == ScoutsCalendar &&
132+
else if (appointment.Calendar == ScoutsCalendarEntityId &&
133133
(appointment.Location!.Contains("Zoom", StringComparison.OrdinalIgnoreCase)
134134
|| appointment.Location.Contains("Online", StringComparison.OrdinalIgnoreCase)))
135135
{
@@ -176,7 +176,7 @@ public static void SetAppointmentReminderOptions(this AppointmentEntity appointm
176176
endReminder.SetDefaultAnnouncementTypes();
177177
endReminder.ArriveLeadMinutes ??= options.DefaultEndLeadMinutes;
178178

179-
if (appointment.Calendar == FamilyCalendar)
179+
if (appointment.Calendar == FamilyCalendarEntityId)
180180
{
181181
if (appointment.TryGetOverrideValue("Reminders", out var reminders))
182182
{
@@ -209,7 +209,7 @@ public static void SetAppointmentReminderOptions(this AppointmentEntity appointm
209209
}
210210
}
211211

212-
if (appointment.Calendar == ScoutsCalendar && !string.IsNullOrWhiteSpace(options.IgnoreScoutsAppointmntRegex) && new Regex(options.IgnoreScoutsAppointmntRegex).IsMatch(appointment.Summary))
212+
if (appointment.Calendar == ScoutsCalendarEntityId && !string.IsNullOrWhiteSpace(options.IgnoreScoutsAppointmntRegex) && new Regex(options.IgnoreScoutsAppointmntRegex).IsMatch(appointment.Summary))
213213
{
214214
startReminder.Cancel();
215215
endReminder.Cancel();

src/NuttyTree.NetDaemon.Application/NuttyTree.NetDaemon.Application.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
<ItemGroup>
1010
<PackageReference Include="FluentDateTime" Version="3.0.0" />
11-
<PackageReference Include="Grpc.AspNetCore" Version="2.61.0" />
12-
<PackageReference Include="NetDaemon.AppModel" Version="24.8.0" />
13-
<PackageReference Include="NetDaemon.Runtime" Version="24.8.0" />
14-
<PackageReference Include="NetDaemon.Client" Version="24.8.0" />
15-
<PackageReference Include="NetDaemon.Extensions.Scheduling" Version="24.8.0" />
16-
<PackageReference Include="NetDaemon.Extensions.Logging" Version="24.8.0" />
17-
<PackageReference Include="NetDaemon.Extensions.Tts" Version="24.8.0" />
18-
<PackageReference Include="Net.Codecrete.QrCodeGenerator" Version="2.0.4" />
11+
<PackageReference Include="Grpc.AspNetCore" Version="2.62.0" />
12+
<PackageReference Include="NetDaemon.AppModel" Version="24.12.0" />
13+
<PackageReference Include="NetDaemon.Runtime" Version="24.12.0" />
14+
<PackageReference Include="NetDaemon.Client" Version="24.12.0" />
15+
<PackageReference Include="NetDaemon.Extensions.Scheduling" Version="24.12.0" />
16+
<PackageReference Include="NetDaemon.Extensions.Logging" Version="24.12.0" />
17+
<PackageReference Include="NetDaemon.Extensions.Tts" Version="24.12.0" />
18+
<PackageReference Include="Net.Codecrete.QrCodeGenerator" Version="2.0.5" />
1919
<PackageReference Include="System.IO.Abstractions" Version="21.0.2" />
2020
<PackageReference Include="System.Reactive" Version="6.0.0" />
2121
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="8.0.0" />

src/NuttyTree.NetDaemon.Infrastructure/HomeAssistant/Models/Appointment.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public sealed class Appointment
1111
public Appointment(string summary, string? description, string? location, DateTime start, DateTime end)
1212
{
1313
Summary = summary;
14-
Description = description;
15-
Location = location;
14+
Description = description ?? string.Empty;
15+
Location = location ?? string.Empty;
1616
Start = start.ToUniversalTime();
1717
End = end.ToUniversalTime();
1818
}
@@ -23,9 +23,9 @@ public Appointment(string summary, string? description, string? location, DateTi
2323

2424
public string Summary { get; }
2525

26-
public string? Description { get; }
26+
public string Description { get; }
2727

28-
public string? Location { get; }
28+
public string Location { get; }
2929

3030
public DateTime Start { get; }
3131

src/NuttyTree.NetDaemon.Infrastructure/NuttyTree.NetDaemon.Infrastructure.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" />
11-
<PackageReference Include="NetDaemon.HassModel" Version="24.8.0" />
11+
<PackageReference Include="NetDaemon.HassModel" Version="24.12.0" />
1212
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

src/NuttyTree.NetDaemon/appsettings.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Debug",
5-
"Microsoft": "Warning"
6-
},
7-
"ConsoleThemeType": "Ansi"
8-
},
92
"NetDaemon": {
103
"ApplicationConfigurationFolder": "./"
114
},

0 commit comments

Comments
 (0)