diff --git a/SewerStammGen.Shared/SewerStammGen.Shared.csproj b/SewerStammGen.Shared/SewerStammGen.Shared.csproj index 10599db..97cf2bf 100644 --- a/SewerStammGen.Shared/SewerStammGen.Shared.csproj +++ b/SewerStammGen.Shared/SewerStammGen.Shared.csproj @@ -12,4 +12,8 @@ + + + + diff --git a/SewerStammGen.Shared/WWRuntime.cs b/SewerStammGen.Shared/WWRuntime.cs new file mode 100644 index 0000000..d5f7cee --- /dev/null +++ b/SewerStammGen.Shared/WWRuntime.cs @@ -0,0 +1,203 @@ +using CodeMeter; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SewerStammGen.Shared +{ + public class DongleNotFoundException : Exception + { + + } + + public class WWRuntime : IDisposable + { + uint FirmCode; + uint ProductCode; + + Api cmApi; + CmCredential cmCred; + CmAccess2 cmAcc; + HCMSysEntry hcmse; + CmBoxInfo cmBoxInfo; + CmBoxEntry2? BoxContent; + + uint ExportCount; + + public WWRuntime(uint ProductCode) + { + this.ProductCode = ProductCode; + FirmCode = 103086; + + cmApi = new Api(); + cmCred = new CmCredential(); + cmAcc = new CmAccess2(); + + cmAcc.Credential = cmCred; + cmAcc.Ctrl |= CmAccess.Option.UserLimit; + cmAcc.FirmCode = FirmCode; + cmAcc.ProductCode = ProductCode; + + hcmse = cmApi.CmAccess2(CmAccessOption.Local, cmAcc); + if(hcmse == null) + { + ErrorCodes2 code = cmApi.CmGetLastErrorCode2(); + throw new DongleNotFoundException(); + } + + cmBoxInfo = new CmBoxInfo(); + + CmGetBoxContentsOption boxOptions = new CmGetBoxContentsOption(); + boxOptions = CmGetBoxContentsOption.AllEntries; + CmBoxEntry2[] tmpBoxContent; + + tmpBoxContent = cmApi.CmGetBoxContents2(hcmse, boxOptions, this.FirmCode, cmBoxInfo); + + CmEntryData[] pCmBoxEntry = (CmEntryData[])cmApi.CmGetInfo(hcmse, CmGetInfoOption.EntryData); + for (int i = 0; i < pCmBoxEntry.Length; i++) + { + switch (pCmBoxEntry[i].Ctrl & 0x0ffff) + { + case (uint)CodeMeter.GlobalEntryOption.UserData: + { + + ExportCount = BitConverter.ToUInt32(pCmBoxEntry[i].Data); + } + break; + + case (uint)CodeMeter.GlobalEntryOption.ProtectedData: + // Transfer to transformed byte + uint length = pCmBoxEntry[i].DataLen; + byte[] datas = new byte[length]; + for (uint f = 0; f < length; f++) + { + datas[f] = pCmBoxEntry[i].Data[f]; + } + + + //SyncfusionKey = Encoding.ASCII.GetString(datas); + break; + } + } + + foreach (CmBoxEntry2 boxes in tmpBoxContent) + { + if (boxes.ProductCode == this.ProductCode) + { + this.BoxContent = boxes; + } + } + } + + public bool IsLicensed(byte neededMask) + { + uint DongleFeature = GetFeatureMap(); + byte DongleFeatureB = (byte)DongleFeature; + + if ((DongleFeatureB & neededMask) == neededMask) + return true; + return false; + } + + private uint GetFeatureMap() + { + if (BoxContent == null) return 0; + return BoxContent.FeatureMap; + } + + string GetDongleSerial() + { + CmBoxInfo res = (CmBoxInfo)cmApi.CmGetInfo(hcmse, CmGetInfoOption.BoxInfo); + if (null != res) + { + return res.SerialNumber.ToString(); + } + else + { + throw new Exception("Fehler beim aufrufen der Seriennummer"); + } + } + + int GetExportCount() + { + return 0; + } + + public void IncrementExportCounter() + { + ExportCount++; + writeExportCount(ExportCount); + } + + public bool CheckDongleVorhanden() + { + return hcmse != null; + } + + public void CleanDongle() + { + cmApi.CmRelease(hcmse); + } + + private void writeExportCount(uint count) + { + CmCreatePioUserData cmCreatePioUserData = new CmCreatePioUserData(); + CmProgramUpdateProductItem pcmUpdateProductItem = new CmProgramUpdateProductItem(); + CmCreateItem hcmCreateItem = new CmCreateItem(); + object pvPio = new object(); + + CmInternalEntryInfo[] cmInternalEntryData; + cmInternalEntryData = (CmInternalEntryInfo[])cmApi.CmGetInfo(hcmse, CmGetInfoOption.InternalEntryInfo); + + byte[] newCount = BitConverter.GetBytes(count); + newCount.CopyTo(cmCreatePioUserData.Data, 0); + cmCreatePioUserData.DataLen = (ushort)newCount.Length; + pvPio = cmCreatePioUserData; + + CmCreatePioProductCode cmPC = new CmCreatePioProductCode(); + cmPC.TvbCtrl = (CmCreatePioProductCode.Option)0; + cmPC.ProductCode = ProductCode; + cmPC.FirmItemReference = cmInternalEntryData[0].FirmItemReference; + cmPC.ProductItemReference = cmInternalEntryData[0].ProductItemReference; + + string? error; + + bool res = cmApi.CmCreateProductItemOption(hcmse, CmCreateProductItemOptionOption.Update | CmCreateProductItemOptionOption.ProductCode, cmPC); + if(!res) + { + error = cmApi.CmGetLastErrorText(); + return; + } + + res = cmApi.CmCreateProductItemOption(hcmse, CmCreateProductItemOptionOption.UserData | CmCreateProductItemOptionOption.Terminate, pvPio); + if(!res) + { + error = cmApi.CmGetLastErrorText(); + return; + } + + + hcmCreateItem.FirmCode = 103086; + hcmCreateItem.ProductCode = ProductCode; + + + byte[] sequence = cmApi.CmCreateSequence(hcmse, GlobalProgrammingOption.UpdateProductItem, ref hcmCreateItem, pcmUpdateProductItem); + + res = cmApi.CmProgram(hcmse, GlobalProgrammingOption.UpdateProductItem, sequence); + + if (!res) + { + error = cmApi.CmGetLastErrorText(); + return; + } + } + + public void Dispose() + { + CleanDongle(); + } + } +} diff --git a/StammGenerator/App.xaml.cs b/StammGenerator/App.xaml.cs index 9667814..b80fc9d 100644 --- a/StammGenerator/App.xaml.cs +++ b/StammGenerator/App.xaml.cs @@ -9,6 +9,9 @@ using System.Data; using System.Linq; using System.Threading.Tasks; using System.Windows; +using CodeMeter; +using SewerStammGen.Shared; +using System.Threading; namespace StammGenerator { @@ -20,7 +23,27 @@ namespace StammGenerator private readonly IHost _host; public App() { - _host = CreateHostBuilder().Build(); + new Mutex(initiallyOwned: true, "Stammdatengenerator", out bool result); + if(!result) + { + MessageBox.Show("Bitte nur 1 Instanz der Software Starten!","Fehler", MessageBoxButton.OK, MessageBoxImage.Error); + Environment.Exit(0); + } + + using (WWRuntime wWRuntime = new WWRuntime(21)) + { + if(wWRuntime.CheckDongleVorhanden()) + { + wWRuntime.CleanDongle(); + _host = CreateHostBuilder().Build(); + } + else + { + MessageBox.Show("Kein Dongle gefunden"); + Environment.Exit(0); + } + } + } static IHostBuilder CreateHostBuilder(string[]? args = null) @@ -34,10 +57,9 @@ namespace StammGenerator protected override void OnStartup(StartupEventArgs e) { + if (_host == null) return; _host.Start(); - - MainWindow? window = new MainWindow() { DataContext = _host.Services.GetRequiredService() }; window.Show(); diff --git a/StammGenerator/Commands/ProjectExportCommand.cs b/StammGenerator/Commands/ProjectExportCommand.cs index a26a905..49656c2 100644 --- a/StammGenerator/Commands/ProjectExportCommand.cs +++ b/StammGenerator/Commands/ProjectExportCommand.cs @@ -1,4 +1,5 @@ -using SewerStammGen.Shared.Contracts; +using SewerStammGen.Shared; +using SewerStammGen.Shared.Contracts; using SewerStammGen.Shared.Domain; using Shared.Contracts; using StammGenerator.Commands; @@ -46,6 +47,10 @@ namespace StammGenerator.Commands { MessageBoxResult result = MessageBox.Show(string.Format("Schnittstelle Export format: {0} ist nicht Implementiert", _selectedProjekt.ExportType), "Fehlende Implementation", MessageBoxButton.OK, MessageBoxImage.Exclamation); } + catch(DongleNotFoundException) + { + MessageBoxResult result = MessageBox.Show("Dongle nicht vorhanden"); + } } } } diff --git a/StammGenerator/StammGenerator.csproj b/StammGenerator/StammGenerator.csproj index d10907e..047f142 100644 --- a/StammGenerator/StammGenerator.csproj +++ b/StammGenerator/StammGenerator.csproj @@ -10,6 +10,7 @@ + diff --git a/WWTech_KanalSchnittstelle/Exporter/ExporterFactory.cs b/WWTech_KanalSchnittstelle/Exporter/ExporterFactory.cs index 1246986..05fb67f 100644 --- a/WWTech_KanalSchnittstelle/Exporter/ExporterFactory.cs +++ b/WWTech_KanalSchnittstelle/Exporter/ExporterFactory.cs @@ -1,4 +1,5 @@ -using SewerStammGen.Shared.Domain; +using SewerStammGen.Shared; +using SewerStammGen.Shared.Domain; using SewerStammGen.Shared.Enum; using Shared.Contracts; using System; @@ -16,6 +17,15 @@ namespace WWTech_KanalSchnittstelle.Exporter { public static IExport Export(EExportType exportType) { + using(WWRuntime wWRuntime = new WWRuntime(21)) + { + if(!wWRuntime.CheckDongleVorhanden()) + { + throw new DongleNotFoundException(); + } + wWRuntime.IncrementExportCounter(); + wWRuntime.CleanDongle(); + } switch(exportType) { case EExportType.KANDIS6: return new KANDIS60(); diff --git a/WWTech_KanalSchnittstelle/Importer/CSVImporter.cs b/WWTech_KanalSchnittstelle/Importer/CSVImporter.cs index ffac5a8..ee744ae 100644 --- a/WWTech_KanalSchnittstelle/Importer/CSVImporter.cs +++ b/WWTech_KanalSchnittstelle/Importer/CSVImporter.cs @@ -100,7 +100,7 @@ namespace WWTech_KanalSchnittstelle.Importer schacht.SohlHoehe = parseKoordinate(parsed[3]); } - if (parsed.Length >= 4) + if (parsed.Length > 4) { schacht.Entwaesserung = entwaesserungKennung[parsed[4]]; schacht.SchachtType = schachtKennung[parsed[4]];