AbstractBaseDevice.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using ProjectBase.Data.Redis;
  2. using SIMDP.Model;
  3. using ProjectBase.Util;
  4. using System.Collections.Generic;
  5. using System.Timers;
  6. namespace SIMDP.Device
  7. {
  8. public abstract class AbstractBaseDevice
  9. {
  10. public AbstractBaseDevice()
  11. {
  12. timer.Elapsed += timer_Elapsed;
  13. }
  14. #region 数据定义
  15. /// <summary>
  16. /// Redis
  17. /// </summary>
  18. protected RedisHelper redis = new RedisHelper(0);
  19. /// <summary>
  20. /// 轮询定时器
  21. /// </summary>
  22. protected Timer timer = new Timer() { Enabled = false, AutoReset = false };
  23. /// <summary>
  24. /// 定时器停止标志
  25. /// </summary>
  26. protected bool timerStopped = false;
  27. /// <summary>
  28. ///PLC配置信息
  29. /// </summary>
  30. protected MoPlcInfo plcInfo;
  31. #endregion
  32. #region 接口函数
  33. /// <summary>
  34. /// 设备启动
  35. /// </summary>
  36. public abstract void Start(MoPlcInfo _plcInfo, List<MoDataPoint> _dataPoints);
  37. /// <summary>
  38. /// 设备停止
  39. /// </summary>
  40. public virtual void Stop()
  41. {
  42. actionStop();
  43. SetRedisPlcDisConnect();
  44. timerStopped = true;
  45. }
  46. /// <summary>
  47. /// 设备连接状态
  48. /// </summary>
  49. public abstract bool isConnected();
  50. /// <summary>
  51. /// 设备连接
  52. /// </summary>
  53. /// <returns></returns>
  54. protected abstract bool Connect();
  55. /// <summary>
  56. /// 设备断开
  57. /// </summary>
  58. protected abstract void Disconnect();
  59. /// <summary>
  60. /// 定时器逻辑
  61. /// </summary>
  62. /// <param name="sender"></param>
  63. /// <param name="e"></param>
  64. public virtual void timer_Elapsed(object sender, ElapsedEventArgs e)
  65. {
  66. if (!isConnected()) // 判断连接状态
  67. {
  68. try
  69. {
  70. Connect();
  71. }
  72. catch
  73. { }
  74. }
  75. else // PLC已连接,执行自定义函数
  76. {
  77. actionTimer();
  78. }
  79. if (!timerStopped)
  80. {
  81. timer.Start();
  82. }
  83. else
  84. {
  85. Disconnect();
  86. }
  87. }
  88. #endregion
  89. #region 自定义函数
  90. /// <summary>
  91. /// 启动时执行的自定义函数,可重写
  92. /// </summary>
  93. public virtual void actionStart() { }
  94. /// <summary>
  95. /// 停止时执行的自定义函数,可重写
  96. /// </summary>
  97. public virtual void actionStop() { }
  98. /// <summary>
  99. /// 连接成功时执行的自定义函数,可重写
  100. /// </summary>
  101. public virtual void actionConnected() { }
  102. /// <summary>
  103. /// 连接成功后每次定时器执行的函数,可重写
  104. /// </summary>
  105. public virtual void actionTimer() { }
  106. #endregion
  107. #region Redis
  108. protected void SetRedisPlcConnected()
  109. {
  110. redis.SetString(SysEnvironment.PlcStatusPrefix + ":" + plcInfo.PlcId.ToString() + plcInfo.PlcName, "2");
  111. redis.Publish(SysEnvironment.PlcStatusChannel, "2");
  112. }
  113. protected void SetRedisPlcDisConnect()
  114. {
  115. if (timerStopped)
  116. return; //设备断开时,已在Stop()中更新连接状态,避免重连后状态覆盖
  117. redis.SetString(SysEnvironment.PlcStatusPrefix + ":" + plcInfo.PlcId.ToString() + plcInfo.PlcName, "3");
  118. redis.Publish(SysEnvironment.PlcStatusChannel, "3");
  119. }
  120. protected void UpdataRedisDataPoint(MoDataPoint mo, string value, bool refresh = false)
  121. {
  122. string key = SysEnvironment.PlcPointsPrefix + ":" + plcInfo.PlcName + ":" + mo.DataPointId.ToString() + mo.DataPointName;
  123. redis.SetString(key, value);
  124. if (refresh)
  125. redis.Publish<string>(SysEnvironment.OpcDataChannel, value);
  126. }
  127. #endregion
  128. }
  129. }