OpcHelper.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using ProjectBase.Data.BaseDAL;
  5. using ProjectBase.Data.Redis;
  6. using SIMDP.BLL;
  7. using SIMDP.Model;
  8. using SIMDP.Util;
  9. namespace SIMDP.OPC.DAClient
  10. {
  11. public class OpcHelper
  12. {
  13. public static OpcHelper Instance { get; private set; }
  14. static OpcHelper()
  15. {
  16. Instance = new OpcHelper();
  17. }
  18. private Timer timer1 = new Timer();
  19. private int PLC_COUNT = 0;
  20. private OpcAutoDA[] opc = null;
  21. private List<MoPlcInfo> moList = new List<MoPlcInfo>();
  22. static RedisHelper redis = null;
  23. public void Opc_Init()
  24. {
  25. Opc_DisConn();
  26. if (redis == null)
  27. redis = new RedisHelper();
  28. moList = BLLFactory<BlPlcInfo>.Instance.GetAll();
  29. PLC_COUNT = moList.Count;
  30. if (PLC_COUNT <= 0)
  31. return;
  32. opc = new OpcAutoDA[PLC_COUNT];
  33. timer1.Interval = 1000;
  34. timer1.Tick += new System.EventHandler(timer1_Tick);
  35. for (int i = 0; i < PLC_COUNT; i++)
  36. {
  37. List<MoDataPoint> opcPoints = BLLFactory<BlDataPoint>.Instance.GetDataPointsByPlcID(moList[i].PlcId);
  38. if (opcPoints.Count == 0) continue;
  39. opc[i] = new OpcAutoDA(moList[i].LinkConfig, opcPoints);
  40. }
  41. timer1.Start();
  42. }
  43. public void Opc_DisConn()
  44. {
  45. timer1.Stop();
  46. for (int i = 0; i < PLC_COUNT; i++)
  47. {
  48. opc[i].Disconnect();
  49. opc[i] = null;
  50. redis.SetString(moList[i].PlcId.ToString() + moList[i].PlcName, "3");
  51. redis.Publish(SysEnvironment.PlcStatusChannel, "3");
  52. }
  53. PLC_COUNT = 0;
  54. opc = null;
  55. }
  56. private void timer1_Tick(object sender, EventArgs e)
  57. {
  58. bool bOpcConnected;
  59. for (int i = 0; i < PLC_COUNT; i++)
  60. {
  61. if (opc[i] == null) continue;
  62. if (opc[i].isConnected())
  63. {
  64. redis.SetString(moList[i].PlcId.ToString() + moList[i].PlcName, "2");
  65. redis.Publish(SysEnvironment.PlcStatusChannel, "2");
  66. timer1.Interval = 30000;
  67. }
  68. else
  69. {
  70. redis.SetString(moList[i].PlcId.ToString() + moList[i].PlcName, "3");
  71. redis.Publish(SysEnvironment.PlcStatusChannel, "3");
  72. timer1.Interval = 1000;
  73. bOpcConnected = opc[i].Connect();
  74. if (bOpcConnected)
  75. bOpcConnected = opc[i].CreateGroup();
  76. }
  77. }
  78. }
  79. }
  80. }