修改系统框架

master
smartwyy 7 months ago
parent 07e7077d25
commit ec623c35e6

@ -50,17 +50,14 @@ public class AutofacModuleRegister : Module
{ {
if (ScopeConst.InstancePerLifetimeScope == scope.Scope) if (ScopeConst.InstancePerLifetimeScope == scope.Scope)
{ {
Log.Debug($"register InstancePerLifetimeScope {type}");
instancePerLifetimeScopeList.Add(type); instancePerLifetimeScopeList.Add(type);
} }
else if (ScopeConst.InstancePerDependency == scope.Scope) else if (ScopeConst.InstancePerDependency == scope.Scope)
{ {
Log.Debug($"register InstancePerDependency {type}");
instancePerDependencyList.Add(type); instancePerDependencyList.Add(type);
} }
else else
{ {
Log.Debug($"register SingleInstance {type}");
defaultList.Add(type); defaultList.Add(type);
} }
} }

@ -1,4 +1,5 @@
using Microsoft.Extensions.Configuration; using log4net;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json; using Microsoft.Extensions.Configuration.Json;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -10,11 +11,8 @@ namespace HybirdFrameworkCore.Configuration;
/// </summary> /// </summary>
public class AppSettingsHelper public class AppSettingsHelper
{ {
public AppSettingsHelper(string contentPath) private static readonly ILog Log = LogManager.GetLogger(typeof(AppSettingsHelper));
{ private static IConfiguration? Configuration { get; set; }
}
private static IConfiguration Configuration { get; set; }
/// <summary> /// <summary>
/// 封装要操作的字符 /// 封装要操作的字符
@ -22,21 +20,35 @@ public class AppSettingsHelper
/// </summary> /// </summary>
/// <param name="sections">节点配置</param> /// <param name="sections">节点配置</param>
/// <returns></returns> /// <returns></returns>
public static string GetContent(params string[] sections) public static string? GetContent(params string[] sections)
{ {
try try
{ {
var contentPath = AppDomain.CurrentDomain.BaseDirectory; if (Configuration == null)
var Path = "appsettings.json"; {
Configuration = new ConfigurationBuilder().SetBasePath(contentPath).Add(new JsonConfigurationSource string? activeProfile = Environment.GetEnvironmentVariable("profiles_active");
{ Path = Path, Optional = false, ReloadOnChange = true }).Build(); if (activeProfile == null)
{
activeProfile = "";
}
else
{
activeProfile += ".";
}
var path = "appsettings."+ activeProfile+ "json";
Log.Info($"read path={path}");
var contentPath = AppDomain.CurrentDomain.BaseDirectory;
Configuration = new ConfigurationBuilder().SetBasePath(contentPath).Add(new JsonConfigurationSource
{ Path = path, Optional = false, ReloadOnChange = true }).Build();
}
if (sections.Any()) return Configuration[string.Join(":", sections)]; if (sections.Any()) return Configuration[string.Join(":", sections)];
} }
catch (Exception) catch (Exception)
{ {
} }
return ""; return null;
} }
public static bool SetContent(string value, params string[] sections) public static bool SetContent(string value, params string[] sections)
@ -44,9 +56,18 @@ public class AppSettingsHelper
try try
{ {
var contentPath = AppDomain.CurrentDomain.BaseDirectory; var contentPath = AppDomain.CurrentDomain.BaseDirectory;
var Path = contentPath + "appsettings.json"; string? activeProfile = Environment.GetEnvironmentVariable("profiles_active");
if (activeProfile == null)
{
activeProfile = "";
}
else
{
activeProfile += ".";
}
var path = "appsettings."+ activeProfile+ "json";
JObject jsonObject; JObject jsonObject;
using (var file = new StreamReader(Path)) using (var file = new StreamReader(path))
using (var reader = new JsonTextReader(file)) using (var reader = new JsonTextReader(file))
{ {
jsonObject = (JObject)JToken.ReadFrom(reader); jsonObject = (JObject)JToken.ReadFrom(reader);
@ -54,7 +75,7 @@ public class AppSettingsHelper
jsonObject[sections[0]][sections[1]] = value; jsonObject[sections[0]][sections[1]] = value;
} }
using (var writer = new StreamWriter(Path)) using (var writer = new StreamWriter(path))
using (var jsonwriter = new JsonTextWriter(writer)) using (var jsonwriter = new JsonTextWriter(writer))
{ {
jsonObject.WriteTo(jsonwriter); jsonObject.WriteTo(jsonwriter);
@ -67,4 +88,24 @@ public class AppSettingsHelper
return true; return true;
} }
/// <summary>
///
/// </summary>
/// <param name="sections"></param>
/// <returns></returns>
public static bool GetBool(params string[] sections)
{
string s = GetContent(sections);
if (string.IsNullOrWhiteSpace(s))
{
return false;
}
if (bool.TryParse(s, out var result))
{
return result;
}
return false;
}
} }

@ -159,6 +159,12 @@ public static class ClientMgr
Thread.Sleep(1000 * 30); Thread.Sleep(1000 * 30);
DateTime now = DateTime.Now; DateTime now = DateTime.Now;
if (StaticStationInfo.AutoChargeEnabled != 1)
{
Log.Info("AutoChargeEnabled = 0 continue");
continue;
}
List<BinInfo> binInfos = binInfoRepository.Query(); List<BinInfo> binInfos = binInfoRepository.Query();
if (binInfos.Count < 0) if (binInfos.Count < 0)
{ {
@ -227,6 +233,7 @@ public static class ClientMgr
int needStopCount = chargingList.Count - (canSwapList.Count - needBatteryCount); int needStopCount = chargingList.Count - (canSwapList.Count - needBatteryCount);
if (needStopCount > 0) if (needStopCount > 0)
{ {
//停电量低的
chargingList.Sort((a,b) => (a.Soc??0).CompareTo(b.Soc??0)); chargingList.Sort((a,b) => (a.Soc??0).CompareTo(b.Soc??0));
for (int i = 0; i < needStopCount; i++) for (int i = 0; i < needStopCount; i++)
{ {
@ -238,6 +245,7 @@ public static class ClientMgr
else else
{ {
List<BinInfo> canChargeList = binInfos.Where(it => it.Soc != null && Convert.ToSingle(it.Soc) < StaticStationInfo.SwapSoc && it.CanChargeFlag == 1).ToList(); List<BinInfo> canChargeList = binInfos.Where(it => it.Soc != null && Convert.ToSingle(it.Soc) < StaticStationInfo.SwapSoc && it.CanChargeFlag == 1).ToList();
//启动电量高的
canChargeList.Sort((a,b) => (b.Soc??0).CompareTo(a.Soc??0)); canChargeList.Sort((a,b) => (b.Soc??0).CompareTo(a.Soc??0));
byte chargeSoc = StaticStationInfo.ChargeSoc; byte chargeSoc = StaticStationInfo.ChargeSoc;
@ -277,8 +285,8 @@ public static class ClientMgr
{ {
Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}"); Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
ChargerClient client = AppInfo.Container.Resolve<ChargerClient>(); ChargerClient client = AppInfo.Container.Resolve<ChargerClient>();
client.BinNo = binInfo.No; client.BinNo = binInfo?.No;
client.BatteryNo = binInfo.BatteryNo; client.BatteryNo = binInfo?.BatteryNo;
client.InitBootstrap(netInfo.NetAddr, int.Parse(netInfo.NetPort)); client.InitBootstrap(netInfo.NetAddr, int.Parse(netInfo.NetPort));
Task.Run(() => Task.Run(() =>

@ -4,11 +4,13 @@ using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Configuration; using HybirdFrameworkCore.Configuration;
using HybirdFrameworkCore.Entity; using HybirdFrameworkCore.Entity;
using HybirdFrameworkCore.Redis; using HybirdFrameworkCore.Redis;
using log4net;
using Service.Charger.Client; using Service.Charger.Client;
using SqlSugar; using SqlSugar;
using SqlSugar.IOC; using SqlSugar.IOC;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
var log = LogManager.GetLogger(typeof(Program));
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(cb => builder.Host.ConfigureContainer<ContainerBuilder>(cb =>
@ -31,15 +33,11 @@ builder.Host.ConfigureContainer<ContainerBuilder>(cb =>
}); });
//redis //redis
var section = builder.Configuration.GetSection("Redis"); var redisConnectionString = AppSettingsHelper.GetContent("Redis", "Connection");
//连接字符串 var instanceName = AppSettingsHelper.GetContent("Redis", "InstanceName");//默认数据库
var redisConnectionString = section.GetSection("Connection").Value; var defaultDb = int.Parse(AppSettingsHelper.GetContent("Redis", "DefaultDB") ?? "0");
//实例名称
var instanceName = section.GetSection("InstanceName").Value;
//默认数据库
var _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "0");
if (redisConnectionString != null && instanceName != null) if (redisConnectionString != null && instanceName != null)
builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, _defaultDB)); builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, defaultDb));
builder.Services.AddControllers(); builder.Services.AddControllers();
@ -79,12 +77,9 @@ var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) app.UseSwagger();
{ app.UseSwaggerUI();
app.UseSwagger(); app.UseStaticFiles();
app.UseSwaggerUI();
app.UseStaticFiles();
}
app.UseAuthorization(); app.UseAuthorization();
app.UseCors("myCors"); app.UseCors("myCors");
@ -93,9 +88,11 @@ app.MapControllers();
var list = AppSettingsHelper.GetContent("Kestrel", "Endpoints", "http", "Url"); var list = AppSettingsHelper.GetContent("Kestrel", "Endpoints", "http", "Url");
foreach (var s in list.Split(";")) foreach (var s in list.Split(";"))
{ {
log.Info($"use url={s}");
app.Urls.Add(s); app.Urls.Add(s);
} }
AppInfo.Container = app.Services.GetAutofacRoot(); AppInfo.Container = app.Services.GetAutofacRoot();
ClientMgr.InitClient(); ClientMgr.InitClient();

@ -16,7 +16,8 @@
"launchUrl": "swagger", "launchUrl": "swagger",
"applicationUrl": "http://localhost:5034", "applicationUrl": "http://localhost:5034",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development",
"profiles_active": "dev"
} }
}, },
"IIS Express": { "IIS Express": {
@ -24,7 +25,8 @@
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "swagger", "launchUrl": "swagger",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development",
"profiles_active": "dev"
} }
} }
} }

