Exemplo n.º 1
0
        public void SynchronizationPoint()
        {
            ConfStorageLoad();
            EventServerLoad();

            foreach (var schedule in ConfStorage.ScheduleList.Values)
            {
                EventServer.ScheduleStateActiveEvent(this, "Initialized", schedule.token, schedule.Name, ConfStorage.ScheduleStateList[schedule.token].Active, ConfStorage.ScheduleStateList[schedule.token].SpecialDay);
            }

            EventServerSave();
            ConfStorageSave();
        }
Exemplo n.º 2
0
        public override void DeleteSchedule(string Token)
        {
            ConfStorageLoad();
            EventServerLoad();

            if (ConfStorage.AwaitingTasks.ContainsKey(Token))
            {
                foreach (var task in ConfStorage.AwaitingTasks[Token])
                {
                    task.Cancel();
                }
            }
            ConfStorage.AwaitingTasks.Remove(Token);

            if (ConfStorage.ScheduleList.ContainsKey(Token))
            {
                EventServer.ScheduleStateActiveEvent(this, "Removed", Token, ConfStorage.ScheduleList[Token].Name, ConfStorage.ScheduleStateList[Token].Active, ConfStorage.ScheduleStateList[Token].SpecialDay);
                EventServer.ConfigurationScheduleRemovedEvent(this, Token);
                ConfStorage.ScheduleList.Remove(Token);
                ConfStorage.ScheduleStateList.Remove(Token);
                LoggingService.LogMessage(string.Format("Schedule with token '{0}' was deleted.", Token), DUT.PACS.Simulator.ExternalLogging.MessageType.Message);
            }
            else
            {
                string message = string.Format("Schedule with token {0} does not exist", Token);
                LoggingService.LogMessage(message, DUT.PACS.Simulator.ExternalLogging.MessageType.Error);
                FaultLib.ReturnFault(message, new string[] { "Sender", "InvalidArgVal", "NotFound" });
            }

            if (ConfStorage.ScheduleStateList.ContainsKey(Token))
            {
                ConfStorage.ScheduleStateList.Remove(Token);
            }

            EventServerSave();
            ConfStorageSave();
        }
Exemplo n.º 3
0
        public void ScheduleTask(DateTime dateStart, DateTime dateEnd, Schedule schedule, ServiceCapabilities capabilities)
        {
            var startCancelSource         = new CancellationTokenSource();
            CancellationToken startCancel = startCancelSource.Token;
            var delayStart = (int)dateStart.Subtract(DateTime.Now).TotalMilliseconds;
            var delayEnd   = (int)dateEnd.Subtract(DateTime.Now).TotalMilliseconds;

            if (delayStart < 1000)
            {
                dateStart = dateStart.AddSeconds(1);
                dateEnd   = dateEnd.AddSeconds(1);
            }
            if (dateEnd.Subtract(dateStart).TotalMilliseconds < 1000)
            {
                dateEnd = dateEnd.AddSeconds(1);
            }

            Task taskStart = Task.Factory.StartNew(() => ScheduleAction(() =>
            {
                ConfStorageLoad();
                EventServerLoad();
                if (ConfStorage.ScheduleList.ContainsKey(schedule.token))
                {
                    var specialDay = IsSpecialDay(schedule);

                    EventServer.ScheduleStateActiveEvent(this, "Changed", schedule.token, schedule.Name, true, specialDay);
                    ScheduleState state = new ScheduleState()
                    {
                        Active = true, SpecialDaySpecified = capabilities.SpecialDaysSupported, SpecialDay = specialDay
                    };
                    if (!ConfStorage.ScheduleStateList.ContainsKey(schedule.token))
                    {
                        ConfStorage.ScheduleStateList.Add(schedule.token, state);
                    }
                    else
                    {
                        ConfStorage.ScheduleStateList[schedule.token] = state;
                    }
                }

                EventServerSave();
                ConfStorageSave();
            }, dateStart),
                                                   startCancel);

            if (!ConfStorage.AwaitingTasks.ContainsKey(schedule.token))
            {
                ConfStorage.AwaitingTasks[schedule.token] = new List <CancellationTokenSource>();
            }
            ConfStorage.AwaitingTasks[schedule.token].Add(startCancelSource);

            var endCancelSource         = new CancellationTokenSource();
            CancellationToken endCancel = endCancelSource.Token;

            Task taskEnd = Task.Factory.StartNew(() => ScheduleAction(() =>
            {
                ConfStorageLoad();
                EventServerLoad();

                var specialDay = IsSpecialDay(schedule);

                EventServer.ScheduleStateActiveEvent(this, "Changed", schedule.token, schedule.Name, false, specialDay);
                ScheduleState state = new ScheduleState()
                {
                    Active = false, SpecialDaySpecified = capabilities.SpecialDaysSupported, SpecialDay = specialDay
                };
                if (!ConfStorage.ScheduleStateList.ContainsKey(schedule.token))
                {
                    ConfStorage.ScheduleStateList.Add(schedule.token, state);
                }
                else
                {
                    ConfStorage.ScheduleStateList[schedule.token] = state;
                }

                EventServerSave();
                ConfStorageSave();
            }, dateEnd),
                                                 endCancel);

            ConfStorage.AwaitingTasks[schedule.token].Add(endCancelSource);
        }
