采用网络对联方式交互数据
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

255 lines
9.0 KiB

using AksWebBrowser;
using AksWebBrowser.Common;
using AKSWebBrowser.Commen;
using CPF;
using CPF.Cef;
using CPF.Controls;
using CPF.Platform;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
namespace AksWebBrowser
{
public class FrmMain : Window
{
public int h = 1080;
public int w = 1920;
protected override void InitializeComponent()
{
//注册gb2312
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
LoadStyleFile("res://AksWebBrowser/StyleSheet.css");
//加载样式文件,文件需要设置为内嵌资源
Title = "控申业务专用浏览器";
CanResize = false;
ShowInTaskbar = true;
WindowState = WindowState.FullScreen;
//WindowState = WindowState.Maximized;
Children.Add(
new Border
{
Width = "100%",
Height = "100%",
Child = new Panel
{
Size = SizeField.Fill,
Children =
{
//Parame.videoV,
new CusWebBrowser
{
PresenterFor = this,
Name = nameof(Parame.webBrowser),
MarginTop=0,
MarginLeft=0,
MarginRight=0,
MarginBottom=0,
},
}
}
}
);
}
private TextBox textBox;
protected override async void OnInitialized()
{
LoadStyleFile("res://AksWebBrowser/StyleSheet.css");
//窗体大小
if (Parame.sfhp)
{
this.Height = h;
this.Width = w;
}
else
{
this.Height = w;
this.Width = h;
}
//SetTaskStatus.Hidetask();
base.OnInitialized();
//初始化网络连接(硬件服务响应)
MainModel.DeviceControllerInit();
Parame.webBrowser = FindPresenterByName<CusWebBrowser>(nameof(Parame.webBrowser));
textBox = FindPresenterByName<TextBox>(nameof(textBox));
Parame.webBrowser.CusRequest.CusResquestEvent += CusRequest_CusResquestEvent;
//浏览器大小
if (Parame.sfhp)
{
Parame.webBrowser.Width = w;
Parame.webBrowser.Height = h;
}
else
{
Parame.webBrowser.Width = h;
Parame.webBrowser.Height = w;
}
//Parame.webBrowser.Url = "http://143.169.62.3:8096/#/main-out";//天府新区
Parame.webBrowser.Url = "http://127.0.0.1:8096/#/main-out";
//Parame.webBrowser.Url = "http://192.168.0.34:8078/#/main-out";
//Parame.webBrowser.Url = Application.StartupPath + @"\html\index.html";
//开发者工具暂时只能支持Windows
//webBrowser.ShowDev();
//SetTaskStatus.Showtask();
Parame.webBrowser.LoadEnd += WebBrowser_LoadEnd;
this.Closing += MainWindow_Closing;
Parame.frm = this;
}
//关闭事件
private void MainWindow_Closing(object sender, ClosingEventArgs e)
{
MainModel.KillProcessByName("AksWebBrowser");
}
bool showDev = true;
private void WebBrowser_LoadEnd(object sender, LoadEndEventArgs e)
{
if (!showDev)
{
showDev = true;
Parame.webBrowser.ShowDev();
}
//WebSocketClientWithHeartbeat webSocket = new WebSocketClientWithHeartbeat();
//Parame.webSocket2 = webSocket;
//获取授权
GetSQObject();
////获取设备
//MainModel.GetSnIndex();
////打开高拍仪
//if (MainModel.OpenSn(Parame.gpydevIndex) == "200")
//{
// Parame.isGPY = true;
//}
////开启/关闭纠偏显示
//MainModel.EnableDeskImage(1);
////开启定时
//System.Timers.Timer timer = new System.Timers.Timer(1000);//3秒钟的时间间隔
//timer.Elapsed += OnTimedEvent;
//timer.AutoReset = true;//重复执行
//timer.Enabled = true;//启动定时器
}
//矫正授权
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
try
{
//检测服务是否启动
MainModel.StartShll();
}
catch { }
finally { }
}
public void Writelog(string str, string dirName = @"logs")
{
try
{
if (string.IsNullOrEmpty(str)) return;
var baseDir = AppDomain.CurrentDomain.BaseDirectory + dirName;
if (!Directory.Exists(baseDir))
{
Directory.CreateDirectory(baseDir);
}
string filePath = System.IO.Path.Combine(baseDir, DateTime.Now.ToString("yyyy-MM-dd") + "_log.txt");
using (StreamWriter sw = File.AppendText(filePath))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "[" + str + "]" + "\r\n");
sw.Flush();
}
}
catch { }
}
private void CusRequest_CusResquestEvent(CefPostData postData, CefRequest request)
{
string postParams = string.Empty;
if (postData != null)
{
//取提交的参数
CefPostData cefPostData = postData;
var elements = cefPostData.GetElements();
foreach (var element in elements)
{
if (element.ElementType == CefPostDataElementType.Bytes)
{
var bytes = element.GetBytes();
postParams = Encoding.UTF8.GetString(bytes);
//Console.WriteLine("请求参数:" + postParams);
}
}
}
//Console.WriteLine("请求地址:" + request.Url.ToString());
}
//调用JS内的JS方法
async void InvokeJS(CpfObject obj, RoutedEventArgs eventArgs)
{
var r = await Parame.webBrowser.ExecuteJavaScript("callback('调用绑定到JS里的C#方法')");
}
/// <summary>
/// 或者可以用授权接口
/// </summary>
public void GetSQObject()
{
Task.Run(async () =>
{
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, $"{Parame.apiUrl}/api/Interface/Getlist?Ytjbm={Parame.key}");
var response = await client.SendAsync(request);
List<Func> list = new List<Func>();
if (response.StatusCode.ToString() == "OK")
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
JObject jo = (JObject)JsonConvert.DeserializeObject(body);
if (jo["IsSucceed"].ToString() == "True")
{
string result = jo["result"].ToString();
if (!string.IsNullOrEmpty("result"))
{
JArray array = (JArray)JsonConvert.DeserializeObject(result);
foreach (JObject item in array)
{
Func func = new Func()
{
Id = item["Id"].ToString(),
Platform = item["Platform"].ToString(),
Interfaceaddress = item["Interfaceaddress"].ToString(),
};
list.Add(func);
}
}
else
{
//// Log.Info("未获取授权");
//Utils.MessagesBox("未获取授权");
}
}
else
{
//// Log.Info("未获取授权");
//Utils.MessagesBox("未获取授权");
}
}
Parame.FuncObject = list;
});
}
}
}