123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using ProjectBase.Data.Redis;
- using SIMDP.Model;
- using ProjectBase.Util;
- using System.Collections.Generic;
- using System.Timers;
- namespace SIMDP.Device
- {
- public abstract class AbstractBaseDevice
- {
- public AbstractBaseDevice()
- {
- timer.Elapsed += timer_Elapsed;
- }
- #region 数据定义
- /// <summary>
- /// Redis
- /// </summary>
- protected RedisHelper redis = new RedisHelper(0);
- /// <summary>
- /// 轮询定时器
- /// </summary>
- protected Timer timer = new Timer() { Enabled = false, AutoReset = false };
- /// <summary>
- /// 定时器停止标志
- /// </summary>
- protected bool timerStopped = false;
- /// <summary>
- ///PLC配置信息
- /// </summary>
- protected MoPlcInfo plcInfo;
- #endregion
- #region 接口函数
- /// <summary>
- /// 设备启动
- /// </summary>
- public abstract void Start(MoPlcInfo _plcInfo, List<MoDataPoint> _dataPoints);
- /// <summary>
- /// 设备停止
- /// </summary>
- public virtual void Stop()
- {
- actionStop();
- SetRedisPlcDisConnect();
- timerStopped = true;
- }
- /// <summary>
- /// 设备连接状态
- /// </summary>
- public abstract bool isConnected();
- /// <summary>
- /// 设备连接
- /// </summary>
- /// <returns></returns>
- protected abstract bool Connect();
- /// <summary>
- /// 设备断开
- /// </summary>
- protected abstract void Disconnect();
- /// <summary>
- /// 定时器逻辑
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public virtual void timer_Elapsed(object sender, ElapsedEventArgs e)
- {
- if (!isConnected()) // 判断连接状态
- {
- try
- {
- Connect();
- }
- catch
- { }
- }
- else // PLC已连接,执行自定义函数
- {
- actionTimer();
- }
- if (!timerStopped)
- {
- timer.Start();
- }
- else
- {
- Disconnect();
- }
- }
- #endregion
- #region 自定义函数
- /// <summary>
- /// 启动时执行的自定义函数,可重写
- /// </summary>
- public virtual void actionStart() { }
- /// <summary>
- /// 停止时执行的自定义函数,可重写
- /// </summary>
- public virtual void actionStop() { }
- /// <summary>
- /// 连接成功时执行的自定义函数,可重写
- /// </summary>
- public virtual void actionConnected() { }
- /// <summary>
- /// 连接成功后每次定时器执行的函数,可重写
- /// </summary>
- public virtual void actionTimer() { }
- #endregion
- #region Redis
- protected void SetRedisPlcConnected()
- {
- redis.SetString(SysEnvironment.PlcStatusPrefix + ":" + plcInfo.PlcId.ToString() + plcInfo.PlcName, "2");
- redis.Publish(SysEnvironment.PlcStatusChannel, "2");
- }
- protected void SetRedisPlcDisConnect()
- {
- if (timerStopped)
- return; //设备断开时,已在Stop()中更新连接状态,避免重连后状态覆盖
- redis.SetString(SysEnvironment.PlcStatusPrefix + ":" + plcInfo.PlcId.ToString() + plcInfo.PlcName, "3");
- redis.Publish(SysEnvironment.PlcStatusChannel, "3");
- }
- protected void UpdataRedisDataPoint(MoDataPoint mo, string value, bool refresh = false)
- {
- string key = SysEnvironment.PlcPointsPrefix + ":" + plcInfo.PlcName + ":" + mo.DataPointId.ToString() + mo.DataPointName;
- redis.SetString(key, value);
- if (refresh)
- redis.Publish<string>(SysEnvironment.OpcDataChannel, value);
- }
- #endregion
- }
- }
|