驱动目录和数据库配置信息,支持优先从环境变量中获取(如果不存在,则使用原有的默认配置)。环境变量名称如下:

IoTGateway_DB
IoTGateway_DBType
IOT_DriverPath
This commit is contained in:
liushoushan_notebook 2022-07-23 08:44:35 +08:00
parent 51d1fb0589
commit 03ca1b5e45
2 changed files with 30 additions and 8 deletions

View File

@ -26,7 +26,11 @@ namespace Plugin
_logger = logger; _logger = logger;
try try
{ {
_logger.LogInformation("LoadDriverFiles Start"); var env_DriverPath = Environment.GetEnvironmentVariable("IOT_DriverPath");
if (!string.IsNullOrEmpty(env_DriverPath) && Directory.Exists(env_DriverPath))
DriverPath = env_DriverPath;
_logger.LogInformation($"LoadDriverFiles Start,Path:{DriverPath}");
driverFiles = Directory.GetFiles(DriverPath).Where(x => Path.GetExtension(x) == ".dll").ToArray(); driverFiles = Directory.GetFiles(DriverPath).Where(x => Path.GetExtension(x) == ".dll").ToArray();
_logger.LogInformation($"LoadDriverFiles EndCount{driverFiles.Count()}"); _logger.LogInformation($"LoadDriverFiles EndCount{driverFiles.Count()}");
} }

View File

@ -1,5 +1,6 @@
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -12,35 +13,52 @@ namespace Plugin
{ {
public class IoTBackgroundService : BackgroundService public class IoTBackgroundService : BackgroundService
{ {
private readonly ILogger<IoTBackgroundService> _logger;
public static DBTypeEnum DBType; public static DBTypeEnum DBType;
public static string connnectSetting; public static string connnectSetting;
public static Guid? VariableSelectDeviceId, ConfigSelectDeviceId; public static Guid? VariableSelectDeviceId, ConfigSelectDeviceId;
public IoTBackgroundService(IConfiguration ConfigRoot) public IoTBackgroundService(IConfiguration ConfigRoot, ILogger<IoTBackgroundService> logger)
{ {
_logger= logger;
var connnectSettings = new List<ConnnectSettingsModel>(); var connnectSettings = new List<ConnnectSettingsModel>();
ConfigRoot.Bind("Connections", connnectSettings); ConfigRoot.Bind("Connections", connnectSettings);
connnectSetting = connnectSettings[0].Value; connnectSetting = connnectSettings[0].Value;
DBType = GetDBType(connnectSettings[0].DBType);
//read from Environment
var IoTGateway_DB = Environment.GetEnvironmentVariable("IoTGateway_DB");
var IoTGateway_DBType = Environment.GetEnvironmentVariable("IoTGateway_DBType");
if (!string.IsNullOrEmpty(IoTGateway_DB) && !string.IsNullOrEmpty(IoTGateway_DBType))
{
connnectSetting = IoTGateway_DB;
DBType = GetDBType(IoTGateway_DBType);
}
_logger.LogInformation($"IoTBackgroundService connnectSetting:{connnectSetting},DBType:{DBType}");
}
switch (connnectSettings[0].DBType.Trim().ToLower()) private DBTypeEnum GetDBType(string dbtypeStr)
{
DBTypeEnum dbType = DBTypeEnum.SQLite;
switch (dbtypeStr.Trim().ToLower())
{ {
case "oracle": case "oracle":
DBType = DBTypeEnum.Oracle; dbType = DBTypeEnum.Oracle;
break; break;
case "mysql": case "mysql":
DBType = DBTypeEnum.MySql; dbType = DBTypeEnum.MySql;
break; break;
case "pgsql": case "pgsql":
DBType = DBTypeEnum.PgSql; dbType = DBTypeEnum.PgSql;
break; break;
case "sqlite": case "sqlite":
DBType = DBTypeEnum.SQLite; dbType = DBTypeEnum.SQLite;
break; break;
case "memory": case "memory":
DBType = DBTypeEnum.Memory; dbType = DBTypeEnum.Memory;
break; break;
default: default:
break; break;
} }
return dbType;
} }
protected override Task ExecuteAsync(CancellationToken stoppingToken) protected override Task ExecuteAsync(CancellationToken stoppingToken)
{ {