From b6899cd6d1bafb1c349f1fbd190e6632183719f5 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期五, 01 十一月 2024 20:44:00 +0800 Subject: [PATCH] 11 --- src/main/java/com/se/simu/controller/WaterController.java | 101 +++++++++++++++++++++++++++++++------------------- 1 files changed, 63 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/se/simu/controller/WaterController.java b/src/main/java/com/se/simu/controller/WaterController.java index 32a55a3..d0f2df3 100644 --- a/src/main/java/com/se/simu/controller/WaterController.java +++ b/src/main/java/com/se/simu/controller/WaterController.java @@ -1,10 +1,13 @@ package com.se.simu.controller; -import com.se.simu.domain.LayerVo; -import com.se.simu.helper.StringHelper; +import com.se.simu.domain.po.SimuPo; +import com.se.simu.domain.vo.R; import com.se.simu.helper.WebHelper; +import com.se.simu.service.SimuService; import com.se.simu.service.WaterService; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -15,10 +18,9 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; -import java.io.File; /** - * 鍐呮稘鎺у埗鍣� + * 鍐呮稘绠$悊 * * @author WWW * @date 2024-07-16 @@ -27,7 +29,10 @@ @Slf4j @RestController @RequestMapping("/waterlogging") -public class WaterController { +public class WaterController extends BaseController { + @Resource + SimuService simuService; + @Resource WaterService waterService; @@ -37,12 +42,6 @@ private final static long Y2000 = 949334400000L; - @ApiOperation(value = "鑾峰彇褰撳墠鏃堕棿") - @GetMapping("/getTime") - public Object getTime() { - return System.currentTimeMillis(); - } - @ApiOperation(value = "鑾峰彇鍏冩暟鎹俊鎭�") @GetMapping("/{serviceName}/layer.json") public void getLayer(@PathVariable String serviceName, HttpServletResponse res) { @@ -51,9 +50,9 @@ return; } - LayerVo layer = waterService.getLayer(serviceName); + byte[] bytes = waterService.getLayerJson(serviceName); - WebHelper.writeJson2Page(res, HttpStatus.OK, layer); + WebHelper.writeBytes(bytes, res); } catch (Exception ex) { log.error(ex.getMessage(), ex); WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); @@ -69,7 +68,8 @@ } String file = waterService.getTerraMap(serviceName, width, height); - writeFile(file, res); + + WebHelper.writePng(file, res); } catch (Exception ex) { log.error(ex.getMessage(), ex); WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); @@ -84,8 +84,9 @@ return; } - String file = waterService.getWaterMap(serviceName, width, height); - writeFile(file, res); + String file = waterService.getWaterMap(serviceName, width, height, timestamp); + + WebHelper.writePng(file, res); } catch (Exception ex) { log.error(ex.getMessage(), ex); WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); @@ -101,15 +102,56 @@ } String file = waterService.getFlowMap(serviceName, width, height, timestamp); - writeFile(file, res); + + WebHelper.writePng(file, res); } catch (Exception ex) { log.error(ex.getMessage(), ex); WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); } } + @ApiOperation(value = "鑾峰彇闄嶆按鏇茬嚎鏂囦欢鏇茬嚎鍥�") + @GetMapping("/{serviceName}/rainfall.json") + public void getRainfall(@PathVariable String serviceName, HttpServletResponse res) { + try { + if (!validate(serviceName, res)) { + return; + } + + byte[] bytes = waterService.getRainfall(serviceName); + + WebHelper.writeBytes(bytes, res); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); + } + } + + @ApiOperation(value = "鏍规嵁鍧愭爣鏌ヨ绉按娣卞害") + @GetMapping("/{serviceName}/getWaterHeight") + @ApiImplicitParams({ + @ApiImplicitParam(name = "serviceName", value = "鏈嶅姟鍚�", dataType = "String", paramType = "path", example = "20241010095328"), + @ApiImplicitParam(name = "x", value = "X", dataType = "double", paramType = "query", example = "116.6447998"), + @ApiImplicitParam(name = "y", value = "Y", dataType = "double", paramType = "query", example = "39.8868915"), + @ApiImplicitParam(name = "timestamp", value = "鏃堕棿鎴�", dataType = "long", paramType = "query", example = "1730217660000") + }) + public R<Double> getWaterHeight(@PathVariable String serviceName, double x, double y, long timestamp, HttpServletResponse res) { + try { + SimuPo simu = simuService.getSimuByServiceName(serviceName); + if (null == simu) { + return null; + } + + // 鏍规嵁鏈嶅姟鍚�+鏃堕棿鎴�+鍧愭爣锛屾煡璇㈠搴旂殑绉按娣卞害 + return success(waterService.getWaterHeight(simu, x, y, timestamp)); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + return fail(ex); + } + } + private boolean validate(String serviceName, HttpServletResponse res) { - if (StringHelper.isEmpty(serviceName)) { + if (WebHelper.isEmpty(serviceName)) { return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "鏈嶅姟鍚嶄笉鑳戒负绌�"); } @@ -124,7 +166,7 @@ * 楠岃瘉 */ private boolean validate(String serviceName, Integer width, Integer height, Long timestamp, HttpServletResponse res) { - if (StringHelper.isEmpty(serviceName)) { + if (WebHelper.isEmpty(serviceName)) { return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "鏈嶅姟鍚嶄笉鑳戒负绌�"); } if (null == width || width < MIN_SIZE || width > MAX_SIZE) { @@ -133,26 +175,9 @@ if (null == height || height < MIN_SIZE || height > MAX_SIZE) { return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "鍥惧儚楂樺害涓嶈兘涓虹┖涓斿彇鍊艰寖鍥翠负" + MIN_SIZE + "~" + MAX_SIZE + "涔嬮棿"); } - if (null == timestamp || timestamp < Y2000) { - return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "鏃堕棿涓嶈兘涓虹┖涓斿ぇ浜�2000骞�"); + if (null == timestamp || timestamp < 0) { + return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "鏃堕棿涓嶈兘涓虹┖涓斿ぇ浜�0"); } - - return true; - } - - /** - * 鍐欐枃浠� - */ - private boolean writeFile(String path, HttpServletResponse res) throws Exception { - if (StringHelper.isEmpty(path)) { - return WebHelper.writeJson2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, "鐢熸垚PNG澶辫触"); - } - File file = new File(path); - if (!file.exists() || file.isDirectory()) { - return WebHelper.writeJson2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, "PNG鏂囦欢鎵句笉鍒�"); - } - - WebHelper.download(path, file.getName(), res); return true; } -- Gitblit v1.9.3