/// <summary> /// Retrieve a Package for usage. This method will create the Package if it does not exist yet or open it if it exists /// </summary> /// <param name="packagePath">Path to the Package to initialize</param> private async Task <ZipArchive> RetrievePackageAsync(string packagePath) { var semaphore = LockingDictionary.GetOrAdd(packagePath, new SemaphoreSlim(1)); try { await semaphore.WaitAsync(); if (PackageDictionary.ContainsKey(packagePath)) { return(PackageDictionary[packagePath]); } if (!File.Exists(packagePath)) { var createdZip = new ZipArchive(File.Open(packagePath, FileMode.OpenOrCreate), ZipArchiveMode.Create); createdZip.Dispose(); } var tmpPackage = new ZipArchive(File.Open(packagePath, FileMode.OpenOrCreate), ZipArchiveMode.Update); PackageDictionary.Add(packagePath, tmpPackage); return(tmpPackage); } finally { semaphore.Release(); } }
public PackageManager() { this.RemotePackages = new PackageDictionary(); this.InstalledPackages = new PackageDictionary(); this.QueuedPackages = new PackageDictionary(); this.LoadPackageDirectory(PackageManager.DIRECTORY_PACKAGES, this.InstalledPackages); this.LoadPackageDirectory(PackageManager.DIRECTORY_UPDATES_PACKAGES, this.QueuedPackages); }
/// <summary> /// Flush (Dispose) the <see cref="ZipArchive"/> in <see cref="PackageDictionary"/> and reconnect to it /// to force data to be persisted to disk after an operation. /// Flush will only occur if the pacakge is in the <see cref="PackageDictionary"/> and <see cref="AutoFlush"/> is set to true. /// <remarks> /// This method does not lock use the Semaphore from the <see cref="LockingDictionary"/> so it must be called when the archive is already locked /// </remarks> /// </summary> /// <param name="packagePath">Package path used to retrieve it from the Dictionary</param> private void AutoFlushPackage(string packagePath) { if (!AutoFlush || !PackageDictionary.ContainsKey(packagePath)) { return; } PackageDictionary[packagePath].Dispose(); PackageDictionary[packagePath] = new ZipArchive(File.Open(packagePath, FileMode.OpenOrCreate), ZipArchiveMode.Update); }
private void LoadPackageDirectory(string path, PackageDictionary target) { try { if (Directory.Exists(path) == true) { foreach (string packagePath in Directory.GetFiles(path, "*.xml")) { try { target.AddPackage(new Package(File.ReadAllText(packagePath))); } catch (Exception) { } } } } catch (Exception) { } }
protected override void PopulateDictionary(PackageDictionary <CommandCode> d) { Action <ServicePacket <CommandCode> > updateDevice = p => ((IUpdateDevice)p).UpdateDevice(); //async error Add <logInternalError>(); Add <logInvalidSystemState>(); Add <logVacuumCrash>(); Add <logTurboPumpFailure>(updateDevice); Add <logPowerFail>(); Add <logInvalidVacuumState>(); Add <logAdcPlaceIonSrc>(); Add <logAdcPlaceScanv>(); Add <logAdcPlaceControlm>(); //async Add <requestCounts>(AutoSend <getCounts>); Add <confirmVacuumReady>(updateDevice, p => OnSystemReady()); Add <confirmShutdowned>(p => OnSystemDown(false)); Add <SystemReseted>(p => { OnAsyncReplyReceived("Система переинициализировалась"); OnSystemDown(true); }); Add <confirmHighVoltageOff>(p => OnOperationBlock(true)); Add <confirmHighVoltageOn>(p => { OnOperationBlock(false); toSend.Enqueue(new sendSVoltage(0));//Set ScanVoltage to low limit SetSettings(); //toSend.Enqueue(new sendIVoltage()); }); //sync error Add <logInvalidCommand>(); Add <logInvalidChecksum>(); Add <logInvalidPacket>(); Add <logInvalidLength>(); Add <logInvalidData>(); Add <logInvalidState>(); //sync Add <updateState>(updateDevice); Add <updateStatus>(updateDevice, p => { if (onTheFly) { // waiting for fake counts reply OnFirstStatus(() => toSend.Enqueue(new getCounts())); onTheFly = false; } }); Add <confirmShutdown>(p => OnOperationToggle(false)); Add <confirmInit>(p => OnOperationToggle(true)); // settings sequence Add <confirmIVoltage>(AutoSend <sendCapacitorVoltage>); Add <confirmCP>(AutoSend <enableHCurrent>); Add <confirmHECurrent>(AutoSend <sendECurrent>); Add <confirmECurrent>(AutoSend <sendHCurrent>); Add <confirmHCurrent>(AutoSend <sendF1Voltage>); Add <confirmF1Voltage>(AutoSend <sendF2Voltage>); Add <confirmF2Voltage>(p => OnMeasurePreconfigured()); Add <confirmSVoltage>(p => OnMeasureSend((t1, t2) => toSend.Enqueue(new sendMeasure(t1, t2)))); Add <confirmMeasure>(); Add <updateCounts>(updateDevice, p => OnMeasureDone()); Add <confirmHighVoltage>(); Add <updateTurboPumpStatus>(updateDevice); Add <confirmForvacuumLevel>(); }
protected override void PopulateDictionary(PackageDictionary <CommandCode> d) { //async error Add <LAMCriticalError>(); Add <LAMInternalError>(); //async Add <RTCMeasureEndLAM>(AutoSend <CountsRequest>); Add <SPIConfDoneLAM>(updateDevice, p => { OnOperationBlock(false);//maybe duplicates event after update device SetSettings(); //AutoSend<GetHeaterVoltageRequest>(p);//fake request to workaround SPI misfunction (last in queue) //set ScanVoltage to minimal limit //toSend.Enqueue(new ParentScanVoltageSetRequest(0)); //toSend.Enqueue(new MainScanVoltageSetRequest(0)); //toSend.Enqueue(new CapacitorVoltageSetRequest(0)); SetMeasureStep(0);//set ScanVoltage to low limit }); Add <HVEnabledLAM>(updateDevice, p => OnSystemReady()); Add <HVDisabledLAM>(updateDevice, p => OnSystemDown(true)); //sync error Add <SyncErrorReply>(); //add(CommandCode.Service_Message, p => { // if (p is LAMEvent && (p as LAMEvent).number == 21) { // OnSystemReady(); // } //}); //sync Add <CPUStatusReply>(/*updateDevice*/); Add <HighVoltagePermittedStatusReply>(updateDevice /*, p => OnSystemReady()*/); Add <OperationBlockReply>(); Add <OperationBlockOnReply>(updateDevice, p => OnOperationBlock(true)); Add <OperationBlockOffReply>(updateDevice); // TODO: proper command detection! Add <TICStatusReply>(updateDevice, p => { if (onTheFly) { OnFirstStatus(() => { }); onTheFly = false; } }); Add <VacuumStatusReply>(updateDevice, p => { if (onTheFly) { OnFirstStatus(() => { }); onTheFly = false; } }); Add <Valve1Reply>(updateDevice); Add <Valve2Reply>(updateDevice); Add <Valve3Reply>(updateDevice); Add <MicroPumpReply>(updateDevice); Add <AllVoltagesReply>(updateDevice); Add <IonSourceSetReply>(); Add <DetectorSetReply>(); Add <InletSetReply>(); Add <GetD1VoltageReply>(AutoSend <GetD2VoltageRequest>); Add <GetD2VoltageReply>(AutoSend <GetD3VoltageRequest>); Add <GetD3VoltageReply>(AutoSend <GetIonizationVoltageRequest>); Add <GetIonizationVoltageReply>(AutoSend <GetEmissionCurrentRequest>); Add <GetEmissionCurrentReply>(AutoSend <GetF1VoltageRequest>); Add <GetF1VoltageReply>(AutoSend <GetF2VoltageRequest>); Add <GetF2VoltageReply>(AutoSend <GetInletVoltageRequest>); Add <GetInletVoltageReply>(AutoSend <GetHeaterVoltageRequest>); Add <GetHeaterVoltageReply>(p => OnMeasurePreconfigured()); Add <ScanVoltageSetReply>(); Add <CapacitorVoltageSetReply>(p => SendMeasure()); Add <SendMeasureReply>(); Add <DelayedMeasureReply>(); Add <CountsReply>(updateDevice, p => OnMeasureDone()); }