
using namespace std;

class schedules
{
	public:
		schedules();
		void sortSchedules();
		void calculateSchedules();
		void readSchedules();
		void runSchedule();
		void monitorSchedules();
		void deleteSchedule(int schID);
		void saveSchedule(int schID, short offOn, char* schName, int start, bool mon, bool tue, bool wed, bool thu, bool fri, bool sat, bool sun, short repeatDays, bool enabled, bool email, bool alert, std::string mL);
		void addSchedule(short source, short type, short offOn, char* schName, int start, bool mon, bool tue, bool wed, bool thu, bool fri, bool sat, bool sun, short repeatDays, short pinID, bool email, bool alert, std::string mL);
		void addDoseGroup(std::string data);  // add group schedule for dosing pump
		void enableSchedule(short schID, short value);
		void smartStart();
		void frontendResponse();  // needed to send response back to front end when schedule is added or edited
	private:
		short* scheduleID;  // track scheduleID
		std::string* scheduleName;  // track schedule name for emails
		short* scheduleTypeID;  // track what the schedule is for
		short* scheduleRuleID;  // track the ruleID tagged to schedule
		short* schedulePinID;  // track pinID from pinsAvailable table
		unsigned long long* scheduleStart;
		std::string* scheduleCreated;  // track when schedule was created
		bool* scheduleMon;  // track schedule day
		bool* scheduleTue;  // track schedule day
		bool* scheduleWed;  // track schedule day
		bool* scheduleThu;  // track schedule day
		bool* scheduleFri;  // track schedule day
		bool* scheduleSat;  // track schedule day
		bool* scheduleSun;  // track schedule day
		short* scheduleRepeatDays;  // track if schedule is repeated and how often in days
		short* scheduleOffOn;  // track if schedule is off or on
		bool* scheduleEnabled;  // track if scheduled is enabled
		bool* scheduleEmail;  // track if email alert is enabled
		bool* scheduleAlert;  // track if dashboard alert is enabled
		float* scheduleML;  // track number of milliliters to dose with schedule
		unsigned long long* nextScheduleTimeStamp;
		unsigned long long* scheduleFirstRunTimeStamp;

		// short scheduleID[100] = {};  // track scheduleID
		// std::string scheduleName[100] = {};  // track schedule name for emails
		// short scheduleTypeID[100] = {};  // track what the schedule is for
		// short scheduleRuleID[100] = {};  // track the ruleID tagged to schedule
		// short schedulePinID[100] = {};  // track pinID from pinsAvailable table
		// // unsigned long long scheduleStart[100] = {};  // track schedule start time
		// unsigned long long* scheduleStart = new unsigned long long[100];
		// std::string scheduleCreated[100] = {};  // track when schedule was created
		// bool scheduleMon[100] = {};  // track schedule day
		// bool scheduleTue[100] = {};  // track schedule day
		// bool scheduleWed[100] = {};  // track schedule day
		// bool scheduleThu[100] = {};  // track schedule day
		// bool scheduleFri[100] = {};  // track schedule day
		// bool scheduleSat[100] = {};  // track schedule day
		// bool scheduleSun[100] = {};  // track schedule day
		// short scheduleRepeatDays[100] = {};  // track if schedule is repeated and how often in days
		// short scheduleOffOn[100] = {};  // track if schedule is off or on
		// bool scheduleEnabled[100] = {};  // track if scheduled is enabled
		// bool scheduleEmail[100] = {};  // track if email alert is enabled
		// bool scheduleAlert[100] = {};  // track if dashboard alert is enabled
		// float scheduleML[100] = {};  // track number of milliliters to dose with schedule
		// // unsigned long long nextScheduleTimeStamp[100] = {};  // track next schedule start time
		// unsigned long long* nextScheduleTimeStamp = new unsigned long long[100];
		// // unsigned long long scheduleFirstRunTimeStamp[100] = {};  // track first time schedule ran start time
		// unsigned long long* scheduleFirstRunTimeStamp = new unsigned long long[100];

		short totalSchedules = 0;  // track total schedules found
		unsigned long long nextUpcomingSchedule = 0;  // track next upcoming schedule time in millis
		short nextUpcomingID = 0;  // track next upcoming scheduleID
        short countIncoming = 0;  // count pieces of new group schedule as they come in
		std::string incomingGroup[18] = {};
};
schedules::schedules(){}

