using Hangfire; using Hangfire.LiteDB; using Infrastructure.Data; using Infrastructure.Email; using Infrastructure.Extensions; using Infrastructure.Web; using IoT.Shared.Services; using IoTNode.DeviceServices.FBee; using IoTNode.DeviceServices.Onvif; using IoTNode.DeviceServices.SerialPortManager; using IoTNode.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.IO; using System.Linq; namespace IoTNode { public class Startup : BaseStartup { public Startup(IConfiguration configuration, IWebHostEnvironment env) : base(configuration, env) { } public override void ConfigureServices(IServiceCollection services) { services.AddHangfire(o => o.UseLiteDbStorage("job.db")); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddHostedService(o => o.GetService()); services.AddHostedService(o => o.GetService()); services.AddHostedService(o => o.GetService()); services.AddHostedService(o => o.GetService()); services.AddTransient(); //services.AddHostedService(); base.ConfigureServices(services); } public override void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { base.Configure(app, env, loggerFactory); var options = new DashboardOptions { Authorization = new[] { new CustomDashboardAuthorizationFilter() } }; app.UseHangfireDashboard("/job", options); app.UseHangfireServer(); UpdateDB(app); } private void UpdateDB(IApplicationBuilder app) { using var scope = app.ApplicationServices.CreateScope(); var services = scope.ServiceProvider; var context = services.GetService(); using var cmd = context.Database.GetDbConnection().CreateCommand(); cmd.CommandText = "select Version from iot_Node"; context.Database.OpenConnection(); var versionValue = cmd.ExecuteScalar().ToString(); var version = this.GetVersion(versionValue); var files = Directory.GetFiles(Path.Combine(Env.ContentRootPath, "upgradescripts")) .ToDictionary(o => this.GetVersion(Path.GetFileNameWithoutExtension(o))).OrderBy(o => o.Key); foreach (var item in files) { if (version < item.Key) { var sql = File.ReadAllText(item.Value); try { context.Database.BeginTransaction(); context.Database.ExecuteSqlRaw(sql); context.Database.ExecuteSqlRaw($"update iot_Node set Version='{Path.GetFileNameWithoutExtension(item.Value)}'"); context.Database.CommitTransaction(); } catch (Exception ex) { Console.WriteLine($"update database by {item.Value} error "); ex.PrintStack(); context.Database.RollbackTransaction(); break; } } } } private long GetVersion(string versionValue) { var values = versionValue.Split('.').Select(o => Convert.ToInt64(o)).Reverse().ToArray(); var result = 0L; for (int i = 0; i < values.Length; i++) { if (i == 0) { result += values[i]; } else { result += values[i] * (long)Math.Pow(10, i + 7); } } return result; } } }