package com.rehome.mqttclienttemperature.service.impl;

import com.rehome.mqttclienttemperature.dao.TemperatureEspRepository;
import com.rehome.mqttclienttemperature.entity.TemperatureEsp;
import com.rehome.mqttclienttemperature.service.TemperatureEspService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class TemperatureEspServiceImpl implements TemperatureEspService {
    @Resource
    private TemperatureEspRepository temperatureEspRepository;

    @Override
    public void saveTemperature(String temperatureValue, String humidityValue) {
        try {
            if (temperatureValue != null && temperatureValue.length() > 0 && humidityValue != null && humidityValue.length() > 0) {
                Date now = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat sdfHour = new SimpleDateFormat("yyyy-MM-dd HH");
                SimpleDateFormat sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                SimpleDateFormat sdfSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String nowDate = sdf.format(now);
                String nowHour = sdfHour.format(now);
                String dataMinute = sdfMinute.format(now);
                sdfSecond.format(now);
                TemperatureEsp temperatureEsp = new TemperatureEsp();
                temperatureEsp.setCreateDate(now);
                temperatureEsp.setDataDate(nowDate);
                temperatureEsp.setDataHour(nowHour);
                temperatureEsp.setDataMinute(dataMinute);
                temperatureEsp.setSource("http");
                String topic = "/device/esp8266/001";
                if (topic != null) {
                    temperatureEsp.setTopic(topic);
                    if (topic.equals("/device/esp8266/001")) {
                        temperatureEsp.setLocationDesc("广东省珠海市高新区唐家湾镇南方软件园B2栋4楼珠海瑞洪智能系统工程有限公司");
                    }

                    if (topic.equals("WifiSHT/7C87CE9F5CBF/SHT20")) {
                        temperatureEsp.setLocationDesc("广东省珠海市金湾区三灶镇百川路1号1栋1单元1508房");
                    }

                    if (topic.equals("WifiSHT/4CEBD686B6AA/SHT20")) {
                        temperatureEsp.setLocationDesc("广西壮族自治区崇左市天等县天等镇荣华村弄在屯113号");
                    }
                }

                temperatureEsp.setHumidity(humidityValue);
                temperatureEsp.setTemperature(temperatureValue);
            }
        } catch (Exception var14) {
            var14.printStackTrace();
        }
    }

    @Override
    public void saveTemperature(String temperatureValue, String humidityValue, String topic) {
        try {
            if (temperatureValue != null && temperatureValue.length() > 0 && humidityValue != null && humidityValue.length() > 0) {
                Date now = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat sdfHour = new SimpleDateFormat("yyyy-MM-dd HH");
                SimpleDateFormat sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                SimpleDateFormat sdfSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String nowDate = sdf.format(now);
                String nowHour = sdfHour.format(now);
                String dataMinute = sdfMinute.format(now);
                sdfSecond.format(now);
                TemperatureEsp temperatureEsp = new TemperatureEsp();
                temperatureEsp.setCreateDate(now);
                temperatureEsp.setDataDate(nowDate);
                temperatureEsp.setDataHour(nowHour);
                temperatureEsp.setDataMinute(dataMinute);
                temperatureEsp.setSource("mqtt");
                log.info("topic:{}",topic);
                if (topic != null) {
                    temperatureEsp.setTopic(topic);
                    if (topic.equals("/device/esp8266/001")) {
                        temperatureEsp.setLocationDesc("广东省珠海市高新区唐家湾镇东岸村水风三街28号501");
                    }

                    if (topic.equals("/device/esp8266/002")) {
                        temperatureEsp.setLocationDesc("广东省珠海市金湾区三灶镇百川路1号1栋1单元1508房");
                    }

                    if (topic.equals("/device/esp8266/003")) {
                        temperatureEsp.setLocationDesc("广东省珠海市高新区唐家湾镇南方软件园B2栋4楼珠海瑞洪智能系统工程有限公司");
                    }

                    if (topic.equals("/device/esp8266/004")) {
                        temperatureEsp.setLocationDesc("广东省珠海市高新区唐家湾镇东岸村水风三街28号501-01");
                    }

                    if (topic.equals("/device/esp8266/005")) {
                        temperatureEsp.setLocationDesc("广西壮族自治区崇左市天等县天等镇荣华村弄在屯113号");
                    }
                }

                temperatureEsp.setHumidity(humidityValue);
                temperatureEsp.setTemperature(temperatureValue);
                this.temperatureEspRepository.save(temperatureEsp);
            }
        } catch (Exception var14) {
            var14.printStackTrace();
        }
    }

    @Override
    public void saveTemperature(TemperatureEsp temperatureEsp) {
        this.temperatureEspRepository.save(temperatureEsp);
    }
}
