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,
ConnectOprServe connectOprServe, AppElTagLocationDao appElTagBaseDao, AppConveyStandDao conveyStandDao, AppApiBaseInfoDao appApiBaseInfoDao)
{
private static string _errMsg = string.Empty;
///
/// 加载必要参数
///
public void LoadingData()
{
LoadingConfig(); // 加载数据库中的配置项 (config 表)
LoadApiBaseInfo(); // 加载API基础资料
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 电子标签客户端
CreateCirculateTask();// 创建并启用定时器
}
///
/// 创建并连接 Plc
///
private void CreatePlcClient()
{
if (CommonData.AppConfig.UseConnectPlc == "1") // 1 表示允许连接PLC
{
connectPLCs.ConnectPlc();
}
}
///
/// 创建并运行 Socket 客户端 ---- 待更新
///
private void CreateSocketClient()
{
if (CommonData.AppConfig.UseSocket == "1") // 1 表示允许开启Socket
{
socketOperation.Connect();
}
}
///
/// 连接Opr电子标签客户端
///
private void ConnectOprServe()
{
if (CommonData.AppConfig.UseOpr == "1") // 1 表示允许连接电子标签
{
connectOprServe.SetBaseAction();
connectOprServe.Connect();
}
}
///
/// 创建并启用定时器
///
private void CreateCirculateTask()
{
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 配置信息
///
private 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;
}
///
/// 加载API基础资料
///
private void LoadApiBaseInfo()
{
if (!string.IsNullOrEmpty(_errMsg)) return;
//ConsoleLog.Info("正在加载API配置信息...");
List? appApiBaseInfos = appApiBaseInfoDao.Query();
if (appApiBaseInfos == default)
{
_errMsg = "加载API配置信息失败,请检查数据库服务器连接是否正常";
CommonData.AppApiBaseInfos = [];
return;
}
CommonData.AppApiBaseInfos = appApiBaseInfos;
}
///
/// 加载堆垛机数据
///
private void LoadingStackerData()
{
if (!string.IsNullOrEmpty(_errMsg)) return;
//ConsoleLog.Info("正在加载堆垛机参数信息...");
List? appStackers = appStackerDao.Select();
if (appStackers == default)
{
_errMsg = "加载堆垛机参数信息失败,请检查数据库服务器连接是否正常";
CommonData.AppStackers = [];
return;
}
CommonData.AppStackers = appStackers;
}
///
/// 加载库位信息
///
private void LoadingLocationData()
{
if (!string.IsNullOrEmpty(_errMsg)) return;
//ConsoleLog.Info("正在加载库位信息...");
List? appLocations = appLocationDao.Select();
if (appLocations == default)
{
_errMsg = "加载库位信息失败,请检查数据库服务器连接是否正常";
CommonData.AppLocations = [];
return;
}
CommonData.AppLocations = appLocations;
}
///
/// 加载电子标签基础资料
///
private void LoadElTagBase()
{
if (!string.IsNullOrEmpty(_errMsg)) return;
//ConsoleLog.Info("正在电子标签基础资料信息...");
List? eltags = appElTagBaseDao.Query();
if (eltags == default)
{
_errMsg = "加载电子标签基础信息失败,请检查数据库服务器连接是否正常";
CommonData.AppElTags = [];
return;
}
CommonData.AppElTags = eltags;
}
///
/// 加载输送线站台基础资料信息
///
private void LoadConveyStand()
{
if (!string.IsNullOrEmpty(_errMsg)) return;
//ConsoleLog.Info("正在加载输送线基础资料信息...");
List? appConveyStands = conveyStandDao.Query();
if (appConveyStands == default)
{
_errMsg = "加载输送线站台基础资料信息失败,请检查数据库服务器连接是否正常";
CommonData.AppConveyStands = [];
return;
}
CommonData.AppConveyStands = appConveyStands;
}
}