diff --git a/Libs/CMS_CORE_Library.dll b/Libs/CMS_CORE_Library.dll index a42cf9aa..c2b23f46 100644 Binary files a/Libs/CMS_CORE_Library.dll and b/Libs/CMS_CORE_Library.dll differ diff --git a/Step.Database/Controllers/NcToolManagerController.cs b/Step.Database/Controllers/NcToolManagerController.cs index ef62244e..29b77cdb 100644 --- a/Step.Database/Controllers/NcToolManagerController.cs +++ b/Step.Database/Controllers/NcToolManagerController.cs @@ -575,7 +575,14 @@ namespace Step.Database.Controllers Tools = FindTools(), Families = FindFamilies(), Shanks = FindShanks() - .Select(x => { x.MagazineId = null; x.PositionId = null; return x; }) + .Select(x => { + // Reset positional Data + x.MagazineId = null; + x.PositionId = null; + x.OriginMagazineId = null; + x.OriginPositionId = null; + + return x; }) .ToList() }; } diff --git a/Step.NC/NcAdapter.cs b/Step.NC/NcAdapter.cs index bae47fb6..66276f90 100644 --- a/Step.NC/NcAdapter.cs +++ b/Step.NC/NcAdapter.cs @@ -17,6 +17,7 @@ using System.Globalization; using System.IO; using System.Linq; using System.Reflection; +using System.Text.RegularExpressions; using static CMS_CORE_Library.Models.DataStructures; using static Step.Config.ServerConfig; using static Step.Model.Constants; @@ -1268,20 +1269,34 @@ namespace Step.NC string strMachNumber = "0"; CmsError cmsError = numericalControl.NC_RMachineNumber(ref strMachNumber); if (cmsError.IsError()) - return cmsError; - int machNumber = int.Parse(strMachNumber); + return cmsError; + + // Check if machine number contains letters + bool containsLetters = Regex.IsMatch(strMachNumber, @".*?[a-zA-Z].*?"); + int machNumber = 0; + if (containsLetters) + { + // Convert ASCII string to a single INT + for (int i = 1; i <= strMachNumber.Count(); i++) + machNumber += strMachNumber[strMachNumber.Count() - i] << (8 * (i - 1)); + } + else + { + // Convert string of digits to a INT + machNumber = int.Parse(strMachNumber); + } // Read Data from NC & elaborate it - int NcCandy = 0; + long NcCandy = 0; cmsError = numericalControl.PLC_RCandy(ref NcCandy); if (cmsError.IsError()) return cmsError; - bool bNC_OK = CandiesController.GetDataFromLincense(NcCandy, out long NCLic, out int NCMatr, out long NCParam); + bool bNC_OK = CandiesController.GetDataFromLincense(NcCandy, containsLetters, out long NCLic, out int NCMatr, out long NCParam); bool bNC_VALID = machNumber == NCMatr && bNC_OK; // Read Data from PC - bool bPC_OK = CandiesController.GetPCLincense(out long PcPLic, out int PCMatr, out long PCParam); + bool bPC_OK = CandiesController.GetPCLincense(containsLetters, out long PcPLic, out int PCMatr, out long PCParam); bool bPC_VALID = machNumber == PCMatr && bPC_OK; // Elaborate Licence and write it @@ -1310,14 +1325,14 @@ namespace Step.NC public void WriteCandy(DateTime value,int machNumber) { long nDays; - int Lic=0; + long Lic=0; nDays = (value.Ticks / TimeSpan.TicksPerDay); //Imposto nel registro CandiesController.SetPCLincense(machNumber,nDays); //Imposto nel CN - Lic = Int32.Parse(CandiesController.SetLincenseFromData(machNumber, nDays)); + Lic = long.Parse(CandiesController.SetLincenseFromData(machNumber, nDays)); numericalControl.PLC_WCandy(Lic); } diff --git a/Step.UI/Dialogs/PasswordForm.cs b/Step.UI/Dialogs/PasswordForm.cs index b1f02d01..65aba8b6 100644 --- a/Step.UI/Dialogs/PasswordForm.cs +++ b/Step.UI/Dialogs/PasswordForm.cs @@ -59,14 +59,12 @@ namespace Step.UI private void checkPSW(String psw) { bool bValid = false; - long Parameter; - int Matr, Command; //Password storica. Serve per abilitare funzionalità particolari if (psw == "cmsserviceonly") bValid = true; - else if (Utils.CandiesController.LincensePasswordDecode(psw, out Matr, out Command, out Parameter)) + else if (Utils.CandiesController.LincensePasswordDecode(psw, out string Matr, out int Command, out long Parameter)) //Controllo la matricola if (this.Matricola != Matr.ToString()) diff --git a/Step.Utils/CandiesController.cs b/Step.Utils/CandiesController.cs index 823f35d1..a325a703 100644 --- a/Step.Utils/CandiesController.cs +++ b/Step.Utils/CandiesController.cs @@ -18,19 +18,20 @@ namespace Step.Utils private const String KEY_VALUE_NAME = "cil"; private const int BASE_TIME = 730119; // 1/1/2000 private static int randomTime = 0; - private static TimeSpan StartTime = new TimeSpan(6, 15, 0); + private static TimeSpan MorningTime = new TimeSpan(6, 15, 0); + private static DateTime StartTimePlusDelay = DateTime.Now; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region PUBLIC_METHODS // Decodifica password esportando i dati. Ereditata da CMS-Control - public static bool LincensePasswordDecode(String Password, out int Matricola, out int Command, out long Parameter) + public static bool LincensePasswordDecode(String Password, out string Matricola, out int Command, out long Parameter) { String szVal; int nChk = 0; - Matricola = 0; + Matricola = ""; Command = 0; Parameter = 0; @@ -47,7 +48,22 @@ namespace Step.Utils if (szVal.Last() != nChk.ToString().Last()) return false; - Matricola = Int16.Parse(szVal.Substring(1, 5)); + if(szVal.Count() > 13) + { + var machinNumberString = szVal.Substring(1, 11); + + int intValue = Int32.Parse(machinNumberString); + + for (int i = 3; i >= 0; i--) + { + Matricola += (char)((byte)(intValue >> (8 * i))); + } + } + else + { + Matricola = szVal.Substring(1, 5); + } + Command = Int16.Parse(szVal.Substring(0, 1)); Parameter = long.Parse(szVal.Substring(6, szVal.Length - 6 - 1)); } @@ -60,10 +76,10 @@ namespace Step.Utils // Lettura della licenza da registro. Ereditata da CMS-Control - public static bool GetPCLincense(out long EndLincence, out int Matricola, out long Parameter) + public static bool GetPCLincense(bool containsLetters, out long EndLincence, out int Matricola, out long Parameter) { string szLicReg_X = String.Empty; - int Lincence = 0; + long Lincence = 0; Matricola = 0; Parameter = 0; EndLincence = 0; @@ -71,8 +87,8 @@ namespace Step.Utils //Leggo nel Registro Licenza_Registry_RW(false, ref szLicReg_X); - Lincence = Int32.Parse(szLicReg_X); - return GetDataFromLincense(Lincence, out EndLincence, out Matricola, out Parameter); + Lincence = long.Parse(szLicReg_X); + return GetDataFromLincense(Lincence, containsLetters, out EndLincence, out Matricola, out Parameter); } @@ -88,7 +104,7 @@ namespace Step.Utils // Gestisce la creazione della licenza, da scrivere poi nel CN public static string SetLincenseFromData(int Matricola, long Parameter) { - int nLic; + long nLic; int days; if (Parameter > BASE_TIME) @@ -97,13 +113,14 @@ namespace Step.Utils days = Int32.MaxValue; else days = 0; + + nLic = ((long)Matricola << 16) + days; - nLic = (Matricola << 16) + days; return AlgoritmoLicence(nLic); } // Gestisce l'export dei dati partendo dalla licenza letta dal CN - public static bool GetDataFromLincense(int RawLincence, out long EndLincence, out int Matricola,out long Parameter) + public static bool GetDataFromLincense(long RawLincence, bool containsLetters, out long EndLincence, out int Matricola, out long Parameter) { Matricola = 0; Parameter = 0; @@ -113,8 +130,12 @@ namespace Step.Utils return false; EndLincence = AlgoritmoLicence_Back(RawLincence.ToString()); + Parameter = EndLincence & 0xFFFF; - Matricola = (int) ((EndLincence >> 16) & 0xFFFF); + if(containsLetters) + Matricola = (int)((EndLincence >> 16) & 0xFFFFFFFF); + else + Matricola = (int)((EndLincence >> 16) & 0xFFFF); if (Matricola > 0) return true; @@ -182,19 +203,30 @@ namespace Step.Utils // Controllo dell'orario per iniziare a gestire la licenza public static bool IsTimeToMangeCandies() { - //Aggiungo un T random in cui inizio il controllo: da 10 minuti a entro 4 ore dall'avvio + //Controllo solo se sono dopo le 6 e 15 + T random (CMS-Control) + if (DateTime.Now.TimeOfDay < MorningTime) + { + randomTime = 0; + return false; + } + + // Aggiungo un T random in cui inizio il controllo: da 10 minuti a entro 4 ore dall'avvio if (randomTime == 0) { Random rand = new Random(); - randomTime = rand.Next((4 * 60) - 10) + 10 ; - StartTime = StartTime.Add(new TimeSpan(0, randomTime, 0)); + randomTime = rand.Next((4 * 60) - 10) + 10; + randomTime = 1; + + StartTimePlusDelay = DateTime.Now; + StartTimePlusDelay = StartTimePlusDelay.Add(new TimeSpan(0, randomTime, 0)); } - //Controllo solo se sono dopo le 6 e 15 + T random (CMS-Control) - if (DateTime.Now.TimeOfDay < StartTime) - return false; + // If time expired return true + if (DateTime.Now > StartTimePlusDelay) + return true; return true; + return false; } @@ -327,9 +359,9 @@ namespace Step.Utils /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region PRIVATE_METHODS - private static long AlgoritmoService_Back(String val) + private static long AlgoritmoService_Back(string val) { - //Base36ToDec + // Base36ToDec long lVal = 0; int ChrPos; const int BASE = 36; @@ -343,11 +375,12 @@ namespace Step.Utils lVal = lVal + ChrPos; val = Mid(val, 2); } + return lVal; } - private static long AlgoritmoLicence_Back(String val) + private static long AlgoritmoLicence_Back(string val) { long lVal = 0; int ChrPos; @@ -366,7 +399,7 @@ namespace Step.Utils } - private static string AlgoritmoLicence(int lVal) + private static string AlgoritmoLicence(long lVal) { String sVal = ""; while(lVal >= 10) @@ -374,7 +407,10 @@ namespace Step.Utils sVal = sVal + AlgoritmoLicence(lVal/10); lVal = lVal - 10 * (lVal/10); } - sVal = sVal + Mid(BASE10, lVal + 1, 1); + + // lVal < 10 + sVal = sVal + Mid(BASE10, (int)(lVal + 1), 1); + return sVal; } @@ -391,7 +427,8 @@ namespace Step.Utils private static string Mid(string value, int index) - { + { + // Check index and string correctness if (string.IsNullOrEmpty(value)) return value; @@ -399,8 +436,8 @@ namespace Step.Utils if (index >= value.Length) return ""; - string newValue = value.Substring(index); - return newValue; + // Create substring + return value.Substring(index); } @@ -416,7 +453,7 @@ namespace Step.Utils { Object objLicence; - //KEY_NAME + // KEY_NAME if (bWrite) Registry.SetValue(KEY_NAME, KEY_VALUE_NAME, Licenza, RegistryValueKind.String);