Exemple #1
0
        private static void ActivateJob(string guid)
        {
            if (guid == null)
            {
                throw new ArgumentNullException(nameof(guid));
            }
            using (DatabaseObjectStorageEntryUsable <ChainScheduleManager <T> > dataInstanceWrapper = GetInstance())
            {
                ChainScheduleManager <T> dataInstance = dataInstanceWrapper.Data;
                ChainedExecutionData     currentChainedExecutionData = dataInstance.Jobs.FirstOrDefault(data => data.Guid == guid);
                if (currentChainedExecutionData == null)
                {
                    HistoryController.AddLogEntry(new HistoryElement(DateTime.Now, "Manual", "Error", $"Es wurde kein Job mit der GUID: {guid} zum aktivieren gefunden!\r\nDieser Eintrag wird nun übersprungen\r\nEs sind noch {dataInstance.Jobs.Count} Aufträge in der Queue.\r\nAktueller Auftrag ist: {dataInstance.CurrentJob}"));
                    return;
                }

                dataInstance.CurrentJob = guid;

                TimeSpan duration = (currentChainedExecutionData.ChainedActionExecutionData.Duration * currentChainedExecutionData.ChainedActionExecutionData.DurationOverride / 100 * GetGlobalOverride());

                currentChainedExecutionData.StartTime = DateTime.Now.TimeOfDay;
                currentChainedExecutionData.ChainedActionExecutionData.ActivateAction(duration);

                currentChainedExecutionData.DeactivationJob = BackgroundJob.Schedule(() => DeactivateJob(currentChainedExecutionData.Guid, null), duration);
                currentChainedExecutionData.Duration        = duration;
            }
        }
Exemple #2
0
        public static void DeactivateJob(string guid, DatabaseObjectStorageEntryUsable <ChainScheduleManager <T> > dataInstanceWrapper = null)
        {
            if (guid == null)
            {
                throw new ArgumentNullException(nameof(guid));
            }
            ChainedExecutionData currentChainedExecutionData;

            using (dataInstanceWrapper = dataInstanceWrapper ?? GetInstance())
            {
                ChainScheduleManager <T> dataInstance = dataInstanceWrapper.Data;
                currentChainedExecutionData = dataInstance.Jobs.FirstOrDefault(data => data.Guid == guid);
                if (currentChainedExecutionData == null)
                {
                    HistoryController.AddLogEntry(new HistoryElement(DateTime.Now, "Manual", "Error", $"Es wurde kein Job mit der GUID: {guid} zum deaktivieren gefunden!\r\nDieser Eintrag wird nun übersprungen\r\nEs sind noch {dataInstance.Jobs.Count} Aufträge in der Queue.\r\nAktueller Auftrag ist: {dataInstance.CurrentJob}"));
                    return;
                }

                dataInstance.CurrentJob = "";

                CallNextJob(currentChainedExecutionData, dataInstanceWrapper);

                currentChainedExecutionData.ChainedActionExecutionData.DeactivateAction();
                dataInstance.Jobs.RemoveAll(data => data.Guid == currentChainedExecutionData.ChainedActionExecutionData.Guid);
            }
        }
Exemple #3
0
        public bool Deactivate(int channelId, string operatingMode, out bool deactivateMasterChannel)
        {
            deactivateMasterChannel = false;
            using (DatabaseObjectStorageEntryUsable <WaterRelaisControlData> dataWrapper = DatabaseObjectStorageEntryUsable <WaterRelaisControlData> .GetData(() => new WaterRelaisControlData()))
            {
                List <WaterRelaisControlData.WaterRelaisControlDataItem> waterRelaisControlDataItems = dataWrapper.Data.Dictionary[channelId];

                #region Cleanup

                // remove entries wich are more than 5 minutes over their endTime
                waterRelaisControlDataItems.RemoveAll(item => item.EndTime.Add(new TimeSpan(0, 5, 0)) > DateTime.Now.TimeOfDay);

                #endregion Cleanup

                List <WaterRelaisControlData.WaterRelaisControlDataItem> currentlyActiveActions = GetCurrentlyActiveActions(waterRelaisControlDataItems);
                if (currentlyActiveActions.Count > 0)
                {
                    if (currentlyActiveActions.All(item => item.ActivateWithMasterChannel == false))
                    {
                        deactivateMasterChannel = true;
                    }
                    return(false);
                }
            }
            return(true);

            //TODO: Implement
            throw new NotImplementedException();
        }
