You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
4.3 KiB
109 lines
4.3 KiB
using Application.Domain.Entities;
|
|
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<SceneTiggerService>();
|
|
services.AddTransient<ISceneTiggerService, CachedSceneTiggerService>();
|
|
services.AddTransient<IDbConfig, DbConfig>();
|
|
services.AddTransient<IRoleService, RoleService>();
|
|
services.AddTransient<DataService>();
|
|
services.AddTransient<IoTNodeJob>();
|
|
services.AddSingleton<IoTNodeClient>();
|
|
services.AddSingleton<IOnvifDeviceManagement, OnvifDeviceManagement>();
|
|
services.AddSingleton<OnvifService>();
|
|
services.AddSingleton<FBeeService>();
|
|
services.AddSingleton<SerialPortService>();
|
|
services.AddHostedService(o => o.GetService<IoTNodeClient>());
|
|
services.AddHostedService(o => o.GetService<OnvifService>());
|
|
services.AddHostedService(o => o.GetService<FBeeService>());
|
|
services.AddHostedService(o => o.GetService<SerialPortService>());
|
|
services.AddTransient<IEmailSender, EmptyEmailSender>();
|
|
//services.AddHostedService<UpdateDameonService>();
|
|
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();
|
|
var version = Convert.ToInt64(Helper.Instance.GetVersion().Replace(".", ""));
|
|
UpdateDataBase(app, "10020082702.sql", () => version < 10020082702);
|
|
UpdateVersion(app);
|
|
}
|
|
|
|
private void UpdateVersion(IApplicationBuilder app)
|
|
{
|
|
var scope = app.ApplicationServices.CreateScope();
|
|
var services = scope.ServiceProvider;
|
|
var context = services.GetService<DbContext>();
|
|
var node = context.Set<Node>().FirstOrDefault();
|
|
node.Version = Helper.Instance.GetVersion();
|
|
context.SaveChanges();
|
|
}
|
|
|
|
private void UpdateDataBase(IApplicationBuilder app, string sqlFile, Func<bool> func)
|
|
{
|
|
using var scope = app.ApplicationServices.CreateScope();
|
|
var services = scope.ServiceProvider;
|
|
var context = services.GetService<DbContext>();
|
|
if (func.Invoke())
|
|
{
|
|
var updateSqlFile = Path.Combine(this.Env.ContentRootPath, sqlFile);
|
|
if (File.Exists(updateSqlFile))
|
|
{
|
|
var sql = File.ReadAllText(updateSqlFile);
|
|
try
|
|
{
|
|
context.Database.BeginTransaction();
|
|
context.Database.ExecuteSqlRaw(sql);
|
|
context.Database.CommitTransaction();
|
|
File.Delete(updateSqlFile);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"update database by {sqlFile} error ");
|
|
ex.PrintStack();
|
|
context.Database.RollbackTransaction();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |