diff --git a/Libs/CMS_CORE_Library.dll b/Libs/CMS_CORE_Library.dll index dff8f077..2bce764e 100644 Binary files a/Libs/CMS_CORE_Library.dll and b/Libs/CMS_CORE_Library.dll differ diff --git a/Step.Model/Constants.cs b/Step.Model/Constants.cs index 8bb2b3de..185ca1cd 100644 --- a/Step.Model/Constants.cs +++ b/Step.Model/Constants.cs @@ -173,6 +173,7 @@ namespace Step.Model public static string WEBSITE_DIRECTORY = Path.Combine(BASE_PATH, "..", "wwwroot"); public static string LANGUAGE_PACK_DIRECTORY = BASE_PATH + "\\languages\\"; public static string LANGUAGE_SCHEMA_PATH = BASE_PATH + "\\LanguageValidator.xsd"; + public static string CANDY_DUMMYFILE_PATH = BASE_PATH + "\\dll.dll"; // MVVM Messages to server UI public const string SEND_STOP_SERVER = "STOP_SERVER"; diff --git a/Step.NC/NcHandler.cs b/Step.NC/NcHandler.cs index 8cac606e..333ece9e 100644 --- a/Step.NC/NcHandler.cs +++ b/Step.NC/NcHandler.cs @@ -14,6 +14,7 @@ using Step.Utils; using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; using static CMS_CORE_Library.Models.DataStructures; using static Step.Config.ServerConfig; @@ -1167,12 +1168,18 @@ namespace Step.NC CmsError cmsError; int NcCandy = 0; int NCMatr, PCMatr; - long NCParam, PCParam,newParam; + long NCParam, PCParam; + long NCLic, PcPLic; bool bNC_OK, bPC_OK; bool bNC_VALID, bPC_VALID; - String strMachNumber="0"; + String strMachNumber = "0"; int machNumber; - bool bRewrite=false; + bool bRewrite, bNcCandy=false, bNewCandy = false; + DateTime newDate; + + //Check if i have to Manage it + if(!CandiesController.isTimeToMangeCandies()) + return NO_ERROR; //Read Machine ID @@ -1185,67 +1192,48 @@ namespace Step.NC cmsError = numericalControl.PLC_RCandy(ref NcCandy); if (cmsError.IsError()) return cmsError; - bNC_OK = CandiesController.getDataFromLincense(NcCandy, out NCMatr, out NCParam); - bNC_VALID = machNumber == NCMatr; + bNC_OK = CandiesController.getDataFromLincense(NcCandy, out NCLic, out NCMatr, out NCParam); + bNC_VALID = machNumber == NCMatr && bNC_OK; //Read Data from PC - bPC_OK = CandiesController.getPCLincense(out PCMatr, out PCParam); - bPC_VALID = machNumber == PCMatr; + bPC_OK = CandiesController.getPCLincense(out PcPLic, out PCMatr, out PCParam); + bPC_VALID = machNumber == PCMatr && bPC_OK; - if(bNC_VALID & bPC_VALID) + //Elaborate Licence and write it + CandiesController.ElaborateLincense(bPC_VALID, bNC_VALID, PCParam, NCParam, PcPLic, NCLic, out newDate, out bRewrite); + if (bRewrite) + WriteCandy(newDate, machNumber); + + //Read Expired Lincense Bit on NC + cmsError = numericalControl.PLC_RExpiredCandy(ref bNcCandy); + if (cmsError.IsError()) + return cmsError; + + //Manage Expired Lincense Bit + if(CandiesController.ElaborateExpiredBit(newDate, bNcCandy, out bNewCandy)) { - if(PCParam == NCParam) - { - newParam = PCParam + Utils.CandiesController.BASE_TIME; - bRewrite = false; - } - else if(PCParam < NCParam) - { - newParam = PCParam + Utils.CandiesController.BASE_TIME; - bRewrite = true; - } - else - { - newParam = NCParam + Utils.CandiesController.BASE_TIME; - bRewrite = true; - } - } - else if (bNC_VALID) - { - newParam = NCParam + Utils.CandiesController.BASE_TIME; - bRewrite = true; - } - else if (bPC_VALID) - { - newParam = PCParam + Utils.CandiesController.BASE_TIME; - bRewrite = true; - } - else - { - // tutto a zero? immagino sia la prima accensione - if () + //Write Expired Lincense Bit on NC + cmsError = numericalControl.PLC_WExpiredCandy(bNewCandy); + if (cmsError.IsError()) + return cmsError; } - Else - 'tutto a zero? immagino sia la prima accensione - If nLicReg_X = 0 And nLicCnc_X = 0 Then - nData = BASETIME - bRewrite = True - - 'ho creato il file? immagino sia la prima accensione con memorie cn sporche - ElseIf nLicReg_X = 0 And bFileCreated Then - bFileCreated = False - nData = BASETIME - bRewrite = True - - Else - 'a questo punto blocco.. ipotizzo una forzatura - nData = 0 - bRewrite = False - End If - End If + return NO_ERROR; } + public void WriteCandy(DateTime value,int machNumber) + { + long nDays; + int Lic=0; + nDays = (value.Ticks / TimeSpan.TicksPerDay); + + //Imposto nel registro + CandiesController.setPCLincense(machNumber,nDays); + + //Imposto nel CN + Lic = Int32.Parse(CandiesController.setLincenseFromData(machNumber, nDays)); + numericalControl.PLC_WCandy(Lic); + } //DTOScadaModel scada = new DTOScadaModel //{ diff --git a/Step.UI/PasswordForm.cs b/Step.UI/PasswordForm.cs index 9b9a5a1b..d587260f 100644 --- a/Step.UI/PasswordForm.cs +++ b/Step.UI/PasswordForm.cs @@ -1,4 +1,5 @@ -using System; +using Step.NC; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,7 +13,8 @@ namespace Step.UI { public partial class PasswordForm : Form { - public String Matricola; + private NcHandler Nc; + private String Matricola; private DateTime Licenza; const long BASETIME = 730119; // 1/1/2000 @@ -21,10 +23,11 @@ namespace Step.UI InitializeComponent(); } - public void setMatricola(String matr) + public void setNcData(String matr, NcHandler Nch) { this.Matricola = matr; this.Text = "Password - " + this.Matricola; + this.Nc = Nch; } private void Cancel_Pwd_Button_Click(object sender, EventArgs e) @@ -41,6 +44,7 @@ namespace Step.UI private void PasswordForm_Load(object sender, EventArgs e) { TextBox_Password.Text = ""; + ErrorProvider1.Clear(); this.ActiveControl = TextBox_Password; } @@ -76,14 +80,22 @@ namespace Step.UI //SET Licenza Temporanea case 2: - Licenza = new DateTime(Parameter * TimeSpan.TicksPerDay); - bValid = false; + if(Nc != null) + { + Licenza = new DateTime(Parameter * TimeSpan.TicksPerDay); + Nc.WriteCandy(Licenza, Int32.Parse(Matricola)); + bValid = false; + } break; //SET Licenza Illimitata case 3: - Licenza = new DateTime(0); - bValid = false; + if (Nc != null) + { + Licenza = new DateTime(0); + Nc.WriteCandy(Licenza, Int32.Parse(Matricola)); + bValid = false; + } break; } diff --git a/Step.UI/ServerControlWindow.Designer.cs b/Step.UI/ServerControlWindow.Designer.cs index 9fafb368..cd55739d 100644 --- a/Step.UI/ServerControlWindow.Designer.cs +++ b/Step.UI/ServerControlWindow.Designer.cs @@ -55,9 +55,17 @@ namespace Step.UI this.LISTThreadStatus = new System.Windows.Forms.ListView(); this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.menuToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.serviceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.passwordToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); + this.goBackToStandardModeToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); + this.resetSpindleHoursToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.NotifyIconMenu.SuspendLayout(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // StepNotifyIcon @@ -130,10 +138,10 @@ namespace Step.UI | System.Windows.Forms.AnchorStyles.Right))); this.tableLayoutPanel1.SetColumnSpan(this.TXTstatus, 2); this.TXTstatus.Enabled = false; - this.TXTstatus.Location = new System.Drawing.Point(3, 416); + this.TXTstatus.Location = new System.Drawing.Point(3, 435); this.TXTstatus.Name = "TXTstatus"; this.TXTstatus.ReadOnly = true; - this.TXTstatus.Size = new System.Drawing.Size(435, 20); + this.TXTstatus.Size = new System.Drawing.Size(438, 20); this.TXTstatus.TabIndex = 8; this.TXTstatus.Text = "..."; // @@ -150,7 +158,7 @@ namespace Step.UI this.tableLayoutPanel1.Controls.Add(this.TXTstatus, 0, 3); this.tableLayoutPanel1.Controls.Add(this.panel1, 0, 1); this.tableLayoutPanel1.Controls.Add(this.LISTThreadStatus, 0, 0); - this.tableLayoutPanel1.Location = new System.Drawing.Point(12, 12); + this.tableLayoutPanel1.Location = new System.Drawing.Point(12, 27); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 3; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); @@ -158,7 +166,7 @@ namespace Step.UI this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 54F)); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 25F)); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); - this.tableLayoutPanel1.Size = new System.Drawing.Size(441, 438); + this.tableLayoutPanel1.Size = new System.Drawing.Size(444, 457); this.tableLayoutPanel1.TabIndex = 58; // // stopServerButton @@ -167,9 +175,9 @@ namespace Step.UI | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.stopServerButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; - this.stopServerButton.Location = new System.Drawing.Point(223, 362); + this.stopServerButton.Location = new System.Drawing.Point(225, 381); this.stopServerButton.Name = "stopServerButton"; - this.stopServerButton.Size = new System.Drawing.Size(215, 48); + this.stopServerButton.Size = new System.Drawing.Size(216, 48); this.stopServerButton.TabIndex = 3; this.stopServerButton.TabStop = false; this.stopServerButton.Text = "Close Active"; @@ -181,9 +189,9 @@ namespace Step.UI | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.openUiButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; - this.openUiButton.Location = new System.Drawing.Point(3, 362); + this.openUiButton.Location = new System.Drawing.Point(3, 381); this.openUiButton.Name = "openUiButton"; - this.openUiButton.Size = new System.Drawing.Size(214, 48); + this.openUiButton.Size = new System.Drawing.Size(216, 48); this.openUiButton.TabIndex = 58; this.openUiButton.TabStop = false; this.openUiButton.Text = "Open CMS-Active Client"; @@ -201,9 +209,9 @@ namespace Step.UI this.panel1.Controls.Add(this.CHNcConnected); this.panel1.Controls.Add(this.label2); this.panel1.Controls.Add(this.TXTMachId); - this.panel1.Location = new System.Drawing.Point(3, 273); + this.panel1.Location = new System.Drawing.Point(3, 292); this.panel1.Name = "panel1"; - this.panel1.Size = new System.Drawing.Size(435, 83); + this.panel1.Size = new System.Drawing.Size(438, 83); this.panel1.TabIndex = 9; // // label3 @@ -282,7 +290,7 @@ namespace Step.UI this.LISTThreadStatus.Dock = System.Windows.Forms.DockStyle.Fill; this.LISTThreadStatus.Location = new System.Drawing.Point(3, 3); this.LISTThreadStatus.Name = "LISTThreadStatus"; - this.LISTThreadStatus.Size = new System.Drawing.Size(435, 264); + this.LISTThreadStatus.Size = new System.Drawing.Size(438, 283); this.LISTThreadStatus.TabIndex = 59; this.LISTThreadStatus.UseCompatibleStateImageBehavior = false; this.LISTThreadStatus.View = System.Windows.Forms.View.Details; @@ -297,14 +305,73 @@ namespace Step.UI this.columnHeader2.Text = "Time"; this.columnHeader2.Width = 78; // + // menuStrip1 + // + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.menuToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(468, 24); + this.menuStrip1.TabIndex = 59; + this.menuStrip1.Text = "menuStrip1"; + // + // menuToolStripMenuItem + // + this.menuToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.serviceToolStripMenuItem}); + this.menuToolStripMenuItem.Name = "menuToolStripMenuItem"; + this.menuToolStripMenuItem.Size = new System.Drawing.Size(50, 20); + this.menuToolStripMenuItem.Text = "Menu"; + // + // serviceToolStripMenuItem + // + this.serviceToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.passwordToolStripMenuItem1, + this.goBackToStandardModeToolStripMenuItem1, + this.toolStripSeparator1, + this.resetSpindleHoursToolStripMenuItem1}); + this.serviceToolStripMenuItem.Name = "serviceToolStripMenuItem"; + this.serviceToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.serviceToolStripMenuItem.Text = "Service"; + // + // passwordToolStripMenuItem1 + // + this.passwordToolStripMenuItem1.Name = "passwordToolStripMenuItem1"; + this.passwordToolStripMenuItem1.Size = new System.Drawing.Size(214, 22); + this.passwordToolStripMenuItem1.Text = "Password"; + this.passwordToolStripMenuItem1.Click += new System.EventHandler(this.passwordToolStripMenuItem1_Click); + // + // toolStripSeparator1 + // + this.toolStripSeparator1.Name = "toolStripSeparator1"; + this.toolStripSeparator1.Size = new System.Drawing.Size(211, 6); + this.toolStripSeparator1.Visible = false; + // + // goBackToStandardModeToolStripMenuItem1 + // + this.goBackToStandardModeToolStripMenuItem1.Name = "goBackToStandardModeToolStripMenuItem1"; + this.goBackToStandardModeToolStripMenuItem1.Size = new System.Drawing.Size(214, 22); + this.goBackToStandardModeToolStripMenuItem1.Text = "Go back to standard mode"; + this.goBackToStandardModeToolStripMenuItem1.Visible = false; + this.goBackToStandardModeToolStripMenuItem1.Click += new System.EventHandler(this.goBackToStandardModeToolStripMenuItem1_Click); + // + // resetSpindleHoursToolStripMenuItem1 + // + this.resetSpindleHoursToolStripMenuItem1.Name = "resetSpindleHoursToolStripMenuItem1"; + this.resetSpindleHoursToolStripMenuItem1.Size = new System.Drawing.Size(214, 22); + this.resetSpindleHoursToolStripMenuItem1.Text = "Reset Spindle Hours"; + this.resetSpindleHoursToolStripMenuItem1.Visible = false; + // // ServerControlWindow // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(465, 462); + this.ClientSize = new System.Drawing.Size(468, 496); + this.Controls.Add(this.menuStrip1); this.Controls.Add(this.tableLayoutPanel1); this.ForeColor = System.Drawing.SystemColors.ControlText; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.MainMenuStrip = this.menuStrip1; this.Margin = new System.Windows.Forms.Padding(2); this.MaximizeBox = false; this.MinimizeBox = false; @@ -317,7 +384,10 @@ namespace Step.UI this.tableLayoutPanel1.PerformLayout(); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -345,5 +415,12 @@ namespace Step.UI private ToolStripMenuItem goBackToStandardModeToolStripMenuItem; private ToolStripSeparator toolStripSeparator; private ToolStripMenuItem resetSpindleHoursToolStripMenuItem; + private MenuStrip menuStrip1; + private ToolStripMenuItem menuToolStripMenuItem; + private ToolStripMenuItem serviceToolStripMenuItem; + private ToolStripMenuItem passwordToolStripMenuItem1; + private ToolStripMenuItem goBackToStandardModeToolStripMenuItem1; + private ToolStripSeparator toolStripSeparator1; + private ToolStripMenuItem resetSpindleHoursToolStripMenuItem1; } } \ No newline at end of file diff --git a/Step.UI/ServerControlWindow.cs b/Step.UI/ServerControlWindow.cs index 79b7be62..4389a842 100644 --- a/Step.UI/ServerControlWindow.cs +++ b/Step.UI/ServerControlWindow.cs @@ -16,7 +16,9 @@ using static Step.Model.Constants; namespace Step.UI { public partial class ServerControlWindow : Form - { + { + private DTONcGenericDataModel model; + private NcHandler nc; private bool ncStatus = false; private bool isUpdatingThreads = false; private List MVVMListeners; @@ -232,18 +234,17 @@ namespace Step.UI } if (ncStatus) - { - NcHandler nc = new NcHandler(); - DTONcGenericDataModel model; - nc.Connect(); - nc.GetNcGenericData(out model); + { if (!IsDisposed) Invoke((MethodInvoker)delegate () { + nc = new NcHandler(); + nc.Connect(); + nc.GetNcGenericData(out model); StepNotifyIcon.Icon = Properties.Resources.CMS_Icon; TXTMachId.Text = model.CmsMachineIdNumber; CHNcConnected.Checked = true; - PswForm.setMatricola(model.CmsMachineIdNumber); + PswForm.setNcData(model.CmsMachineIdNumber,nc); passwordToolStripMenuItem.Enabled = true; }); } @@ -295,30 +296,54 @@ namespace Step.UI return false; } - private void adminMachineToolStripMenuItem_Click(object sender, EventArgs e) + private void ShowPasswordDialog() { - } - - private void passwordToolStripMenuItem_Click(object sender, EventArgs e) - { - DialogResult dr = PswForm.ShowDialog(); + DialogResult dr = PswForm.ShowDialog(); if (dr == DialogResult.OK) { resetSpindleHoursToolStripMenuItem.Visible = true; + resetSpindleHoursToolStripMenuItem1.Visible = true; goBackToStandardModeToolStripMenuItem.Visible = true; + goBackToStandardModeToolStripMenuItem1.Visible = true; toolStripSeparator.Visible = true; + toolStripSeparator1.Visible = true; passwordToolStripMenuItem.Enabled = false; + passwordToolStripMenuItem1.Enabled = false; MessageBox.Show("Machine Unlocked for administrator use", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } + private void backStdMode() + { + resetSpindleHoursToolStripMenuItem.Visible = false; + resetSpindleHoursToolStripMenuItem1.Visible = false; + goBackToStandardModeToolStripMenuItem.Visible = false; + goBackToStandardModeToolStripMenuItem1.Visible = false; + toolStripSeparator.Visible = false; + toolStripSeparator1.Visible = false; + passwordToolStripMenuItem.Enabled = true; + passwordToolStripMenuItem1.Enabled = true; + MessageBox.Show("Machine Locked for standard use", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); + } + + + private void passwordToolStripMenuItem_Click(object sender, EventArgs e) + { + ShowPasswordDialog(); + } private void goBackToStandardModeToolStripMenuItem_Click(object sender, EventArgs e) { - resetSpindleHoursToolStripMenuItem.Visible = false; - goBackToStandardModeToolStripMenuItem.Visible = false; - toolStripSeparator.Visible = false; - passwordToolStripMenuItem.Enabled = true; - MessageBox.Show("Machine Locked for standard use", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); + backStdMode(); + } + + private void passwordToolStripMenuItem1_Click(object sender, EventArgs e) + { + ShowPasswordDialog(); + } + + private void goBackToStandardModeToolStripMenuItem1_Click(object sender, EventArgs e) + { + backStdMode(); } } } \ No newline at end of file diff --git a/Step.UI/ServerControlWindow.resx b/Step.UI/ServerControlWindow.resx index f77f97c4..44b4e675 100644 --- a/Step.UI/ServerControlWindow.resx +++ b/Step.UI/ServerControlWindow.resx @@ -320,6 +320,9 @@ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAEAAMADAAD//wAA + + 290, 17 + AAABAAMAMDAAAAAAGACoHAAANgAAACAgAAAAABgAqAwAAN4cAAAQEAAAAAAYAGgDAACGKQAAKAAAADAA diff --git a/Step.Utils/CandiesController.cs b/Step.Utils/CandiesController.cs index 8f04f20d..e2f95699 100644 --- a/Step.Utils/CandiesController.cs +++ b/Step.Utils/CandiesController.cs @@ -1,6 +1,8 @@ using Microsoft.Win32; +using Step.Model; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -15,6 +17,8 @@ namespace Step.Utils private const String KEY_NAME = "HKEY_LOCAL_MACHINE\\SOFTWARE\\CMS\\ACTIVE"; 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); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -56,18 +60,19 @@ namespace Step.Utils // Lettura della licenza da registro. Ereditata da CMS-Control - public static bool getPCLincense(out int Matricola, out long Parameter) + public static bool getPCLincense(out long EndLincence, out int Matricola, out long Parameter) { string szLicReg_X = String.Empty; - int nLicReg_X = 0; + int Lincence = 0; Matricola = 0; Parameter = 0; + EndLincence = 0; //Leggo nel Registro Licenza_Registry_RW(false, ref szLicReg_X); - nLicReg_X = Int32.Parse(szLicReg_X); - return getDataFromLincense(nLicReg_X, out Matricola, out Parameter); + Lincence = Int32.Parse(szLicReg_X); + return getDataFromLincense(Lincence, out EndLincence, out Matricola, out Parameter); } @@ -75,33 +80,41 @@ namespace Step.Utils // Scrittura della licenza nel registro. Ereditata da CMS-Control public static void setPCLincense(int Matricola, long Parameter) + { + string Licenza = setLincenseFromData(Matricola, Parameter); + Licenza_Registry_RW(true, ref Licenza); + } + + //Gestisce la creazione della licenza, da scrivere poi nel CN + public static string setLincenseFromData(int Matricola, long Parameter) { int nLic; int days; if (Parameter > BASE_TIME) - days = (int) (Parameter - BASE_TIME); + days = (int)(Parameter - BASE_TIME); else if (Parameter > Int32.MaxValue) days = Int32.MaxValue; else days = 0; nLic = (Matricola << 16) + days; - string Licenza = AlgoritmoLicence(nLic); - Licenza_Registry_RW(true, ref Licenza); + return AlgoritmoLicence(nLic); } - public static bool getDataFromLincense(int Lincence, out int Matricola,out long Parameter) + // 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) { Matricola = 0; Parameter = 0; + EndLincence = 0; - if (Lincence == 0) + if (RawLincence == 0) return false; - long szLic = AlgoritmoLicence_Back(Lincence.ToString()); - Parameter = szLic & 0xFFFF; - Matricola = (int) ((szLic >> 16) & 0xFFFF); + EndLincence = AlgoritmoLicence_Back(RawLincence.ToString()); + Parameter = EndLincence & 0xFFFF; + Matricola = (int) ((EndLincence >> 16) & 0xFFFF); if (Matricola > 0) return true; @@ -109,6 +122,140 @@ namespace Step.Utils return false; } + //Elabora la licenza e decide se va riscritta + public static void ElaborateLincense(bool bPC_VALID, bool bNC_VALID, long PCParam, long NCParam, long PcPLic, long NCLic, out DateTime newDate, out bool toRewrite) + { + long newParam=0; + + if (bNC_VALID & bPC_VALID) + { + if (PCParam == NCParam) + { + newParam = PCParam + BASE_TIME; + toRewrite = false; + } + else if (PCParam < NCParam) + { + newParam = PCParam + BASE_TIME; + toRewrite = true; + } + else + { + newParam = NCParam + BASE_TIME; + toRewrite = true; + } + } + else if (bNC_VALID) + { + newParam = NCParam + BASE_TIME; + toRewrite = true; + } + else if (bPC_VALID) + { + newParam = PCParam + BASE_TIME; + toRewrite = true; + } + else + { + // tutto a zero? immagino sia la prima accensione + if (PcPLic == 0 && NCLic == 0) + { + newParam = BASE_TIME; + toRewrite = true; + } + //ho creato il file? immagino sia la prima accensione con memorie cn sporche + else if (NCLic == 0 && File.Exists(Constants.CANDY_DUMMYFILE_PATH)) + { + newParam = BASE_TIME; + toRewrite = true; + } + //a questo punto blocco.. ipotizzo una forzatura + else + { + newParam = 0; + toRewrite = true; + } + } + newDate = new DateTime(newParam * TimeSpan.TicksPerDay); + } + + //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 + if (randomTime == 0) + { + Random rand = new Random(); + randomTime = rand.Next((4 * 60) - 10) + 10 ; + StartTime = StartTime.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; + + return true; + } + + + //Controllo dell'orario per iniziare a controllare + public static bool ElaborateExpiredBit(DateTime date,bool NcExpiredBit, out bool NewExpiredBit) + { + DateTime LastWriteTime; + NewExpiredBit = false; + + //Manage Dummy file + if (File.Exists(Constants.CANDY_DUMMYFILE_PATH)) + { + if (File.GetLastWriteTime(Constants.CANDY_DUMMYFILE_PATH) < DateTime.Now) + File.SetLastWriteTime(Constants.CANDY_DUMMYFILE_PATH, DateTime.Now); + } + else + File.Create(Constants.CANDY_DUMMYFILE_PATH); + LastWriteTime = File.GetLastWriteTime(Constants.CANDY_DUMMYFILE_PATH); + + + //Manage Lincese Expired Bit + //1. If unlimited + if(date.Ticks / TimeSpan.TicksPerDay == BASE_TIME) + { + if (NcExpiredBit) + { + NewExpiredBit = false; + return true; + } + } + //2. If limited + else + { + DateTime SevenDaysDate = DateTime.Now + new TimeSpan(7 * TimeSpan.TicksPerDay); + + //2.1 today > Expired Lincense Date + //2.2 Expired File Date > Expired Lincense Date + //2.3 today + 1 Week > Expired File Date + if (date < DateTime.Now || date < LastWriteTime || SevenDaysDate < LastWriteTime) + { + if (!NcExpiredBit) + { + NewExpiredBit = true; + return true; + } + } + else + { + if (NcExpiredBit) + { + NewExpiredBit = false; + return true; + } + } + } + + //Nothing to write + return false; + + } + #endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////