using IOTContainer.Common; using IOTContainer.Model; using Microsoft.Web.WebView2.Core; using Newtonsoft.Json; using SpeechLib; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; using static IOTContainer.Common.WinApi; namespace IOTContainer.View { /// /// EdgeWindow.xaml 的交互逻辑 /// public partial class EdgeWindow : Window { private int _maxTryCount = 5; LASTINPUTINFO _mLastInputInfo; private DispatcherTimer _timer = null; //倒计时 public DispatcherTimer CountdownTimer; public EdgeWindow() { try { InitializeComponent(); if (!HasInstallWebview2()) { this.Close(); return; } this.Top = 0; this.Left = 0; this.WindowState = WindowState.Maximized; this.Height = SystemParameters.PrimaryScreenHeight; this.Width = SystemParameters.PrimaryScreenWidth; ComParameters.Parameters.EdgeWindow = this; #region 定时器 //_mLastInputInfo =new LASTINPUTINFO(); //_mLastInputInfo.cbSize = Marshal.SizeOf(_mLastInputInfo); //InitTimer(); #endregion //CheckAndPlay(); CreateWebView(); Exit exit = new Exit(); exit.Show(); WinApi.FindSetTop(null, "Exit"); } catch (Exception ex) { Log.MyLog.WriteLogFile($"Edge初始化出错:{ex}", "EdgeError"); } } #region 事件 private async void Window_Closed(object sender, EventArgs e) { ComParameters.Parameters.EdgeWindow = null; Exit exit = new Exit(); exit.Close(); if (null != webView && null != webView.CoreWebView2) { webView.CoreWebView2.Stop(); await webView.CoreWebView2.Profile.ClearBrowsingDataAsync(); //webView.Dispose(); } } #endregion #region 逻辑方法 /// /// 有节目才出信发 没节目则一直在导视 /// public void CheckAndPlay() { if (!ComParameters.Parameters.IsPlay) { try { List result = HttpComm.Http.GetResource(); if (result == null || result.Count == 0) { //如果没有节目,则继续展示导视 ComParameters.Parameters.EdgeWindow.LeaveScreenSave(); return; } else { result = result.Where(i => Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + i.endTime) > DateTime.Now).OrderBy(i => i.beginTime).ToList(); if (result != null && result.Count > 0) { if (Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + result[0].beginTime) <= DateTime.Now) { ComParameters.Parameters.IsPlay = true; this.Dispatcher.Invoke(() => { var screenview = new ScreenWindowNew(0); screenview.Show(); WinApi.FindSetTop(null, "ScreenWindowNew"); }); } else { ComParameters.Parameters.EdgeWindow.LeaveScreenSave(); } } else { ComParameters.Parameters.EdgeWindow.LeaveScreenSave(); } } } catch (Exception ex) { Log.MyLog.WriteLogFile("CheckAndPlay:" + ex.ToString(), "ScreenWindowNew"); } } //if (!ComParameters.Parameters.IsPlay) //{ // List pro = null; // List sub = null; // var now = DateTime.Now; // ComParameters.rwl.EnterReadLock(); // var content = FileManage.ReadJsonFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Programme", "ProgrammeList.txt"));//普通节目 // ComParameters.rwl.ExitReadLock(); // if (!string.IsNullOrEmpty(content)) // { // pro = JsonConvert.DeserializeObject>(content); // pro = pro.Where(p => p.LaunchTime <= now && p.ExpiryDate >= now).ToList(); // } // ComParameters.srwl.EnterReadLock(); // var subcontent = FileManage.ReadJsonFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Programme", "SubtitleList.txt"));//字幕 // ComParameters.srwl.ExitReadLock(); // if (!string.IsNullOrEmpty(subcontent)) // { // sub = JsonConvert.DeserializeObject>(subcontent); // sub = sub.Where(p => p.startTime <= now && p.endTime >= now).ToList(); // } // if ((pro != null && pro.Any()) || (sub != null && sub.Any())) // { // if (!ComParameters.Parameters.IsPlay) // { // ComParameters.Parameters.IsPlay = true; // this.Dispatcher.Invoke(() => // { // var scerrnview = new ScreenWindowNew(); // scerrnview.Show(); // }); // } // } //} } private void LongDetect(object sender,EventArgs e) { if (WinApi.GetLastInputInfo(ref _mLastInputInfo)) { if ((Environment.TickCount - (long)_mLastInputInfo.dwTime)> ComParameters.Parameters.totalNoTouchSeconds) { CheckAndPlay(); } } } private void InitTimer() { _timer = new DispatcherTimer(); _timer.Tick += new EventHandler(LongDetect); _timer.Interval = TimeSpan.FromSeconds(1); _timer.Start(); } public void StartNewTimer() { if (null != _timer && _timer.IsEnabled == false) { _timer.Start(); } } public void StopTimer() { if (null != _timer && _timer.IsEnabled == true) { _timer.Stop(); } } #endregion #region 内部方法 public async void CreateWebView() { try { #region 启动导视网页 webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted; //--incognito 隐私模式 --kiosk 大屏导视模式 开发人员要调试 就取消了 --disable-pinch 禁用缩放 //var env = await CoreWebView2Environment.CreateAsync(null, null, new CoreWebView2EnvironmentOptions(additionalBrowserArguments: "--remote-debugging-port=9221 --incognito --kiosk --disable-pinch")); //var env = await CoreWebView2Environment.CreateAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WebView2"), null, new CoreWebView2EnvironmentOptions(additionalBrowserArguments: "--remote-debugging-port=9221 --disable-web-security", allowSingleSignOnUsingOSPrimaryAccount:true)); var env = await CoreWebView2Environment.CreateAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WebView2"), null, new CoreWebView2EnvironmentOptions(additionalBrowserArguments: "google-chrome --remote-debugging-port=9221 --remote-debugging-address=0.0.0.0 --disable-web-security", allowSingleSignOnUsingOSPrimaryAccount: true)); //var env = await CoreWebView2Environment.CreateAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WebView2"), null, null); //var env = await CoreWebView2Environment.CreateAsync(); await webView.EnsureCoreWebView2Async(env); webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;//取消右键 webView.CoreWebView2.Settings.IsZoomControlEnabled = false;//取消缩放 webView.CoreWebView2.Settings.IsPinchZoomEnabled = false;//取消捏合缩放 webView.CoreWebView2.Settings.IsGeneralAutofillEnabled = false;//取消自动填充 webView.CoreWebView2.Settings.IsPasswordAutosaveEnabled = false;//取消密码自动保存 webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;//禁用右键菜单 webView.CoreWebView2.Settings.IsScriptEnabled = true;//启用js脚本 //webView.CoreWebView2.Settings.IsStatusBarEnabled = true; //webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived; webView.CoreWebView2.ProcessFailed += CoreWebView2_ProcessFailed; #endregion } catch (Exception ex) { if (_maxTryCount > 0) { _maxTryCount -= 1; CreateWebView(); }else { ExternalCall.StartExplorer(); Log.MyLog.WriteLogFile($"打开WebView2浏览器出错:{ex}", "EdgeError"); Environment.Exit(0); } } } //private void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e) //{ // var jo = JObject.Parse(e.WebMessageAsJson); //} private void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e) { try { if (null != webView && null != webView.CoreWebView2) { //调用方法js:csobj_sync.EnterScreenSaver() 或 await csobj.EnterScreenSaver() //注册csobj脚本c#互操作 webView.CoreWebView2.AddHostObjectToScript("csobj", new ScriptCallbackObject()); //注册全局变量csobj webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("var csobj = window.chrome.webview.hostObjects.csobj;"); webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("var csobj_sync = window.chrome.webview.hostObjects.sync.csobj;"); webView.CoreWebView2.SetVirtualHostNameToFolderMapping("navigation", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "www"), CoreWebView2HostResourceAccessKind.Allow); Thread.Sleep(3000); webView.CoreWebView2.Navigate("http://navigation/index.html"); //webView.Source = new Uri("http://192.168.1.114:5500/index.html"); } } catch (Exception ex) { Log.MyLog.WriteLogFile($"打开应用出错:{ex}", "EdgeError"); } } //离开屏保 public async void LeaveScreenSave() { if (webView != null && webView.CoreWebView2 != null) { try { Log.MyLog.WriteLogFile($"离开屏保", "EdgeFunction"); await webView.CoreWebView2.ExecuteScriptAsync("leaveScreenSave()"); } catch (Exception ex) { Log.MyLog.WriteLogFile($"LeaveScreenSave:{ex}", "EdgeError"); } } } //发送语音消息 public async void SendVoiceMsg(string VoiceMsg) { try { Log.MyLog.WriteLogFile($"发送语音:{VoiceMsg}", "EdgeFunction"); await webView.CoreWebView2.ExecuteScriptAsync($"voiceContent('{VoiceMsg}')"); IflytekSpecch.iflyteSpeech.stopVoice(); } catch (Exception ex) { Log.MyLog.WriteLogFile($"SendVoiceMsg:{ex}", "EdgeFunction"); } } public void SendVoiceMsgBaidu(string VoiceMsg) { try { Log.MyLog.WriteLogFile($"发送语音:{VoiceMsg}", "EdgeFunction"); this.Dispatcher.Invoke(new Action(() => { webView.CoreWebView2.ExecuteScriptAsync($"voiceContent('{VoiceMsg}')"); })); } catch (Exception ex) { Log.MyLog.WriteLogFile($"SendVoiceMsg:{ex}", "EdgeFunction"); } } //发送视频流 public void pushFaceBase(string FaceData) { try { this.Dispatcher.Invoke(new Action(() => { //Log.MyLog.WriteLogFile($"发送视频流:{FaceData}", "EdgeFunction"); webView.CoreWebView2.ExecuteScriptAsync($"pushFaceBase('{FaceData}')"); })); } catch (Exception ex) { Log.MyLog.WriteLogFile($"pushFaceBase:{ex}", "EdgeFunction"); } } private void CoreWebView2_ProcessFailed(object sender, CoreWebView2ProcessFailedEventArgs e) { Log.MyLog.WriteLogFile("WebView2进程错误:" + e.ToString(), "WebView2Error") ; Reload(); Activate(); //if (_maxTryCount > 0) //{ // _maxTryCount -= 1; // CreateWebView(); //} } /// /// 检测浏览器运行环境是否安装 /// /// public static bool HasInstallWebview2() { string result = ""; try { result = CoreWebView2Environment.GetAvailableBrowserVersionString(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WebView2")); } catch (System.Exception) { } if (result == "" || result == null) { return false; } return true; } /// /// 浏览器重新加载 /// public async void Reload() { if (null != webView && null != webView.CoreWebView2) { webView.CoreWebView2.Reload(); await webView.CoreWebView2.Profile.ClearBrowsingDataAsync(); } //else //{ // SystemManage.CloseProgress("chrome"); // ExternalCall.RunCmdComm(ComParameters.Parameters.CmdChrome); //} } /// /// 挂起网页以减少内存使用 /// /// public async Task SuspendCore() { bool result = false; if (null != webView && null != webView.CoreWebView2) { result = await webView.CoreWebView2.TrySuspendAsync(); } return result; } /// /// 判断网页是否挂起 /// /// public bool IsSuspendCore() { bool result = false; if (null != webView && null != webView.CoreWebView2) { result = webView.CoreWebView2.IsSuspended; } return result; } /// /// 激活网页 /// public void ResumeCore() { if (null != webView && null != webView.CoreWebView2) { webView.CoreWebView2.Resume(); } } /// /// 应用启动是否正常判断,配合detectOnline()方法 /// int Countdown = 15; private void Countdown_timer() { CountdownTimer = new DispatcherTimer() { IsEnabled = true, Interval = TimeSpan.FromSeconds(1) }; CountdownTimer.Tick += new EventHandler((sender, e) => { if (--Countdown < 0) { Reload(); } }); } #region 通信类 /// /// 发送Message /// /// public void SendString(string message) { if (null != webView && null != webView.CoreWebView2) { webView.CoreWebView2.PostWebMessageAsString(message); } } /// /// 发送Json /// /// public void SendJson(string json) { if (null != webView && null != webView.CoreWebView2) { webView.CoreWebView2.PostWebMessageAsJson(json); } } /// /// 调用JS代码(可直接执行定义好的方法) /// /// public async void ExecuteJSScriptAsync(string script) { if (null != webView && null != webView.CoreWebView2) { await webView.CoreWebView2.ExecuteScriptAsync(script); } } #endregion #endregion } /// /// 数据交互的类 /// [ClassInterface(ClassInterfaceType.AutoDual)] [ComVisible(true)] public class ScriptCallbackObject { //提供设备信息 public string deviceInfo() { var dic = new Dictionary(); dic.Add("width", SystemParameters.PrimaryScreenWidth.ToString()); dic.Add("height", SystemParameters.PrimaryScreenHeight.ToString()); dic.Add("ip", SystemManage.GetLocalIp()); dic.Add("mac", SystemManage.GetMac()); dic.Add("code", ComParameters.Parameters.devCode); dic.Add("serverIP", ComParameters.Parameters.httpServer); return JsonConvert.SerializeObject(dic); } //屏保跳转 public void goScreenSave() { IntPtr Win = FindWindow(null, "ScreenWindowNew"); if (Win == null || Win == IntPtr.Zero) { ComParameters.Parameters.EdgeWindow?.Dispatcher.Invoke(() => { Log.MyLog.WriteLogFile($"进入屏保", "EdgeFunction"); ComParameters.Parameters.EdgeWindow.CheckAndPlay(); }); } else { WinApi.FindSetTop(null, "ScreenWindowNew"); } } //判断有没有节目 public bool hasProgram() { try { List result = HttpComm.Http.GetResource(); if (result == null || result.Count == 0) { return false; } return true; } catch (Exception ex) { Log.MyLog.WriteLogFile("CheckAndPlay:" + ex.ToString(), "ScreenWindowNew"); return true; } } //开始语音识别 private bool initVoice = false; public void startVoice() { #region 科大语音 //if (!initVoice) //{ // initVoice = true; // IflytekSpecch.iflyteSpeech.InitSpeech(); //} //IflytekSpecch.iflyteSpeech.startVoice(); #endregion #region 百度语音 BaiduSpeechForWebApi.baiduSpeechForWebApi.startVoice(); #endregion } //结束语音识别 public void stopVoice() { #region 科大语音 //IflytekSpecch.iflyteSpeech.stopVoice(); #endregion #region 百度语音 BaiduSpeechForWebApi.baiduSpeechForWebApi.stopVoice(); #endregion } //程序退出功能 public void exitIOTContainer() { ExternalCall.CheckStartExplorer(); Environment.Exit(0); } //守护导视进程 public bool detectOnline() { var web = new EdgeWindow(); web.CountdownTimer.IsEnabled = false; return true; } //语音播报 public void TTS(string testSpeech) { try { SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpVoice Voice = new SpVoice(); Voice.Speak(testSpeech, SpFlags); } catch (Exception ex) { Log.MyLog.WriteLogFile($"语音播报出错:{ex}", "EdgeError"); } } //开启人脸功能 private bool initFace = false; public void startFace() { Task.Run(() => { try { if (!initFace) { initFace = true; //百度SDK初始化 BaiduFace.SDK.Init(); BaiduFace.SDK.USBVideoTract(); } FaceParameters.Parameters._isOpenLive = true; if (!FaceParameters.Parameters.isInit) { var faceData = new FaceSendModel { age = 28, genderMale = "男", vipId = "", faceID = "", carNo = "", isOK = "1", faceImage = "" }; //主要逻辑 Thread.Sleep(20000); ComParameters.Parameters.EdgeWindow.pushFaceBase(JsonConvert.SerializeObject(faceData)); } } catch (Exception ex) { Log.MyLog.WriteLogFile($"开启人脸功能出错:{ex}", "EdgeError"); var faceData = new FaceSendModel { age = 28, genderMale = "男", vipId = "", faceID = "", carNo = "", isOK = "1", faceImage = "" }; Thread.Sleep(20000); ComParameters.Parameters.EdgeWindow.pushFaceBase(JsonConvert.SerializeObject(faceData)); } }); } //结束人脸功能 public void stopFace() { try { Log.MyLog.WriteLogFile($"结束人脸功能", "EdgeFunction"); FaceParameters.Parameters._isOpenLive = false; //IntPtr Win = FindWindow(null, "face"); //if (Win != null && Win != IntPtr.Zero) //{ // SendMessage(Win, WM_CLOSE, 0, 0); //} } catch (Exception ex) { Log.MyLog.WriteLogFile($"结束人脸功能出错:{ex}", "EdgeError"); } } public void WriteLog(string text) { Log.MyLog.WriteLogFile(text, "Kiosk"); } } }