198 lines
6.3 KiB
C#
198 lines
6.3 KiB
C#
using WcsMain.Socket;
|
||
using WcsMain.PlcOperation;
|
||
using WcsMain.Common;
|
||
using WcsMain.WcsAttribute.AutoFacAttribute;
|
||
using WcsMain.Plugins;
|
||
using System.Reflection;
|
||
using WcsMain.AppEntity.SystemData;
|
||
using WcsMain.DataBase.Dao;
|
||
using WcsMain.DataBase.TableEntity;
|
||
using WcsMain.WcsAttribute.AppConfig;
|
||
using WcsMain.Business.Convey;
|
||
using WcsMain.ElTag.Atop;
|
||
|
||
namespace WcsMain.StartAction;
|
||
|
||
/// <summary>
|
||
/// 服务启动主体类
|
||
/// </summary>
|
||
[Component]
|
||
public class ServiceStart(WcsCirculation wcsCirculation, ConnectPLCs connectPLCs, SocketOperation socketOperation,
|
||
AppStackerDao appStackerDao, AppLocationDao appLocationDao, AppConfigDao appConfigDao, ConnectPlcServe connectPlcServe,
|
||
ConnectOprServe connectOprServe)
|
||
{
|
||
private readonly AppConfigDao _appconfigDao = appConfigDao;
|
||
private readonly AppLocationDao _applocationDao = appLocationDao;
|
||
private readonly AppStackerDao _appStackerDao = appStackerDao;
|
||
private readonly SocketOperation _socketOperation = socketOperation;
|
||
private readonly ConnectPLCs _connectPlcs = connectPLCs;
|
||
private readonly WcsCirculation _wcsCirculation = wcsCirculation;
|
||
private readonly ConnectPlcServe _connectPlcServe = connectPlcServe;
|
||
private readonly ConnectOprServe _connectOprServe = connectOprServe;
|
||
|
||
private static string _errMsg = string.Empty;
|
||
|
||
/// <summary>
|
||
/// 加载必要参数
|
||
/// </summary>
|
||
public void LoadingData()
|
||
{
|
||
LoadingConfig(); // 加载数据库中的配置项 (config 表)
|
||
LoadingStackerData(); // 加载数据库中的堆垛机信息
|
||
LoadingLocationData(); // 加载数据库中的库位信息
|
||
|
||
if (!string.IsNullOrEmpty(_errMsg))
|
||
{
|
||
ConsoleLog.Error($"【异常】启动加载运行文件出错,WCS功能受到限制,您可以检查网络连接后重新启动或者联系我们,参考信息:{_errMsg}");
|
||
return;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 启动 ——-- 主方法
|
||
/// </summary>
|
||
public void Start()
|
||
{
|
||
/* 指定线程池规格 */
|
||
ThreadPool.SetMinThreads(30, 10);
|
||
|
||
CreatePlcClient(); // 连接 PLC 客户端
|
||
CreateSocketClient(); // Socket客户端
|
||
ConnectOprServe(); // 连接 Opr 电子标签客户端
|
||
CreateCieculateTask();// 创建并启用定时器
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建并连接 Plc
|
||
/// </summary>
|
||
public void CreatePlcClient()
|
||
{
|
||
if (CommonData.AppConfig.UseConnectPlc == "1") // 1 表示允许连接PLC
|
||
{
|
||
_connectPlcs.ConnectPlc();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建并运行 Socket 客户端 ---- 待更新
|
||
/// </summary>
|
||
public void CreateSocketClient()
|
||
{
|
||
if (CommonData.AppConfig.UseSocket == "1") // 1 表示允许开启Socket
|
||
{
|
||
_socketOperation.Connect();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 连接Opr电子标签客户端
|
||
/// </summary>
|
||
public void ConnectOprServe()
|
||
{
|
||
if (CommonData.AppConfig.UseOpr == "1") // 1 表示允许连接电子标签
|
||
{
|
||
_connectOprServe.SetBaseAction();
|
||
_connectOprServe.Connect();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
public void CreateTcpClient()
|
||
{
|
||
_connectPlcServe.SetBaseAction();
|
||
_connectPlcServe.ConnectPlc();
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 创建并启用定时器
|
||
/// </summary>
|
||
public void CreateCieculateTask()
|
||
{
|
||
if (CommonData.AppConfig.UseCirculation == "1") // 1 表示允许开启 定时器
|
||
{
|
||
_wcsCirculation.ExceptionHandler += (methodDescription, ex) =>
|
||
{
|
||
ConsoleLog.Error($"定时器:{methodDescription} 发生异常,异常信息:{ex}");
|
||
};
|
||
_wcsCirculation.MessageHandler += (msg) =>
|
||
{
|
||
//ConsoleLog.Tip(msg);
|
||
};
|
||
_wcsCirculation.StartAssemblyCirculation(GetType().Assembly);
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 加载数据库内的 Config 配置信息
|
||
/// </summary>
|
||
public void LoadingConfig()
|
||
{
|
||
if (!string.IsNullOrEmpty(_errMsg)) return;
|
||
ConsoleLog.Info("正在加载数据库配置信息...");
|
||
List<AppConfig>? Configs = _appconfigDao.Query();
|
||
if (Configs == default)
|
||
{
|
||
_errMsg = "加载 Config 资源失败,请检查数据库服务器连接是否正常";
|
||
return;
|
||
}
|
||
AppConfigEntity? appConfigEntity = new();
|
||
var type = appConfigEntity.GetType();
|
||
var propertys = type.GetProperties();
|
||
foreach (var property in propertys)
|
||
{
|
||
string? propertyName = property.Name;
|
||
var attribute = property.GetCustomAttribute(typeof(ConfigKeyAttribute));
|
||
if (attribute != default)
|
||
{
|
||
var configKeyAttribute = attribute as ConfigKeyAttribute;
|
||
if (configKeyAttribute != default && configKeyAttribute.KeyName != default)
|
||
{
|
||
propertyName = configKeyAttribute.KeyName;
|
||
}
|
||
}
|
||
AppConfig? appConfig = Configs.Find(f => f.ConfigKey == propertyName);
|
||
if (appConfig == default) continue;
|
||
property.SetValue(appConfigEntity, appConfig.ConfigValue);
|
||
}
|
||
CommonData.AppConfig = appConfigEntity;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载堆垛机数据
|
||
/// </summary>
|
||
public void LoadingStackerData()
|
||
{
|
||
if (!string.IsNullOrEmpty(_errMsg)) return;
|
||
ConsoleLog.Info("正在加载堆垛机参数信息...");
|
||
List<AppStacker>? appStackers = _appStackerDao.Select();
|
||
if (appStackers == default)
|
||
{
|
||
_errMsg = "加载堆垛机参数信息失败,请检查数据库服务器连接是否正常";
|
||
CommonData.AppStackers = [];
|
||
return;
|
||
}
|
||
CommonData.AppStackers = appStackers;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 加载库位信息
|
||
/// </summary>
|
||
public void LoadingLocationData()
|
||
{
|
||
if (!string.IsNullOrEmpty(_errMsg)) return;
|
||
ConsoleLog.Info("正在加载库位信息...");
|
||
List<AppLocation>? appLocations = _applocationDao.Select();
|
||
if (appLocations == default)
|
||
{
|
||
_errMsg = "加载库位信息失败,请检查数据库服务器连接是否正常";
|
||
CommonData.AppLocations = [];
|
||
return;
|
||
}
|
||
CommonData.AppLocations = appLocations;
|
||
}
|
||
|
||
} |