#include "RobotWork.h"

bool init2() {
	if (websocket_connect(&websocket,"ws://172.16.1.131:10088/cxx") < 0) {
		printf("WebSocket连接失败");
		return false;
	}
	printf("websocket连接成功");
	createPthread(); 
	initRobot();
	return true;
}

//初始化机器人场景
void initRobot() {
	  event_id2 = event_id2 + 1;
	  robot.event_id = 1;
	  strcpy(robot.type, "other");
	  strcpy(robot.commond, "init_scene");
	  char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	  sprintf(json_str, "{\"event_id\":\"%i\",\"type\":%s,\"commond\":%s}"
       , robot.event_id, robot.type, robot.commond);
	  sendMessage(json_str);	
}

void initPosition() {
	 // 为结构体成员变量赋值
     strcpy(robot.type, "jqr");
     strcpy(robot.commond, "init_position");
     robot.rotate = 0;
     number2 = number2 + 1;
     event_id2 = event_id2 + 1;
     robot.number = number2;
     robot.event_id = event_id2;
     char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	 sprintf(json_str, "{\"number\":\"%i\",\"event_id\":\"%i\",\"point\":\"%s\",\"type\":%s,\"commond\":%s}"
       , robot.number,robot.event_id,"[0.0,0.0]", robot.type, robot.commond);
     sendMessage(json_str);	
}

void setName() {
	char name;
	printf("输入机器人名称:\n");
	scanf("%s", &name);
	strcpy(robot.type, "jqr");
    strcpy(robot.commond, "set_name");
    robot.name = name;
    number2 = number2 + 1;
    event_id2 = event_id2 + 1;
    robot.number = number2;
    robot.event_id = event_id2;
    char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	sprintf(json_str, "{\"number\":\"%i\",\"event_id\":\"%i\",\"name\":\"%s\",\"type\":%s,\"commond\":%s}"
       , robot.number,robot.event_id,name, robot.type, robot.commond);
    sendMessage(json_str);
}

void setColor() {
	char color[8];
	printf("设置机器人颜色:");
	scanf("%s", &color);
	if(color == ""){
	   strcpy(color, "#FFFFFF");
	}
	strcpy(robot.type, "jqr");
    strcpy(robot.commond, "set_color");
    strcpy(robot.color, color);
    number2 = number2 + 1;
    event_id2 = event_id2 + 1;
    robot.number = number2;
    robot.event_id = event_id2;
    char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	sprintf(json_str, "{\"number\":\"%i\",\"event_id\":\"%i\",\"color\":\"%s\",\"type\":\"%s\",\"commond\":\"%s\"}"
       , robot.number,robot.event_id,color, robot.type, robot.commond);
    sendMessage(json_str);
}

void servoMotorControl() {
	int servoId;
	printf("设置舵机id:");
	scanf("%i", &servoId);
	
	int value;
	printf("设置舵机值:");
	scanf("%i", &value);
	
	strcpy(robot.type, "jqr");
	strcpy(robot.commond, "servo_motor_control");
	robot.servoId = servoId;
	robot.value = value;
	number2 = number2 + 1;
    event_id2 = event_id2 + 1;
    robot.number = number2;
    robot.event_id = event_id2;
    char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	sprintf(json_str, "{\"number\":\"%i\",\"event_id\":\"%i\",\"servo_id\":\"%i\",\"value\":\"%i\",\"type\":\"%s\",\"commond\":\"%s\"}"
       , robot.number,robot.event_id,robot.servoId,robot.value, robot.type, robot.commond);
    sendMessage(json_str);
}

void getActionLab() {
	 strcpy(robot.type, "jqr");
	 strcpy(robot.commond, "get_action_lab");
	 number2 = number2 + 1;
     event_id2 = event_id2 + 1;
     robot.number = number2;
     robot.event_id = event_id2;
     char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	 sprintf(json_str, "{\"number\":\"%i\",\"event_id\":\"%i\",\"type\":\"%s\",\"commond\":\"%s\"}"
       , robot.number,robot.event_id,robot.type, robot.commond);
     sendMessage(json_str);
}

void playAction() {
	char action[20];
	printf("设置动作名称:");
	scanf("%s", &action);
	
	float duration;
	printf("设置持续时间:");
	scanf("%f", &duration);
	
	float loopTimes;
	printf("设置循环次数:");
	scanf("%f", &loopTimes);
	
	strcpy(robot.type, "jqr");
	strcpy(robot.commond, "play_action");
	number2 = number2 + 1;
    event_id2 = event_id2 + 1;
    robot.number = number2;
    robot.event_id = event_id2;
    strcpy(robot.action, action);
    robot.duration = duration;
    robot.loopTimes = loopTimes;
    char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
    sprintf(json_str, "{\"number\":\"%i\",\"event_id\":\"%i\",\"type\":\"%s\",\"commond\":\"%s\",\"action\":\"%s\",\"duration\":%f,\"loopTimes\":%f}"
       , robot.number,robot.event_id,robot.type, robot.commond,robot.action,robot.duration,robot.loopTimes);
    sendMessage(json_str);
}

void getSpeechText() {
	printf("设置动作名称:");
}

void speak() {
	char txt[20];
	printf("请输入文本内容:");
	scanf("%s", &txt);
	number2 = number2 + 1;
    event_id2 = event_id2 + 1;
    strcpy(robot.type, "jqr");
	strcpy(robot.commond, "speak");
	strcpy(robot.txt, txt);
	strcpy(robot.tone, "机器人");
	robot.speed = 0;
	robot.save = false;
	strcpy(robot.fileName, "");
	robot.number = number2;
    robot.event_id = event_id2;
    char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
    sprintf(json_str, "{\"number\":\"%i\",\"event_id\":\"%i\",\"type\":\"%s\",\"commond\":\"%s\",\"txt\":\"%s\",\"tone\":%s,\"speed\":%i,\"save\":%s}"
       , robot.number,robot.event_id,robot.type, robot.commond,robot.txt,robot.tone,robot.speed,robot.save);
    sendMessage(json_str);
}

void defineAction() {
	printf("打开动作编辑面板...\n");
	printf("动作编辑面板：");
}

void openColorDetect() {
	printf("打开图像识别...");
	number2 = number2 + 1;
    event_id2 = event_id2 + 1;
	strcpy(robot.type, "jqr");
	strcpy(robot.commond, "openColorDetect");
	robot.number = number2;
    robot.event_id = event_id2;
    char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	sprintf(json_str, "{\"number\":\"%i\",\"event_id\":\"%i\",\"type\":\"%s\",\"commond\":\"%s\"}"
       , robot.number,robot.event_id,robot.type, robot.commond);
    sendMessage(json_str);
}

void stopColorDetect() {
	printf("停止图像识别...");
	number2 = number2 + 1;
    event_id2 = event_id2 + 1;
	strcpy(robot.type, "jqr");
	strcpy(robot.commond, "stopColorDetect");
	robot.number = number2;
    robot.event_id = event_id2;
    char *json_str = (char *)malloc(sizeof(char) * 100); // 假设 JSON 字符串最大长
	sprintf(json_str, "{\"number\":\"%i\",\"event_id\":\"%i\",\"type\":\"%s\",\"commond\":\"%s\"}"
       , robot.number,robot.event_id,robot.type, robot.commond);
    sendMessage(json_str);
}




