265 lines
12 KiB
C#
265 lines
12 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Globalization;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Net.Sockets;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Data;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using System.Windows.Media.Imaging;
|
||
using System.Windows.Navigation;
|
||
using System.Windows.Shapes;
|
||
using System.Windows.Threading;
|
||
using ESSupport.Lib;
|
||
using ESSupport.Pos;
|
||
|
||
namespace ConnectPoint
|
||
{
|
||
/// <summary>
|
||
/// MainWindow.xaml 的交互逻辑
|
||
/// </summary>
|
||
public partial class MainWindow : Window
|
||
{
|
||
/// <summary>
|
||
/// 状态反馈主定时器
|
||
/// </summary>
|
||
readonly DispatcherTimer WorkerTimer;
|
||
/// <summary>
|
||
/// 数据传输接口地址
|
||
/// </summary>
|
||
string WebServiceUrl { get; set; }
|
||
/// <summary>
|
||
/// 主支付通道接口地址
|
||
/// </summary>
|
||
string PayServiceUrl { get; set; }
|
||
|
||
public MainWindow()
|
||
{
|
||
InitializeComponent();
|
||
WorkerTimer = new DispatcherTimer
|
||
{
|
||
Interval = TimeSpan.FromSeconds(60)
|
||
};
|
||
WorkerTimer.Tick += WorkerTimer_Tick;
|
||
WorkerTimer.Start();
|
||
}
|
||
/// <summary>
|
||
/// 主程序启动加载事件
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||
{
|
||
#region 程序启动时,进行过期日志、备份文件清理操作
|
||
new Thread(() =>
|
||
{
|
||
int _FileCount = 0;
|
||
//清理程序根目录下过期日志文件
|
||
try { FileHelper.DeleteFiles(AppDomain.CurrentDomain.BaseDirectory, new string[] { ".log" }, false, false, 10, ref _FileCount); } catch { }
|
||
//清理错误日志文件夹中过期日志文件
|
||
try { FileHelper.DeleteFiles(AppDomain.CurrentDomain.BaseDirectory + "\\log", new string[] { ".log" }, false, false, 10, ref _FileCount); } catch { }
|
||
//清理流水日志文件
|
||
try { FileHelper.DeleteFiles(AppDomain.CurrentDomain.BaseDirectory + "\\selldata", new string[] { ".log" }, false, false, 60, ref _FileCount); } catch { }
|
||
//清理过期备份数据文件
|
||
try { DataBaseUpdate.ClearDataBackup(AppDomain.CurrentDomain.BaseDirectory + "\\DataBackup", 60, true); } catch { }
|
||
})
|
||
{ IsBackground = true }.Start();
|
||
#endregion
|
||
this.Hide();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 主定时器运行事件
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void WorkerTimer_Tick(object sender, EventArgs e)
|
||
{
|
||
//初始化数据传输接口地址
|
||
if (string.IsNullOrWhiteSpace(WebServiceUrl))
|
||
{
|
||
string _ServiceUrl = $"http://{PosConfigInit.ConfigurationValues("server_ip", string.Empty)}:" +
|
||
$"{PosConfigInit.ConfigurationValues("DataServicePort", "7080")}/DataServices/Service.asmx";
|
||
|
||
//检查主数据传输接口是否可以正常访问,不能正常访问的话,使用备用接口
|
||
if (HttpHelper.UrlIsExist(_ServiceUrl))
|
||
{
|
||
WebServiceUrl = _ServiceUrl;
|
||
}
|
||
else
|
||
{
|
||
WebServiceUrl = $"http://{PosConfigInit.ConfigurationValues("server_ip", string.Empty)}:" +
|
||
$"{PosConfigInit.ConfigurationValues("service_port", "7080")}/Service.asmx";
|
||
}
|
||
}
|
||
//初始化移动支付交易检测接口地址
|
||
if (string.IsNullOrWhiteSpace(PayServiceUrl))
|
||
{
|
||
PayServiceUrl = $"http://{PosConfigInit.ConfigurationValues("server_ip", string.Empty)}:" +
|
||
$"{PosConfigInit.ConfigurationValues("service_port", "7080")}/Service.asmx";
|
||
}
|
||
//设置十分钟统计启动时间
|
||
if (Common.PosControl.DataCollectionTime == DateTime.MinValue)
|
||
{
|
||
Common.PosControl.DataCollectionTime = DateTime.Now.AddMilliseconds(-Environment.TickCount);
|
||
}
|
||
//收银机指令执行线程
|
||
if (Common.PosControl.CommodityMachineThread == null ||
|
||
!Common.PosControl.CommodityMachineThread.IsAlive)
|
||
{
|
||
Common.PosControl.CommodityMachineThread = new Thread(() =>
|
||
{
|
||
ThreadHelper.CommodityMachine(WebServiceUrl);
|
||
ThreadHelper.InstructionExecute(WebServiceUrl);
|
||
ThreadHelper.License();
|
||
})
|
||
{
|
||
IsBackground = true
|
||
};
|
||
Common.PosControl.CommodityMachineThread.Start();
|
||
}
|
||
//启动运行日志记录线程
|
||
if (Common.PosControl.RuningLogThread == null ||
|
||
!Common.PosControl.RuningLogThread.IsAlive)
|
||
{
|
||
Common.PosControl.RuningLogThread = new Thread(() =>
|
||
{
|
||
if (ThreadHelper.RuningLog(Common.PosControl.RuningTime))
|
||
{
|
||
Common.PosControl.RuningTime = DateTime.Now;
|
||
}
|
||
})
|
||
{
|
||
IsBackground = true
|
||
};
|
||
Common.PosControl.RuningLogThread.Start();
|
||
}
|
||
//定时启动实时交易统计线程
|
||
if (!Common.PosControl.DataCollectionRun &&
|
||
(DateTime.Now - Common.PosControl.DataCollectionTime).Minutes % 10 == 0)
|
||
{
|
||
if (Common.PosControl.DataCollectionThread == null ||
|
||
!Common.PosControl.DataCollectionThread.IsAlive)
|
||
{
|
||
Common.PosControl.DataCollectionThread = new Thread(() =>
|
||
{
|
||
Common.PosControl.DataCollectionRun = true;
|
||
ThreadHelper.DataCollectionUpload(WebServiceUrl);
|
||
ThreadHelper.NewSystemConfig();
|
||
})
|
||
{
|
||
IsBackground = true
|
||
};
|
||
Common.PosControl.DataCollectionThread.Start();
|
||
//Common.PosControl.DataCollectionTime = DateTime.Now.AddMinutes(10);
|
||
}
|
||
}
|
||
if (Common.PosControl.DataCollectionRun &&
|
||
(DateTime.Now - Common.PosControl.DataCollectionTime).Minutes % 10 != 0)
|
||
{
|
||
Common.PosControl.DataCollectionRun = false;
|
||
}
|
||
|
||
//定时启动移动支付交易校验、配置下发线程
|
||
if (DateTime.Now > Common.PosControl.MobilePayCheckTime)
|
||
{
|
||
if (Common.PosControl.MobilePayCheckThread == null ||
|
||
!Common.PosControl.MobilePayCheckThread.IsAlive)
|
||
{
|
||
Common.PosControl.MobilePayCheckThread = new Thread(() =>
|
||
{
|
||
ThreadHelper.MobilePayCheck(PayServiceUrl, DateTime.Today.AddDays(-3));
|
||
ThreadHelper.MobilePayConfigUpdate(PosConfigInit.ConfigurationValues("server_ip", string.Empty));
|
||
})
|
||
{
|
||
IsBackground = true
|
||
};
|
||
Common.PosControl.MobilePayCheckThread.Start();
|
||
Common.PosControl.MobilePayCheckTime = DateTime.Now.AddMinutes(10);
|
||
}
|
||
}
|
||
|
||
//定时启动状态反馈线程
|
||
if (DateTime.Now > Common.PosControl.StateFeedBackTime)
|
||
{
|
||
if (Common.PosControl.StateFeedbackThread == null ||
|
||
!Common.PosControl.StateFeedbackThread.IsAlive)
|
||
{
|
||
Common.PosControl.StateFeedbackThread = new Thread(() =>
|
||
{
|
||
ThreadHelper.StateFeedbackUpload(WebServiceUrl);
|
||
})
|
||
{
|
||
IsBackground = true
|
||
};
|
||
Common.PosControl.StateFeedbackThread.Start();
|
||
Common.PosControl.StateFeedBackTime = DateTime.Now.AddMinutes(1);
|
||
}
|
||
}
|
||
|
||
//启动基础数据版本上报线程
|
||
if (DateTime.Now > Common.PosControl.BaseInfoFeedbackTime)
|
||
{
|
||
if (Common.PosControl.BaseInfoFeedbackThread == null ||
|
||
!Common.PosControl.BaseInfoFeedbackThread.IsAlive)
|
||
{
|
||
Common.PosControl.BaseInfoFeedbackThread = new Thread(() =>
|
||
{
|
||
//基础数据版本接口上报
|
||
ThreadHelper.BaseInfoFeedbackUpload(WebServiceUrl);
|
||
//收银机器信息接口上报
|
||
ThreadHelper.MachineInfoFeedbackUpload(WebServiceUrl);
|
||
try
|
||
{
|
||
//读取后台配置的更新服务地址
|
||
string _UpdateIP = PosConfigInit.ConfigurationValues("UpdateIP", string.Empty);
|
||
if (string.IsNullOrWhiteSpace(_UpdateIP))
|
||
{
|
||
//更新服务地址未配置时,默认同收银服务器地址
|
||
_UpdateIP = PosConfigInit.ConfigurationValues("server_ip", string.Empty);
|
||
}
|
||
//读取后台配置的更新服务端口,默认:11000
|
||
string _UpdatePort = PosConfigInit.ConfigurationValues("UpdatePort", "11000");
|
||
if (!string.IsNullOrWhiteSpace(_UpdateIP) && !string.IsNullOrWhiteSpace(_UpdatePort))
|
||
{
|
||
//检测更新站点是否存在,存在则更新配置文件
|
||
if (HttpHelper.UrlIsExist($"http://{_UpdateIP}:{_UpdatePort}/Update.asmx"))
|
||
{
|
||
//修改更新服务IP地址
|
||
if (ConfigHelper.GetAppConfig(AppDomain.CurrentDomain.BaseDirectory + "\\update.xml", "IP") != _UpdateIP)
|
||
{
|
||
ConfigHelper.UpdateAppConfig(AppDomain.CurrentDomain.BaseDirectory + "\\update.xml", "IP", _UpdateIP);
|
||
}
|
||
//修改更新服务端口
|
||
if (ConfigHelper.GetAppConfig(AppDomain.CurrentDomain.BaseDirectory + "\\update.xml", "Port") != _UpdatePort)
|
||
{
|
||
ConfigHelper.UpdateAppConfig(AppDomain.CurrentDomain.BaseDirectory + "\\update.xml", "Port", _UpdatePort);
|
||
}
|
||
}
|
||
}
|
||
//更新配置文件不存在端口号配置项时,写入默认端口
|
||
if (string.IsNullOrWhiteSpace(ConfigHelper.GetAppConfig(AppDomain.CurrentDomain.BaseDirectory + "\\update.xml", "Port")))
|
||
{
|
||
ConfigHelper.UpdateAppConfig(AppDomain.CurrentDomain.BaseDirectory + "\\update.xml", "Port", "11000");
|
||
}
|
||
}
|
||
catch { }
|
||
})
|
||
{
|
||
IsBackground = true
|
||
};
|
||
Common.PosControl.BaseInfoFeedbackThread.Start();
|
||
Common.PosControl.BaseInfoFeedbackTime = DateTime.Now.AddMinutes(30);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|