Skip to end of metadata
Go to start of metadata

Use this Function / Script to create a Shift / Schedule 

Step-by-step guide

In the Script Console :

class IgnitionScheduleCreator:
	"""
	Creates an Ignition Schedule using a passed dictionary.

	Example passed dictionaries: 
	{
		'Schedule Name': 'Schedule Name',
		'All Days': '1:00-5:00, 5:10-12:00',
	}

	{
		'Schedule Name': 'Every Day Except Sunday',
		'Weekdays': '1:00-6:00',
		'Saturday': '2:00-7:00'
	}
	"""
	
	SCHEDULE_NAME_KEY = 'Schedule Name'
	SCHEDULE_DESCRIPTION_KEY = 'Schedule Description'

	ALL_DAYS_KEY = 'All Days'
	WEEKDAYS_KEY = 'Weekdays'
	MONDAY_KEY = 'Monday'
	TUESDAY_KEY = 'Tuesday'
	WEDNESDAY_KEY = 'Wednesday'
	THURSDAY_KEY = 'Thursday'
	FRIDAY_KEY = 'Friday'
	SATURDAY_KEY = 'Saturday'
	SUNDAY_KEY = 'Sunday' 

	SPECIFIC_DAYS_LIST = [
		MONDAY_KEY,
		TUESDAY_KEY,
		WEDNESDAY_KEY,
		THURSDAY_KEY,
		FRIDAY_KEY,
		SATURDAY_KEY,
		SUNDAY_KEY,
	]

	SPECIFIC_WEEKDAYS_LIST = [
		MONDAY_KEY,
		TUESDAY_KEY,
		WEDNESDAY_KEY,
		THURSDAY_KEY,
		FRIDAY_KEY,
	]
	
	ALWAYS_SCHEDULE_NAME = 'Always'
	
	def __init__(self, schedule_dict):
		self.__all_days_defined, self.__all_weekdays_defined, self.__any_specfic_day_defined = self.__validate_dict(schedule_dict)
		self.__schedule_dict = schedule_dict

	def __validate_dict(self, schedule_dict):
		"""
		Confirms that the passed dictionary is valid. 
		Checks for the following:
		
		- "Schedule Name" key exists
		- Some sort of schedulable time is passed
		- No overlapping schedule entries

		:param schedule_dict: Dictionary of schedule times
		:type schedule_dict: dict
		:raises ValueError: If "Schedule Name" key is not defined
		:raises ValueError: If no schedulable time is passed
		:raises ValueError: If other schedule keys were specified when using the "All Days" key
		:raises ValueError: If any other specific weekdays were specified when using "Weekdays"
		:return: Tuple of boolean values representing scheduling state
		:rtype: (bool, bool, bool)
		"""

		schedule_name_is_blank = schedule_dict.get(self.SCHEDULE_NAME_KEY) is None

		if schedule_name_is_blank:
			raise ValueError('Required entry "%s" was not specified in passed dictionary' % self.SCHEDULE_NAME_KEY)

		all_days_defined = schedule_dict.get(self.ALL_DAYS_KEY) is not None
		all_weekdays_defined = schedule_dict.get(self.WEEKDAYS_KEY) is not None
		any_specfic_day_defined = any(
			[
				schedule_dict.get(day) is not None
				for day
				in self.SPECIFIC_DAYS_LIST
			]
		)

		any_specfic_weekday_defined = any(
			[
				schedule_dict.get(day) is not None
				for day
				in self.SPECIFIC_WEEKDAYS_LIST
			]
		)

		all_days_and_all_weekdays_overlapping = all_days_defined and all_weekdays_defined
		all_days_and_specfic_day_overlaping = all_days_defined and any_specfic_day_defined
		all_weekdays_and_specific_weekdays_overlapping = any_specfic_weekday_defined and all_weekdays_defined
		
		if all([not all_days_defined, not all_weekdays_defined, not any_specfic_day_defined]):
			raise ValueError('You did not specify any schedule time parameters with which to create the schedule')

		if all_days_and_all_weekdays_overlapping or all_days_and_specfic_day_overlaping:
			raise ValueError('When using "%s" you cannot specify any other schedule time parameters' % self.ALL_DAYS_KEY)

		if all_weekdays_and_specific_weekdays_overlapping:
			raise ValueError('When using "%s" you cannot specify any other WEEKDAY specifically' % self.WEEKDAYS_KEY)

		return all_days_defined, all_weekdays_defined, any_specfic_day_defined

	def __schedule_specfic_days(self, schedule):
		"""
		Schedules each specified specfic days. 

		:param schedule: Schedule dict passed to constructor
		:type schedule: dict
		"""
		monday_time = self.__schedule_dict.get(self.MONDAY_KEY)
		tuesday_time = self.__schedule_dict.get(self.TUESDAY_KEY)
		wednesday_time = self.__schedule_dict.get(self.WEDNESDAY_KEY)
		thursday_time = self.__schedule_dict.get(self.THURSDAY_KEY)
		friday_time = self.__schedule_dict.get(self.FRIDAY_KEY)
		saturday_time = self.__schedule_dict.get(self.SATURDAY_KEY)
		sunday_time = self.__schedule_dict.get(self.SUNDAY_KEY)

		if monday_time is not None:
			schedule.setMondayTime(monday_time)
			schedule.setMonday(True)
		else:
			schedule.setMonday(False)
			
		if tuesday_time is not None:
			schedule.setTuesdayTime(tuesday_time)
			schedule.setTuesday(True)
		else:
			schedule.setTuesday(False)

		if wednesday_time is not None:
			schedule.setWednesdayTime(wednesday_time)
			schedule.setWednesday(True)
		else:
			schedule.setWednesday(False)

		if thursday_time is not None:
			schedule.setThursdayTime(thursday_time)
			schedule.setThursday(True)
		else:
			schedule.setThursday(False)

		if friday_time is not None:
			schedule.setFridayTime(friday_time)
			schedule.setFriday(True)
		else:
			schedule.setFriday(False)

		if saturday_time is not None:
			schedule.setSaturdayTime(saturday_time)
			schedule.setSaturday(True)
		else:
			schedule.setSaturday(False)

		if sunday_time is not None:
			schedule.setSundayTime(sunday_time)
			schedule.setSunday(True)
		else:
			schedule.setSunday(False)


	def create_schedule(self):
		"""
		Creates schedule passed to constructor
		"""
		schedule_name = self.__schedule_dict.get(self.SCHEDULE_NAME_KEY)
	
		# Delete schedule if it already exists
		system.user.removeSchedule(schedule_name)


		# Need to a schedule to modify and save as new.
		# "Always" will always be available to get. So lets use that
		schedule = system.user.getSchedule(self.ALWAYS_SCHEDULE_NAME)
		schedule.setName(schedule_name)
		schedule_desc = self.__schedule_dict.get(self.SCHEDULE_DESCRIPTION_KEY)

		if schedule_desc is not None:
			schedule.setDescription(schedule_desc)
		else:
			schedule.setDescription('')

		if self.__all_days_defined:
			schedule.setAllDayTime(self.__schedule_dict[self.ALL_DAYS_KEY])
			schedule.setAllDays(True)
		else:
			schedule.setAllDays(False)
		
		if self.__all_weekdays_defined:
			schedule.setWeekDayTime(self.__schedule_dict[self.WEEKDAYS_KEY])
			schedule.setWeekDays(True)
		else:
			schedule.setWeekDays(False)

		if self.__any_specfic_day_defined:
			self.__schedule_specfic_days(schedule)

		system.user.addSchedule(schedule)
		
# call example ... add the keys you need to fine tune

my_schedule = {		
	"Schedule Name": "PYSchedule", 
	"Schedule Description": "Life of PY",	
	"All Days": "1:00-5:00, 5:10-12:00"
}
				
IgnitionScheduleCreator(my_schedule).create_schedule()	


Call the Shift Manager to check (if it was open, you'll have to close it and re-open to refresh the content)



Tested on Ignition 7.9.14 and MES 2.79 SP3

Courtesy of DM (wink)

Shift Configuration