|
|
using Microsoft.Extensions.Logging;
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
using SlnMesnac.Config;
|
|
|
using SlnMesnac.Plc.Impl;
|
|
|
using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace SlnMesnac.Plc
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// PLC连接池
|
|
|
/// </summary>
|
|
|
public class PlcPool
|
|
|
{
|
|
|
|
|
|
private ILogger<PlcPool> _logger;
|
|
|
|
|
|
private Dictionary<string, IPlc> keyValuePairs = new Dictionary<string, IPlc>();
|
|
|
|
|
|
private readonly InovancePlc _inovancePlc;
|
|
|
private readonly MelsecBinaryPlc _melsecBinaryPlc;
|
|
|
private readonly OmronNJPlc _omronNJPlc;
|
|
|
private readonly SiemensPlc _siemensPlc;
|
|
|
private readonly AppConfig _appConfig;
|
|
|
|
|
|
public PlcPool(ILogger<PlcPool> logger, InovancePlc inovancePlc, MelsecBinaryPlc melsecBinaryPlc, OmronNJPlc omronNJPlc, SiemensPlc siemensPlc, AppConfig appConfig)
|
|
|
{
|
|
|
_logger = logger;
|
|
|
_inovancePlc = inovancePlc;
|
|
|
_melsecBinaryPlc = melsecBinaryPlc;
|
|
|
_omronNJPlc = omronNJPlc;
|
|
|
_siemensPlc = siemensPlc;
|
|
|
_appConfig = appConfig;
|
|
|
|
|
|
//this.Init();
|
|
|
}
|
|
|
|
|
|
public void Init()
|
|
|
{
|
|
|
if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
|
|
|
{
|
|
|
_logger.LogInformation("HslCommunication激活失败,可用时长24小时");
|
|
|
}
|
|
|
_logger.LogInformation("HslCommunication 11.0.6.0激活成功");
|
|
|
|
|
|
try
|
|
|
{
|
|
|
if (_appConfig.plcConfig != null)
|
|
|
{
|
|
|
Task.Run(() =>
|
|
|
{
|
|
|
foreach (var item in _appConfig.plcConfig)
|
|
|
{
|
|
|
AddPlc(item.plcType, item.plcIp, item.plcPort, item.plcKey);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
_logger.LogInformation("PLC配置信息为空");
|
|
|
}
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
_logger.LogError($"PLC初始化连接异常:{e.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 添加PLC连接
|
|
|
/// </summary>
|
|
|
/// <param name="plcType"></param>
|
|
|
/// <param name="ip"></param>
|
|
|
/// <param name="port"></param>
|
|
|
/// <param name="key"></param>
|
|
|
public void AddPlc(string plcType, string ip, int port, string key)
|
|
|
{
|
|
|
IPlc _plc = null;
|
|
|
switch (plcType)
|
|
|
{
|
|
|
case "InovancePlc":
|
|
|
_plc = _inovancePlc;
|
|
|
break;
|
|
|
case "MelsecBinaryPlc":
|
|
|
_plc = _melsecBinaryPlc;
|
|
|
break;
|
|
|
case "OmronNJPlc":
|
|
|
_plc = _omronNJPlc;
|
|
|
break;
|
|
|
case "SiemensPlc":
|
|
|
_plc = _siemensPlc;
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
var connectResult = _plc.Connect(ip, port);
|
|
|
if (connectResult)
|
|
|
{
|
|
|
keyValuePairs.Add(key, _plc);
|
|
|
if (!keyValuePairs.ContainsKey(key))
|
|
|
{
|
|
|
keyValuePairs.Add(key, _plc);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
keyValuePairs[key] = _plc;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取PLC
|
|
|
/// </summary>
|
|
|
/// <param name="key"></param>
|
|
|
/// <returns></returns>
|
|
|
public IPlc GetPlcByKey(string key)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return keyValuePairs[key];
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
throw new ArgumentException($"根据PLC Key获取连接信息异常:{e.Message}"); ;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取所有PLC信息
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public Dictionary<string, IPlc> GetAll()
|
|
|
{
|
|
|
return keyValuePairs;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 关闭所有连接
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public bool disConnectAll()
|
|
|
{
|
|
|
bool result = false;
|
|
|
try
|
|
|
{
|
|
|
foreach (var kv in keyValuePairs)
|
|
|
{
|
|
|
if (kv.Value != null)
|
|
|
{
|
|
|
kv.Value.DisConnect();
|
|
|
}
|
|
|
}
|
|
|
result = true;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogError("关闭PLC连接异常", ex);
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
}
|