Files
cms_thermo_active/Client/Browser_Tools/BrowserJSObject.cs
T

212 lines
7.2 KiB
C#

using Chromium;
using Chromium.Remote;
using Chromium.Remote.Event;
using Chromium.WebBrowser;
using Client.Config;
using Client.Config.SubModels;
using CMS_Client.View;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Client.Utils.Constants;
namespace CMS_Client.Browser_Tools
{
public class BrowserJSObject : JSObject
{
//The first letter of All PUBLIC Variables and Methods must be Lower-Case (CEF Settings)
private MainForm mainForm;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region CONSTRUCTOR_METHOD
//Constructor Method
public BrowserJSObject(MainForm f)
{
mainForm = f;
AddFunction("minimizeForm").Execute += minimizeForm;
AddFunction("maximizeForm").Execute += maximizeForm;
AddFunction("closeForm").Execute += closeForm;
AddFunction("forceStepFocus").Execute += forceStepFocus;
AddFunction("setNcWindowState").Execute += setNcWindowState;
AddFunction("getNcWindowState").Execute += getNcWindowState;
AddFunction("getScreenBase64").Execute += getScreenBase64;
AddFunction("getChromiumVersion").Execute += getChromiumVersion;
AddFunction("getClientID").Execute += getClientID;
AddFunction("getConfiguredProcesses").Execute += getConfiguredProcesses;
AddFunction("startNewProcess").Execute += startNewProcess;
AddFunction("openOrStartProcess").Execute += openOrStartProcess;
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region FORM_BEHAVIOUR_METHODS
//Minimize Main Window
public void minimizeForm(object sender, CfrV8HandlerExecuteEventArgs e)
{
//Invoke method if is needed or call the method in STD mode
if (mainForm.InvokeRequired)
mainForm.Invoke((MethodInvoker)delegate () {
mainForm.WindowState = FormWindowState.Minimized;
});
else
{
mainForm.WindowState = FormWindowState.Minimized;
}
}
//Maximize Main Window
public void maximizeForm(object sender, CfrV8HandlerExecuteEventArgs e)
{
//Invoke method if is needed or call the method in STD mode
if (mainForm.InvokeRequired)
mainForm.Invoke((MethodInvoker)delegate () {
mainForm.WindowState = FormWindowState.Maximized;
});
else
{
mainForm.WindowState = FormWindowState.Maximized;
}
}
//Close Main Window
public void closeForm(object sender, CfrV8HandlerExecuteEventArgs e)
{
//If the mainform is disposed do nothing
if (mainForm.IsDisposed)
return;
//Invoke method if is needed or call the method in STD mode
mainForm.Close();
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region NC_BEHAVIOUR_METHODS
public void setNcWindowState(object sender, CfrV8HandlerExecuteEventArgs e)
{
if (e.Arguments.Count() == 0)
return;
NcWindow.SetState((NcState) e.Arguments[0].IntValue );
if (NcWindow.State == NcState.SHOW)
mainForm.ShowNCWindow();
else
mainForm.HideNCWindow();
}
public void getNcWindowState(object sender, CfrV8HandlerExecuteEventArgs e)
{
e.SetReturnValue((int) NcWindow.State);
}
public void getScreenBase64(object sender, CfrV8HandlerExecuteEventArgs e)
{
if (e.Arguments.Count() > 0 && e.Arguments[0].BoolValue)
NcWindow.CaptureHMI();
e.SetReturnValue(NcWindow.NcCapture);
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region CHROMIUM_METHODS
//Get the Version of Chromium
public void getChromiumVersion(object sender, CfrV8HandlerExecuteEventArgs e)
{
e.SetReturnValue(CfxRuntime.GetChromeVersion());
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region STEP_METHODS
//Get the ID of STEP Client
public void getClientID(object sender, CfrV8HandlerExecuteEventArgs e)
{
e.SetReturnValue((int) Config.ConnectionConfig.Id);
}
public void forceStepFocus(object sender, CfrV8HandlerExecuteEventArgs e)
{
NcWindow.ForceStepFocus();
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region PROCESSES_METHODS
//Read all configured processes
public void getConfiguredProcesses(object sender, CfrV8HandlerExecuteEventArgs e)
{
e.SetReturnValue(JsonConvert.SerializeObject(Config.ExtSoftwaresConfig));
}
//Start a new process
public void startNewProcess(object sender, CfrV8HandlerExecuteEventArgs e)
{
if (e.Arguments.Count() == 0)
return;
String id = e.Arguments[0].StringValue;
Software sft = Config.ExtSoftwaresConfig.FirstOrDefault(X => X.Id == id);
if (sft != null)
{
Process.Start(sft.Path, sft.Arguments);
}
}
//Open the last window or Start a new process
public void openOrStartProcess(object sender, CfrV8HandlerExecuteEventArgs e)
{
if (e.Arguments.Count() == 0)
return;
String id = e.Arguments[0].StringValue;
Software sft = Config.ExtSoftwaresConfig.FirstOrDefault(X => X.Id == id);
if (sft != null)
{
Process[] p = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(sft.Path)).OrderByDescending(X => X.StartTime).ToArray();
if (p.Count() > 0 && p[0].MainWindowHandle != IntPtr.Zero)
NcWindow.ForceExtFocus(p[0].MainWindowHandle);
else
Process.Start(sft.Path, sft.Arguments);
}
}
#endregion
}
}