package org.springblade.sms.service.impl;

import cn.hutool.json.JSONUtil;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.sms.service.IPepNoticeService;
import org.springblade.third.sms.dto.PepSmsSendDto;
import org.springblade.third.sms.dto.PepVoiceSendDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * @author lidexi
 * @date 2021/5/21 11:40
 * 感知平台告警通知实现层
 */
@Service
public class PepNoticeServiceImpl implements IPepNoticeService {

	@Autowired
	private RedisTemplate<String,String > redisTemplate;
	/**
	 * 短信通知
	 * @param smsSendDto
	 */
	@Override
	@Async
	public void sendSms(PepSmsSendDto smsSendDto) {
		RestTemplate restTemplate = new RestTemplate();
		//获取accessToken
		String accessToken = redisTemplate.opsForValue().get("sptg:iot:auth:accessToken@#617fb531-c8bb-47c0-b3c4-d2fa4162a18e");


		ResponseEntity responseEntity = null;
		HttpHeaders headers = new HttpHeaders();
		headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
		headers.add("accessToken", accessToken);

		HttpEntity<String> requestEntity = new HttpEntity<String>(JsonUtil.toJson(smsSendDto), headers);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};
		String url = "https://scep-api.ctwing.cn:8000/ctwing/smoke3/sms/sendSms";
		URI uri = URI.create(url);
		restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
	}

	/**
	 * 语音通知
	 * @param pepVoiceSendDto
	 */
	@Override
	@Async
	public void sendVoice(PepVoiceSendDto pepVoiceSendDto) {
		RestTemplate restTemplate = new RestTemplate();
		String accessToken = redisTemplate.opsForValue().get("sptg:iot:auth:accessToken@#617fb531-c8bb-47c0-b3c4-d2fa4162a18e");


		ResponseEntity responseEntity = null;
		HttpHeaders headers = new HttpHeaders();
		headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
		headers.add("accessToken", accessToken);

		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(pepVoiceSendDto), headers);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};

		//发送语音
		String url = "https://scep-api.ctwing.cn:8000/ctwing/smoke3/voice/sendVoice";
		URI uri = URI.create(url);
		restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);

	}
}
