Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            try
            {
                //Configure the Logger
                Logging.ConfigureLogger();

                //Load the configuration from appsettings.json
                Config.LoadAppSettings();

                //load the cities list
                CitiesConfig.LoadCitiesList();

                //Initialize DB
                using (BSMContext context = new BSMContext(Config.DBConnection))
                {
                    if (context.Database.EnsureCreated())
                    {
                        Log.Information("Database schemas initialized");
                    }

                    var adminuser = context.Users.FirstOrDefault(u => u.LoginID.Equals("admin", StringComparison.OrdinalIgnoreCase));
                    if (adminuser == null)
                    {
                        adminuser         = new Common.Model.User();
                        adminuser.LoginID = "admin";
                        adminuser.Name    = "admin";
                        adminuser.Title   = "Administrator";
                        adminuser.IsAdmin = true;

                        PasswordHasher <User> hasher = new PasswordHasher <User>();
                        string hashedPass            = hasher.HashPassword(adminuser, "admin");
                        adminuser.Password          = hashedPass;
                        adminuser.PasswordChangedAt = DateTime.Now;
                        adminuser.CreatedAt         = DateTime.Now;

                        context.Users.Add(adminuser);
                        context.SaveChanges();

                        Log.Information("admin user is created in system");
                    }

                    var alertsetting = context.AlertSettings.FirstOrDefault();
                    if (alertsetting == null)
                    {
                        alertsetting = new AlertSetting();
                        alertsetting.TiltThreshold    = null;
                        alertsetting.SkewingThreshold = null;
                        alertsetting.SpeedThreshold   = null;
                        alertsetting.TempThreshold    = null;

                        context.AlertSettings.Add(alertsetting);
                        context.SaveChanges();
                        Log.Information("alert settings is configured");
                    }
                    Log.Information("Database initialized: {0}", context.DataSourceString);
                }

                //start up the udp server
                if (Config.UdpEnabled)
                {
                    UdpServer udpserver = new UdpServer();
                    udpserver.Init();
                }
                else
                {
                    Log.Information("UDP server is disabled, will not start up to listen on udp port");
                }

                //Start the web server host (include websocket)
                CreateWebHostBuilder(args).Build().Run();
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error when startup the service");
            }
        }