Files
cms_thermo_active/Step.Utils/CandiesController.cs
T
Nicola Carminati d8a5d2de6b Fix features
2019-04-01 12:57:44 +02:00

366 lines
12 KiB
C#

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;
namespace Step.Utils
{
public static class CandiesController
{
private const String BASE36 = "4K6IJZL1M27PQRSNOVWXTU80FGH39ABCDEY5";
private const String BASE10 = "0431786295";
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);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region PUBLIC_METHODS
// Decodifica password esportando i dati. Ereditata da CMS-Control
public static bool PasswordDecode(String Password, out int Matricola, out int Command, out long Parameter)
{
String szVal;
int nChk = 0;
Matricola = 0;
Command = 0;
Parameter = 0;
try
{
szVal = AlgoritmoService_Back(Password).ToString();
for (int i = 0; i <= szVal.Length - 1 - 1; i++)
{
nChk += (int)char.GetNumericValue(szVal[i]);
}
//inserita password con checksum errato
if (szVal.Last() != nChk.ToString().Last())
return false;
Matricola = Int16.Parse(szVal.Substring(1, 5));
Command = Int16.Parse(szVal.Substring(0, 1));
Parameter = long.Parse(szVal.Substring(6, szVal.Length - 6 - 1));
}
catch (Exception e)
{
return false;
}
return true;
}
// Lettura della licenza da registro. Ereditata da CMS-Control
public static bool getPCLincense(out long EndLincence, out int Matricola, out long Parameter)
{
string szLicReg_X = String.Empty;
int Lincence = 0;
Matricola = 0;
Parameter = 0;
EndLincence = 0;
//Leggo nel Registro
Licenza_Registry_RW(false, ref szLicReg_X);
Lincence = Int32.Parse(szLicReg_X);
return getDataFromLincense(Lincence, out EndLincence, out Matricola, out Parameter);
}
// 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);
else if (Parameter > Int32.MaxValue)
days = Int32.MaxValue;
else
days = 0;
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)
{
Matricola = 0;
Parameter = 0;
EndLincence = 0;
if (RawLincence == 0)
return false;
EndLincence = AlgoritmoLicence_Back(RawLincence.ToString());
Parameter = EndLincence & 0xFFFF;
Matricola = (int) ((EndLincence >> 16) & 0xFFFF);
if (Matricola > 0)
return true;
else
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region PRIVATE_METHODS
private static long AlgoritmoService_Back(String val)
{
//Base36ToDec
long lVal = 0;
int ChrPos;
const int BASE = 36;
val = val.ToUpper();
while (val.Length > 0)
{
lVal = lVal * BASE;
ChrPos = BASE36.IndexOf(Left(val, 1));
if (ChrPos >= 0)
lVal = lVal + ChrPos;
val = Mid(val, 2);
}
return lVal;
}
private static long AlgoritmoLicence_Back(String val)
{
long lVal = 0;
int ChrPos;
const int BASE = 10;
val = val.ToUpper();
while (val.Length > 0)
{
lVal = lVal * BASE;
ChrPos = BASE10.IndexOf(Left(val, 1));
if (ChrPos >= 0)
lVal = lVal + ChrPos;
val = Mid(val, 2);
}
return lVal;
}
private static string AlgoritmoLicence(int lVal)
{
String sVal = "";
while(lVal >= 10)
{
sVal = sVal + AlgoritmoLicence(lVal/10);
lVal = lVal - 10 * (lVal/10);
}
sVal = sVal + Mid(BASE10, lVal + 1, 1);
return sVal;
}
private static string Left(string value, int size)
{
if (string.IsNullOrEmpty(value))
return value;
size = (size >= value.Length ? value.Length : size);
string newValue = value.Substring(0, size);
return newValue;
}
private static string Mid(string value, int index)
{
if (string.IsNullOrEmpty(value))
return value;
index = index - 1;
if (index >= value.Length)
return "";
string newValue = value.Substring(index);
return newValue;
}
private static string Mid(string value, int index,int lenght)
{
value = Mid(value, index);
if (lenght <= value.Length)
value = value.Substring(0, lenght);
return value;
}
private static void Licenza_Registry_RW(bool bWrite, ref string Licenza)
{
Object objLicence;
//KEY_NAME
if (bWrite)
Registry.SetValue(KEY_NAME, KEY_VALUE_NAME, Licenza, RegistryValueKind.String);
objLicence = Registry.GetValue(KEY_NAME, KEY_VALUE_NAME,0);
if (objLicence == null) objLicence = 0;
Licenza = objLicence.ToString();
}
#endregion
}
}