|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using ICSharpCode.Core;
|
|
|
|
|
using Mesnac.Action.Base;
|
|
|
|
|
using Mesnac.Basic;
|
|
|
|
|
|
|
|
|
|
namespace Mesnac.Action.ChemicalWeighing.Sys
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 定时器运行服务类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class TimerRunService
|
|
|
|
|
{
|
|
|
|
|
#region 字段定义
|
|
|
|
|
|
|
|
|
|
private System.Timers.Timer _timer = null;
|
|
|
|
|
private int _cnt = 0; //重连计数器
|
|
|
|
|
private SocketClient socketClientTest = new SocketClient();
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 单例实现
|
|
|
|
|
|
|
|
|
|
private static TimerRunService _instance = null;
|
|
|
|
|
|
|
|
|
|
private TimerRunService() { }
|
|
|
|
|
|
|
|
|
|
public static TimerRunService Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
|
|
|
|
_instance = new TimerRunService();
|
|
|
|
|
}
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 启动定时器运行服务
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 启动定时器运行服务
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
if (this._timer == null)
|
|
|
|
|
{
|
|
|
|
|
this._timer = new System.Timers.Timer();
|
|
|
|
|
this._timer.Interval = 1000;
|
|
|
|
|
this._timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
|
|
|
|
|
this._timer.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 停止定时器运行服务
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 停止定时器运行服务
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
if (this._timer != null)
|
|
|
|
|
{
|
|
|
|
|
this._timer.Stop();
|
|
|
|
|
this._timer.Dispose();
|
|
|
|
|
this._timer = null;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 定时器服务事件处理
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 定时器服务事件处理
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
protected void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
lock (String.Empty)
|
|
|
|
|
{
|
|
|
|
|
this.SocketReConnect();
|
|
|
|
|
this.ReceiveMsgProcess();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 定时任务
|
|
|
|
|
|
|
|
|
|
#region 定时检测Socket通断情况
|
|
|
|
|
|
|
|
|
|
public void SocketReConnect()
|
|
|
|
|
{
|
|
|
|
|
#region 实现重连方法
|
|
|
|
|
|
|
|
|
|
//if (_cnt >= 20)
|
|
|
|
|
//{
|
|
|
|
|
// ICSharpCode.Core.LoggingService<TimerRunService>.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
|
|
|
|
|
}
|
|
|
|
|
}
|