using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Collections.Concurrent;
using System.ServiceModel;
using GZ.Modular.Log;
using GZ.Modular.Redis;
using MongoDB.Driver;
using Dapper;
namespace GZ.Projects.HnSx
{
///
/// 事件
///
public partial class ViewModel : System.ComponentModel.INotifyPropertyChanged
{
bool IsExitApp = false;
ConcurrentStack ExitJudgeStack = new ConcurrentStack();
System.Net.HttpListener HttpSvcHost = null;
#region View1事件
///
/// View1打开事件
///
///
///
public void View1_LoadEvent(object sender, RoutedEventArgs e)
{
try
{
//HttpSvcHost = new System.Net.HttpListener();
//HttpSvcHost.AuthenticationSchemes = System.Net.AuthenticationSchemes.Anonymous;
//HttpSvcHost.Prefixes.Add("http://127.0.0.1:8808/");
//HttpSvcHost.Start();
//HttpSvcHost.BeginGetContext(HttpSvcListenerCallback, null);
}
catch(Exception ex)
{
Conn.log默认日志.Error(ex.ToString());
}
View1_Init(sender, e);
try
{
#region [脚本][20250701110610426][业务逻辑.Program1]
AutoThread.InvokeMethod(AutoThread.Instance, "TaskEverythingRun");
#endregion [脚本][20250701110610426][业务逻辑.Program1]
}
catch(Exception ex)
{
Conn.log默认日志.Error(ex.ToString());
}
finally
{
}
///
/// 程序片段:Program2
/// 描述:
///
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
System.Threading.Thread.CurrentThread.IsBackground = true;
ExitJudgeStack.Push(0);
//一直执行,直到满足结束条件
while(true)
{
if (IsExitApp) { break; }
try
{
#region [脚本][20250704090126015][业务逻辑.Program2]
AutoThread.InvokeMethod(AutoThread.Instance, "ThreadSettingInit", new object[] { tag });
#endregion [脚本][20250704090126015][业务逻辑.Program2]
}
catch(Exception ex)
{
Conn.log默认日志.Error(ex.ToString());
}
finally
{
System.Threading.Thread.Sleep(10000);
}
}
ExitJudgeStack.TryPop(out int exitJudgeVal);
});
}
///
/// View1关闭事件
///
///
///
public void View1_CloseEvent(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
System.Threading.Tasks.Task.Factory.StartNew(() => { MessageBox.Show("程序正在关闭,请等待..."); });
}
catch(Exception ex)
{
Conn.log默认日志.Error(ex.ToString());
}
try
{
Conn.log默认日志.Debug("[关闭处理][Start]关闭Http服务");
if(HttpSvcHost!=null)
{
HttpSvcHost.Stop();
}
}
catch(Exception ex)
{
Conn.log默认日志.Error(ex.ToString());
}
finally
{
Conn.log默认日志.Debug("[关闭处理][End]关闭Http服务");
}
try
{
Conn.log默认日志.Debug("[关闭处理][Start]判断业务处理状态");
IsExitApp = true;
//int exitCnt = 0;
while
(
ExitJudgeStack.Count > 0
)
{
System.Threading.Thread.Sleep(100);
//++exitCnt;
//10秒未结束则强制关闭
//if (exitCnt >= 100)
//{
// break;
//}
}
}
catch (Exception ex)
{
Conn.log默认日志.Error(ex.ToString());
}
finally
{
Conn.log默认日志.Debug("[关闭处理][End]判断业务处理状态");
}
e.Cancel = true;
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
try
{
Conn.log默认日志.Debug("[关闭处理][Start]关闭PLC");
Device.PLC1?.Stop();
}
catch(Exception ex)
{
Conn.log默认日志.Error(ex.ToString());
}
finally
{
Conn.log默认日志.Debug("[关闭处理][End]关闭PLC");
}
try
{
Conn.log默认日志.Debug("[关闭处理][Start]判断PLC状态");
//int exitCnt = 0;
while
(
false
|| (Device.PLC1 == null ? false : Device.PLC1.State != GZ.Device.PLC.PlcRunState.Stoped)
)
{
System.Threading.Thread.Sleep(1000);
//++exitCnt;
//10秒未结束则强制关闭
//if (exitCnt >= 100)
//{
// break;
//}
}
}
catch (Exception ex)
{
Conn.log默认日志.Error(ex.ToString());
}
finally
{
Conn.log默认日志.Debug("[关闭处理][End]判断PLC状态");
}
Application.Current.Dispatcher.Invoke(() => { Environment.Exit(0); });
});
//System.Windows.Application.Current.Shutdown();
}
///
/// View1初始化
///
///
///
public void View1_Init(object sender, RoutedEventArgs e)
{
}
#endregion
#region 切换tab页
public void tab_SelectionChanged(object sender, EventArgs e)
{
//TabItem ti = (sender as TabControl).SelectedItem as TabItem;
//MessageBox.Show(ti.Name);
}
#endregion
#region Http服务监听
private void HttpSvcListenerCallback(IAsyncResult ar)
{
try
{
HttpSvcHost.BeginGetContext(HttpSvcListenerCallback, null);
System.Net.HttpListenerContext context = HttpSvcHost.EndGetContext(ar);
System.Net.HttpListenerRequest request = context.Request;
System.Net.HttpListenerResponse response = context.Response;
switch(request.LocalEndPoint.ToString())
{
case "127.0.0.1:8808":
{
using (var reader = new System.IO.StreamReader(request.InputStream, System.Text.Encoding.UTF8))
{
string requestJson = reader.ReadToEnd();
string respstr = HttpSvcListenerCallback_Api(request.HttpMethod,request.Url.AbsolutePath,requestJson,out System.Net.HttpStatusCode statusCode);
string logContent="";
logContent += $"\r\n[{request.HttpMethod}]{request.Url.AbsolutePath}";
logContent += $"\r\n[request]{requestJson}";
logContent += $"\r\n[response]{respstr}";
Conn.log默认日志?.Info(logContent);
byte[] bytstr = Encoding.UTF8.GetBytes(respstr);
response.StatusCode = (int)statusCode;
response.SendChunked = false;
response.ContentLength64 = bytstr.Length;
response.OutputStream.Write(bytstr, 0, bytstr.Length);
response.Close();
}
break;
}
}
}
catch(Exception ex)
{
Conn.log默认日志.Error(ex.ToString());
}
}
private System.String HttpSvcListenerCallback_Api(System.String method,System.String path,System.String requestJson,out System.Net.HttpStatusCode statusCode)
{
try
{
switch(method)
{
case "POST":
{
switch(path)
{
case "/Api/sendTask":
{
statusCode = System.Net.HttpStatusCode.OK;
return "";
}
case "/Api/cancelTask":
{
statusCode = System.Net.HttpStatusCode.OK;
return "";
}
}
break;
}
}
statusCode = System.Net.HttpStatusCode.NotFound;
return "";
}
catch(Exception ex)
{
Conn.log默认日志.Error(ex.ToString());
statusCode = System.Net.HttpStatusCode.InternalServerError;
return "";
}
}
#endregion
}
}