forked from equinor/flotilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseUtilities.cs
151 lines (138 loc) · 5.87 KB
/
DatabaseUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Api.Controllers.Models;
using Api.Database.Context;
using Api.Database.Models;
using Api.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Moq;
namespace Api.Test.Database
{
public class DatabaseUtilities
{
private readonly AccessRoleService _accessRoleService;
private readonly AreaService _areaService;
private readonly DeckService _deckService;
private readonly InstallationService _installationService;
private readonly MissionRunService _missionRunService;
private readonly PlantService _plantService;
private readonly RobotModelService _robotModelService;
private readonly RobotService _robotService;
public DatabaseUtilities(FlotillaDbContext context)
{
var defaultLocalizationPoseService = new DefaultLocalizationPoseService(context);
_accessRoleService = new AccessRoleService(context, new HttpContextAccessor());
_installationService = new InstallationService(context, _accessRoleService);
_plantService = new PlantService(context, _installationService, _accessRoleService);
_deckService = new DeckService(context, defaultLocalizationPoseService, _installationService, _plantService, _accessRoleService, new MockSignalRService());
_areaService = new AreaService(context, _installationService, _plantService, _deckService, defaultLocalizationPoseService, _accessRoleService);
_missionRunService = new MissionRunService(context, new MockSignalRService(), new Mock<ILogger<MissionRunService>>().Object, _accessRoleService);
_robotModelService = new RobotModelService(context);
_robotService = new RobotService(context, new Mock<ILogger<RobotService>>().Object, _robotModelService, new MockSignalRService(), _accessRoleService, _installationService, _areaService, _missionRunService);
}
public async Task<MissionRun> NewMissionRun(
string installationCode,
Robot robot,
Area area,
bool writeToDatabase = false,
MissionRunPriority missionRunPriority = MissionRunPriority.Normal,
MissionStatus missionStatus = MissionStatus.Pending,
string? isarMissionId = null
)
{
var missionRun = new MissionRun
{
Name = "testMission",
Robot = robot,
MissionId = null,
IsarMissionId = isarMissionId,
MissionRunPriority = missionRunPriority,
Status = missionStatus,
DesiredStartTime = DateTime.Now,
Area = area,
Tasks = [],
Map = new MapMetadata(),
InstallationCode = installationCode
};
if (missionRunPriority == MissionRunPriority.Localization)
{
missionRun.Tasks = new List<MissionTask>
{
new(new Pose(), MissionTaskType.Localization)
};
}
if (writeToDatabase)
{
return await _missionRunService.Create(missionRun, false);
}
return missionRun;
}
public async Task<Installation> NewInstallation()
{
var createInstallationQuery = new CreateInstallationQuery
{
InstallationCode = "testInstallationCode",
Name = "testInstallation"
};
return await _installationService.Create(createInstallationQuery);
}
public async Task<Plant> NewPlant(string installationCode)
{
var createPlantQuery = new CreatePlantQuery
{
InstallationCode = installationCode,
PlantCode = "testPlantCode",
Name = "testPlant"
};
return await _plantService.Create(createPlantQuery);
}
public async Task<Deck> NewDeck(string installationCode, string plantCode)
{
var createDeckQuery = new CreateDeckQuery
{
InstallationCode = installationCode,
PlantCode = plantCode,
Name = "testDeck",
DefaultLocalizationPose = new Pose()
};
return await _deckService.Create(createDeckQuery);
}
public async Task<Area> NewArea(string installationCode, string plantCode, string deckName)
{
var createAreaQuery = new CreateAreaQuery
{
InstallationCode = installationCode,
PlantCode = plantCode,
DeckName = deckName,
AreaName = "testArea",
DefaultLocalizationPose = new Pose()
};
return await _areaService.Create(createAreaQuery);
}
public async Task<Robot> NewRobot(RobotStatus status, Installation installation, Area? area = null)
{
var createRobotQuery = new CreateRobotQuery
{
Name = "TestBot",
IsarId = Guid.NewGuid().ToString(),
RobotType = RobotType.Robot,
SerialNumber = "0001",
CurrentInstallationCode = installation.InstallationCode,
CurrentAreaName = area?.Name,
VideoStreams = new List<CreateVideoStreamQuery>(),
Host = "localhost",
Port = 3000,
Enabled = true,
Status = status
};
var robotModel = await _robotModelService.ReadByRobotType(createRobotQuery.RobotType);
var robot = new Robot(createRobotQuery, installation, area)
{
Model = robotModel!
};
return await _robotService.Create(robot);
}
}
}