@ -26,7 +26,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Update="appsettings.json"> <Content Update="appsettings.prod.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>

@ -1,8 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

@ -0,0 +1,31 @@
{
"ConnectionStrings": {
"ConfigId": "master",
"DbType": "MySql",
"SqlConnection": "server=192.168.2.2;Port=3306;Database=huanneng_dev;Uid=root;Pwd=Rszn123;Charset=utf8;"
},
"Update": {
"AutoUpdate": "false",
"Version": "1.1.0.1",
"Url": "http://121.4.95.243:8090/Updates/AutoUpdaterStarter.xml"
},
"Redis": {
"Connection": "106.12.36.89:6379,password=123456",
"InstanceName": "local",
"DefaultDB": "8"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Kestrel": {
"Endpoints": {
"http": {
"Url": "http://*:15035"
}
}
},
"AllowedHosts": "*"
}

@ -1,31 +1,8 @@
{ {
"ConnectionStrings": {
"ConfigId": "master",
"DbType": "MySql",
"SqlConnection": "server=127.0.0.1;Port=3306;Database=huanneng_dev;Uid=root;Pwd=anyixing2023!@#;Charset=utf8;"
},
"Update": {
"AutoUpdate": "false",
"Version": "1.1.0.1",
"Url": "http://121.4.95.243:8090/Updates/AutoUpdaterStarter.xml"
},
"Redis": {
"Connection": "106.12.36.89:6379,password=123456",
"InstanceName": "local",
"DefaultDB": "8"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, }
"Kestrel": {
"Endpoints": {
"http": {
"Url": "http://0.0.0.0:5035"
}
}
},
"AllowedHosts": "*"
} }

@ -0,0 +1,31 @@
{
"ConnectionStrings": {
"ConfigId": "master",
"DbType": "MySql",
"SqlConnection": "server=localhost;Port=3306;Database=huanneng_dev;Uid=root;Pwd=123456;Charset=utf8;"
},
"Update": {
"AutoUpdate": "false",
"Version": "1.1.0.1",
"Url": "http://121.4.95.243:8090/Updates/AutoUpdaterStarter.xml"
},
"Redis": {
"Connection": "localhost:6379,password=123456",
"InstanceName": "local",
"DefaultDB": "8"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Kestrel": {
"Endpoints": {
"http": {
"Url": "http://*:5035"
}
}
},
"AllowedHosts": "*"
}

@ -1,8 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Loading…
Cancel
Save