esp8266和dht11温湿度传感器 数据采集http入库
parent
9531b6ede2
commit
62b870fbd5
@ -0,0 +1,44 @@
|
||||
package com.rehome.mqttclienttemperature.controller;
|
||||
|
||||
import com.rehome.mqttclienttemperature.dto.RequestNowTemperatureDto;
|
||||
import com.rehome.mqttclienttemperature.dto.RequestTemperatureDto;
|
||||
import com.rehome.mqttclienttemperature.entity.Temperature;
|
||||
import com.rehome.mqttclienttemperature.response.Result;
|
||||
import com.rehome.mqttclienttemperature.service.TemperatureEspService;
|
||||
import com.rehome.mqttclienttemperature.service.TemperatureService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author HuangWenfei
|
||||
* App自动更新
|
||||
* 2024-10-16 11:11
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
@RequestMapping("/web/temperature/esp")
|
||||
public class TemperatureEspController {
|
||||
@Resource
|
||||
private TemperatureEspService temperatureEspService;
|
||||
|
||||
/**
|
||||
* APP更新版本号
|
||||
*/
|
||||
@GetMapping("/saveEspTemperature")
|
||||
private String save(@Param("temperature")String temperature, @Param("humidity")String humidity) {
|
||||
log.info(temperature);
|
||||
log.info(humidity);
|
||||
this.temperatureEspService.saveTemperature(temperature,humidity);
|
||||
return "success";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package com.rehome.mqttclienttemperature.service;
|
||||
|
||||
import com.rehome.mqttclienttemperature.entity.Temperature;
|
||||
import com.rehome.mqttclienttemperature.entity.TemperatureEsp;
|
||||
|
||||
//Esp8266 DHT温湿度传感器服务
|
||||
public interface TemperatureEspService {
|
||||
//保存 Esp8266通过DHT传感器获取到的温湿度数据
|
||||
void saveTemperature(String temperatureValue,String humidityValue);
|
||||
void saveTemperature(String temperatureValue,String humidityValue,String topic);
|
||||
void saveTemperature(TemperatureEsp temperatureEsp);
|
||||
}
|
||||
@ -0,0 +1,114 @@
|
||||
package com.rehome.mqttclienttemperature.service.impl;
|
||||
|
||||
import com.rehome.mqttclienttemperature.dao.TemperatureEspRepository;
|
||||
import com.rehome.mqttclienttemperature.dao.TemperatureRepository;
|
||||
import com.rehome.mqttclienttemperature.entity.Temperature;
|
||||
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);
|
||||
String dataSecond = sdfSecond.format(now);
|
||||
TemperatureEsp temperatureEsp = new TemperatureEsp();
|
||||
temperatureEsp.setCreateDate(now);
|
||||
temperatureEsp.setDataDate(nowDate);
|
||||
temperatureEsp.setDataHour(nowHour);
|
||||
temperatureEsp.setDataMinute(dataMinute);
|
||||
temperatureEsp.setSource("http");
|
||||
|
||||
String topic = "BF9D7939ND2N";
|
||||
if(topic!=null){
|
||||
temperatureEsp.setTopic(topic);
|
||||
if(topic.equals("BF9D7939ND2N")){
|
||||
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);
|
||||
|
||||
//mysql
|
||||
this.temperatureEspRepository.save(temperatureEsp);
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.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);
|
||||
String dataSecond = sdfSecond.format(now);
|
||||
TemperatureEsp temperatureEsp = new TemperatureEsp();
|
||||
temperatureEsp.setCreateDate(now);
|
||||
temperatureEsp.setDataDate(nowDate);
|
||||
temperatureEsp.setDataHour(nowHour);
|
||||
temperatureEsp.setDataMinute(dataMinute);
|
||||
temperatureEsp.setSource("mqtt");
|
||||
|
||||
if(topic!=null){
|
||||
temperatureEsp.setTopic(topic);
|
||||
if(topic.equals("/device/BF9D7939ND2NBM8WVDK0SO5D/r")){
|
||||
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);
|
||||
|
||||
//mysql
|
||||
this.temperatureEspRepository.save(temperatureEsp);
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveTemperature(TemperatureEsp temperatureEsp) {
|
||||
this.temperatureEspRepository.save(temperatureEsp);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue