Files
cms_thermo_active/Thermo.Active.UI/ServerControlWindow.cs
2020-07-06 19:37:13 +02:00

397 lines
15 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using TeamDev.SDK.MVVM;
using Thermo.Active.Core;
using Thermo.Active.Model;
using Thermo.Active.Model.DTOModels;
using Thermo.Active.NC;
using Thermo.Active.Utils;
using static Thermo.Active.Config.ServerConfig;
using static Thermo.Active.Model.Constants;
namespace Thermo.Active.UI
{
public partial class ServerControlWindow : Form
{
private DTONcGenericDataModel model;
private NcAdapter nc;
private bool ncStatus = false;
private bool isUpdatingThreads = false;
private List<RegistrationInfo> MVVMListeners;
private static ServerControlWindow ctrlwindow = null;
PasswordForm PswForm;
ResetSpindleForm ResSpForm;
ResetCountersForm ResCouForm;
ResetMachineHoursForm ResMaHouForm;
LockForm LckFrm;
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public ServerControlWindow()
{
InitializeComponent();
if (ServerStartupConfig.AutoOpenCmsClient)
this.Opacity = 0;
//Begin the update
LISTThreadStatus.BeginUpdate();
//clear the List
LISTThreadStatus.Items.Clear();
//Add all items
LISTThreadStatus.Items.Add(new ListViewItem(new string[] { "TryNcConnection", "---" }));
//End the update
LISTThreadStatus.EndUpdate();
// Set software version
TXTVersion.Text = SupportFunctions.GetSoftwareVersionAndBuildDate();
PswForm = new PasswordForm();
ResSpForm = new ResetSpindleForm();
ResCouForm = new ResetCountersForm();
ResMaHouForm = new ResetMachineHoursForm();
if (NcConfig != null && NcConfig.NcVendor != null)
TXTType.Text = NcConfig.NcVendor;
InitializeMessageListeners();
MessageServices.Current.Subscribe(SHOW_MSG_UI, (a, b) =>
{
Invoke((MethodInvoker)delegate () { Focus(); MessageBox.Show(a.ToString()); });
});
}
private void ServerControlWindow_FormClosing(object sender, FormClosingEventArgs e)
{
if (!_closing)
{
e.Cancel = true;
this.Opacity = 0;
Hide();
}
}
private void ServerControlWindow_Load(object sender, EventArgs e)
{
if (ServerStartupConfig.AutoOpenCmsClient)
{
BeginInvoke(new MethodInvoker(delegate
{
Hide();
}));
}
}
public static void Start()
{ // Open WinForm
Thread th = new Thread(() =>
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ctrlwindow = new ServerControlWindow();
Application.Run(ctrlwindow);
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
public static void Stop()
{ // Close WinForm
if (ctrlwindow != null)
{
ctrlwindow.Invoke((ThreadStart)delegate ()
{
ctrlwindow._closing = true;
ctrlwindow.Close();
ctrlwindow = null;
});
}
}
private bool _closing = false;
private void OpenUiButton_Click(object sender, EventArgs e)
{
//Open CMS Client
ThreadsHandler.StartClientFromUI();
}
private void StopServerButton_Click(object sender, EventArgs e)
{
// Send message to listeners and close server
MessageServices.Current.Publish(SEND_STOP_SERVER);
}
private void NotifyIcon_Click(object sender, EventArgs e)
{
if ((e as MouseEventArgs) != null && (e as MouseEventArgs).Button == MouseButtons.Left)
{
Show();
this.Opacity = 1;
this.WindowState = FormWindowState.Normal;
this.TopMost = true;
this.TopMost = false;
}
}
private void StopServerItem_Click(object sender, EventArgs e)
{
MessageServices.Current.Publish(SEND_STOP_SERVER);
}
private void InitializeMessageListeners()
{
MVVMListeners = new List<RegistrationInfo>
{
MessageServices.Current.Subscribe(SEND_MESSAGE, (a, b) =>
{
// Cast object to ErrorMessageModel
ErrorMessageModel message = (ErrorMessageModel)a;
if(message.ErrorLevel <= ERROR_LEVEL.ERROR)
{
// If error has a fatal level, icon must be error
message.ErrorLevel = message.ErrorLevel > ERROR_LEVEL.ERROR ? ERROR_LEVEL.ERROR : message.ErrorLevel;
if (!this.IsDisposed)
this.Invoke((MethodInvoker)delegate ()
{
// Open BalloonTip with data
StepNotifyIcon.ShowBalloonTip(1000, message.Title, message.Message, (ToolTipIcon)message.ErrorLevel);
//ShowMessage on UI
TXTstatus.Text = "[" + DateTime.Now.ToString("T") + "] " + message.Message;
});
}
else
{
if (!this.IsDisposed)
this.Invoke((MethodInvoker)delegate ()
{
// Open BalloonTip with data
StepNotifyIcon.ShowBalloonTip(1000, message.Title, message.Message, (ToolTipIcon.Error));
//ShowMessage on UI
TXTstatus.Text = "[" + DateTime.Now.ToString("T") + "] " + message.Message;
// Notify user
if(MessageBox.Show(new Form { TopMost = true }, message.Message, message.Title, MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
MessageServices.Current.Publish(SEND_STOP_SERVER);
});
}
}),
// NC status handler
MessageServices.Current.Subscribe(SEND_NC_STATUS_UI, (a, b) =>
{
bool newNcStatus = (bool)a;
if (ncStatus != newNcStatus)
{
ncStatus = newNcStatus;
string title = "NC not connected";
string message = "Check NC connection";
ToolTipIcon toolTipIcon = ToolTipIcon.Error;
if (ncStatus)
{
title = "Nc connection established";
message = "Comunication started";
toolTipIcon = ToolTipIcon.Info;
}
if (!IsDisposed)
Invoke((MethodInvoker)delegate ()
{
// Show balloon with new status
StepNotifyIcon.ShowBalloonTip(1000, title, message, toolTipIcon);
TXTstatus.Text = "[" + DateTime.Now.ToString("T") + "] " + title;
});
}
if (ncStatus)
{
if (!IsDisposed)
Invoke((MethodInvoker)delegate ()
{
nc = new NcAdapter();
nc.Connect();
nc.GetNcGenericData(out model);
StepNotifyIcon.Icon = Properties.Resources.ACTIVE_ICONA;
TXTMachId.Text = model.CmsMachineIdNumber;
CHNcConnected.Checked = true;
PswForm.setNcData(model.CmsMachineIdNumber, nc);
ResSpForm.setNcData(model.CmsMachineIdNumber, nc);
ResCouForm.setNcData(model.CmsMachineIdNumber, nc);
ResMaHouForm.setNcData(model.CmsMachineIdNumber, nc);
passwordToolStripMenuItem.Enabled = true;
passwordToolStripMenuItem1.Enabled = true;
});
}
else
{
if (!IsDisposed)
Invoke((MethodInvoker)delegate ()
{
StepNotifyIcon.Icon = Properties.Resources.ACTIVE_ICONA_BW;
TXTMachId.Text = "---";
CHNcConnected.Checked = false;
passwordToolStripMenuItem.Enabled = false;
passwordToolStripMenuItem1.Enabled = false;
});
}
}),
// Threads status handler
MessageServices.Current.Subscribe(SEND_THREADS_STATUS, (a, b) =>
{
//Other type
if (!IsDisposed)
Invoke((MethodInvoker)delegate ()
{
if (!isUpdatingThreads)
{
isUpdatingThreads = true;
Dictionary<String, String> Threads = new Dictionary<String, String>((Dictionary<String, String>)a);
//Begin the update
LISTThreadStatus.BeginUpdate();
//clear the List
LISTThreadStatus.Items.Clear();
//Add all items
foreach (KeyValuePair<string, String> Thr in Threads)
LISTThreadStatus.Items.Add(new ListViewItem(new String[]{Thr.Key,Thr.Value }));
//End the update
LISTThreadStatus.EndUpdate();
isUpdatingThreads = false;
}
});
})
};
}
private static bool ClientIsRunning()
{
Process[] p = Process.GetProcessesByName(CLIENT_EXE_NAME_NOEXT);
foreach (Process pr in p)
{
if (pr.MainWindowHandle != IntPtr.Zero)
{
ShowWindow(pr.MainWindowHandle, 9);
SetForegroundWindow(pr.MainWindowHandle);
return true;
}
}
return false;
}
private void ShowPasswordDialog()
{
DialogResult dr = PswForm.ShowDialog();
if (dr == DialogResult.OK)
{
resetCountersToolStripMenuItem.Visible = true;
resetCountersToolStripMenuItem1.Visible = true;
resetSpindleHoursToolStripMenuItem.Visible = true;
resetSpindleHoursToolStripMenuItem1.Visible = true;
goBackToStandardModeToolStripMenuItem.Visible = true;
goBackToStandardModeToolStripMenuItem1.Visible = true;
resetMachineWorkingHoursToolStripMenuItem.Visible = true;
resetMachineWorkingHoursToolStripMenuItem1.Visible = true;
toolStripSeparator.Visible = true;
toolStripSeparator1.Visible = true;
passwordToolStripMenuItem.Enabled = false;
passwordToolStripMenuItem1.Enabled = false;
LckFrm = new LockForm(false);
LckFrm.ShowDialog();
}
}
private void backStdMode()
{
resetCountersToolStripMenuItem.Visible = false;
resetCountersToolStripMenuItem1.Visible = false;
resetSpindleHoursToolStripMenuItem.Visible = false;
resetSpindleHoursToolStripMenuItem1.Visible = false;
goBackToStandardModeToolStripMenuItem.Visible = false;
goBackToStandardModeToolStripMenuItem1.Visible = false;
resetMachineWorkingHoursToolStripMenuItem.Visible = false;
resetMachineWorkingHoursToolStripMenuItem1.Visible = false;
toolStripSeparator.Visible = false;
toolStripSeparator1.Visible = false;
passwordToolStripMenuItem.Enabled = true;
passwordToolStripMenuItem1.Enabled = true;
LckFrm = new LockForm(true);
LckFrm.ShowDialog();
}
private void passwordToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowPasswordDialog();
}
private void goBackToStandardModeToolStripMenuItem_Click(object sender, EventArgs e)
{
backStdMode();
}
private void passwordToolStripMenuItem1_Click(object sender, EventArgs e)
{
ShowPasswordDialog();
}
private void goBackToStandardModeToolStripMenuItem1_Click(object sender, EventArgs e)
{
backStdMode();
}
private void closeCMSServerToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageServices.Current.Publish(SEND_STOP_SERVER);
}
private void resetSpindleHoursToolStripMenuItem1_Click(object sender, EventArgs e)
{
ResSpForm.ShowDialog();
}
private void resetSpindleHoursToolStripMenuItem_Click(object sender, EventArgs e)
{
ResSpForm.ShowDialog();
}
private void resetCountersToolStripMenuItem_Click(object sender, EventArgs e)
{
ResCouForm.ShowDialog();
}
private void resetCountersToolStripMenuItem1_Click(object sender, EventArgs e)
{
ResCouForm.ShowDialog();
}
private void resetMachineWorkingHoursToolStripMenuItem_Click(object sender, EventArgs e)
{
ResMaHouForm.ShowDialog();
}
private void resetMachineWorkingHoursToolStripMenuItem1_Click(object sender, EventArgs e)
{
ResMaHouForm.ShowDialog();
}
}
}