Skip to content

Commit

Permalink
Fix bug where robot and robot model is not properly linked
Browse files Browse the repository at this point in the history
  • Loading branch information
Christdej committed Oct 11, 2023
1 parent 40156c5 commit c36efc8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 64 deletions.
120 changes: 59 additions & 61 deletions backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Api.Services.Events;
using Api.Services.Models;
using Api.Utilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
namespace Api.EventHandlers
{
Expand Down Expand Up @@ -112,86 +113,83 @@ private async void OnIsarRobotInfo(object? sender, MqttReceivedArgs mqttArgs)
{
var provider = GetServiceProvider();
var robotService = provider.GetRequiredService<IRobotService>();
var robotModelService = provider.GetRequiredService<IRobotModelService>();

var isarRobotInfo = (IsarRobotInfoMessage)mqttArgs.Message;
var robot = await robotService.ReadByIsarId(isarRobotInfo.IsarId);

if (robot == null)
try
{
_logger.LogInformation(
"Received message from new ISAR instance '{id}' with robot name '{name}'. Adding new robot to database.",
isarRobotInfo.IsarId,
isarRobotInfo.RobotName
);
var robot = await robotService.ReadByIsarId(isarRobotInfo.IsarId);

var robotQuery = new CreateRobotQuery
{
IsarId = isarRobotInfo.IsarId,
Name = isarRobotInfo.RobotName,
RobotType = isarRobotInfo.RobotType,
SerialNumber = isarRobotInfo.SerialNumber,
CurrentInstallation = isarRobotInfo.CurrentInstallation,
VideoStreams = isarRobotInfo.VideoStreamQueries,
Host = isarRobotInfo.Host,
Port = isarRobotInfo.Port,
Status = RobotStatus.Available,
Enabled = true
};

var robotModel = await robotModelService.ReadByRobotType(robotQuery.RobotType);
if (robotModel == null)
if (robot == null)
{
_logger.LogError(
"Could not create new robot for ISAR instance '{id}' because the provided robot type '{robotType}' does not exist",
_logger.LogInformation(
"Received message from new ISAR instance '{id}' with robot name '{name}'. Adding new robot to database.",
isarRobotInfo.IsarId,
isarRobotInfo.RobotType
isarRobotInfo.RobotName
);

var robotQuery = new CreateRobotQuery
{
IsarId = isarRobotInfo.IsarId,
Name = isarRobotInfo.RobotName,
RobotType = isarRobotInfo.RobotType,
SerialNumber = isarRobotInfo.SerialNumber,
CurrentInstallation = isarRobotInfo.CurrentInstallation,
VideoStreams = isarRobotInfo.VideoStreamQueries,
Host = isarRobotInfo.Host,
Port = isarRobotInfo.Port,
Status = RobotStatus.Available,
Enabled = true
};

var newRobot = new Robot(robotQuery);
newRobot = await robotService.Create(newRobot);
_logger.LogInformation(
"Added robot '{robotName}' with ISAR id '{isarId}' to database",
newRobot.Name,
newRobot.IsarId
);

return;
}

var newRobot = new Robot(robotQuery)
{
Model = robotModel
};
newRobot = await robotService.Create(newRobot);
_logger.LogInformation(
"Added robot '{robotName}' with ISAR id '{isarId}' to database",
newRobot.Name,
newRobot.IsarId
);
List<string> updatedFields = new();

return;
}
if (isarRobotInfo.VideoStreamQueries is not null)
{
UpdateVideoStreamsIfChanged(isarRobotInfo.VideoStreamQueries, ref robot, ref updatedFields);
}

List<string> updatedFields = new();
if (isarRobotInfo.Host is not null)
{
UpdateHostIfChanged(isarRobotInfo.Host, ref robot, ref updatedFields);
}

if (isarRobotInfo.VideoStreamQueries is not null)
{
UpdateVideoStreamsIfChanged(isarRobotInfo.VideoStreamQueries, ref robot, ref updatedFields);
}
UpdatePortIfChanged(isarRobotInfo.Port, ref robot, ref updatedFields);

if (isarRobotInfo.Host is not null)
{
UpdateHostIfChanged(isarRobotInfo.Host, ref robot, ref updatedFields);
}
if (isarRobotInfo.CurrentInstallation is not null)
{
UpdateCurrentInstallationIfChanged(isarRobotInfo.CurrentInstallation, ref robot, ref updatedFields);
}

UpdatePortIfChanged(isarRobotInfo.Port, ref robot, ref updatedFields);
if (!updatedFields.IsNullOrEmpty())
{
robot = await robotService.Update(robot);
_logger.LogInformation(
"Updated robot '{id}' ('{robotName}') in database: {updates}",
robot.Id,
robot.Name,
updatedFields
);
}

if (isarRobotInfo.CurrentInstallation is not null)
}
catch (DbUpdateException e)
{
UpdateCurrentInstallationIfChanged(isarRobotInfo.CurrentInstallation, ref robot, ref updatedFields);
_logger.LogError("Could not add robot to db", e);
}

if (!updatedFields.IsNullOrEmpty())
catch (Exception e)
{
robot = await robotService.Update(robot);
_logger.LogInformation(
"Updated robot '{id}' ('{robotName}') in database: {updates}",
robot.Id,
robot.Name,
updatedFields
);
_logger.LogError("Could not update robot in db", e);
}
}

Expand Down
12 changes: 9 additions & 3 deletions backend/api/Services/RobotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ public RobotService(FlotillaDbContext context)

public async Task<Robot> Create(Robot newRobot)
{
await _context.Robots.AddAsync(newRobot);
await _context.SaveChangesAsync();
return newRobot;
var existingModel = _context.RobotModels.FirstOrDefault(r => r.Id.Equals(newRobot.Model.Id));
if (existingModel != null)
{
newRobot.Model = existingModel;
await _context.Robots.AddAsync(newRobot);
await _context.SaveChangesAsync();
return newRobot;
}
throw new DbUpdateException("Could not create new robot in database as robot model does not exist");
}

public async Task<IEnumerable<Robot>> ReadAll()
Expand Down

0 comments on commit c36efc8

Please sign in to comment.