using System; using System.Collections.Generic; using System.Windows.Forms; using ProjectBase.Data.BaseDAL; using ProjectBase.Data.Redis; using SIMDP.BLL; using SIMDP.Model; using SIMDP.Util; namespace SIMDP.OPC.DAClient { public class OpcHelper { public static OpcHelper Instance { get; private set; } static OpcHelper() { Instance = new OpcHelper(); } private Timer timer1 = new Timer(); private int PLC_COUNT = 0; private OpcAutoDA[] opc = null; private List moList = new List(); static RedisHelper redis = null; public void Opc_Init() { Opc_DisConn(); if (redis == null) redis = new RedisHelper(); moList = BLLFactory.Instance.GetAll(); PLC_COUNT = moList.Count; if (PLC_COUNT <= 0) return; opc = new OpcAutoDA[PLC_COUNT]; timer1.Interval = 1000; timer1.Tick += new System.EventHandler(timer1_Tick); for (int i = 0; i < PLC_COUNT; i++) { List opcPoints = BLLFactory.Instance.GetDataPointsByPlcID(moList[i].PlcId); if (opcPoints.Count == 0) continue; opc[i] = new OpcAutoDA(moList[i].LinkConfig, opcPoints); } timer1.Start(); } public void Opc_DisConn() { timer1.Stop(); for (int i = 0; i < PLC_COUNT; i++) { opc[i].Disconnect(); opc[i] = null; redis.SetString(moList[i].PlcId.ToString() + moList[i].PlcName, "3"); redis.Publish(SysEnvironment.PlcStatusChannel, "3"); } PLC_COUNT = 0; opc = null; } private void timer1_Tick(object sender, EventArgs e) { bool bOpcConnected; for (int i = 0; i < PLC_COUNT; i++) { if (opc[i] == null) continue; if (opc[i].isConnected()) { redis.SetString(moList[i].PlcId.ToString() + moList[i].PlcName, "2"); redis.Publish(SysEnvironment.PlcStatusChannel, "2"); timer1.Interval = 30000; } else { redis.SetString(moList[i].PlcId.ToString() + moList[i].PlcName, "3"); redis.Publish(SysEnvironment.PlcStatusChannel, "3"); timer1.Interval = 1000; bOpcConnected = opc[i].Connect(); if (bOpcConnected) bOpcConnected = opc[i].CreateGroup(); } } } } }