using WcsMain.Socket;
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;
using WcsMain.EquipOperation;
namespace WcsMain.StartAction;
///
/// 服务启动主体类
///
[Component]
public class ServiceStart(WcsCirculation wcsCirculation, ConnectPLCs connectPLCs, SocketOperation socketOperation,
AppStackerDao appStackerDao, AppLocationDao appLocationDao, AppConfigDao appConfigDao, ConnectPlcServe connectPlcServe,
ConnectOprServe connectOprServe, AppElTagBaseDao appElTagBaseDao, AppConveyStandDao conveyStandDao)
{
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 readonly AppElTagBaseDao _appElTagBaseDao = appElTagBaseDao;
private readonly AppConveyStandDao _conveyStandDao = conveyStandDao;
private static string _errMsg = string.Empty;
///
/// 加载必要参数
///
public void LoadingData()
{
LoadingConfig(); // 加载数据库中的配置项 (config 表)
LoadingStackerData(); // 加载数据库中的堆垛机信息
LoadingLocationData(); // 加载数据库中的库位信息
LoadElTagBase(); // 加载电子标签基础资料
LoadConveyStand(); // 加载输送线站台基础资料
if (!string.IsNullOrEmpty(_errMsg))
{
ConsoleLog.Error($"【异常】启动加载运行文件出错,WCS功能受到限制,您可以检查网络连接后重新启动或者联系我们,参考信息:{_errMsg}");
return;
}
}
///
/// 启动 ——-- 主方法
///
public void Start()
{
/* 指定线程池规格 */
ThreadPool.SetMinThreads(30, 10);
CreatePlcClient(); // 连接 PLC 客户端
CreateSocketClient(); // Socket客户端
ConnectOprServe(); // 连接 Opr 电子标签客户端
CreateCieculateTask();// 创建并启用定时器
}
///
/// 创建并连接 Plc
///
public void CreatePlcClient()
{
if (CommonData.AppConfig.UseConnectPlc == "1") // 1 表示允许连接PLC
{
_connectPlcs.ConnectPlc();
}
}
///
/// 创建并运行 Socket 客户端 ---- 待更新
///
public void CreateSocketClient()
{
if (CommonData.AppConfig.UseSocket == "1") // 1 表示允许开启Socket
{
_socketOperation.Connect();
}
}
///
/// 连接Opr电子标签客户端
///
public void ConnectOprServe()
{
if (CommonData.AppConfig.UseOpr == "1") // 1 表示允许连接电子标签
{
_connectOprServe.SetBaseAction();
_connectOprServe.Connect();
}
}
public void CreateTcpClient()
{
_connectPlcServe.SetBaseAction();
_connectPlcServe.ConnectPlc();
}
///
/// 创建并启用定时器
///
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);
}
}
///
/// 加载数据库内的 Config 配置信息
///
public void LoadingConfig()
{
if (!string.IsNullOrEmpty(_errMsg)) return;
//ConsoleLog.Info("正在加载数据库配置信息...");
List? 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;
}
///
/// 加载堆垛机数据
///
public void LoadingStackerData()
{
if (!string.IsNullOrEmpty(_errMsg)) return;
//ConsoleLog.Info("正在加载堆垛机参数信息...");
List? appStackers = _appStackerDao.Select();
if (appStackers == default)
{
_errMsg = "加载堆垛机参数信息失败,请检查数据库服务器连接是否正常";
CommonData.AppStackers = [];
return;
}
CommonData.AppStackers = appStackers;
}
///
/// 加载库位信息
///
public void LoadingLocationData()
{
if (!string.IsNullOrEmpty(_errMsg)) return;
//ConsoleLog.Info("正在加载库位信息...");
List? appLocations = _applocationDao.Select();
if (appLocations == default)
{
_errMsg = "加载库位信息失败,请检查数据库服务器连接是否正常";
CommonData.AppLocations = [];
return;
}
CommonData.AppLocations = appLocations;
}
///
/// 加载电子标签基础资料
///
public void LoadElTagBase()
{
if (!string.IsNullOrEmpty(_errMsg)) return;
//ConsoleLog.Info("正在电子标签基础资料信息...");
List? eltags = _appElTagBaseDao.Query();
if (eltags == default)
{
_errMsg = "加载电子标签基础信息失败,请检查数据库服务器连接是否正常";
CommonData.AppElTags = [];
return;
}
CommonData.AppElTags = eltags;
}
///
/// 加载输送线站台基础资料信息
///
public void LoadConveyStand()
{
if (!string.IsNullOrEmpty(_errMsg)) return;
//ConsoleLog.Info("正在加载输送线基础资料信息...");
List? appConveyStands = _conveyStandDao.Query();
if (appConveyStands == default)
{
_errMsg = "加载输送线站台基础资料信息失败,请检查数据库服务器连接是否正常";
CommonData.AppConveyStands = [];
return;
}
CommonData.AppConveyStands = appConveyStands;
}
}