191 lines
6.5 KiB
C#
191 lines
6.5 KiB
C#
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace RobotApp.Data;
|
|
|
|
public static class ApplicationDbExtensions
|
|
{
|
|
public static async Task SeedApplicationDbAsync(this IServiceProvider serviceProvider)
|
|
{
|
|
using var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
|
|
|
using var appDb = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
await appDb.Database.MigrateAsync();
|
|
//await appDb.Database.EnsureCreatedAsync();
|
|
await appDb.SaveChangesAsync();
|
|
|
|
await scope.ServiceProvider.SeedRolesAsync();
|
|
await scope.ServiceProvider.SeedUsersAsync();
|
|
await scope.ServiceProvider.SeedConfigAsync();
|
|
}
|
|
|
|
private static async Task SeedRolesAsync(this IServiceProvider serviceProvider)
|
|
{
|
|
var roleManager = serviceProvider.GetRequiredService<RoleManager<ApplicationRole>>();
|
|
|
|
if (!await roleManager.RoleExistsAsync("Administrator"))
|
|
{
|
|
await roleManager.CreateAsync(new ApplicationRole()
|
|
{
|
|
Name = "Administrator",
|
|
NormalizedName = "ADMINISTRATOR",
|
|
});
|
|
}
|
|
|
|
if (!await roleManager.RoleExistsAsync("Distributor"))
|
|
{
|
|
await roleManager.CreateAsync(new ApplicationRole()
|
|
{
|
|
Name = "Distributor",
|
|
NormalizedName = "DISTRIBUTOR",
|
|
});
|
|
}
|
|
}
|
|
|
|
private static async Task SeedUsersAsync(this IServiceProvider serviceProvider)
|
|
{
|
|
using var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
|
if (await userManager.FindByNameAsync("admin") is null)
|
|
{
|
|
var admin = new ApplicationUser()
|
|
{
|
|
UserName = "admin",
|
|
Email = "administrator@phenikaa-x.com",
|
|
NormalizedUserName = "ADMINISTRATOR",
|
|
NormalizedEmail = "ADMINISTRATOR@PHENIKAA-X.COM",
|
|
EmailConfirmed = true,
|
|
};
|
|
|
|
await userManager.CreateAsync(admin, "robotics");
|
|
await userManager.AddToRoleAsync(admin, "Administrator");
|
|
}
|
|
|
|
if (await userManager.FindByNameAsync("distributor") is null)
|
|
{
|
|
var distributor = new ApplicationUser()
|
|
{
|
|
UserName = "distributor",
|
|
Email = "distributor@phenikaa-x.com",
|
|
NormalizedUserName = "DISTRIBUTOR",
|
|
NormalizedEmail = "DISTRIBUTOR@PHENIKAA-X.COM",
|
|
EmailConfirmed = true,
|
|
};
|
|
|
|
await userManager.CreateAsync(distributor, "robotics");
|
|
await userManager.AddToRoleAsync(distributor, "Distributor");
|
|
}
|
|
}
|
|
|
|
private static async Task SeedConfigAsync(this IServiceProvider serviceProvider)
|
|
{
|
|
using var appDb = serviceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
if (!await appDb.RobotConfigs.AnyAsync())
|
|
{
|
|
var defaultConfig = new RobotConfig
|
|
{
|
|
ConfigName = "Default",
|
|
Description = "Default robot configuration",
|
|
NavigationType = Common.Shares.Enums.NavigationType.Differential,
|
|
RadiusWheel = 0.1,
|
|
Width = 0.6,
|
|
Length = 1.1,
|
|
Height = 0.5,
|
|
IsActive = true,
|
|
CreatedAt = DateTime.Now,
|
|
UpdatedAt = DateTime.Now,
|
|
};
|
|
|
|
appDb.RobotConfigs.Add(defaultConfig);
|
|
await appDb.SaveChangesAsync();
|
|
}
|
|
|
|
if (!await appDb.RobotPlcConfigs.AnyAsync())
|
|
{
|
|
var defaultConfig = new RobotPlcConfig
|
|
{
|
|
ConfigName = "Default",
|
|
Description = "Default robot PLC configuration",
|
|
PLCAddress = "127.0.0.1",
|
|
PLCPort = 502,
|
|
PLCUnitId = 1,
|
|
IsActive = true,
|
|
CreatedAt = DateTime.Now,
|
|
UpdatedAt = DateTime.Now,
|
|
};
|
|
|
|
appDb.RobotPlcConfigs.Add(defaultConfig);
|
|
await appDb.SaveChangesAsync();
|
|
}
|
|
|
|
if (!await appDb.RobotSimulationConfigs.AnyAsync())
|
|
{
|
|
var defaultConfig = new RobotSimulationConfig
|
|
{
|
|
ConfigName = "Default",
|
|
Description = "Default robot simulation configuration",
|
|
EnableSimulation = true,
|
|
SimulationMaxVelocity = 1.5,
|
|
SimulationMaxAngularVelocity = 0.5,
|
|
SimulationAcceleration = 2,
|
|
SimulationDeceleration = 10,
|
|
IsActive = true,
|
|
CreatedAt = DateTime.Now,
|
|
UpdatedAt = DateTime.Now,
|
|
};
|
|
|
|
appDb.RobotSimulationConfigs.Add(defaultConfig);
|
|
await appDb.SaveChangesAsync();
|
|
}
|
|
|
|
if (!await appDb.RobotVDA5050Configs.AnyAsync())
|
|
{
|
|
var defaultConfig = new RobotVDA5050Config
|
|
{
|
|
ConfigName = "Default",
|
|
Description = "Default robot VDA5050 configuration",
|
|
SerialNumber = "T800-002",
|
|
VDA5050HostServer = "127.0.0.1",
|
|
VDA5050Port = 1883,
|
|
VDA5050Version = "2.1.0",
|
|
VDA5050Manufacturer = "PhenikaaX",
|
|
VDA5050PublishRepeat = 2,
|
|
VDA5050EnablePassword = true,
|
|
VDA5050EnableTls = false,
|
|
VDA5050UserName = "robotics",
|
|
VDA5050Password = "robotics",
|
|
VDA5050TopicPrefix = "uagv/v2",
|
|
IsActive = true,
|
|
CreatedAt = DateTime.Now,
|
|
UpdatedAt = DateTime.Now,
|
|
};
|
|
|
|
appDb.RobotVDA5050Configs.Add(defaultConfig);
|
|
await appDb.SaveChangesAsync();
|
|
}
|
|
|
|
if (!await appDb.RobotSafetyConfigs.AnyAsync())
|
|
{
|
|
var defaultConfig = new RobotSafetyConfig
|
|
{
|
|
ConfigName = "Default",
|
|
Description = "Default robot Safety configuration",
|
|
SafetySpeedVerySlow = 0.15,
|
|
SafetySpeedSlow = 0.3,
|
|
SafetySpeedNormal = 0.6,
|
|
SafetySpeedMedium = 0.9,
|
|
SafetySpeedOptimal = 1.2,
|
|
SafetySpeedFast = 1.5,
|
|
SafetySpeedVeryFast = 1.9,
|
|
IsActive = true,
|
|
CreatedAt = DateTime.Now,
|
|
UpdatedAt = DateTime.Now,
|
|
};
|
|
|
|
appDb.RobotSafetyConfigs.Add(defaultConfig);
|
|
await appDb.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|