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.
Aucma.Scada/HighWayIot.Plc/PlcPool.cs

241 lines
7.1 KiB
C#

5 months ago
using HighWayIot.Config;
using HighWayIot.Log4net;
11 months ago
using HighWayIot.Plc.Impl;
using System;
using System.Collections.Generic;
5 months ago
using System.Threading.Tasks;
using System.Timers;
11 months ago
namespace HighWayIot.Plc
{
public sealed class PlcPool
{
private static readonly Lazy<PlcPool> lazy = new Lazy<PlcPool>(() => new PlcPool());
5 months ago
private AppConfig appConfig = AppConfig.Instance;
private PlcConfig plcConfig = PlcConfig.Instance;
11 months ago
public static PlcPool Instance
{
get
{
return lazy.Value;
}
}
private LogHelper logHelper = LogHelper.Instance;
5 months ago
public Dictionary<string, IPlc> keyValuePairs = new Dictionary<string, IPlc>();
11 months ago
private PlcPool() { }
/// <summary>
/// 初始化Plc
/// </summary>
/// <param name="plcType"></param>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <param name="key"></param>
public void InitPlc(string plcType, string ip, int port, string key)
11 months ago
{
5 months ago
if (!keyValuePairs.ContainsKey(key))
11 months ago
{
5 months ago
IPlc _plc = null;
switch (plcType)
{
case "InovancePlc":
_plc = new InovancePlc();
break;
case "MelsecBinaryPlc":
_plc = new MelsecBinaryPlc();
break;
case "OmronNJPlc":
_plc = new OmronNJPlc();
break;
case "SiemensPlc":
_plc = new SiemensPlc();
break;
default:
break;
}
var connectResult = _plc.Connect(ip, port);
if (connectResult)
{
keyValuePairs.Add(key, _plc);
}
11 months ago
}
5 months ago
}
#region 心跳
public async Task StartMelsecPlcAsync()
{
System.Timers.Timer timer = new System.Timers.Timer(3000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(PlcHeartTask); //到达时间的时候执行事件;
timer.AutoReset = true;//设置是执行一次false还是一直执行(true)
timer.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它
timer.Start();//timer.Start()的内部原理还是设置timer.Enabled = true;
await Task.CompletedTask;
}
private async void PlcHeartTask(object sender, ElapsedEventArgs e)
{
ShellHeartTask();
LinerHeartTask();
}
private async void ShellHeartTask()
{
await Task.Run(() =>
11 months ago
{
5 months ago
keyValuePairs.TryGetValue(appConfig.shellStoreCode, out IPlc shellplc);
if (shellplc == null)
{
this.InitPlc("MelsecBinaryPlc", plcConfig.shell_Plc_Ip, plcConfig.shell_Plc_Port, appConfig.shellStoreCode);
}
else
{
var result = shellplc.ReadHeart("M100").Result;
if (result)
{
logHelper.Info("PLC连接成功");
}
else
{
logHelper.Info("PLC连接失败");
keyValuePairs.Remove(appConfig.shellStoreCode);
}
}
});
}
private async void LinerHeartTask()
{
await Task.Run(() =>
{
keyValuePairs.TryGetValue(appConfig.linerStoreCode, out IPlc linerplc);
if (linerplc == null)
{
this.InitPlc("MelsecBinaryPlc", plcConfig.liner_Plc_Ip, plcConfig.liner_Plc_Port, appConfig.linerStoreCode);
}
else
{
var result = linerplc.ReadHeart("M100").Result;
if (result)
{
logHelper.Info("PLC连接成功");
}
else
{
logHelper.Info("PLC连接失败");
keyValuePairs.Remove(appConfig.linerStoreCode);
}
}
});
11 months ago
}
5 months ago
//private async void ExecMelsecMcHeartTask(object sender, ElapsedEventArgs e)
//{
// keyValuePairs.TryGetValue(appConfig.shellStoreCode, out IPlc shellplc);
// keyValuePairs.TryGetValue(appConfig.linerStoreCode, out IPlc linerplc);
// if (shellplc == null)
// {
// this.InitPlc("MelsecBinaryPlc", plcConfig.shell_Plc_Ip, plcConfig.shell_Plc_Port, appConfig.shellStoreCode);
// }
// else
// {
// var result = shellplc.ReadHeart("M100").Result;
// if (result)
// {
// logHelper.Info("PLC连接成功");
// }
// else
// {
// logHelper.Info("PLC连接失败");
// keyValuePairs.Remove(appConfig.shellStoreCode);
// }
// }
// if (linerplc == null)
// {
// this.InitPlc("MelsecBinaryPlc", plcConfig.liner_Plc_Ip, plcConfig.shell_Plc_Port, appConfig.shellStoreCode);
// }
// else
// {
// var result = linerplc.ReadHeart("M100").Result;
// if (result)
// {
// logHelper.Info("PLC连接成功");
// }
// else
// {
// logHelper.Info("PLC连接失败");
// keyValuePairs.Remove(appConfig.linerStoreCode);
// }
// }
//}
#endregion
11 months ago
/// <summary>
/// 获取PLC
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public IPlc GetPlcByKey(string key)
{
return keyValuePairs[key];
}
/// <summary>
/// 获取所有PLC信息
/// </summary>
/// <returns></returns>
public Dictionary<string, IPlc> GetAll()
11 months ago
{
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)
11 months ago
{
logHelper.Error("关闭PLC连接异常", ex);
}
return result;
}
}
}