void schedules::smartStart() {  // run at startup
    // std::cout << " " << std::endl;
    // std::cout << " " << std::endl;
    // std::cout << "smartStart--------------------------------" << std::endl;
	for(short a=0; a < (outlets.getTotalOutlets()); a++)  // go through AC outlets and turn on/off based on schedules
	{
		unsigned long long outletSch[100] = {};  // temporarily store schedules for outlet to process
		short schID[100] = {};  // temporarily store pinID for outlet to process
		short count = 0;
		// std::cout << "getPinID       = " << outlets.getPinID(a) << endl;
		for(short b=0; b < (totalSchedules); b++)  // 
		{
			if (schedulePinID[b] == outlets.getPinID(a)) 
			{
				outletSch[count] = nextScheduleTimeStamp[b];
				schID[count] = scheduleID[b];
				// std::cout << "outletSch[count]           - " << outletSch[count] << " - " << schID[count] << std::endl;
				count++;
			}
		}

		if (count != 0)
		{
			// sort found schedule descending so we can get the last upcoming schedule
			for(short n=0; n<(count-1); n++) {
				for(short o=0; o<(count-(n+1)); o++) {
					if(outletSch[o] < outletSch[o+1]) {
						unsigned long long aa = outletSch[o];
						outletSch[o] = outletSch[o+1];
						outletSch[o+1] = aa;
						
						short bb = schID[o];
						schID[o] = schID[o+1];
						schID[o+1] = bb;
					}
				}
			}

				
			// std::cout << "outletSch[0]               - " << outletSch[0] << " - " << schID[0] << std::endl;

			short arrayLoc = 255;
			for (short a = 0; a < totalSchedules; a++)  // get array location for schedule
			{
				if (scheduleID[a] == schID[0]) {arrayLoc = a; break;}
			}
			// std::cout << "arrayLoc - " << arrayLoc << std::endl;

			if (scheduleOffOn[arrayLoc] == 0) {outlets.outletOff(schedulePinID[arrayLoc],7); systemSettings.logActivity(7,1,schedulePinID[arrayLoc],0,"");}
			else if (scheduleOffOn[arrayLoc] == 1) {outlets.outletOn(schedulePinID[arrayLoc],7); systemSettings.logActivity(7,1,schedulePinID[arrayLoc],1,"");}
		}
	}

    // std::cout << " " << std::endl;
	for(short a=0; a < (dcPorts.getTotalPorts()); a++)  // go through DC ports and turn on/off based on schedules
	{
		unsigned long long dcSch[100] = {};  // temporarily store schedules for outlet to process
		short schID[100] = {};  // temporarily store pinID for outlet to process
		short count = 0;
		// std::cout << "getPinID       = " << dcPorts.getPinID(a) << endl;
		for(short b=0; b < (totalSchedules); b++)  // 
		{
			if (schedulePinID[b] == dcPorts.getPinID(a)) 
			{
				dcSch[count] = nextScheduleTimeStamp[b];
				schID[count] = scheduleID[b];
				// std::cout << "dcSch[count]           - " << dcSch[count] << " - " << schID[count] << std::endl;
				count++;
			}
		}

		if (count != 0)
		{
			// sort found schedule descending so we can get the last upcoming schedule
			for(short n=0; n<(count-1); n++) {
				for(short o=0; o<(count-(n+1)); o++) {
					if(dcSch[o] < dcSch[o+1]) {
						unsigned long long aa = dcSch[o];
						dcSch[o] = dcSch[o+1];
						dcSch[o+1] = aa;
						
						short bb = schID[o];
						schID[o] = schID[o+1];
						schID[o+1] = bb;
					}
				}
			}

				
			// std::cout << "dcSch[0]               - " << dcSch[0] << " - " << schID[0] << std::endl;

			short arrayLoc = 255;
			for (short a = 0; a < totalSchedules; a++)  // get array location for schedule
			{
				if (scheduleID[a] == schID[0]) {arrayLoc = a; break;}
			}
			// std::cout << "arrayLoc - " << arrayLoc << std::endl;

			if (scheduleOffOn[arrayLoc] == 0) {dcPorts.dcOff(schedulePinID[arrayLoc],7); systemSettings.logActivity(7,2,schedulePinID[arrayLoc],0,"");}
			else if (scheduleOffOn[arrayLoc] == 1) {dcPorts.dcOn(schedulePinID[arrayLoc],7); systemSettings.logActivity(7,2,schedulePinID[arrayLoc],1,"");}
		}
	}
    // std::cout << " " << std::endl;
}