Exemplo n.º 4
0
        public override string CreateSchedule(Schedule Schedule)
        {
            ConfStorageLoad();

            EventServerLoad();

            if (Schedule.token == "")
            {
                int i = 1;

                Schedule.token = "schedule" + i.ToString();

                while (ConfStorage.ScheduleList.Keys.Contains(Schedule.token))
                {
                    Schedule.token = "schedule" + i.ToString();
                    i++;
                }
            }
            else
            {
                string message = string.Format("Not empty token.");
                LoggingService.LogMessage(message, DUT.PACS.Simulator.ExternalLogging.MessageType.Error);
                FaultLib.ReturnFault(message, new string[] { "Sender", "InvalidArgs" });
            }

            ServiceCapabilities capabilities = Simulator.SystemCapabilities.Instance.ScheduleCapabilities;
            string res = Schedule.token;

            ICalendar standardICalendar        = new ICalendar(Schedule.Standard);
            var       standardICalendarVEvents = standardICalendar.GetVEvents();

            if (standardICalendarVEvents != null && standardICalendarVEvents.Count > capabilities.MaxTimePeriodsPerDay)
            {
                string message = string.Format("There are too many TimePeriods in a day schedule, see MaxTimePeriodsPerDay capability.");
                LoggingService.LogMessage(message, DUT.PACS.Simulator.ExternalLogging.MessageType.Error);
                FaultLib.ReturnFault(message, new string[] { "Sender", "CapabilityViolated", "MaxTimePeriodsPerDay" });
            }

            //Check that there is no schedule with the same token exists
            if (ConfStorage.ScheduleList.ContainsKey(Schedule.token))
            {
                string message = string.Format("Schedule with token {0} aleady exist.", Schedule.token);
                LoggingService.LogMessage(message, DUT.PACS.Simulator.ExternalLogging.MessageType.Error);
                FaultLib.ReturnFault(message, new string[] { "Sender", "InvalidArgVal" });
            }


            //Check MaxSchedules capability
            if (ConfStorage.ScheduleList.Count() >= capabilities.MaxSchedules)
            {
                string message = string.Format("There is not enough space to create new schedule, see the MaxSchedules capability.");
                LoggingService.LogMessage(message, DUT.PACS.Simulator.ExternalLogging.MessageType.Error);
                FaultLib.ReturnFault(message, new string[] { "Receiver", "CapabilityViolated", "MaxSchedules" });
            }


            if (Schedule.SpecialDays != null)
            {
                if (Schedule.SpecialDays.Count() >= capabilities.MaxSpecialDaysSchedules)
                {
                    string message = string.Format("There are too many SpecialDaysSchedule entities referred in this schedule, see MaxSpecialDays-Schedules capability.");
                    LoggingService.LogMessage(message, DUT.PACS.Simulator.ExternalLogging.MessageType.Error);
                    FaultLib.ReturnFault(message, new string[] { "Sender", "CapabilityViolated", "MaxSpecialDaysSchedules" });
                }

                foreach (var specialDays in Schedule.SpecialDays)
                {
                    if (specialDays.TimeRange.Count() > capabilities.MaxTimePeriodsPerDay)
                    {
                        string message = string.Format("There are too many TimePeriods in a day schedule, see MaxTimePeriodsPerDay capability.");
                        LoggingService.LogMessage(message, DUT.PACS.Simulator.ExternalLogging.MessageType.Error);
                        FaultLib.ReturnFault(message, new string[] { "Sender", "CapabilityViolated", "MaxTimePeriodsPerDay" });
                    }
                    if (specialDays.TimeRange != null)
                    {
                        foreach (var timeRange in specialDays.TimeRange)
                        {
                            if (timeRange.Until < timeRange.From)
                            {
                                string message = string.Format("Schedule SpecialDayGroupToken.TimeRange.Until value is less that From value.");
                                LoggingService.LogMessage(message, DUT.PACS.Simulator.ExternalLogging.MessageType.Error);
                                FaultLib.ReturnFault(message, new string[] { "Sender", "InvalidArgVal", "ReferenceNotFound" });
                            }
                        }
                    }
                }

                //Check that all Schedules exists
                if (Schedule.SpecialDays.Any(C => !(ConfStorage.SpecialDayGroupList.Keys.Contains(C.GroupToken))))
                {
                    string message = string.Format("Schedule special day group token does not exist.");
                    LoggingService.LogMessage(message, DUT.PACS.Simulator.ExternalLogging.MessageType.Error);
                    FaultLib.ReturnFault(message, new string[] { "Sender", "InvalidArgVal" });
                }
            }

            ConfStorage.ScheduleList.Add(Schedule.token, Schedule);
            ScheduleState scheduleState = new ScheduleState();

            DateTime dateStart = standardICalendarVEvents.First().DateStart;
            DateTime dateEnd   = standardICalendarVEvents.First().DateEnd;

            if (dateStart.ToFileTime() < DateTime.Now.ToFileTime() && dateEnd.ToFileTime() > DateTime.Now.ToFileTime())
            {
                scheduleState.Active = true;
            }
            else
            {
                scheduleState.Active = false;
            }

            scheduleState.SpecialDaySpecified = capabilities.SpecialDaysSupported;
            scheduleState.SpecialDay          = IsSpecialDay(Schedule);

            ConfStorage.ScheduleStateList.Add(Schedule.token, scheduleState);

            EventServer.ConfigurationScheduleChangedEvent(this, Schedule.token);
            EventServer.ScheduleStateActiveEvent(this, "Initialized", Schedule.token, Schedule.Name, scheduleState.Active, scheduleState.SpecialDay);



            if (dateStart.ToFileTime() > DateTime.Now.ToFileTime() && standardICalendarVEvents.First().Rrule.Contains("DAILY"))
            {
                ScheduleTask(dateStart, dateEnd, Schedule, capabilities);
            }
            else if (dateEnd.ToFileTime() > DateTime.Now.ToFileTime())
            {
                ScheduleTask(DateTime.Now, dateEnd, Schedule, capabilities);
            }

            var specialDayGroupToken = string.Empty;

            if (Schedule.SpecialDays != null)
            {
                specialDayGroupToken = Schedule.SpecialDays.First().GroupToken;
            }

            if (!string.IsNullOrEmpty(specialDayGroupToken))
            {
                if (ConfStorage.SpecialDayGroupList.ContainsKey(specialDayGroupToken))
                {
                    var specialDayGroup = ConfStorage.SpecialDayGroupList[specialDayGroupToken];

                    ICalendar specialDayGroupICalendar        = new ICalendar(specialDayGroup.Days);
                    var       specialDayGroupICalendarVEvents = specialDayGroupICalendar.GetVEvents();

                    DateTime dateSpecialDayStart = specialDayGroupICalendarVEvents.First().DateStart;
                    DateTime dateSpecialDayEnd   = specialDayGroupICalendarVEvents.First().DateEnd;

                    if (string.IsNullOrEmpty(specialDayGroupICalendarVEvents.First().Rrule) || specialDayGroupICalendarVEvents.First().Rrule.Contains("DAILY"))
                    {
                        if (dateSpecialDayStart.ToFileTime() > DateTime.Now.ToFileTime())
                        {
                            ScheduleTask(dateSpecialDayStart, dateSpecialDayEnd, Schedule, capabilities);
                        }
                        else if (dateSpecialDayEnd.ToFileTime() > DateTime.Now.ToFileTime())
                        {
                            ScheduleTask(DateTime.Now, dateSpecialDayEnd, Schedule, capabilities);
                        }
                    }
                }
            }

            EventServerSave();
            ConfStorageSave();

            return(res);
        }