Files
cms_thermo_active/Step.UI/ServerControlWindow.cs
T
CMS3762\carminatini be173af2dd Removed Login_Required from Language
Added login in Swagger
Bug Fixed
2018-01-09 14:17:28 +01:00

221 lines
7.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Step.Model;
using TeamDev.SDK.MVVM;
using static Step.Config.StartupConfig;
using static Step.Utils.Constants;
using Step.Model.DTOModels;
namespace Step.UI
{
public partial class ServerControlWindow : MetroFramework.Forms.MetroForm
{
String HomePageUI;
String TestJSPageUI;
bool ncStatus = false;
public ServerControlWindow()
{
InitializeComponent();
HomePageUI = "http://localhost:" + ServerConfig.ServerPort.ToString() + "/index.html";
TestJSPageUI = "http://localhost:" + ServerConfig.ServerPort.ToString() + "/Testjavascript//index.html";
InitializeMessageListeners();
}
private static ServerControlWindow ctrlwindow = null;
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;
// Avoid closing the window
protected override void OnClosing(CancelEventArgs e)
{
if (!_closing)
{
e.Cancel = true;
Hide();
}
}
private void OpenUiButton_Click(object sender, EventArgs e)
{
//Open CMS Client
StartCMSClient(HomePageUI);
}
private void StopServerButton_Click(object sender, EventArgs e)
{
// Send message to listeners and close server
MessageServices.Current.Publish(STOP_SERVER);
}
private void NotifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.TopMost = true;
this.TopMost = false;
}
private void StopServerItem_Click(object sender, EventArgs e)
{
MessageServices.Current.Publish("StopServer");
}
private void StartCMSClient(string url)
{
//Setup the Path Variable
String CMSClientPath = "";
//Check if the system is 64/32 bit
if (Environment.Is64BitOperatingSystem && File.Exists(BASE_PATH + @"\Client\x64\CMS_Client.exe"))
CMSClientPath = BASE_PATH + @"\Client\x64\CMS_Client.exe";
else if (File.Exists(BASE_PATH + @"\Client\x86\CMS_Client.exe"))
CMSClientPath = BASE_PATH + @"\Client\x86\CMS_Client.exe";
if (!String.IsNullOrEmpty(CMSClientPath))
Process.Start(CMSClientPath, url);
else
MessageBox.Show("CMS_Client.Exe not found");
}
private void TestJSButton_Click(object sender, EventArgs e)
{
StartCMSClient(TestJSPageUI);
}
private void InitializeMessageListeners()
{
MessageServices.Current.Subscribe(SEND_MESSAGE, (a, b) =>
{
// Cast object to ErrorMessageModel
ErrorMessageModel message = (ErrorMessageModel)a;
// If error has a fatal level, icon must be error
message.ErrorLevel = message.ErrorLevel > (int)ERROR_LEVEL.ERROR ? (int)ERROR_LEVEL.ERROR : message.ErrorLevel;
// Open BalloonTip with data
StepNotifyIcon.ShowBalloonTip(1000, message.Title, message.Message, (ToolTipIcon)message.ErrorLevel);
});
MessageServices.Current.Subscribe(NC_STATUS, (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 == true)
{
title = "Nc connection established";
message = "Comunication started";
toolTipIcon = ToolTipIcon.Info;
}
this.Invoke((MethodInvoker)delegate () {
StepNotifyIcon.ShowBalloonTip(1000, title, message, toolTipIcon);
});
}
if (ncStatus)
this.Invoke((MethodInvoker)delegate ()
{
StepNotifyIcon.Icon = Properties.Resources.Step_Icon;
});
else
{
this.Invoke((MethodInvoker)delegate ()
{
TXTType.Text = "";
TXTName.Text = "";
TXTNcSerial.Text = "";
TXTCMSMach.Text = "";
TXTSftVers.Text = "";
TXTNCProc.Text = "";
TXTTime.Text = "";
TXTLang.Text = "";
StepNotifyIcon.Icon = Properties.Resources.Step_Disconnected;
});
}
//Other type
this.Invoke((MethodInvoker)delegate () {
CHNcConnected.Checked = ncStatus;
});
});
MessageServices.Current.Subscribe(SEND_GENERIC_DATA, (a, b) =>
{
DTONcGenericDataModel data = (DTONcGenericDataModel)a;
//Nc Type
String NcType = "";
if (NcConfig.NcVendor == 0)
NcType = "Demo";
else if (NcConfig.NcVendor == 1)
NcType = "Fanuc";
else if (NcConfig.NcVendor == 2)
NcType = "Siemens";
else if (NcConfig.NcVendor == 3)
NcType = "Osai";
//Other type
this.Invoke((MethodInvoker)delegate () {
TXTType.Text = NcType;
TXTName.Text = data.Model;
TXTNcSerial.Text = data.SerialNumber;
TXTCMSMach.Text = data.MachineNumber;
TXTSftVers.Text = data.SoftwareVersion;
TXTNCProc.Text = data.ProcessNumber.ToString();
TXTLang.Text = data.Language.EnglishName;
TXTTime.Text = data.DateTime.ToShortDateString() + " " + data.DateTime.ToShortTimeString();
});
});
}
}
}