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

Add ISAR robot capabilities info to robot #1510

Merged
merged 2 commits into from
Apr 4, 2024
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
4 changes: 4 additions & 0 deletions backend/api.test/Client/MissionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ public async Task ScheduleDuplicateCustomMissionDefinitions()
Port = 3000,
CurrentInstallationCode = installationCode,
CurrentAreaName = null,
RobotCapabilities = [],
VideoStreams = new List<CreateVideoStreamQuery>()
};

Expand Down Expand Up @@ -509,6 +510,7 @@ public async Task GetNextRun()
Port = 3000,
CurrentInstallationCode = installation.InstallationCode,
CurrentAreaName = areaName,
RobotCapabilities = [],
VideoStreams = new List<CreateVideoStreamQuery>()
};

Expand Down Expand Up @@ -691,6 +693,7 @@ public async Task MissionDoesNotStartIfRobotIsNotInSameInstallationAsMission()
Port = 3000,
CurrentInstallationCode = otherInstallation.InstallationCode,
CurrentAreaName = null,
RobotCapabilities = [],
VideoStreams = new List<CreateVideoStreamQuery>()
};

Expand Down Expand Up @@ -758,6 +761,7 @@ public async Task MissionFailsIfRobotIsNotInSameDeckAsMission()
Port = 3000,
CurrentInstallationCode = installation.InstallationCode,
CurrentAreaName = null,
RobotCapabilities = [],
VideoStreams = new List<CreateVideoStreamQuery>()
};

Expand Down
2 changes: 2 additions & 0 deletions backend/api/Controllers/Models/CreateRobotQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public struct CreateRobotQuery

public int Port { get; set; }

public IList<RobotCapabilitiesEnum> RobotCapabilities { get; set; }

public RobotStatus Status { get; set; }
}
}
3 changes: 3 additions & 0 deletions backend/api/Controllers/Models/RobotResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class RobotResponse

public string IsarUri { get; set; }

public IList<RobotCapabilitiesEnum>? RobotCapabilities { get; set; }

[JsonConstructor]
#nullable disable
public RobotResponse() { }
Expand All @@ -68,6 +70,7 @@ public RobotResponse(Robot robot)
Pose = robot.Pose;
CurrentMissionId = robot.CurrentMissionId;
IsarUri = robot.IsarUri;
RobotCapabilities = robot.RobotCapabilities;
}
}
}
20 changes: 20 additions & 0 deletions backend/api/Database/Context/FlotillaDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Api.Database.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Api.Database.Context
Expand Down Expand Up @@ -50,6 +51,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
);
});

AddConverterForListOfEnums(modelBuilder.Entity<Robot>()
.Property(r => r.RobotCapabilities));

modelBuilder.Entity<MissionDefinition>()
.Property(m => m.InspectionFrequency)
.HasConversion(new TimeSpanToTicksConverter());
Expand Down Expand Up @@ -112,5 +116,21 @@ private static void AddConverterForDateTimeOffsets<T>(ref EntityTypeBuilder<T> e
entity.Property(property.Name).HasConversion(new DateTimeOffsetToBinaryConverter());
}
}

private static void AddConverterForListOfEnums<T>(PropertyBuilder<IList<T>?> propertyBuilder)
where T : Enum
{
#pragma warning disable IDE0305
var valueComparer = new ValueComparer<IList<T>?>(
(c1, c2) => (c1 == null && c2 == null) || ((c1 != null == (c2 != null)) && c1!.SequenceEqual(c2!)),
c => c == null ? 0 : c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
c => c == null ? null : (IList<T>?)c.ToList());
#pragma warning restore IDE0305

propertyBuilder.HasConversion(
r => r != null ? string.Join(';', r) : "",
r => r.Split(';', StringSplitOptions.RemoveEmptyEntries).Select(r => (T)Enum.Parse(typeof(T), r)).ToList())
.Metadata.SetValueComparer(valueComparer);
}
}
}
15 changes: 15 additions & 0 deletions backend/api/Database/Models/Robot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public Robot(CreateRobotQuery createQuery, Installation installation, Area? area
Port = createQuery.Port;
IsarConnected = true;
Deprecated = false;
RobotCapabilities = createQuery.RobotCapabilities;
Status = createQuery.Status;
Pose = new Pose();
}
Expand Down Expand Up @@ -94,6 +95,8 @@ public bool IsRobotBatteryLevelHighEnoughToStartMissions()
[Required]
public int Port { get; set; }

public IList<RobotCapabilitiesEnum>? RobotCapabilities { get; set; }

[Required]
public bool IsarConnected { get; set; }

Expand Down Expand Up @@ -134,4 +137,16 @@ public enum RobotStatus
Offline,
Blocked,
}

public enum RobotCapabilitiesEnum
{
take_thermal_image,
take_image,
take_video,
take_thermal_video,
drive_to_pose,
record_audio,
localize,
andchiind marked this conversation as resolved.
Show resolved Hide resolved
docking_procedure
}
}
10 changes: 10 additions & 0 deletions backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private async void OnIsarRobotInfo(object? sender, MqttReceivedArgs mqttArgs)
VideoStreams = isarRobotInfo.VideoStreamQueries,
Host = isarRobotInfo.Host,
Port = isarRobotInfo.Port,
RobotCapabilities = isarRobotInfo.Capabilities,
Status = RobotStatus.Available,
};

Expand All @@ -145,6 +146,7 @@ private async void OnIsarRobotInfo(object? sender, MqttReceivedArgs mqttArgs)
UpdatePortIfChanged(isarRobotInfo.Port, ref robot, ref updatedFields);

if (isarRobotInfo.CurrentInstallation is not null) UpdateCurrentInstallationIfChanged(installation, ref robot, ref updatedFields);
if (isarRobotInfo.Capabilities is not null) UpdateRobotCapabilitiesIfChanged(isarRobotInfo.Capabilities, ref robot, ref updatedFields);
if (updatedFields.IsNullOrEmpty()) return;

robot = await robotService.Update(robot);
Expand Down Expand Up @@ -204,6 +206,14 @@ private static void UpdateCurrentInstallationIfChanged(Installation newCurrentIn
robot.CurrentInstallation = newCurrentInstallation;
}

public static void UpdateRobotCapabilitiesIfChanged(IList<RobotCapabilitiesEnum> newRobotCapabilities, ref Robot robot, ref List<string> updatedFields)
{
if (robot.RobotCapabilities != null && Enumerable.SequenceEqual(newRobotCapabilities, robot.RobotCapabilities)) return;

updatedFields.Add($"\nRobotCapabilities ({robot.RobotCapabilities} -> {newRobotCapabilities})\n");
robot.RobotCapabilities = newRobotCapabilities;
}

private async void OnIsarMissionUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var provider = GetServiceProvider();
Expand Down
3 changes: 3 additions & 0 deletions backend/api/MQTT/MessageModels/IsarRobotInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class IsarRobotInfoMessage : MqttMessage
[JsonPropertyName("port")]
public int Port { get; set; }

[JsonPropertyName("capabilities")]
public List<RobotCapabilitiesEnum> Capabilities { get; set; }

[JsonPropertyName("timestamp")]
public DateTime Timestamp { get; set; }
}
Expand Down
Loading
Loading