|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using SlnMesnac.Plc.Factory;
|
|
|
|
|
using SlnMesnac.Plc;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using SlnMesnac.Config;
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
|
|
#region << 版 本 注 释 >>
|
|
|
|
|
/*--------------------------------------------------------------------
|
|
|
|
|
* 版权所有 (c) 2024 WenJY 保留所有权利。
|
|
|
|
|
* CLR版本:4.0.30319.42000
|
|
|
|
|
* 机器名称:LAPTOP-E0N2L34V
|
|
|
|
|
* 命名空间:SlnMesnac.Extensions
|
|
|
|
|
* 唯一标识:f75da6dc-edf6-4328-848b-1f45a9b9f38b
|
|
|
|
|
*
|
|
|
|
|
* 创建者:WenJY
|
|
|
|
|
* 电子邮箱:wenjy@mesnac.com
|
|
|
|
|
* 创建时间:2024-04-15 09:52:10
|
|
|
|
|
* 版本:V1.0.0
|
|
|
|
|
* 描述:
|
|
|
|
|
*
|
|
|
|
|
*--------------------------------------------------------------------
|
|
|
|
|
* 修改人:
|
|
|
|
|
* 时间:
|
|
|
|
|
* 修改说明:
|
|
|
|
|
*
|
|
|
|
|
* 版本:V1.0.0
|
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
#endregion << 版 本 注 释 >>
|
|
|
|
|
namespace SlnMesnac.Extensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// PLC工厂
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class PlcFactorySetup
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public static void AddPlcFactorySetup(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<List<PlcAbsractFactory>>(x =>
|
|
|
|
|
{
|
|
|
|
|
AppConfig appConfig = x.GetService<AppConfig>();
|
|
|
|
|
|
|
|
|
|
List<PlcAbsractFactory> absractFactories = new List<PlcAbsractFactory>();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!HslCommunication.Authorization.SetAuthorizationCode("1839541f-8fb4-42c4-a13f-733b027fe5af"))
|
|
|
|
|
{
|
|
|
|
|
Log.Information("HslCommunication激活失败,可用时长24小时");
|
|
|
|
|
}
|
|
|
|
|
Log.Information("HslCommunication 11.8.1.0激活成功");
|
|
|
|
|
if (appConfig.plcConfig != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in appConfig.plcConfig)
|
|
|
|
|
{
|
|
|
|
|
if (item.isFlage)
|
|
|
|
|
{
|
|
|
|
|
PlcAbsractFactory _plc = InitPlc(x, item.plcType);
|
|
|
|
|
|
|
|
|
|
var connectResult = _plc.Connect(item.plcIp, item.plcPort);
|
|
|
|
|
if (connectResult)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Log.Information($"PLC:{item.plcIp}:{item.plcPort};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Information($"PLC:{item.plcIp}:{item.plcPort};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
|
|
|
|
|
}
|
|
|
|
|
_plc.ConfigKey = item.plcKey;
|
|
|
|
|
if (absractFactories.Contains(_plc))
|
|
|
|
|
{
|
|
|
|
|
absractFactories.Remove(_plc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
absractFactories.Add(_plc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"PLC初始化连接异常:{e.Message}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return absractFactories;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PlcAbsractFactory InitPlc(IServiceProvider serviceProvider, string plcType)
|
|
|
|
|
{
|
|
|
|
|
PlcAbsractFactory _plc = null;
|
|
|
|
|
var _inovance = serviceProvider.GetRequiredService<InovanceFactory>();
|
|
|
|
|
var _melsecBinary = serviceProvider.GetRequiredService<MelsecBinaryFactory>();
|
|
|
|
|
var _omronNj = serviceProvider.GetRequiredService<OmronNJFactory>();
|
|
|
|
|
var _siemens = serviceProvider.GetRequiredService<SiemensFactory>();
|
|
|
|
|
|
|
|
|
|
switch (plcType)
|
|
|
|
|
{
|
|
|
|
|
case "InovancePlc":
|
|
|
|
|
_plc = _inovance;
|
|
|
|
|
break;
|
|
|
|
|
case "MelsecBinaryPlc":
|
|
|
|
|
_plc = _melsecBinary;
|
|
|
|
|
break;
|
|
|
|
|
case "OmronNJPlc":
|
|
|
|
|
_plc = _omronNj;
|
|
|
|
|
break;
|
|
|
|
|
case "SiemensPlc":
|
|
|
|
|
_plc = _siemens;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _plc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|