// File: /workspace/github_cpp_makefile_star2_O2/CDISC2020[P]Projet_satellite_agile/src/planManager/Controller.cpp
// Function: Controller::executeInstruction(GenericInstruction*, int*)
void Controller::executeInstruction(GenericInstruction* instruction, int* responseController)
{
	if(instruction->getType() == 'p')
	{
		Camera C;
		C.code=1;
		C.exposure = ((PhotoInstruction*)instruction)->getExposure();
		string aux = ((PhotoInstruction*)instruction)->getPhotoName();
		strcpy(C.photoName,aux.c_str());

		cout << endl << "New photo! Smile!" << endl;
		cout<<"name ="<<C.photoName<<"    exposure = "<<C.exposure<<endl;
		string path="src/communication/toSend/" + (string) C.photoName; // where to save pictures
                sc.returnControl = myCameraController.photoShoot(path, C.exposure);
		if(sc.returnControl)
		{
			//call traitement image
			/*char cmde[100]="";
			remove("res_image_processing.txt")
			sprintf(cmde,"python image_processing.py %s &", path.c_str());
			system(cmde);
			sleep(3);
			ifstream file;
			file.open("res_image_processing.txt");
			if(!file){
				cout << "No response from image processing" << endl;
				*responseController=1; // Let the Ground choose if ok
			}
			else
			{
				string line;
				getline(file,line);
				if(atoi(line.c_str()))
					*responseController=1;
				else
					*responseController=-1;
				file.close();
				remove("res_image_processing.txt")
			}
			*/

			// by pass algo, algo expensive to execute, never really tested
			// remove this line if the above isn't commented
			*responseController=1;
			//////
		}
		else
			*responseController=-1;
	}
	else if(instruction->getType() == 'a')
	{
		Attitude A; 
		A.code = 0; 
		A.yaw = ((AttitudeInstruction*)instruction)->getYaw(); 
		A.pitch = ((AttitudeInstruction*)instruction)->getPitch(); 
		A.roll = ((AttitudeInstruction*)instruction)->getRoll(); 

		cout << endl << "New attitude change!!" << endl;
		cout<<"yaw ="<<A.yaw<<"    pitch = "<<A.pitch<<"    roll = "<<A.roll<<endl;
                sc.returnControl = myAttitudeController.attitudeChange(A.roll, A.pitch, A.yaw);
		if(sc.returnControl)
			*responseController=1;
		else
			*responseController=-1;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r14
movq %rsi, %rdi
callq 0x2eec
cmpb $0x70, %al
jne 0x4a42
leaq -0xfc(%rbp), %r15
movl $0x1, -0x4(%r15)
movq %r13, %rdi
callq 0x30e4
movl %eax, 0x40(%r15)
leaq -0xb8(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x30ee
movq (%r12), %rsi
movq %r15, %rdi
callq 0x2210
movq 0x3675(%rip), %rdi      # 0x7fd0
callq 0x2150
leaq 0xe93(%rip), %rsi       # 0x57fa
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
callq 0x2150
movq 0x3652(%rip), %rdi      # 0x7fd0
leaq 0xe87(%rip), %rsi       # 0x580c
callq 0x2240
movq %rax, %rdi
movq %r15, %rsi
callq 0x2240
leaq 0xe77(%rip), %rsi       # 0x5813
movq %rax, %rdi
callq 0x2240
movl -0xbc(%rbp), %esi
movq %rax, %rdi
callq 0x2400
movq %rax, %rdi
callq 0x2150
leaq -0x78(%rbp), %rdi
leaq -0x29(%rbp), %rdx
movq %r15, %rsi
callq 0x2110
leaq 0xe52(%rip), %rsi       # 0x5823
leaq -0x98(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x4b6a
leaq -0x78(%rbp), %rdi
callq 0x2140
leaq -0x58(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x2300
movl -0xbc(%rbp), %edx
leaq -0x58(%rbp), %rsi
movq %r14, %rdi
callq 0x407a
movb %al, 0x1c(%r14)
leaq -0x58(%rbp), %rdi
callq 0x2140
movzbl 0x1c(%r14), %eax
decl %eax
orl $0x1, %eax
movl %eax, (%rbx)
leaq -0x98(%rbp), %rdi
callq 0x2140
leaq -0xb8(%rbp), %rdi
callq 0x2140
jmp 0x4b17
movq %r13, %rdi
callq 0x2eec
cmpb $0x61, %al
jne 0x4b17
movq %r13, %rdi
callq 0x3280
movl %eax, %r15d
movq %r13, %rdi
callq 0x3276
movl %eax, %r12d
movq %r13, %rdi
callq 0x328a
movl %eax, %r13d
movq %rbx, -0x38(%rbp)
movq 0x3552(%rip), %rbx      # 0x7fd0
movq %rbx, %rdi
callq 0x2150
leaq 0xdb0(%rip), %rsi       # 0x583d
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
callq 0x2150
leaq 0xdaf(%rip), %rsi       # 0x5853
movq %rbx, %rdi
callq 0x2240
movq %rax, %rdi
movl %r15d, %esi
callq 0x2400
leaq 0xd9b(%rip), %rsi       # 0x5859
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
movl %r12d, %esi
callq 0x2400
leaq 0xd8e(%rip), %rsi       # 0x5866
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
movl %r13d, %esi
callq 0x2400
movq %rax, %rdi
callq 0x2150
leaq 0x1(%r14), %rdi
movl %r13d, %esi
movl %r12d, %edx
movl %r15d, %ecx
callq 0x3fde
movb %al, 0x1c(%r14)
movzbl %al, %eax
decl %eax
orl $0x1, %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x2140
jmp 0x4b3a
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
jmp 0x4b4a
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x2140
jmp 0x4b56
jmp 0x4b53
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x2140
movq %rbx, %rdi
callq 0x2420