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.
122 lines
4.7 KiB
122 lines
4.7 KiB
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();
|
|
|
|
UpdateDB(app);
|
|
}
|
|
|
|
private void UpdateDB(IApplicationBuilder app)
|
|
{
|
|
using var scope = app.ApplicationServices.CreateScope();
|
|
var services = scope.ServiceProvider;
|
|
var context = services.GetService<DbContext>();
|
|
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;
|
|
}
|
|
}
|
|
} |