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.
iot/projects/IoTNode/Startup.cs

141 lines
5.2 KiB

using Infrastructure.Email;
using Infrastructure.Extensions;
using Infrastructure.UI;
using Infrastructure.Web;
using IoT.Shared.Services;
using IoTNode.Data;
using IoTNode.DeviceServices.FBee;
using IoTNode.DeviceServices.LiChuang;
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.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
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.AddTransient<IUserService, UserService>();
services.AddTransient<DataService>();
services.AddTransient<IoTNodeEventHandler>();
services.AddSingleton<IoTNodeClient>();
services.AddSingleton<IOnvifDeviceManagement, OnvifDeviceManagement>();
services.AddSingleton<SerialPortService>();
services.AddSingleton<OnvifService>();
services.AddSingleton<FBeeService>();
services.AddSingleton<LCDAUE800DService>();
services.AddHostedService(o => o.GetService<IoTNodeClient>());
services.AddTransient<IEmailSender, EmptyEmailSender>();
if (Env.IsDevelopment())
{
services.AddHostedService(o => o.GetService<SerialPortService>());
services.AddHostedService(o => o.GetService<OnvifService>());
services.AddHostedService(o => o.GetService<FBeeService>());
services.AddHostedService(o => o.GetService<LCDAUE800DService>());
}
else
{
services.AddHostedService(o => o.GetService<SerialPortService>());
services.AddHostedService(o => o.GetService<OnvifService>());
services.AddHostedService(o => o.GetService<FBeeService>());
services.AddHostedService(o => o.GetService<LCDAUE800DService>());
}
base.ConfigureServices(services);
}
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
base.Configure(app, env, loggerFactory);
UpdateDB(app);
}
public override void ConfigureOptions(IServiceCollection services)
{
//services.ConfigureOptions(new FileConfigureOptions(Env, new List<string> { "IoT.Shared" }));
}
public override void AddDbContext(IServiceCollection services)
{
this.AddDbContext<IoTNodeDbContext>(services);
}
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();
var dbVersionValue = "";
try
{
cmd.CommandText = "select Version from IoTGateway";
context.Database.OpenConnection();
dbVersionValue = cmd.ExecuteScalar().ToString();
}
catch (Exception ex)
{
ex.PrintStack();
}
try
{
if (string.IsNullOrEmpty(dbVersionValue))
{
cmd.CommandText = "select Version from iot_Node";
dbVersionValue = cmd.ExecuteScalar().ToString();
}
}
catch (Exception ex)
{
ex.PrintStack();
}
var dbVersion = new Version(dbVersionValue);
var files = Directory.GetFiles(Path.Combine(Env.ContentRootPath, "upgradescripts"))
.ToDictionary(o => new Version(Path.GetFileNameWithoutExtension(o).Replace('-', '.').Replace('_', '.')))
.OrderBy(o => o.Key.Major)
.ThenBy(o => o.Key.Minor)
.ThenBy(o => o.Key.Build)
.ThenBy(o => o.Key.Revision)
.ToList();
foreach (var item in files)
{
if (dbVersion.CompareTo(item.Key) < 0)
{
var sql = File.ReadAllText(item.Value);
try
{
context.Database.BeginTransaction();
context.Database.ExecuteSqlRaw(sql);
context.Database.CommitTransaction();
}
catch (Exception ex)
{
context.Database.RollbackTransaction();
Console.WriteLine($"update database by {item.Key} error ");
ex.PrintStack();
break;
}
}
}
}
}
}