Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent collisions in signalr messages in local development #1260

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ private async void OnPressureUpdate(object? sender, MqttReceivedArgs mqttArgs)
}
else
{
if (pressureStatus.PressureLevel == robot.PressureLevel) return;

await robotService.UpdateRobotPressureLevel(robot.Id, pressureStatus.PressureLevel);
await timeseriesService.Create(
new RobotPressureTimeseries
Expand Down
2 changes: 2 additions & 0 deletions backend/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

Console.WriteLine($"\nENVIRONMENT IS SET TO '{builder.Environment.EnvironmentName}'\n");

builder.Configuration.AddEnvironmentVariables();

builder.AddAzureEnvironmentVariables();

if (builder.Configuration.GetSection("KeyVault").GetValue<bool>("UseKeyVault"))
Expand Down
22 changes: 18 additions & 4 deletions backend/api/Services/SignalRService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface ISignalRService
public Task SendMessageAsync(string label, Installation? installation, string message);
}

public class SignalRService(IHubContext<SignalRHub> signalRHub) : ISignalRService
public class SignalRService(IHubContext<SignalRHub> signalRHub, IConfiguration configuration) : ISignalRService
{
private readonly JsonSerializerOptions _serializerOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };

Expand All @@ -23,10 +23,24 @@ public async Task SendMessageAsync<T>(string label, Installation? installation,

public async Task SendMessageAsync(string label, Installation? installation, string message)
{
if (installation != null)
await signalRHub.Clients.Group(installation.InstallationCode.ToUpper(CultureInfo.CurrentCulture)).SendAsync(label, "all", message);
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Local")
{
string? localDevUser = configuration.GetSection("Local")["DevUserId"];
if (localDevUser is null || localDevUser.Equals("", StringComparison.Ordinal)) return;

if (installation != null)
await signalRHub.Clients.Group(localDevUser + installation.InstallationCode.ToUpper(CultureInfo.CurrentCulture)).SendAsync(label, "all", message);
else
await signalRHub.Clients.Group(localDevUser).SendAsync(label, "all", message);
}
else
await signalRHub.Clients.All.SendAsync(label, "all", message);
{
if (installation != null)
await signalRHub.Clients.Group(installation.InstallationCode.ToUpper(CultureInfo.CurrentCulture)).SendAsync(label, "all", message);
else
await signalRHub.Clients.All.SendAsync(label, "all", message);
}


await Task.CompletedTask;
}
Expand Down
18 changes: 15 additions & 3 deletions backend/api/SignalR/SignalRHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface ISignalRClient
{
}

public class SignalRHub(IAccessRoleService accessRoleService) : Hub<ISignalRClient>
public class SignalRHub(IAccessRoleService accessRoleService, IConfiguration configuration) : Hub<ISignalRClient>
{
/// <summary>
/// Called when a new connection is made.
Expand All @@ -20,9 +20,21 @@ public override async Task OnConnectedAsync()
.Where((c) => c.Type.EndsWith("/role", StringComparison.CurrentCulture)).Select((c) => c.Value).ToList();

var installationCodes = await accessRoleService.GetAllowedInstallationCodes(roles);
foreach (string installationCode in installationCodes)

if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Local")
{
string? localDevUser = configuration.GetSection("Local")["DevUserId"];
if (localDevUser is null || localDevUser.Equals("", StringComparison.Ordinal))
throw new HubException("Running in development mode, but missing Local__DevUserId value in environment");

await Groups.AddToGroupAsync(Context.ConnectionId, localDevUser); // This is used instead of Users.All
foreach (string installationCode in installationCodes)
await Groups.AddToGroupAsync(Context.ConnectionId, localDevUser + installationCode.ToUpperInvariant());
}
else
{
await Groups.AddToGroupAsync(Context.ConnectionId, installationCode.ToUpperInvariant());
foreach (string installationCode in installationCodes)
await Groups.AddToGroupAsync(Context.ConnectionId, installationCode.ToUpperInvariant());
}
}

Expand Down
3 changes: 3 additions & 0 deletions backend/api/appsettings.Local.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@
},
"Database": {
"UseInMemoryDatabase": true
},
"Local": {
"DevUserId": ""
}
}
20 changes: 20 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ if [ "$backend_abort" != "true" ]; then
dotnet user-secrets set "AzureAd:ClientSecret" $az_client_secret --project backend/api > /dev/null
echo -e "Added client secret to ASP.NET secret manager"

echo -e "A username is needed for local development with SignalR"
echo -en "Input a username for yourself (this only needs to be unique within your development environment):\n"

while [ true ]
do
read -s local_dev_username

if [ -z "$local_dev_username" ]; then
echo "The local dev username cannot be empty"
echo "Try again:"
else
break
fi
done


echo "Local__DevUserId='$local_dev_username'" >> $flotilla_dir/.env
dotnet user-secrets set "Local:DevUserId" $az_client_secret --project backend/api > /dev/null
echo -e "Added local development username to .env file"

echo -e "Backup setup - Done!"
echo -e "-----------------------------\n"
fi
Expand Down
Loading