void schedules::sortSchedules() {  // sort schedules by next schedule start time
    // std::cout << "sortSchedules  --------- " << endl;

	for(short n=0; n < (totalSchedules-1); n++) {  // sort
		for(short o=0; o < (totalSchedules-(n+1)); o++) {
			if(nextScheduleTimeStamp[o] > nextScheduleTimeStamp[o+1]) {
				unsigned long long rr = nextScheduleTimeStamp[o];
				nextScheduleTimeStamp[o] = nextScheduleTimeStamp[o+1];
				nextScheduleTimeStamp[o+1] = rr;

				unsigned long long aa = scheduleStart[o];
				scheduleStart[o] = scheduleStart[o+1];
				scheduleStart[o+1] = aa;

				short bb = scheduleID[o];
				scheduleID[o] = scheduleID[o+1];
				scheduleID[o+1] = bb;
				
				std::string tt = scheduleName[o];
				scheduleName[o] = scheduleName[o+1];
				scheduleName[o+1] = tt;
				
				short cc = scheduleTypeID[o];
				scheduleTypeID[o] = scheduleTypeID[o+1];
				scheduleTypeID[o+1] = cc;
				
				short dd = scheduleRuleID[o];
				scheduleRuleID[o] = scheduleRuleID[o+1];
				scheduleRuleID[o+1] = dd;
				
				std::string ee = scheduleCreated[o];
				scheduleCreated[o] = scheduleCreated[o+1];
				scheduleCreated[o+1] = ee;
				
				bool ff = scheduleMon[o];
				scheduleMon[o] = scheduleMon[o+1];
				scheduleMon[o+1] = ff;
				
				bool gg = scheduleTue[o];
				scheduleTue[o] = scheduleTue[o+1];
				scheduleTue[o+1] = gg;
				
				bool hh = scheduleWed[o];
				scheduleWed[o] = scheduleWed[o+1];
				scheduleWed[o+1] = hh;
				
				bool ii = scheduleThu[o];
				scheduleThu[o] = scheduleThu[o+1];
				scheduleThu[o+1] = ii;
				
				bool jj = scheduleFri[o];
				scheduleFri[o] = scheduleFri[o+1];
				scheduleFri[o+1] = jj;
				
				bool kk = scheduleSat[o];
				scheduleSat[o] = scheduleSat[o+1];
				scheduleSat[o+1] = kk;
				
				bool ll = scheduleSun[o];
				scheduleSun[o] = scheduleSun[o+1];
				scheduleSun[o+1] = ll;
				
				short mm = scheduleRepeatDays[o];
				scheduleRepeatDays[o] = scheduleRepeatDays[o+1];
				scheduleRepeatDays[o+1] = mm;
				
				short nn = schedulePinID[o];
				schedulePinID[o] = schedulePinID[o+1];
				schedulePinID[o+1] = nn;
				
				short oo = scheduleOffOn[o];
				scheduleOffOn[o] = scheduleOffOn[o+1];
				scheduleOffOn[o+1] = oo;
				
				bool pp = scheduleEnabled[o];
				scheduleEnabled[o] = scheduleEnabled[o+1];
				scheduleEnabled[o+1] = pp;
				
				unsigned long long ss = scheduleFirstRunTimeStamp[o];
				scheduleFirstRunTimeStamp[o] = scheduleFirstRunTimeStamp[o+1];
				scheduleFirstRunTimeStamp[o+1] = ss;
				
				float uu = scheduleML[o];
				scheduleML[o] = scheduleML[o+1];
				scheduleML[o+1] = uu;
			}
		}
	}

	if (startupComplete == 0) {
		std::cout << "totalSchedules   - " << totalSchedules << std::endl;
		std::cout << "scheduleID      ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleID[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleName    ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleName[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleTypeID  ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleTypeID[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleRuleID  ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleRuleID[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "schedulePinID   ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << schedulePinID[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleOffOn   ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleOffOn[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleStart   ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleStart[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleSun     ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleSun[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleMon     ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleMon[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleTue     ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleTue[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleWed     ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleWed[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleThu     ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleThu[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleFri     ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleFri[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleSat     ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleSat[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleRepeatDays -- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleRepeatDays[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleEnabled ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleEnabled[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleEmail   ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleEmail[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleAlert   ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleAlert[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleCreated ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleCreated[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "scheduleML      ----- ";
		for(short a=0; a < totalSchedules; a++) {std::cout << scheduleML[a] << ", ";}
		std::cout << "" << std::endl;

		std::cout << "nextScheduleTimeStamp - ";
		for(short a=0; a < totalSchedules; a++) {std::cout << nextScheduleTimeStamp[a] << ", ";}
		std::cout << "" << std::endl;
	}

	for(short a=0; a < totalSchedules; a++) {  // print to console
		if (startupComplete == 0) {
    		std::cout << scheduleID[a] << " - " << nextScheduleTimeStamp[a] << " ---  ";
		}

		// used to convert schedule seconds to readable date/time format - ONLY FOR DEBUGGING
		unsigned long long secon = nextScheduleTimeStamp[a] / 1000;
		time_t year = (time_t)secon;
		tm* timePtr = localtime(&year);
		short currentDay2 = timePtr->tm_mday;  // 1-31
		short currentMonth2 = timePtr->tm_mon;  // 0 = January
		short currentYear2 = timePtr->tm_year + 1900;
		short currentWeekday2 = timePtr->tm_wday;  // 0 = Sunday
		short currentHour = timePtr->tm_hour;  // 0 = Sunday
		short currentMin = timePtr->tm_min;  // 0 = Sunday
		short currentSec = timePtr->tm_sec;  // 0 = Sunday
		if (startupComplete == 0) {
			std::cout << currentMonth2 + 1 << "-" << currentDay2 << "-" << currentYear2 << ", " << currentHour << ":" << currentMin << ":" << currentSec << std::endl;
		}
	}
}

void schedules::calculateSchedules() {  // calculate schedules in a usable format
    // std::cout << " " << std::endl;
    // std::cout << "calculateSchedules--------------------------------" << std::endl;
	// std::cout << "currentMillisTime - " << currentMillisTime << std::endl;

    delete[] scheduleFirstRunTimeStamp;  // reset array
    delete[] nextScheduleTimeStamp;  // reset array

	nextScheduleTimeStamp = new unsigned long long[totalSchedules];
	scheduleFirstRunTimeStamp = new unsigned long long[totalSchedules];
    // std::fill_n(scheduleFirstRunTimeStamp, 100, 0);  // reset array
    // std::fill_n(nextScheduleTimeStamp, 100, 0);  // reset array

	for(short a=0; a < totalSchedules; a++)  // go through all schedules found
	{
		if (scheduleEnabled[a] == 1)  // check if schedule is enabled
		{
			short schHour = floor(scheduleStart[a] / 3600); // extract hours from total seconds
			short schMinute = floor((scheduleStart[a] - (schHour * 3600)) / 60); // extract minutes from total seconds
			short schSecond = scheduleStart[a] - (schHour * 3600) - (schMinute * 60); //  extract seconds from total seconds

			short nextSchFound = 0;  // flag when next schedule is found

			if (scheduleRepeatDays[a] != 0)  // schedule is set to repeat X number of days
			{
				// make sure days of week aren't active as schedule uses repeat
				scheduleMon[a] = 0;
				scheduleTue[a] = 0;
				scheduleWed[a] = 0;
				scheduleThu[a] = 0;
				scheduleFri[a] = 0;
				scheduleSat[a] = 0;
				scheduleSun[a] = 0;

				nextScheduleTimeStamp[a] = rtc.tmConvert_t(rtc.getCurrentYear(),rtc.getCurrentMonth(),rtc.getCurrentDay(),schHour,schMinute,schSecond);  // get next schedule in millis
				if (nextScheduleTimeStamp[a] <= currentMillisTime) {nextScheduleTimeStamp[a] = nextScheduleTimeStamp[a] + 86400000;}  // add day as schedule has already past

				// disassemble date schedule was created
				std::string createYear = scheduleCreated[a].substr(0,4);
				std::string createMonth = scheduleCreated[a].substr(5,2);
				std::string createDay = scheduleCreated[a].substr(8,2);
				
				scheduleFirstRunTimeStamp[a] = rtc.tmConvert_t(std::stoi(createYear),std::stoi(createMonth)-1,std::stoi(createDay),schHour,schMinute,schSecond);  // calculate first time schedule was ever run in millis
			
				// 1 day is counted after the schedule runs
				unsigned long long daysPast = (currentMillisTime - scheduleFirstRunTimeStamp[a]) / 86400000;  // calculate days gone by since schedule first ran
				if (scheduleFirstRunTimeStamp[a] == nextScheduleTimeStamp[a]) {daysPast = 0;}  // if schedule was just created but hasn't run yet set daysPast to 0
				else if ((daysPast == 0) && (nextScheduleTimeStamp[a] > currentMillisTime)) {daysPast = 1;}  // if time isn't 24hrs after schedule running days past is zero so we manually add one to get started
				
				unsigned long long cycleCount = (daysPast / scheduleRepeatDays[a]) + 1; // number of times the schedule has run since created
				if (daysPast == 0) {cycleCount = 0;}  // if daysPast is 0 schedule cycles must be 0

				// calculate how many millis needs to be added to our first run time to get next schedule
				nextScheduleTimeStamp[a] = (((cycleCount * scheduleRepeatDays[a]) * 86400000) + scheduleFirstRunTimeStamp[a]);

				// std::cout << "repeat - " << scheduleID[a] << " - " << scheduleRepeatDays[a] << " - " << daysPast << " - " << cycleCount << std::endl;
			}
			// schedule runs select days of the week
			else if ((scheduleSun[a] == 1) || (scheduleMon[a] == 1) || (scheduleTue[a] == 1) || (scheduleWed[a] == 1) || (scheduleThu[a] == 1) || (scheduleFri[a] == 1) || (scheduleSat[a] == 1))  // make sure at least one day is selected
			{
				nextScheduleTimeStamp[a] = rtc.tmConvert_t(rtc.getCurrentYear(),rtc.getCurrentMonth(),rtc.getCurrentDay(),schHour,schMinute,schSecond);  // get next schedule in millis
				
				short wd = rtc.getCurrentWeekday();  // used so we can change current weekday during search
				short dayCounter = 1;  // updates if another day needs to be searched
				for (short c = 0; c < dayCounter; c++)  // search days of week
				{
					if (nextScheduleTimeStamp[a] > currentMillisTime)  // check if possible schedule time has already past
					{
						if ((scheduleSun[a] == 1) && (wd == 0)) nextSchFound = 1;  // check if schedule should run today
						else if ((scheduleMon[a] == 1) && (wd == 1)) nextSchFound = 1;  // check if schedule should run today
						else if ((scheduleTue[a] == 1) && (wd == 2)) nextSchFound = 1;  // check if schedule should run today
						else if ((scheduleWed[a] == 1) && (wd == 3)) nextSchFound = 1;  // check if schedule should run today
						else if ((scheduleThu[a] == 1) && (wd == 4)) nextSchFound = 1;  // check if schedule should run today
						else if ((scheduleFri[a] == 1) && (wd == 5)) nextSchFound = 1;  // check if schedule should run today
						else if ((scheduleSat[a] == 1) && (wd == 6)) nextSchFound = 1;  // check if schedule should run today
						else nextSchFound = 2;  // nothing found today go to next day
					}
					else  // search next day
					{
						wd = wd + 1;
						if (wd == 7) wd = 0;
						if (scheduleRepeatDays[a] == 0) {nextScheduleTimeStamp[a] = nextScheduleTimeStamp[a] + 86400000;}  // add day
						dayCounter++;
					}
					if (nextSchFound == 2)  // search next day
					{
						wd = wd + 1;
						if (wd == 7) wd = 0;
						if (scheduleRepeatDays[a] == 0) {nextScheduleTimeStamp[a] = nextScheduleTimeStamp[a] + 86400000;}  // add day
						dayCounter++;
					}
				}
			}
			// std::cout << scheduleID[a] << " - " << scheduleFirstRunTimeStamp[a] << " - " << nextScheduleTimeStamp[a] << std::endl;
		}
	}
	
	// std::cout << "currentWeekday - " << rtc.getCurrentWeekday() << std::endl;
	// std::cout << "currentDay     - " << rtc.getCurrentDay() << std::endl;
	// std::cout << "currentMonth   - " << rtc.getCurrentMonth() << std::endl;
	// std::cout << "currentYear    - " << rtc.getCurrentYear() << std::endl;

	sortSchedules();
	nextUpcomingSchedule = 0;
	nextUpcomingID = 0;
	for(short a=0; a < totalSchedules; a++)  // find next schedule to be triggered
	{
		if (scheduleEnabled[a] == 1)  // check if schedule is enabled
		{
			if (nextScheduleTimeStamp[a] != 0) {nextUpcomingSchedule = nextScheduleTimeStamp[a]; nextUpcomingID = scheduleID[a]; break;}
		}
	}
	
	std::cout << " " << std::endl;
	std::cout << "nextUpcomingSchedule - " << nextUpcomingSchedule << std::endl;
	std::cout << "nextUpcomingID       - " << nextUpcomingID << std::endl;

}

void schedules::readSchedules() {  // read schedules from database
	if (startupComplete == 0) {
		std::cout << " " << std::endl;
		std::cout << "readSchedules--------------------------------" << std::endl;
	}
    MYSQL_RES *res;
    MYSQL_ROW row;

    delete[] scheduleID;  // reset array
    delete[] scheduleName;  // reset array
    delete[] scheduleTypeID;  // reset array
    delete[] scheduleRuleID;  // reset array
    delete[] schedulePinID;  // reset array
    delete[] scheduleOffOn;  // reset array
    delete[] scheduleStart;  // reset array
    delete[] scheduleSun;  // reset array
    delete[] scheduleMon;  // reset array
    delete[] scheduleTue;  // reset array
    delete[] scheduleWed;  // reset array
    delete[] scheduleThu;  // reset array
    delete[] scheduleFri;  // reset array
    delete[] scheduleSat;  // reset array
    delete[] scheduleRepeatDays;  // reset array
    delete[] scheduleEnabled;  // reset array
    delete[] scheduleEmail;  // reset array
    delete[] scheduleAlert;  // reset array
    delete[] scheduleCreated;  // reset array
    delete[] scheduleML;  // reset array
	countIncoming = 0;

    // std::fill_n(scheduleID, 100, 0);  // reset array
    // std::fill_n(scheduleName, 100, 0);  // reset array
    // std::fill_n(scheduleTypeID, 100, 0);  // reset array
    // std::fill_n(scheduleRuleID, 100, 0);  // reset array
    // std::fill_n(schedulePinID, 100, 0);  // reset array
    // std::fill_n(scheduleOffOn, 100, 0);  // reset array
    // std::fill_n(scheduleStart, 100, 0);  // reset array
    // std::fill_n(scheduleSun, 100, 0);  // reset array
    // std::fill_n(scheduleMon, 100, 0);  // reset array
    // std::fill_n(scheduleTue, 100, 0);  // reset array
    // std::fill_n(scheduleWed, 100, 0);  // reset array
    // std::fill_n(scheduleThu, 100, 0);  // reset array
    // std::fill_n(scheduleFri, 100, 0);  // reset array
    // std::fill_n(scheduleSat, 100, 0);  // reset array
    // std::fill_n(scheduleRepeatDays, 100, 0);  // reset array
    // std::fill_n(scheduleEnabled, 100, 0);  // reset array
    // std::fill_n(scheduleEmail, 100, 0);  // reset array
    // std::fill_n(scheduleAlert, 100, 0);  // reset array
    // std::fill_n(scheduleCreated, 100, 0);  // reset array
    // std::fill_n(scheduleML, 100, 0);  // reset array
	
    totalSchedules = 0;
    std::string query = "SELECT COUNT(*) FROM schedules";
    connectMysql();
    mysql_query(conn, query.c_str());  // run query
    res = mysql_use_result(conn);
    while ((row = mysql_fetch_row(res)) != NULL) {totalSchedules = atoi(row[0]);}
    mysql_free_result(res);
    // mysql_disconnect();

    scheduleID = new short[totalSchedules];
    scheduleName = new std::string[totalSchedules];
    scheduleTypeID = new short[totalSchedules];
    scheduleRuleID = new short[totalSchedules];
    schedulePinID = new short[totalSchedules];
    scheduleOffOn = new short[totalSchedules];
    scheduleStart = new unsigned long long[totalSchedules];
    scheduleSun = new bool[totalSchedules];
    scheduleMon = new bool[totalSchedules];
    scheduleTue = new bool[totalSchedules];
    scheduleWed = new bool[totalSchedules];
    scheduleThu = new bool[totalSchedules];
    scheduleFri = new bool[totalSchedules];
    scheduleSat = new bool[totalSchedules];
    scheduleRepeatDays = new short[totalSchedules];
    scheduleEnabled = new bool[totalSchedules];
    scheduleEmail = new bool[totalSchedules];
    scheduleAlert = new bool[totalSchedules];
    scheduleCreated = new std::string[totalSchedules];
    scheduleML = new float[totalSchedules];

    query = "SELECT schedules.scheduleID, schName, scheduleTypeID, ruleID, pinID, offOn, start,\
		sun, mon, tue, wed, thu, fri, sat, repeatDays, enabled, schEmail, schAlert, dateCreated,\
		dosingSchedules.mL\
		FROM schedules\
        LEFT JOIN dosingSchedules\
        ON schedules.scheduleID = dosingSchedules.scheduleID\
		ORDER BY scheduleID ASC";  // construct query string

    // connectMysql();
    // std::string query = "SELECT scheduleID, scheduleTypeID, ruleID, offOn, start, sun, mon, tue, wed, thu, fri, sat, repeatDays, enabled FROM schedules ORDER BY scheduleID ASC";  // construct query string
    mysql_query(conn, query.c_str());  // run query

    res = mysql_use_result(conn);
	totalSchedules = 0;
    while ((row = mysql_fetch_row(res)) != NULL)
    {
		scheduleID[totalSchedules] = atoi(row[0]);
		scheduleName[totalSchedules] = row[1];
		scheduleTypeID[totalSchedules] = atoi(row[2]);
		if (row[3] != NULL) {scheduleRuleID[totalSchedules] = atoi(row[3]);}
		else {scheduleRuleID[totalSchedules] = 0;}
		schedulePinID[totalSchedules] = atoi(row[4]);
		scheduleOffOn[totalSchedules] = atoi(row[5]);
		scheduleStart[totalSchedules] = atoi(row[6]);
		scheduleSun[totalSchedules] = atoi(row[7]);
		scheduleMon[totalSchedules] = atoi(row[8]);
		scheduleTue[totalSchedules] = atoi(row[9]);
		scheduleWed[totalSchedules] = atoi(row[10]);
		scheduleThu[totalSchedules] = atoi(row[11]);
		scheduleFri[totalSchedules] = atoi(row[12]);
		scheduleSat[totalSchedules] = atoi(row[13]);
		scheduleRepeatDays[totalSchedules] = atoi(row[14]);
		scheduleEnabled[totalSchedules] = atoi(row[15]);
		scheduleEmail[totalSchedules] = atoi(row[16]);
		scheduleAlert[totalSchedules] = atoi(row[17]);
		scheduleCreated[totalSchedules] = row[18];
		if (row[19] != NULL) {scheduleML[totalSchedules] = atof(row[19]);}
		else {scheduleML[totalSchedules] = 0;}
		totalSchedules++;
    }
    mysql_disconnect(); 
    mysql_free_result(res);

	// std::cout << "totalSchedules      - " << totalSchedules << std::endl;

    // std::cout << "scheduleID      ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleID[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleName    ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleName[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleTypeID  ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleTypeID[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleRuleID  ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleRuleID[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "schedulePinID   ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << schedulePinID[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleOffOn   ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleOffOn[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleStart   ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleStart[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleSun     ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleSun[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleMon     ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleMon[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleTue     ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleTue[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleWed     ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleWed[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleThu     ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleThu[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleFri     ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleFri[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleSat     ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleSat[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleRepeatDays -- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleRepeatDays[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleEnabled ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleEnabled[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleEmail   ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleEmail[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleAlert   ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleAlert[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleCreated ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleCreated[a] << ", ";}
    // std::cout << "" << std::endl;

    // std::cout << "scheduleML      ----- ";
    // for(short a=0; a < totalSchedules; a++) {std::cout << scheduleML[a] << ", ";}
    // std::cout << "" << std::endl;


	calculateSchedules();
	smartStart();
}

void schedules::runSchedule() {  // run schedule when time
    // std::cout << " " << std::endl;
    std::cout << "RUNNING SCHEDULE     = " << nextUpcomingID << std::endl;

	short arrayLoc = 255;

	for (short a = 0; a < totalSchedules; a++)  // get array location for schedule
	{
		if (scheduleID[a] == nextUpcomingID) {arrayLoc = a; break;}
	}
	// std::cout << "arrayLoc - " << arrayLoc << std::endl;

	if (scheduleEmail[arrayLoc] == 1) {emails.sendEmail("Schedule " + scheduleName[arrayLoc] + " Ran", "Message from Robo-Tank Aquarium Controller");}

	if (scheduleTypeID[arrayLoc] == 1)  // schedule is for AC outlet
	{
		if (scheduleOffOn[arrayLoc] == 0) {outlets.outletOff(schedulePinID[arrayLoc],3); systemSettings.logActivity(3,1,schedulePinID[arrayLoc],0,scheduleName[arrayLoc]);}
		else if (scheduleOffOn[arrayLoc] == 1) {outlets.outletOn(schedulePinID[arrayLoc],3); systemSettings.logActivity(3,1,schedulePinID[arrayLoc],1,scheduleName[arrayLoc]);}
	}
	else if (scheduleTypeID[arrayLoc] == 2)  // schedule is for DC ports
	{
		if (scheduleOffOn[arrayLoc] == 0) {dcPorts.dcOff(schedulePinID[arrayLoc],3); systemSettings.logActivity(3,2,schedulePinID[arrayLoc],0,scheduleName[arrayLoc]);}
		else if (scheduleOffOn[arrayLoc] == 1) {dcPorts.dcOn(schedulePinID[arrayLoc],3); systemSettings.logActivity(3,2,schedulePinID[arrayLoc],1,scheduleName[arrayLoc]);}
	}
	else if (scheduleTypeID[arrayLoc] == 3)  // schedule is for dosing pumps
	{
		dosing.startStirrer(schedulePinID[arrayLoc],3,scheduleML[arrayLoc]);
        systemSettings.logActivity(3,3,schedulePinID[arrayLoc],1,scheduleName[arrayLoc]);  // log action
	}

	// check if another schedule for same time is waiting
	if (nextScheduleTimeStamp[arrayLoc] == nextScheduleTimeStamp[arrayLoc+1]) {nextUpcomingID = scheduleID[arrayLoc+1]; runSchedule();}
}

void schedules::monitorSchedules() {  // continually monitor schedules
	if ((currentMillisTime > nextUpcomingSchedule) && (nextUpcomingSchedule != 0))
	{
		// std::cout << " " << std::endl;
		// std::cout << "SCHEDULE ACTIVIATED -------------------" << std::endl;
		runSchedule();
		calculateSchedules();
	}
}

void schedules::deleteSchedule(int schID) {  // delete schedule from frontend
    // std::cout << "deleteSchedule  - " << schID << endl;
    connectMysql();
    std::string query = "DELETE FROM schedules WHERE scheduleID = '" + to_string(schID) + "'";
    mysql_query(conn, query.c_str());  // run query
    mysql_disconnect(); 

	readSchedules();
}

void schedules::addDoseGroup(std::string data) {  // add group schedule for dosing pump
    // std::cout << "addDoseGroup - " << endl;

	if (countIncoming <= 17)  // get incoming data
    {
		incomingGroup[countIncoming] = data;
    	// std::cout << "incomingGroup[" << countIncoming << "] - " << incomingGroup[countIncoming] << std::endl;
    	countIncoming++;
	}

	if (countIncoming == 17)  // all data received, process it.
	{
		int start = std::stoi(incomingGroup[1]);
		int end = std::stoi(incomingGroup[2]);
		int length = 0;

		float doses = 0;
		float mL = 0.0;
		
		if (end > start) {length = end - start;}  // get length of group schedule
		else {length = (end - start) + 86400;}

		if (std::stoi(incomingGroup[4]) == 0)  // using mL's per dose
		{
			doses = (std::stof(incomingGroup[0]) / std::stof(incomingGroup[3]));
			mL = std::stof(incomingGroup[3]);
		}
		else  // using number of doses during time frame 
		{
			doses = std::stof(incomingGroup[4]);
			mL = std::stof(incomingGroup[0]) / std::stof(incomingGroup[4]);
		}
		
		float fractionPart, intPart;
		fractionPart = modff (doses , &intPart);  // extract fractional part so we know if we need to adjust number of doses

		if (fractionPart >= 0.5) 
		{
			doses = (doses + 1) - fractionPart;  // add extra dose
			mL = std::stof(incomingGroup[0]) / doses;
		}
		else if (fractionPart < 0.5) 
		{
			doses = doses - fractionPart;  // remove fractional part
			mL = std::stof(incomingGroup[0]) / doses;
		}

		countIncoming = doses;  // using temporarily so we don't recalcate schedules until group has been added to database
		int steps = length / doses;  // calculate number of steps between schedules

        if (incomingGroup[5] == "1") 
		{
            incomingGroup[6] = "1";
            incomingGroup[7] = "1";
            incomingGroup[8] = "1";
            incomingGroup[9] = "1";
            incomingGroup[10] = "1";
            incomingGroup[11] = "1";
            incomingGroup[12] = "1";
            incomingGroup[5] = "0";
        }

		int* doseTime;
    	doseTime = new int[(int)doses];

		doseTime[0] = std::stoi(incomingGroup[1]);  // get first dose time
		std::string n = incomingGroup[13] + " - 1 of " + to_string((int)doses);  // create schedule name
		
		char name[n.length() + 1];
		strcpy(name, n.c_str());  // convert string to char array
		// std::cout << "doseTime[" << 0 << "] - " << name << " - " << doseTime[0] << std::endl;
		addSchedule(1,3,1,name,doseTime[0],std::stoi(incomingGroup[6]),std::stoi(incomingGroup[7]),std::stoi(incomingGroup[8]),std::stoi(incomingGroup[9]),std::stoi(incomingGroup[10]),std::stoi(incomingGroup[11]),std::stoi(incomingGroup[12]),std::stoi(incomingGroup[5]),std::stoi(incomingGroup[16]),std::stoi(incomingGroup[14]),std::stoi(incomingGroup[15]),to_string(mL));  // add light mode - modeNumber, start, end, name
		
		for (short i = 1; i < doses; i++)  // calculate and add extra schedules
		{
			doseTime[i] = doseTime[i-1] + steps;  // add schedule step
			if (doseTime[i] > 86400) {doseTime[i] = doseTime[i] - 86400;}  // remove a day when we go over midnight
			std::string n = incomingGroup[13] + " - " + to_string(i + 1) + " of " + to_string((int)doses);  // create schedule name
			
			char name[n.length() + 1];
			strcpy(name, n.c_str());  // convert string to char array

    		// std::cout << "doseTime[" << i << "] - " << name << " - " << doseTime[i] << std::endl;
			addSchedule(i+1,3,1,name,doseTime[i],std::stoi(incomingGroup[6]),std::stoi(incomingGroup[7]),std::stoi(incomingGroup[8]),std::stoi(incomingGroup[9]),std::stoi(incomingGroup[10]),std::stoi(incomingGroup[11]),std::stoi(incomingGroup[12]),std::stoi(incomingGroup[5]),std::stoi(incomingGroup[16]),std::stoi(incomingGroup[14]),std::stoi(incomingGroup[15]),to_string(mL));  // add light mode - modeNumber, start, end, name

		}
		countIncoming = 0;
	}
	// incomingGroup[0] = 0;  // total milliliters
	// incomingGroup[1] = 0;  // start time
	// incomingGroup[2] = 0;  // finish time
	// incomingGroup[3] = 0;  // milliliters per dose
	// incomingGroup[4] = 0;  // number of doses during time frame
	// incomingGroup[5] = 0;  // repeat number of days
	// incomingGroup[6] = 0;  // Monday
	// incomingGroup[7] = 0;  // Tuesday
	// incomingGroup[8] = 0;  // Wednesday
	// incomingGroup[9] = 0;  // Thursday
	// incomingGroup[10] = 0;  // Friday
	// incomingGroup[11] = 0;  // Saturday
	// incomingGroup[12] = 0;  // Sunday
	// incomingGroup[13] = 0;  // description
	// incomingGroup[14] = 0;  // email
	// incomingGroup[15] = 0;  // alert
	// incomingGroup[16] = 0;  // pinID
}

void schedules::addSchedule(short source, short type, short offOn, char* schName, int start, bool mon, bool tue, bool wed, bool thu, bool fri, bool sat, bool sun, short repeatDays, short pinID, bool email, bool alert, std::string mL) {  // update existing

    // std::cout << "addSchedule - " << endl;
    std::cout << "addSchedule    - " << source << std::endl;
    // std::cout << "type        - " << type << std::endl;
    // std::cout << "offOn       - " << offOn << std::endl;
    // std::cout << "name        - " << schName << std::endl;
    // std::cout << "start       - " << start << std::endl;
    // std::cout << "mon         - " << mon << std::endl;
    // std::cout << "tue         - " << tue << std::endl;
    // std::cout << "wed         - " << wed << std::endl;
    // std::cout << "thu         - " << thu << std::endl;
    // std::cout << "fri         - " << fri << std::endl;
    // std::cout << "sat         - " << sat << std::endl;
    // std::cout << "sun         - " << sun << std::endl;
    // std::cout << "repeatDays  - " << repeatDays << std::endl;
    // std::cout << "pinID       - " << pinID << std::endl;
    // std::cout << "email       - " << email << std::endl;
    // std::cout << "alert       - " << alert << std::endl;
    // std::cout << "mL          - " << mL << std::endl;

	std::string date = to_string(rtc.getCurrentYear()) + "-" + to_string(rtc.getCurrentMonth()+1) + "-" + to_string(rtc.getCurrentDay());

    // std::cout << "date        - " << date << std::endl;
	connectMysql();
	std::string query = "INSERT INTO schedules(scheduleTypeID,pinID,offOn,schName,start,mon,tue,wed,thu,fri,sat,sun,repeatDays,schEmail,schAlert,dateCreated) VALUES('" + to_string(type) + "','" + to_string(pinID) + "','" + to_string(offOn) + "','" + schName + "','" + to_string(start) + "','" + to_string(mon) + "','" + to_string(tue) + "','" + to_string(wed) + "','" + to_string(thu) + "','" + to_string(fri) + "','" + to_string(sat) + "','" + to_string(sun) + "','" + to_string(repeatDays) + "','" + to_string(email) + "','" + to_string(alert) + "','" + date + "')";
	mysql_query(conn, query.c_str());  // run query

	mysql_disconnect();

	
	if (type == 3)
	{
		MYSQL_RES *res;
		MYSQL_ROW row;
		connectMysql();
		int ID = 0;
		std::string query = "SELECT scheduleID FROM schedules ORDER BY scheduleID DESC LIMIT 1";  // construct query string

		mysql_query(conn, query.c_str());  // run query
		res = mysql_use_result(conn);
		
		while ((row = mysql_fetch_row(res)) != NULL)
		{
			ID = atoi(row[0]);
		}

		query = "INSERT INTO dosingSchedules(scheduleID,mL) VALUES('" + to_string(ID) + "','" + mL + "')";
		mysql_query(conn, query.c_str());  // run query

		mysql_disconnect();
		mysql_free_result(res);
	}

	if (source == 0) {readSchedules();}
	else if (source == countIncoming) {readSchedules();}
}

void schedules::saveSchedule(int schID, short offOn, char* schName, int start, bool mon, bool tue, bool wed, bool thu, bool fri, bool sat, bool sun, short repeatDays, bool enabled, bool email, bool alert, std::string mL) {  // update existing
    // std::cout << "saveSchedule----------- " << endl;
    // std::cout << "schID       - " << schID << std::endl;
    // std::cout << "offOn       - " << offOn << std::endl;
    // std::cout << "schName     - " << schName << std::endl;
    // std::cout << "start       - " << start << std::endl;
    // std::cout << "mon         - " << mon << std::endl;
    // std::cout << "tue         - " << tue << std::endl;
    // std::cout << "wed         - " << wed << std::endl;
    // std::cout << "thu         - " << thu << std::endl;
    // std::cout << "fri         - " << fri << std::endl;
    // std::cout << "sat         - " << sat << std::endl;
    // std::cout << "sun         - " << sun << std::endl;
    // std::cout << "repeatDays  - " << repeatDays << std::endl;
    // std::cout << "enabled     - " << enabled << std::endl;
    // std::cout << "email       - " << email << std::endl;
    // std::cout << "alert       - " << alert << std::endl;
    // std::cout << "mL          - " << mL << std::endl;

	short arrayLoc = 255;
	for (short a = 0; a < totalSchedules; a++)  // get array location for schedule
	{
		if (scheduleID[a] == schID) {arrayLoc = a; break;}
	}
	// std::cout << "arrayLoc - " << arrayLoc << std::endl;

	scheduleOffOn[arrayLoc] = offOn;
	scheduleStart[arrayLoc] = start;
	scheduleSun[arrayLoc] = sun;
	scheduleMon[arrayLoc] = mon;
	scheduleTue[arrayLoc] = tue;
	scheduleWed[arrayLoc] = wed;
	scheduleThu[arrayLoc] = thu;
	scheduleFri[arrayLoc] = fri;
	scheduleSat[arrayLoc] = sat;
	scheduleRepeatDays[arrayLoc] = repeatDays;
	scheduleEnabled[arrayLoc] = enabled;
	scheduleEmail[arrayLoc] = email;
	scheduleAlert[arrayLoc] = alert;
	scheduleML[arrayLoc] = std::stof(mL);

	connectMysql();
    std::string query = "UPDATE schedules SET offOn = '" + to_string(offOn) + "', schName = '" + schName + "', start = '" + to_string(start) + "', mon = '" + to_string(mon) + "', tue = '" + to_string(tue) + "', wed = '" + to_string(wed) + "', thu = '" + to_string(thu) + "', fri = '" + to_string(fri) + "', sat = '" + to_string(sat) + "', sun = '" + to_string(sun) + "', repeatDays = '" + to_string(repeatDays) + "', enabled = '" + to_string(enabled) + "', schEmail = '" + to_string(email) + "', schAlert = '" + to_string(alert) + "' WHERE scheduleID = '" + to_string(schID) + "'";
    mysql_query(conn, query.c_str());  // run query
	
    query = "UPDATE dosingSchedules SET mL = '" + mL + "' WHERE scheduleID = '" + to_string(schID) + "'";
    mysql_query(conn, query.c_str());  // run query
    mysql_disconnect(); 

	calculateSchedules();
}

void schedules::enableSchedule(short schID, short value) {  // enable/disable schedule from frontend
    // std::cout << "enableSchedule  - " << endl;
	short arrayLoc = 255;
	for (short a = 0; a < totalSchedules; a++)  // get array location for schedule
	{
		if (scheduleID[a] == schID) {arrayLoc = a; break;}
	}
	// std::cout << "arrayLoc - " << arrayLoc << std::endl;

	scheduleEnabled[arrayLoc] = value;

    connectMysql();
    std::string query = "UPDATE schedules SET enabled = '" + to_string(value) + "' WHERE scheduleID = '" + to_string(schID) + "'";
    mysql_query(conn, query.c_str());  // run query
    mysql_disconnect(); 

	calculateSchedules();
}

void schedules::frontendResponse() {  // needed to send response back to front end 
	outgoingDataString = "{\"sc\":[";
	outgoingDataString += "{\"id\":\"";
	outgoingDataString += "0";
	outgoingDataString += "\"}";
	outgoingDataString += "]}";
}

schedules schedules;
