#include "main.h"



//继电器引脚初始化
void relayInit(void) {
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  GPIOB_RCC_ENABLE;
	GPIOD_RCC_ENABLE;

  GPIO_InitStruct.Pin = Realy1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(Realy1_GPIO_Port, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = Realy2_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(Realy2_GPIO_Port, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = Realy3_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(Realy3_GPIO_Port, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = Realy4_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(Realy4_GPIO_Port, &GPIO_InitStruct);

	//从存储器中恢复继电器状态
	MqttInfoStr.Relay_State[1] ? Relay1_OFF : Relay1_ON;
	MqttInfoStr.Relay_State[2] ? Relay2_OFF : Relay2_ON;
	MqttInfoStr.Relay_State[3] ? Relay3_OFF : Relay3_ON;
	MqttInfoStr.Relay_State[4] ? Relay4_OFF : Relay4_ON;
	
  log_info("继电器1状态为:%s",MqttInfoStr.Relay_State[1] ? "开" : "关");
  log_info("继电器2状态为:%s",MqttInfoStr.Relay_State[2] ? "开" : "关");
  log_info("继电器3状态为:%s",MqttInfoStr.Relay_State[3] ? "开" : "关");
  log_info("继电器4状态为:%s",MqttInfoStr.Relay_State[4] ? "开" : "关");

	//读取存储器的继电器状态发送给云平台
  CAT1_printf("AT+MPUB=\"/iot/data/up/%s\",0,0,\"{\\22header\\22:\\22iot.prop.post\\22,\\22body\\22:{\\22sw1\\22:%d,\\22sw2\\22:%d,\\22sw3\\22:%d,\\22sw4\\22:%d}}\"\r\n",
              MqttInfoStr.ClientID,
              MqttInfoStr.Relay_State[1]?1:0,
              MqttInfoStr.Relay_State[2]?1:0,
              MqttInfoStr.Relay_State[3]?1:0,
              MqttInfoStr.Relay_State[4]?1:0);	
	
}


// 继电器响应网络数据
//收到主机控制指令：+MSUB: "/iot/data/down/cfd6dba662690a5e",118 byte,{"id":"977013940154814794","header":"iot.prop.set","version":"1.0","body":{"sw1":{"time":1713104741005,"value":true}}}
void relayAction(uint8_t *data) {
  uint8_t i = 0;
  char *TempPointer;	  // 临时指针
  char CmdId[18] = {0}; // 存贮id的数据，以响应服务器下发的数据
  char IdFlag = 0;	  // id的计数标志位，用于判断ID中是否出现异常字符
  if(strstr((char *)data,MqttInfoStr.Topic)) { //查找  /iot/data/down/ 出现的位置
    TempPointer = strstr((char *)data,"\"id\":"); // 记录ID位置 {"id":"1117
    TempPointer += 6;  													//除去"id": 5个字符  应该从第6个开始取
    IdFlag = 0;
    log_info("ID起始位置为%s", TempPointer); //打印结果：ID起始位置为977187088401715548","header":"iot.sub.prop.set","version":"1.0","body":{"sw2":{"time":1713146022764,"value":true}}}
    for (i = 0; i < 18; i++) {
      // 存储id(响应帧，必要参数)
      if (((TempPointer[i] >= '0') && (TempPointer[i] <= '9')) || ((TempPointer[i] >= 'a') && (TempPointer[i] <= 'z')) || ((TempPointer[i] >= 'A') && (TempPointer[i] <= 'Z'))) {
        CmdId[i] = TempPointer[i];
        IdFlag++;
      } else {
        IdFlag = 0;
        log_info("ID中混有非法字符,将不会发送反馈数据");
        log_info("ID指针为%s", TempPointer); //此语句千万不能用printf 而是得用log_info 否则程序卡死  很难找到原因
      }
    }
    if (IdFlag >= 18) {  // ID为18位，当ID>=18时说明ID正常
      log_info("ID中一切字符正常");
    }

    if(strstr((char *)data,"\"sw")) { //先判断收到的数据是不是开关量
      TempPointer = strstr((char *)data,"\"sw");   // 记录sw位置
      switch(TempPointer[3]) {    // 判断操作哪个开关
      case '1':
        if (strstr((char *)data, "true")) {
          log_info("开关1打开");
          Relay1_ON;//
					MqttInfoStr.Relay_State[1] = 1;
        }
        if (strstr((char *)data, "false")) {
          log_info("开关1关闭");
					MqttInfoStr.Relay_State[1] = 0;
          Relay1_OFF;//
        }
        break;
      case '2':
        if (strstr((char *)data, "true")) {
          log_info("开关2打开");
          Relay2_ON;//
					MqttInfoStr.Relay_State[2] = 1;
        }
        if (strstr((char *)data, "false")) {
          log_info("开关2关闭");
          Relay2_OFF;//
					MqttInfoStr.Relay_State[2] = 0;
        }
        break;
      case '3':
        if (strstr((char *)data, "true")) {
          log_info("开关3打开");
          Relay3_ON;//
					MqttInfoStr.Relay_State[3] = 1;
        }
        if (strstr((char *)data, "false")) {
          log_info("开关3关闭");
          Relay3_OFF;//
					MqttInfoStr.Relay_State[3] = 0;
        }
        break;
      case '4':
        if (strstr((char *)data, "true")) {
          log_info("开关4打开");
          Relay4_ON;//
					MqttInfoStr.Relay_State[4] = 1;
        }
        if (strstr((char *)data, "false")) {
          log_info("开关4关闭");
          Relay4_OFF;//
					MqttInfoStr.Relay_State[4] = 0;
        }
        break;
      }
    }
		eepromWriteData(0, &MqttInfoStr, MQTT_STRUCT_LEN);
		CAT1_printf("AT+MPUB=\"/iot/data/up/%s\",0,0,\"{\\22header\\22:\\22iot.prop.post\\22,\\22body\\22:{\\22sw1\\22:%d,\\22sw2\\22:%d,\\22sw3\\22:%d,\\22sw4\\22:%d}}\"\r\n",
              MqttInfoStr.ClientID,
              MqttInfoStr.Relay_State[1]?1:0,
              MqttInfoStr.Relay_State[2]?1:0,
              MqttInfoStr.Relay_State[3]?1:0,
              MqttInfoStr.Relay_State[4]?1:0);	
		HAL_Delay(50);//需要延时 可以很短	
    //推送回复消息 OK
    CAT1_printf("AT+MPUB=\"/iot/data/up/%s\",0,0,\"{\\22id\\22:\\22%s\\22,\\22code\\22:0,\\22message\\22:\\22OK\\22}\"\r\n",MqttInfoStr.ClientID, CmdId);
  }
}

