// File: /workspace/github_cpp_makefile_star2_O2/CDISC2020[P]Projet_satellite_agile/src/planManager/planManager.cpp
// Function: PlanManager::executePlan(Controller*, QueuingPort*, bool)
void PlanManager::executePlan(Controller* control,QueuingPort* ChannelCom, bool mode)
{
	if(plan!=NULL)
	{
		GenericInstruction* currentInst;

		bool jumpInstruction;

		currentInst = plan->getInstruction(plan->ptInstruction);
		jumpInstruction = false;

		time_t t = time(0);
		struct tm * now = localtime(&t);
		cout << "Inst " << plan->ptInstruction+1 << "/" << plan->getnInstructions() << endl;
		cout << "now         = " << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << endl;
		cout << "Instruction = " << currentInst->getHour() << ":" << currentInst->getMin() << ":" << currentInst->getSec() << endl;

		int group = currentInst->getIndex();
		if ((bannedInstructions[group]) == true) jumpInstruction = true;

		if ((jumpInstruction == false))
		{
			if ((now->tm_hour == currentInst->getHour()) & (now->tm_min == currentInst->getMin()) & (now->tm_sec == currentInst->getSec()))
			{
			/*
			/ Codes des messages a envoyer :
			/ 0 = Attitude
			/ 1 = Photo
			/ 2 = RetourController
			/ 3 = PlanFilePath
			/ 4 = Status
			/ 5 = data
			*/
				cout<<"C'est l'heure "<<endl;
				bool start_timeout = false;
				/*--------------Call Controller to execute instruction-------------*/
				if(currentInst->getType() == 'p' || currentInst->getType() == 'a')
				{
					cout << "control EXECUTE!" << endl;
					if(mode)
						control->executeInstruction(currentInst, &responseController);
					else
						responseController=1;
					start_timeout = true;
				}
				else
				{
					cout << "hum probleme" << endl;
					Status S;
					S.code = 4;
					S.errorID = 2; // A changer.
					sprintf(S.description, "Wrong instruction type, instruction #%d : group #%d ", plan->ptInstruction+1,  group);

					ChannelCom->SendQueuingMsg((char*)&S, sizeof(Status));
				}
				/*---------------------------------------------------------*/

				/*-------------Wait for Time Out of the controller response-----------*/
				int time_out = 1000000;
				if (start_timeout == true)
				{
					while (time_out > 0 && responseController == 0)
					{
						usleep(1);
						time_out--;
					}
				}
				/*-------------------------------------------------------------------*/

				/*--------------------   Management of the errors    ----------------*/
				if (responseController != 1)
				{
					bannedInstructions[group] = true;
					if (responseController == -1)
					{
						cout<<"Instruction "<<group<<" banned : error from controller"<<endl;
						Status S_controller;
						S_controller.code = 4;

						if (currentInst->getType() == 'a')
						{
							// The attitude was not reached
							S_controller.errorID = 0; 	// Look the error list
							sprintf(S_controller.description, "Attitude not reached. instruction #%d : group #%d ", plan->ptInstruction+1,  group);
						}
						if (currentInst->getType() == 'p')
						{
							// The photo was not taken
							S_controller.errorID = 1; // Look the error list
							sprintf(S_controller.description, "Photo not talken. instruction #%d : group #%d ", plan->ptInstruction+1,  group);
						}

						cout << S_controller.description << endl;

						ChannelCom->SendQueuingMsg((char*)&S_controller, sizeof(Status));
					}
					if (time_out == 0)
					{
						cout<<"Instruction "<<group<<" banned : timeout."<<endl;
						// Time out in the response of the attitude controller
					}
				}
				/*-------------------------------------------------------------------*/

				/*-----------------   Photo sending to the ComGrdMnger  -------------*/
				if ( responseController == 1  && currentInst->getType() == 'p')
				{
					PlanFilePath PhotoPath;
					PhotoPath.code = 3;
					string aux =  ((PhotoInstruction*)currentInst)->getPhotoName(); // attention chemin
					strcpy (PhotoPath.filepath , aux.c_str()) ;
					printf("Sending the photo %s \n", PhotoPath.filepath);
					ChannelCom->SendQueuingMsg((char*)&PhotoPath, sizeof(PlanFilePath));
				}
				/*-------------------------------------------------------------------*/

				responseController = 0;
				plan->ptInstruction++;


			}
			// Cas où le temps de l'instruction est déjà passé... on l'aura jamais !
			else if ( (now->tm_hour > currentInst->getHour())
				|| ((now->tm_hour == currentInst->getHour()) && (now->tm_min > currentInst->getMin()))
				|| ((now->tm_min  == currentInst->getMin())  && (now->tm_sec > currentInst->getSec())))
			{
				cout<<"L'heure est deja passé "<<plan->ptInstruction<<endl;
				Status S_heure;
				S_heure.code = 4;
				S_heure.errorID = 3;
				ChannelCom->SendQueuingMsg((char*)&S_heure, sizeof(Status));

				plan->ptInstruction++;
			}
		}
		else if (jumpInstruction)
		{
			plan->ptInstruction++;
			cout<<"Instruction jumped"<<endl;
		}


		if (plan->ptInstruction >= plan->getnInstructions()) // Fin d'un plan d'instruction, on efface le plan en cours
		{
			cout << "Le plan est terminé" << endl;
			this->destructPlan();
		}
	}

	else
		cout << "No plan to execute !" << endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x37e9
movq %rdx, -0x58(%rbp)
movl %ecx, -0x44(%rbp)
movq %rsi, -0x68(%rbp)
movl 0x324(%rdi), %esi
callq 0x35e4
movq %rax, %r15
xorl %edi, %edi
callq 0x2200
leaq -0x70(%rbp), %rdi
movq %rax, (%rdi)
callq 0x2190
movq %rax, %r13
movq 0x4937(%rip), %r12      # 0x7fd0
leaq 0x1c48(%rip), %rsi      # 0x52e8
movq %r12, %rdi
callq 0x2240
movq (%rbx), %rcx
movl 0x324(%rcx), %esi
incl %esi
movq %rax, %rdi
callq 0x2400
leaq 0x2179(%rip), %rsi      # 0x583b
movq %rax, %rdi
callq 0x2240
movq %rax, %r14
movq (%rbx), %rdi
callq 0x35d8
movq %r14, %rdi
movl %eax, %esi
callq 0x2400
movq %rax, %rdi
callq 0x2150
leaq 0x1c00(%rip), %rsi      # 0x52ee
movq %r12, %rdi
callq 0x2240
movl 0x8(%r13), %esi
movq %rax, %rdi
callq 0x2400
leaq 0x1bf4(%rip), %r14      # 0x52fd
movq %rax, %rdi
movq %r14, %rsi
callq 0x2240
movl 0x4(%r13), %esi
movq %rax, %rdi
callq 0x2400
movq %rax, %rdi
movq %r14, %rsi
callq 0x2240
movl (%r13), %esi
movq %rax, %rdi
callq 0x2400
movq %rax, %rdi
callq 0x2150
leaq 0x1bb9(%rip), %rsi      # 0x52ff
movq %r12, %rdi
callq 0x2240
movq %rax, %r12
movq %r15, %rdi
callq 0x2ece
movq %r12, %rdi
movl %eax, %esi
callq 0x2400
movq %rax, %rdi
movq %r14, %rsi
callq 0x2240
movq %rax, %r12
movq %r15, %rdi
callq 0x2ed8
movq %r12, %rdi
movq %r15, %r12
movl %eax, %esi
callq 0x2400
movq %rax, %rdi
movq %r14, %rsi
callq 0x2240
movq %rax, %r14
movq %r15, %rdi
callq 0x2ee2
movq %r14, %rdi
movl %eax, %esi
callq 0x2400
movq %rax, %rdi
callq 0x2150
movq %r15, %rdi
callq 0x2ef6
movslq %eax, %rcx
cmpb $0x0, 0x8(%rbx,%rcx)
je 0x3815
movq (%rbx), %rax
incl 0x324(%rax)
movq 0x4800(%rip), %rdi      # 0x7fd0
leaq 0x1c6b(%rip), %rsi      # 0x5442
callq 0x2240
movq %rax, %rdi
callq 0x2150
jmp 0x3bf1
movq 0x47e0(%rip), %rdi      # 0x7fd0
leaq 0x1c73(%rip), %rsi      # 0x546a
callq 0x2240
movq %rax, %rdi
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2150
movq %rcx, -0x60(%rbp)
movl %eax, -0x2c(%rbp)
movq %rbx, -0x50(%rbp)
movl 0x8(%r13), %r15d
movq %r12, %rdi
callq 0x2ece
movl %eax, %ebx
movl 0x4(%r13), %eax
movl %eax, -0x40(%rbp)
movq %r12, %rdi
callq 0x2ed8
movl %eax, %r14d
movl (%r13), %eax
movl %eax, -0x3c(%rbp)
movq %r12, -0x38(%rbp)
movq %r12, %rdi
callq 0x2ee2
cmpl %ebx, %r15d
jne 0x38e8
cmpl %r14d, -0x40(%rbp)
jne 0x38e8
cmpl %eax, -0x3c(%rbp)
jne 0x38e8
movq 0x475e(%rip), %rdi      # 0x7fd0
leaq 0x1a95(%rip), %rsi      # 0x530e
callq 0x2240
movq %rax, %rdi
callq 0x2150
movq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0x2eec
cmpb $0x70, %al
movq -0x58(%rbp), %r15
movq -0x50(%rbp), %rbx
je 0x38ae
movq %r12, %rdi
callq 0x2eec
cmpb $0x61, %al
jne 0x39e4
movq 0x471b(%rip), %rdi      # 0x7fd0
leaq 0x1a61(%rip), %rsi      # 0x531d
callq 0x2240
movq %rax, %rdi
callq 0x2150
leaq 0x6c(%rbx), %rdx
cmpb $0x0, -0x44(%rbp)
je 0x39a8
movq -0x68(%rbp), %rdi
movq %r12, %rsi
callq 0x48ee
jmp 0x39ae
movl 0x8(%r13), %r14d
movq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0x2ece
cmpl %eax, %r14d
movq -0x58(%rbp), %r15
movq -0x50(%rbp), %rbx
jle 0x3957
movq 0x46c4(%rip), %rdi      # 0x7fd0
leaq 0x1b16(%rip), %rsi      # 0x5429
callq 0x2240
movq (%rbx), %rcx
movl 0x324(%rcx), %esi
movq %rax, %rdi
callq 0x2400
movq %rax, %rdi
callq 0x2150
movabsq $0x300000004, %rax      # imm = 0x300000004
leaq -0x118(%rbp), %rsi
movq %rax, (%rsi)
movq %r15, %rdi
movl $0x88, %edx
callq 0x29b2
jmp 0x3be8
movl 0x8(%r13), %r14d
movq %r12, %rdi
callq 0x2ece
cmpl %eax, %r14d
jne 0x3979
movl 0x4(%r13), %r14d
movq %r12, %rdi
callq 0x2ed8
cmpl %eax, %r14d
jg 0x3905
movl 0x4(%r13), %r14d
movq %r12, %rdi
callq 0x2ed8
cmpl %eax, %r14d
jne 0x3bf1
movl (%r13), %r14d
movq %r12, %rdi
callq 0x2ee2
cmpl %eax, %r14d
jg 0x3905
jmp 0x3bf1
movl $0x1, (%rdx)
movl $0xf4240, %r12d         # imm = 0xF4240
pushq $0x1
popq %r14
cmpl $0x0, 0x6c(%rbx)
jne 0x39d7
movl %r14d, %edi
callq 0x2310
leal -0x1(%r12), %eax
cmpl $0x1, %r12d
movl %eax, %r12d
ja 0x39b8
xorl %r12d, %r12d
testl %r12d, %r12d
sete %r14b
movq -0x38(%rbp), %r12
jmp 0x3a47
movq 0x45e5(%rip), %rdi      # 0x7fd0
leaq 0x193c(%rip), %rsi      # 0x532e
callq 0x2240
movq %rax, %rdi
callq 0x2150
movabsq $0x200000004, %rax      # imm = 0x200000004
leaq -0x110(%rbp), %rdi
movq %rax, -0x8(%rdi)
movq (%rbx), %rax
movl 0x324(%rax), %edx
incl %edx
leaq 0x1915(%rip), %rsi      # 0x533b
xorl %r14d, %r14d
movl -0x2c(%rbp), %ecx
xorl %eax, %eax
callq 0x2080
leaq -0x118(%rbp), %rsi
movq %r15, %rdi
movl $0x88, %edx
callq 0x29b2
movl 0x6c(%rbx), %eax
cmpl $0x1, %eax
je 0x3b7c
movq -0x60(%rbp), %rcx
movb $0x1, 0x8(%rbx,%rcx)
cmpl $-0x1, %eax
jne 0x3b3c
movq 0x4564(%rip), %rdi      # 0x7fd0
leaq 0x18fd(%rip), %rsi      # 0x5370
callq 0x2240
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x2400
leaq 0x18f3(%rip), %rsi      # 0x537d
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
callq 0x2150
movl $0x4, -0x118(%rbp)
movq %r12, %rdi
callq 0x2eec
cmpb $0x61, %al
jne 0x3ad7
leaq -0x110(%rbp), %rdi
andl $0x0, -0x4(%rdi)
movq (%rbx), %rax
movl 0x324(%rax), %edx
incl %edx
leaq 0x18d0(%rip), %rsi      # 0x539d
movl -0x2c(%rbp), %ecx
xorl %eax, %eax
callq 0x2080
movq %r12, %rdi
callq 0x2eec
cmpb $0x70, %al
jne 0x3b0d
leaq -0x110(%rbp), %rdi
movl $0x1, -0x4(%rdi)
movq (%rbx), %rax
movl 0x324(%rax), %edx
incl %edx
leaq 0x18cd(%rip), %rsi      # 0x53d0
movl -0x2c(%rbp), %ecx
xorl %eax, %eax
callq 0x2080
leaq -0x110(%rbp), %rsi
movq 0x44b5(%rip), %rdi      # 0x7fd0
callq 0x2240
movq %rax, %rdi
callq 0x2150
leaq -0x118(%rbp), %rsi
movq %r15, %rdi
movl $0x88, %edx
callq 0x29b2
testb %r14b, %r14b
je 0x3b76
movq 0x4488(%rip), %rdi      # 0x7fd0
leaq 0x1821(%rip), %rsi      # 0x5370
callq 0x2240
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x2400
leaq 0x1899(%rip), %rsi      # 0x53ff
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
callq 0x2150
cmpl $0x1, 0x6c(%rbx)
jne 0x3be4
movq %r12, %rdi
callq 0x2eec
cmpb $0x70, %al
jne 0x3be4
leaq -0x114(%rbp), %r14
movl $0x3, -0x4(%r14)
movq %r12, %rsi
leaq -0x90(%rbp), %r12
movq %r12, %rdi
callq 0x30ee
movq (%r12), %rsi
movq %r14, %rdi
callq 0x2210
leaq 0x1856(%rip), %rdi      # 0x5412
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
leaq -0x118(%rbp), %rsi
pushq $0x68
popq %rdx
movq %r15, %rdi
callq 0x29b2
leaq -0x90(%rbp), %rdi
callq 0x2140
andl $0x0, 0x6c(%rbx)
movq (%rbx), %rax
incl 0x324(%rax)
movq (%rbx), %rdi
movl 0x324(%rdi), %r14d
callq 0x35d8
cmpl %eax, %r14d
jl 0x3c28
movq 0x43c4(%rip), %rdi      # 0x7fd0
leaq 0x1842(%rip), %rsi      # 0x5455
callq 0x2240
movq %rax, %rdi
callq 0x2150
movq %rbx, %rdi
callq 0x3c52
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x2140
movq %rbx, %rdi
callq 0x2420
nop