fa485d902b
* Added first centralized database config and added machine self-registration into db * Fix database migration with new database * Refactor
137 lines
4.7 KiB
C#
137 lines
4.7 KiB
C#
using System;
|
|
using System.Data.Entity;
|
|
using Step.Model.DatabaseModels;
|
|
using MySql.Data.Entity;
|
|
using static Step.Utils.ExceptionManager;
|
|
using static Step.Utils.Constants;
|
|
using Step.Database.Migrations;
|
|
using System.IO;
|
|
using Step.Database.Controllers;
|
|
using static Step.Config.ServerConfig;
|
|
using Microsoft.Win32;
|
|
|
|
namespace Step.Database
|
|
{
|
|
[DbConfigurationType(typeof(MySqlEFConfiguration))]
|
|
public class DatabaseContext : DbContext
|
|
{
|
|
public DbSet<UserModel> Users { get; set; }
|
|
public DbSet<MachineModel> Machines { get; set; }
|
|
public DbSet<MachinesUsersModel> MachinesUsers { get; set; }
|
|
public DbSet<RoleModel> Roles { get; set; }
|
|
public DbSet<FunctionAccessModel> FunctionsAccess { get; set; }
|
|
|
|
public DatabaseContext()
|
|
: base("mySQLDatabaseConnection")
|
|
{
|
|
}
|
|
|
|
public static DatabaseContext Create()
|
|
{
|
|
return new DatabaseContext();
|
|
}
|
|
|
|
public static void TestDatabaseConnection()
|
|
{
|
|
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, Configuration>());
|
|
|
|
using (DatabaseContext dbContext = new DatabaseContext())
|
|
{
|
|
try
|
|
{
|
|
FindOrCreateMachineUniqueId();
|
|
}
|
|
catch (MySql.Data.MySqlClient.MySqlException ex)
|
|
{
|
|
if (ex.Number == 0)
|
|
{
|
|
dbContext.Database.Initialize(true);
|
|
}
|
|
else if (ex.Number == 1042) // Can't find MySQLServer
|
|
{
|
|
Manage(ERROR_LEVEL.FATAL, ex);
|
|
}
|
|
else
|
|
{
|
|
Manage(ERROR_LEVEL.ERROR, ex);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Manage(ERROR_LEVEL.FATAL, ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void FindOrCreateMachineUniqueId()
|
|
{
|
|
// Find machine unique id in the register
|
|
string uniqueId = ReadUniqueIdFromRegister();
|
|
|
|
// If not found
|
|
if (uniqueId == null || uniqueId == "")
|
|
{
|
|
if (!File.Exists("uniqueid.txt"))
|
|
{
|
|
uniqueId = Guid.NewGuid().ToString();
|
|
// Save uinique id into file
|
|
File.WriteAllText("uniqueid.txt", uniqueId);
|
|
// Save uinique id in the registry
|
|
WriteUniqueIdIntoRegister(uniqueId);
|
|
}
|
|
else
|
|
{
|
|
// Find machine unique id in the storage file
|
|
uniqueId = File.ReadAllText("uniqueid.txt");
|
|
|
|
// If null or empty
|
|
if (uniqueId == null || uniqueId == "")
|
|
{
|
|
// Delete empty file and recreate
|
|
File.Delete(uniqueId);
|
|
FindOrCreateMachineUniqueId();
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
WriteUniqueIdIntoRegister(uniqueId);
|
|
}
|
|
}
|
|
}
|
|
// Set machine info into static server config
|
|
using (MachineController machineController = new MachineController())
|
|
{
|
|
MachineConfig = machineController.GetMachineByUniqueId(uniqueId);
|
|
if (MachineConfig == null)
|
|
{
|
|
MachineConfig = machineController.Create(uniqueId);
|
|
using (UsersController usersController = new UsersController())
|
|
{
|
|
usersController.CreateCmsDefaultUserIfNotExists();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void WriteUniqueIdIntoRegister(string uniqueId)
|
|
{
|
|
// Get path to the HKEY_LOCAL_MACHINE/SOFTWARE
|
|
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
|
|
// Create or get ..//CMS/Step subkey
|
|
key = key.CreateSubKey("Cms").CreateSubKey("Step");
|
|
// Set machine unique id Key
|
|
key.SetValue(REGISTER_MACHINE_ID_KEY_NAME, uniqueId);
|
|
}
|
|
|
|
private static string ReadUniqueIdFromRegister()
|
|
{
|
|
// Get path to the HKEY_LOCAL_MACHINE/SOFTWARE
|
|
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
|
|
// Get ..//CMS/Step subkey
|
|
key = key.CreateSubKey("Cms").CreateSubKey("Step");
|
|
// Return value
|
|
return (string)key.GetValue(REGISTER_MACHINE_ID_KEY_NAME);
|
|
}
|
|
}
|
|
}
|