Files
activestep/Step.UI/Dialogs/ResetCountersForm.cs
2020-09-12 16:11:43 +02:00

134 lines
4.3 KiB
C#

using CMS_CORE_Library.Models;
using Step.NC;
using Step.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static CMS_CORE_Library.Models.DataStructures;
namespace Step.UI
{
public partial class ResetCountersForm : Form
{
private NcAdapter Nc;
private String Matricola;
private int MatricolaNum;
public ResetCountersForm()
{
InitializeComponent();
}
public void setNcData(String matr, NcAdapter Nch)
{
this.Matricola = matr;
SupportFunctions.ConvertStringMachineNumberIntoNumber(matr, out bool containsLetters, out this.MatricolaNum);
this.Text = "Reset Counters - " + this.Matricola;
this.Nc = Nch;
}
private void Cancel_Pwd_Button_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void OK_Pwd_Button_Click(object sender, EventArgs e)
{
checkPSW(TextBox_Password.Text);
}
private void PasswordForm_Load(object sender, EventArgs e)
{
TextBox_Password.Text = "";
comboBoxSpindle.SelectedIndex = 0;
this.ActiveControl = comboBoxSpindle;
}
private void TextBox_Password_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
checkPSW(TextBox_Password.Text);
}
}
private void comboBoxSpindle_SelectedIndexChanged(object sender, EventArgs e)
{
ReadHours();
}
private void checkPSW(String psw)
{
if(Matricola== null || Matricola.Trim() == "" || MatricolaNum <= 0)
{
MessageBox.Show("Machine ID non correct", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxSpindle.SelectedIndex == 0)
{
MessageBox.Show("Counter must be selected", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// sfrutto password delle teste, passando la testa 0
if (!Utils.CandiesController.HeadsPasswordCheck(psw, MatricolaNum, 0))
{
MessageBox.Show("Wrong password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!Nc.ResetMaintenanceCounter((uint) comboBoxSpindle.SelectedIndex))
{
MessageBox.Show("Error While Resetting time", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
MessageBox.Show("Time resetted", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
ReadHours();
TextBox_Password.Text = "";
}
private void ReadHours()
{
if (comboBoxSpindle.SelectedIndex == 0)
{
labelValue.Text = "";
textBoxHours.Text = "";
}
else
{
uint realVal = 0;
uint secs = 0;
uint hours = 0;
List<CounterModel> Contatori = new List<CounterModel>();
if (Nc.GetMainenancesCounter(out Contatori) != NO_ERROR)
{
MessageBox.Show("Error While reading time", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
realVal = Contatori.FirstOrDefault(X => X.Id == (comboBoxSpindle.SelectedIndex - 1)).Value;
uint min = Convert.ToUInt32(Math.Floor((double)realVal / 60));
secs = realVal % 60;
if (min >= 60)
{
hours = Convert.ToUInt32(Math.Floor((double)min / 60));
min = min % 60;
}
textBoxHours.Text = hours.ToString() + "h " + min.ToString() + "m " + secs.ToString() + "s";
labelValue.Text = realVal.ToString();
}
}
}
}
}