You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.9 KiB
C#

3 months ago
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using SlnMesnac.Config;
using SlnMesnac.Plc;
using SlnMesnac.Plc.Factory;
using System;
using System.Collections.Generic;
using System.Linq;
3 months ago
using System.Text;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2024 WenJY
* CLR4.0.30319.42000
* LAPTOP-E0N2L34V
* SlnMesnac.Extensions
* 9bf604b4-3937-476a-adb0-27adc6fbea28
*
* WenJY
* wenjy@mesnac.com
* 2024-04-12 15:25:47
* 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
// {
// do
// {
// if (!HslCommunication.Authorization.SetAuthorizationCode("1839541f-8fb4-42c4-a13f-733b027fe5af"))
// {
// Log.Information("HslCommunication激活失败可用时长为24小时");
// break;
// }
// else
// {
// Log.Information("HslCommunication激活成功");
// }
// 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")}");
// _plc.ConfigKey = item.plcKey;
// if (absractFactories.Contains(_plc))
// {
// absractFactories.Remove(_plc);
// }
// absractFactories.Add(_plc);
// }
// else
// {
// Log.Information($"PLC{item.plcIp}:{item.plcPort};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
// }
// }
// }
// }
// }while(false);
// }
// catch (Exception e)
// {
// Log.Error($"PLC初始化连接异常{e.Message}");
// }
// return absractFactories;
//});
services.AddSingleton<PlcAbsractFactory>(x =>
3 months ago
{
AppConfig appConfig = x.GetService<AppConfig>();
//List<PlcAbsractFactory> absractFactories = new List<PlcAbsractFactory>();
3 months ago
try
{
do
{
if (!HslCommunication.Authorization.SetAuthorizationCode("1839541f-8fb4-42c4-a13f-733b027fe5af"))
{
Log.Information("HslCommunication激活失败可用时长为24小时");
break;
}
else
{
Log.Information("HslCommunication激活成功");
}
if (appConfig.plcConfig != null)
{
PlcConfig item = appConfig.plcConfig.Where(x => x.plcKey == "iot").FirstOrDefault();
if (item.isFlage)
3 months ago
{
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")}");
_plc.ConfigKey = item.plcKey;
return _plc;
}
else
3 months ago
{
Log.Information($"PLC{item.plcIp}:{item.plcPort};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
3 months ago
}
}
}
} while (false);
3 months ago
}
catch (Exception e)
{
Log.Error($"PLC初始化连接异常{e.Message}");
}
return null;
3 months ago
});
}
private 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;
}
}
}