Exemple #4
0
 public static void AddEntry(T manualActionExecution, string elementEventSource)
 {
     if (manualActionExecution == null)
     {
         throw new ArgumentNullException(nameof(manualActionExecution));
     }
     if (elementEventSource == null)
     {
         throw new ArgumentNullException(nameof(elementEventSource));
     }
     using (DatabaseObjectStorageEntryUsable <ChainScheduleManager <T> > dataInstanceWrapper = GetInstance())
     {
         ChainScheduleManager <T> dataInstance = dataInstanceWrapper.Data;
         if (dataInstance.CurrentJob == "" && dataInstance.Jobs.Count > 0)
         {
             HistoryController.AddLogEntry(new HistoryElement(DateTime.Now, "System", "Warn", $"ChainScheduleManager: Es ist kein aktuell ausgeführter Job spezifiziert, jedoch sind noch {dataInstance.Jobs.Count} Jobs in der Queue. Diese werden nun vom ältesten beginnend abgearbeitet. {JsonConvert.SerializeObject(dataInstance.Jobs.First())}"));
             Task.Run(() => ActivateJob(dataInstance.Jobs.First().Guid));
         }
         if (dataInstance.Jobs.Count == 0 || dataInstance.Jobs.All(data => data.NextGuid != ""))
         {
             ChainedExecutionData newChainedExecutionData = new ChainedExecutionData(manualActionExecution, elementEventSource, "");
             dataInstance.Jobs.Add(newChainedExecutionData);
             Task.Run(() => ActivateJob(newChainedExecutionData.Guid));
         }
         else
         {
             ChainedExecutionData previousChainedExecutionData = dataInstance.Jobs.Last(data => data.NextGuid == "");
             previousChainedExecutionData.NextGuid = manualActionExecution.Guid;
             dataInstance.Jobs.Add(new ChainedExecutionData(manualActionExecution, elementEventSource, previousChainedExecutionData.Guid));
         }
     }
 }
Exemple #5
0
        public static void SetupChannel(ChannelData channelData)
        {
            List <string> removedGuids = new List <string>();

            using (DatabaseObjectStorageEntryUsable <AutomaticJobStore> automaticJobStoreWrapper = DatabaseObjectStorageEntryUsable <AutomaticJobStore> .GetData(() => new AutomaticJobStore()))
            {
                AutomaticJobStore automaticJobStore = automaticJobStoreWrapper.Data;
                if (automaticJobStore.ChannelProgramDictionary.ContainsKey(channelData.ChannelId) == false)
                {
                    automaticJobStore.ChannelProgramDictionary.Add(channelData.ChannelId, new List <string>());
                }

                // alle einträge von automaticJobStore.ChannelProgramDictionary[channelData.ChannelId] wo kein !gültiger! Eintrag in channelData.ProgramList enthalten ist
                foreach (string removedGuid in automaticJobStore.ChannelProgramDictionary[channelData.ChannelId].Where(s => channelData.ProgramList.Any(data => data.Guid == s && IsInvalid(data) == false) == false))
                {
                    RecurringJob.RemoveIfExists(removedGuid);
                    removedGuids.Add(removedGuid);
                }
                foreach (string removedGuid in removedGuids)
                {
                    automaticJobStore.ChannelProgramDictionary[channelData.ChannelId].Remove(removedGuid);
                }

                foreach (ChannelProgramData channelProgramData in channelData.ProgramList.Where(data => IsInvalid(data) == false))
                {
                    AutomaticJobContext automaticJobContext = new AutomaticJobContext(channelProgramData, channelData);
                    RecurringJob.AddOrUpdate(automaticJobContext.ChannelProgramData.Guid, () => ExecuteScheduledJob(automaticJobContext), GetExecutionCron(automaticJobContext), TimeZoneInfo.Local);
                    if (automaticJobStore.ChannelProgramDictionary[channelData.ChannelId].Contains(automaticJobContext.ChannelProgramData.Guid) == false)
                    {
                        // new Entry was added
                        automaticJobStore.ChannelProgramDictionary[channelData.ChannelId].Add(automaticJobContext.ChannelProgramData.Guid);
                    }
                }
            }
        }
