// File: 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
subq $0x2f0, %rsp            # imm = 0x2F0
movb %cl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x288(%rbp)
cmpq $0x0, (%rax)
je 0x5ae8
movq -0x288(%rbp), %rax
movq (%rax), %rdi
movq (%rax), %rax
movl 0x324(%rax), %esi
callq 0x5160
movq %rax, -0x28(%rbp)
movb $0x0, -0x29(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3240
movq %rax, -0x38(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31b0
movq %rax, -0x40(%rbp)
movq 0x5cfc(%rip), %rdi      # 0xafd0
leaq 0x3012(%rip), %rsi      # 0x82ed
callq 0x3280
movq %rax, %rdi
movq -0x288(%rbp), %rax
movq (%rax), %rax
movl 0x324(%rax), %esi
addl $0x1, %esi
callq 0x3490
movq %rax, %rdi
leaq 0x3548(%rip), %rsi      # 0x884d
callq 0x3280
movq %rax, %rcx
movq -0x288(%rbp), %rax
movq %rcx, -0x2a8(%rbp)
movq (%rax), %rdi
callq 0x5140
movq -0x2a8(%rbp), %rdi
movl %eax, %esi
callq 0x3490
movq %rax, %rdi
movq 0x5c85(%rip), %rsi      # 0xafc0
callq 0x32b0
movq 0x5c89(%rip), %rdi      # 0xafd0
leaq 0x2fa5(%rip), %rsi      # 0x82f3
callq 0x3280
movq %rax, %rdi
movq -0x40(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x3490
movq %rax, %rdi
leaq 0x2f96(%rip), %rsi      # 0x8302
callq 0x3280
movq %rax, %rdi
movq -0x40(%rbp), %rax
movl 0x4(%rax), %esi
callq 0x3490
movq %rax, %rdi
leaq 0x2f78(%rip), %rsi      # 0x8302
callq 0x3280
movq %rax, %rdi
movq -0x40(%rbp), %rax
movl (%rax), %esi
callq 0x3490
movq %rax, %rdi
movq 0x5c19(%rip), %rsi      # 0xafc0
callq 0x32b0
movq 0x5c1d(%rip), %rdi      # 0xafd0
leaq 0x2f4a(%rip), %rsi      # 0x8304
callq 0x3280
movq %rax, -0x2a0(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4440
movq -0x2a0(%rbp), %rdi
movl %eax, %esi
callq 0x3490
movq %rax, %rdi
leaq 0x2f1b(%rip), %rsi      # 0x8302
callq 0x3280
movq %rax, -0x298(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4460
movq -0x298(%rbp), %rdi
movl %eax, %esi
callq 0x3490
movq %rax, %rdi
leaq 0x2eee(%rip), %rsi      # 0x8302
callq 0x3280
movq %rax, -0x290(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4480
movq -0x290(%rbp), %rdi
movl %eax, %esi
callq 0x3490
movq %rax, %rdi
movq 0x5b7f(%rip), %rsi      # 0xafc0
callq 0x32b0
movq -0x28(%rbp), %rdi
callq 0x44c0
movl %eax, %ecx
movq -0x288(%rbp), %rax
movl %ecx, -0x44(%rbp)
movslq -0x44(%rbp), %rcx
movb 0x8(%rax,%rcx), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x5471
movb $0x1, -0x29(%rbp)
movb -0x29(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x5a4b
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x2bc(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4440
movl %eax, %ecx
movl -0x2bc(%rbp), %eax
cmpl %ecx, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x2b4(%rbp)
movq -0x40(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2b8(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4460
movl -0x2b8(%rbp), %ecx
movl %eax, %edx
movl -0x2b4(%rbp), %eax
cmpl %edx, %ecx
sete %cl
andb $0x1, %cl
movzbl %cl, %ecx
andl %ecx, %eax
movl %eax, -0x2ac(%rbp)
movq -0x40(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2b0(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4480
movl -0x2b0(%rbp), %ecx
movl %eax, %edx
movl -0x2ac(%rbp), %eax
cmpl %edx, %ecx
sete %cl
andb $0x1, %cl
movzbl %cl, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x591a
movq 0x5aa9(%rip), %rdi      # 0xafd0
leaq 0x2de5(%rip), %rsi      # 0x8313
callq 0x3280
movq %rax, %rdi
movq 0x5a83(%rip), %rsi      # 0xafc0
callq 0x32b0
movb $0x0, -0x45(%rbp)
movq -0x28(%rbp), %rdi
callq 0x44a0
movsbl %al, %eax
cmpl $0x70, %eax
je 0x5568
movq -0x28(%rbp), %rdi
callq 0x44a0
movsbl %al, %eax
cmpl $0x61, %eax
jne 0x55be
movq 0x5a61(%rip), %rdi      # 0xafd0
leaq 0x2dac(%rip), %rsi      # 0x8322
callq 0x3280
movq %rax, %rdi
movq 0x5a3b(%rip), %rsi      # 0xafc0
callq 0x32b0
testb $0x1, -0x19(%rbp)
je 0x55aa
movq -0x288(%rbp), %rdx
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x6c, %rdx
callq 0x6e90
jmp 0x55b8
movq -0x288(%rbp), %rax
movl $0x1, 0x6c(%rax)
movb $0x1, -0x45(%rbp)
jmp 0x5638
movq 0x5a0b(%rip), %rdi      # 0xafd0
leaq 0x2d67(%rip), %rsi      # 0x8333
callq 0x3280
movq %rax, %rdi
movq 0x59e5(%rip), %rsi      # 0xafc0
callq 0x32b0
movq -0x288(%rbp), %rax
movl $0x4, -0xd0(%rbp)
movl $0x2, -0xcc(%rbp)
leaq -0xd0(%rbp), %rdi
addq $0x8, %rdi
movq (%rax), %rax
movl 0x324(%rax), %edx
addl $0x1, %edx
movl -0x44(%rbp), %ecx
leaq 0x2d24(%rip), %rsi      # 0x8340
movb $0x0, %al
callq 0x3090
movq -0x18(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
movl $0x88, %edx
callq 0x3c00
movl $0xf4240, -0xd4(%rbp)   # imm = 0xF4240
movb -0x45(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x569f
jmp 0x5651
xorl %eax, %eax
cmpl $0x0, -0xd4(%rbp)
movb %al, -0x2bd(%rbp)
jle 0x5676
movq -0x288(%rbp), %rax
cmpl $0x0, 0x6c(%rax)
sete %al
movb %al, -0x2bd(%rbp)
movb -0x2bd(%rbp), %al
testb $0x1, %al
jne 0x5682
jmp 0x569d
movl $0x1, %edi
callq 0x3390
movl -0xd4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x5651
jmp 0x569f
movq -0x288(%rbp), %rax
cmpl $0x1, 0x6c(%rax)
je 0x5826
movq -0x288(%rbp), %rax
movslq -0x44(%rbp), %rcx
movb $0x1, 0x8(%rax,%rcx)
cmpl $-0x1, 0x6c(%rax)
jne 0x57df
movq 0x58ff(%rip), %rdi      # 0xafd0
leaq 0x2c9d(%rip), %rsi      # 0x8375
callq 0x3280
movq %rax, %rdi
movl -0x44(%rbp), %esi
callq 0x3490
movq %rax, %rdi
leaq 0x2c90(%rip), %rsi      # 0x8382
callq 0x3280
movq %rax, %rdi
movq 0x58bf(%rip), %rsi      # 0xafc0
callq 0x32b0
movl $0x4, -0x15c(%rbp)
movq -0x28(%rbp), %rdi
callq 0x44a0
movsbl %al, %eax
cmpl $0x61, %eax
jne 0x575a
movq -0x288(%rbp), %rax
movl $0x0, -0x158(%rbp)
leaq -0x15c(%rbp), %rdi
addq $0x8, %rdi
movq (%rax), %rax
movl 0x324(%rax), %edx
addl $0x1, %edx
movl -0x44(%rbp), %ecx
leaq 0x2c4f(%rip), %rsi      # 0x83a2
movb $0x0, %al
callq 0x3090
movq -0x28(%rbp), %rdi
callq 0x44a0
movsbl %al, %eax
cmpl $0x70, %eax
jne 0x57a4
movq -0x288(%rbp), %rax
movl $0x1, -0x158(%rbp)
leaq -0x15c(%rbp), %rdi
addq $0x8, %rdi
movq (%rax), %rax
movl 0x324(%rax), %edx
addl $0x1, %edx
movl -0x44(%rbp), %ecx
leaq 0x2c38(%rip), %rsi      # 0x83d5
movb $0x0, %al
callq 0x3090
leaq -0x15c(%rbp), %rsi
addq $0x8, %rsi
movq 0x581a(%rip), %rdi      # 0xafd0
callq 0x3280
movq %rax, %rdi
movq 0x57fb(%rip), %rsi      # 0xafc0
callq 0x32b0
movq -0x18(%rbp), %rdi
leaq -0x15c(%rbp), %rsi
movl $0x88, %edx
callq 0x3c00
cmpl $0x0, -0xd4(%rbp)
jne 0x5824
movq 0x57e1(%rip), %rdi      # 0xafd0
leaq 0x2b7f(%rip), %rsi      # 0x8375
callq 0x3280
movq %rax, %rdi
movl -0x44(%rbp), %esi
callq 0x3490
movq %rax, %rdi
leaq 0x2bf4(%rip), %rsi      # 0x8404
callq 0x3280
movq %rax, %rdi
movq 0x57a1(%rip), %rsi      # 0xafc0
callq 0x32b0
jmp 0x5826
movq -0x288(%rbp), %rax
cmpl $0x1, 0x6c(%rax)
jne 0x58f5
movq -0x28(%rbp), %rdi
callq 0x44a0
movsbl %al, %eax
cmpl $0x70, %eax
jne 0x58f5
movl $0x3, -0x1c4(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x1e8(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x4800
movq -0x2d0(%rbp), %rdi
leaq -0x1c0(%rbp), %rax
movq %rax, -0x2c8(%rbp)
callq 0x3110
movq -0x2c8(%rbp), %rdi
movq %rax, %rsi
callq 0x3250
movq -0x2c8(%rbp), %rsi
leaq 0x2b73(%rip), %rdi      # 0x8417
xorl %eax, %eax
callq 0x3050
jmp 0x58ad
movq -0x18(%rbp), %rdi
leaq -0x1c4(%rbp), %rsi
movl $0x68, %edx
callq 0x3c00
jmp 0x58c4
leaq -0x1e8(%rbp), %rdi
callq 0x3180
jmp 0x58f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x3180
jmp 0x5b13
movq -0x288(%rbp), %rax
movl $0x0, 0x6c(%rax)
movq (%rax), %rax
movl 0x324(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x324(%rax)
jmp 0x5a49
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x2d4(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4440
movl %eax, %ecx
movl -0x2d4(%rbp), %eax
cmpl %ecx, %eax
jg 0x59cb
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x2d8(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4440
movl %eax, %ecx
movl -0x2d8(%rbp), %eax
cmpl %ecx, %eax
jne 0x5984
movq -0x40(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2dc(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4460
movl %eax, %ecx
movl -0x2dc(%rbp), %eax
cmpl %ecx, %eax
jg 0x59cb
movq -0x40(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2e0(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4460
movl %eax, %ecx
movl -0x2e0(%rbp), %eax
cmpl %ecx, %eax
jne 0x5a47
movq -0x40(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2e4(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4480
movl %eax, %ecx
movl -0x2e4(%rbp), %eax
cmpl %ecx, %eax
jle 0x5a47
movq 0x55fe(%rip), %rdi      # 0xafd0
leaq 0x2a55(%rip), %rsi      # 0x842e
callq 0x3280
movq %rax, %rdi
movq -0x288(%rbp), %rax
movq (%rax), %rax
movl 0x324(%rax), %esi
callq 0x3490
movq %rax, %rdi
movq 0x55c0(%rip), %rsi      # 0xafc0
callq 0x32b0
movl $0x4, -0x27c(%rbp)
movl $0x3, -0x278(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x27c(%rbp), %rsi
movl $0x88, %edx
callq 0x3c00
movq -0x288(%rbp), %rax
movq (%rax), %rax
movl 0x324(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x324(%rax)
jmp 0x5a49
jmp 0x5a8e
testb $0x1, -0x29(%rbp)
je 0x5a8c
movq -0x288(%rbp), %rax
movq (%rax), %rax
movl 0x324(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x324(%rax)
movq 0x555f(%rip), %rdi      # 0xafd0
leaq 0x29cf(%rip), %rsi      # 0x8447
callq 0x3280
movq %rax, %rdi
movq 0x5539(%rip), %rsi      # 0xafc0
callq 0x32b0
jmp 0x5a8e
movq -0x288(%rbp), %rax
movq (%rax), %rcx
movl 0x324(%rcx), %ecx
movl %ecx, -0x2e8(%rbp)
movq (%rax), %rdi
callq 0x5140
movl %eax, %ecx
movl -0x2e8(%rbp), %eax
cmpl %ecx, %eax
jl 0x5ae6
movq 0x5511(%rip), %rdi      # 0xafd0
leaq 0x2994(%rip), %rsi      # 0x845a
callq 0x3280
movq %rax, %rdi
movq 0x54eb(%rip), %rsi      # 0xafc0
callq 0x32b0
movq -0x288(%rbp), %rdi
callq 0x5b20
jmp 0x5b0a
movq 0x54e1(%rip), %rdi      # 0xafd0
leaq 0x2979(%rip), %rsi      # 0x846f
callq 0x3280
movq %rax, %rdi
movq 0x54bb(%rip), %rsi      # 0xafc0
callq 0x32b0
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
movq -0x1f0(%rbp), %rdi
callq 0x34b0
nop