using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using ICSharpCode.Core; using Mesnac.Action.Base; using Mesnac.Action.Compressor.SubStation; using Mesnac.Basic; using Mesnac.Equips; using SocketProcess; namespace Mesnac.Action.Compressor.Sys { /// /// 定时器运行服务类 /// public class TimerRunService { #region 字段定义 private System.Timers.Timer _timer = null; private int _cnt = 0; //重连计数器 //private SocketClient socketClientTest = new SocketClient(); SubStationHelper subStationHelper = new SubStationHelper(); #endregion #region 单例实现 private static TimerRunService _instance = null; private TimerRunService() { } public static TimerRunService Instance { get { if (_instance == null) { _instance = new TimerRunService(); } return _instance; } } #endregion #region 启动定时器运行服务 /// /// 启动定时器运行服务 /// public void Start() { if (this._timer == null) { this._timer = new System.Timers.Timer(); this._timer.Interval = 1000 * 60; this._timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed); this._timer.Start(); ICSharpCode.Core.LoggingService.Debug("》》》定时器任务已启动!"); } } #endregion #region 停止定时器运行服务 /// /// 停止定时器运行服务 /// public void Stop() { if (this._timer != null) { this._timer.Stop(); this._timer.Dispose(); this._timer = null; ICSharpCode.Core.LoggingService.Debug("》》》定时器任务已停止!"); } } #endregion #region 定时器服务事件处理 /// /// 定时器服务事件处理 /// /// /// protected void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { lock (String.Empty) { //定时读取OEE任务 //ReadDeviceOeeData(); //ReadDevcieState(); } } #endregion #region 定时任务 #region 定时读取设备OEE参数并插库 BaseEquip _equip; public void ReadDeviceOeeData() { try { FindPLC("A1"); _equip.Read(_equip.Group["QtyCount"].Block, _equip.Group["QtyCount"].Start, _equip.Group["QtyCount"].Len, out object[] data); int QTYCount = Convert.ToInt16(data[2]); int NGCount = Convert.ToInt16(data[0]); if (QTYCount != 0) { //ICSharpCode.Core.LoggingService.Debug("合格数量:" + QTYCount + "NG数量:"+ NGCount); //插库 subStationHelper.InsertDeviceOeeInfo("A1", QTYCount+ NGCount, NGCount); } } catch (Exception ex) { } } private void ReadDevcieState() { try { FindPLC("A1"); _equip.Read(_equip.Group["B1"].Block, _equip.Group["B1"].Start, 1, out object[] data); ArrayList array = ConvertToBit(data, 0); string DeviceState = array[5].ToString(); //插库 subStationHelper.InsertDeviceStateInfo("A1", DeviceState,"A"); } catch (Exception ex) { } } public ArrayList ConvertToBit(object[] obj, int index) { ArrayList al = new ArrayList(); int IntValue = Convert.ToInt16(obj[index]); for (int i = 0; i < 16; i++) { int value = IntValue & 1; al.Add(value); IntValue = IntValue >> 1; } return al; } public void FindPLC(string plcname) { foreach (BaseEquip equip in Factory.Instance.AllEquips.Values) { if (equip.Name == plcname) { _equip = equip; break; } } } #endregion #region 定时检测Socket通断情况 public void SocketReConnect() { #region 实现重连方法 //if (_cnt >= 20) //{ // ICSharpCode.Core.LoggingService.Info("尝试重连失败,请检查服务器端!"); // _cnt = 0; // return; //} //if (!SocketClient.Instance.connected) //{ // SocketClient.Instance.ReConnect(); // _cnt++; // if (SocketClient.Instance.connected) // { // _cnt = 0; // } //} #endregion #region 检测Socket是否在侦听,如果没有侦听,则重启侦听服务(每5次循环,检测一次端口占用) //this._cnt++; //if (this._cnt > 10) //{ // if (!socketClientTest.connFlag) // { // socketClientTest.ReConnect(); // } // this._cnt = 0; //} #endregion } #endregion #region 定时检测接收信息的情况,处理并下传 public void ReceiveMsgProcess() { //判断 当接收到服务器端发送的有效信息时 //if (!string.IsNullOrEmpty(socketClientTest.receiveStr)) //{ // //处理下传 // bool downFlag = ChemicalWeighingPlc.PlcPlanHelper.ScanCodeDown(socketClientTest.receiveStr); // socketClientTest.receiveStr = null; //} } #endregion #endregion } }