Exemple #6
0
 public static void DeleteEntry(ChainedExecutionData loadedDataJob)
 {
     if (loadedDataJob == null)
     {
         throw new ArgumentNullException(nameof(loadedDataJob));
     }
     using (DatabaseObjectStorageEntryUsable <ChainScheduleManager <T> > dataInstanceWrapper = GetInstance())
     {
         ChainScheduleManager <T> dataInstance = dataInstanceWrapper.Data;
         if (loadedDataJob.Guid == dataInstance.CurrentJob)
         {
             RemoveCurrentEntry(loadedDataJob, dataInstance, dataInstanceWrapper);
         }
         else
         {
             RemoveFutureEntry(loadedDataJob, dataInstance);
         }
     }
 }
Exemple #7
0
        public bool Activate(int channelId, bool activateWithMasterChannel, string operatingMode, TimeSpan duration, out bool activateMasterChannel)
        {
            activateMasterChannel = activateWithMasterChannel;
            using (DatabaseObjectStorageEntryUsable <WaterRelaisControlData> dataWrapper = DatabaseObjectStorageEntryUsable <WaterRelaisControlData> .GetData(() => new WaterRelaisControlData()))
            {
                Dictionary <int, List <WaterRelaisControlData.WaterRelaisControlDataItem> > dataDictionary = dataWrapper.Data.Dictionary;
                if (dataDictionary.ContainsKey(channelId) == false)
                {
                    dataDictionary.Add(channelId, new List <WaterRelaisControlData.WaterRelaisControlDataItem>());
                }

                dataDictionary[channelId].Add(new WaterRelaisControlData.WaterRelaisControlDataItem(activateWithMasterChannel, operatingMode, duration, DateTime.Now.TimeOfDay));

                List <WaterRelaisControlData.WaterRelaisControlDataItem> currentlyActiveActions = GetCurrentlyActiveActions(dataDictionary[channelId]);
                if (currentlyActiveActions.Count > 0)
                {
                    activateMasterChannel = currentlyActiveActions.Any(item => item.ActivateWithMasterChannel);
                }
            }
            return(true);
        }
Exemple #8
0
 private static void RemoveCurrentEntry(ChainedExecutionData loadedDataJob, ChainScheduleManager <T> dataInstance, DatabaseObjectStorageEntryUsable <ChainScheduleManager <T> > dataInstanceWrapper)
 {
     if (loadedDataJob == null)
     {
         throw new ArgumentNullException(nameof(loadedDataJob));
     }
     if (dataInstance == null)
     {
         throw new ArgumentNullException(nameof(dataInstance));
     }
     if (loadedDataJob.Guid != dataInstance.CurrentJob)
     {
         throw new InvalidOperationException("RemoveFutureEntry can not remove a future Entry");
     }
     BackgroundJob.Delete(loadedDataJob.DeactivationJob);
     DeactivateJob(loadedDataJob.Guid, dataInstanceWrapper);
 }
Exemple #9
0
 private static DatabaseObjectStorageEntryUsable <ChainScheduleManager <T> > GetInstance() =>
 DatabaseObjectStorageEntryUsable <ChainScheduleManager <T> > .GetDataLocked(() =>
                                                                             new ChainScheduleManager <T>());
Exemple #10
0
        private static void CallNextJob(ChainedExecutionData currentChainedExecutionData, DatabaseObjectStorageEntryUsable <ChainScheduleManager <T> > dataInstanceWrapper)
        {
            if (currentChainedExecutionData == null)
            {
                throw new ArgumentNullException(nameof(currentChainedExecutionData));
            }
            if (dataInstanceWrapper == null)
            {
                throw new ArgumentNullException(nameof(dataInstanceWrapper));
            }
            ChainScheduleManager <T> dataInstance             = dataInstanceWrapper.Data;
            ChainedExecutionData     nextChainedExecutionData = dataInstance.Jobs.FirstOrDefault(data => data.Guid == currentChainedExecutionData.NextGuid);

            if (nextChainedExecutionData != null)
            {
                Task.Run(() => ActivateJob(nextChainedExecutionData.Guid));
            }
        }