diff --git a/pom.xml b/pom.xml index cea660a..b2a9759 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ cc.niushuai gas-web - 0.0.2-SNAPSHOT + 0.1.0 gas-web gas-web @@ -94,6 +94,7 @@ + gas-web org.springframework.boot diff --git a/src/main/java/cc/niushuai/gasweb/controller/ChartController.java b/src/main/java/cc/niushuai/gasweb/controller/ChartController.java index 8eb4f7b..ddc6af8 100644 --- a/src/main/java/cc/niushuai/gasweb/controller/ChartController.java +++ b/src/main/java/cc/niushuai/gasweb/controller/ChartController.java @@ -16,8 +16,10 @@ package cc.niushuai.gasweb.controller; +import cc.niushuai.gasweb.entity.CH2O; import cc.niushuai.gasweb.entity.ChartResp; import cc.niushuai.gasweb.entity.Gas; +import cc.niushuai.gasweb.repository.CH2ORepository; import cc.niushuai.gasweb.repository.GasRepository; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; @@ -50,6 +52,8 @@ public class ChartController { @Resource private GasRepository gasRepository; + @Resource + private CH2ORepository ch2ORepository; @Operation(summary = "test") @GetMapping("/test") @@ -65,37 +69,72 @@ public class ChartController { public Map fetch(String startDateTime, String endDateTime) { Map map = new HashMap<>(); - List allList = gasRepository.findAllByTimeBetweenOrderByIdAsc(DateUtil.parseDateTime(startDateTime), DateUtil.parseDateTime(endDateTime)); - - if (CollUtil.isNotEmpty(allList)) { - - ChartResp co = new ChartResp("CO浓度"); - ChartResp co2 = new ChartResp("CO2浓度"); - ChartResp ch4 = new ChartResp("CH4浓度"); - ChartResp temperature = new ChartResp("环境温度"); - ChartResp rp2024_temperature = new ChartResp("rp2024温度"); - - - for (Gas gas : allList) { - - String dateTime = DateUtil.formatDateTime(gas.getTime()); - - co.getData().add(new ChartResp.Data(dateTime, gas.getCo())); - co2.getData().add(new ChartResp.Data(dateTime, gas.getCo2())); - ch4.getData().add(new ChartResp.Data(dateTime, gas.getCh4())); - temperature.getData().add(new ChartResp.Data(dateTime, gas.getTemperature())); - rp2024_temperature.getData().add(new ChartResp.Data(dateTime, gas.getRp2040Temperature())); - } - - map.put("co", co); - map.put("co2", co2); - map.put("ch4", ch4); - map.put("temperature", temperature); - map.put("rp2024_temperature", rp2024_temperature); - } + fetchGas(startDateTime, endDateTime, map); + fetchCH2O(startDateTime, endDateTime, map); return map; } + private void fetchCH2O(String startDateTime, String endDateTime, Map map) { + try { + List allList = ch2ORepository.findAllByTimeBetweenOrderByIdAsc(DateUtil.parseDateTime(startDateTime), DateUtil.parseDateTime(endDateTime)); + + if (CollUtil.isNotEmpty(allList)) { + + ChartResp ch2o = new ChartResp("CH2O浓度"); + + for (CH2O ch2O : allList) { + + String dateTime = DateUtil.formatDateTime(ch2O.getTime()); + + ch2o.getData().add(new ChartResp.Data(dateTime, ch2O.getCh2o())); + } + + map.put("ch2o", ch2o); + } + } catch (Exception e) { + log.error("fetch ch2o failed", e); + map.put("ch2oError", new ChartResp("fetch error: " + e.getMessage())); + } + } + + private void fetchGas(String startDateTime, String endDateTime, Map map) { + + try { + List allList = gasRepository.findAllByTimeBetweenOrderByIdAsc(DateUtil.parseDateTime(startDateTime), DateUtil.parseDateTime(endDateTime)); + + if (CollUtil.isNotEmpty(allList)) { + + ChartResp co = new ChartResp("CO浓度"); + ChartResp co2 = new ChartResp("CO2浓度"); + ChartResp ch4 = new ChartResp("CH4浓度"); + ChartResp temperature = new ChartResp("环境温度"); + ChartResp rp2024_temperature = new ChartResp("rp2024温度"); + + + for (Gas gas : allList) { + + String dateTime = DateUtil.formatDateTime(gas.getTime()); + + co.getData().add(new ChartResp.Data(dateTime, gas.getCo())); + co2.getData().add(new ChartResp.Data(dateTime, gas.getCo2())); + ch4.getData().add(new ChartResp.Data(dateTime, gas.getCh4())); + temperature.getData().add(new ChartResp.Data(dateTime, gas.getTemperature())); + rp2024_temperature.getData().add(new ChartResp.Data(dateTime, gas.getRp2040Temperature())); + } + + map.put("co", co); + map.put("co2", co2); + map.put("ch4", ch4); + map.put("temperature", temperature); + map.put("rp2024_temperature", rp2024_temperature); + } + } catch (Exception e) { + log.error("fetch gas failed", e); + map.put("gasError", new ChartResp("fetch error: " + e.getMessage())); + } + + } + } diff --git a/src/main/java/cc/niushuai/gasweb/entity/CH2O.java b/src/main/java/cc/niushuai/gasweb/entity/CH2O.java new file mode 100644 index 0000000..0e31530 --- /dev/null +++ b/src/main/java/cc/niushuai/gasweb/entity/CH2O.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2023 niushuai233 niushuai.cc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cc.niushuai.gasweb.entity; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.Date; + +/** + * 甲烷数据 + * + * @author niushuai233 + * @date 2024/11/18 15:41 + * @since 0.0.1 + */ +@Data +@Entity +@Table(name = "ch2o") +@Schema(title = "甲烷数据") +public class CH2O { + + @Id + @Schema(title = "id") + private Long id; + + /** + * 记录时间 + */ + @Schema(title = "记录时间") + @Column(name = "time") + private Date time; + + /** + * 一氧化碳气体 + */ + @Schema(title = "甲烷浓度") + @Column(name = "ch2o") + private String ch2o; + +} diff --git a/src/main/java/cc/niushuai/gasweb/entity/ChartResp.java b/src/main/java/cc/niushuai/gasweb/entity/ChartResp.java index 6ad2b96..5db1e36 100644 --- a/src/main/java/cc/niushuai/gasweb/entity/ChartResp.java +++ b/src/main/java/cc/niushuai/gasweb/entity/ChartResp.java @@ -29,7 +29,11 @@ public class ChartResp { private String y; public String getY() { - return y.replace("ppm", "").replace("°C", ""); + return y + .replace("ppm", "") + .replace("ppb", "") + .replace("°C", "") + ; } } } diff --git a/src/main/java/cc/niushuai/gasweb/repository/CH2ORepository.java b/src/main/java/cc/niushuai/gasweb/repository/CH2ORepository.java new file mode 100644 index 0000000..c461c2a --- /dev/null +++ b/src/main/java/cc/niushuai/gasweb/repository/CH2ORepository.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023 niushuai233 niushuai.cc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cc.niushuai.gasweb.repository; + +import cc.niushuai.gasweb.entity.CH2O; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.Date; +import java.util.List; + +/** + * ch2o sql repository + * + * @author niushuai233 + * @date 2024/11/18 15:47 + * @since 0.0.1 + */ +@Repository +public interface CH2ORepository extends JpaRepository { + + List findAllByTimeBetweenOrderByIdAsc(Date startDateTime, Date endDateTime); + +} diff --git a/src/main/resources/templates/index.ftl b/src/main/resources/templates/index.ftl index 36e6d11..66df775 100644 --- a/src/main/resources/templates/index.ftl +++ b/src/main/resources/templates/index.ftl @@ -59,6 +59,11 @@
+
+
+
+
+
<#--jq--> @@ -77,7 +82,7 @@