package com.se.simu.controller;
|
|
import com.se.simu.domain.LayerVo;
|
import com.se.simu.helper.StringHelper;
|
import com.se.simu.helper.WebHelper;
|
import com.se.simu.service.WaterService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.File;
|
|
/**
|
* 内涝控制器
|
*
|
* @author WWW
|
* @date 2024-07-16
|
*/
|
@Api(tags = "内涝管理")
|
@Slf4j
|
@RestController
|
@RequestMapping("/waterlogging")
|
public class WaterController {
|
@Resource
|
WaterService waterService;
|
|
private final static int MIN_SIZE = 10;
|
|
private final static int MAX_SIZE = 80000;
|
|
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) {
|
try {
|
if (!validate(serviceName, res)) {
|
return;
|
}
|
|
LayerVo layer = waterService.getLayer(serviceName);
|
|
WebHelper.writeJson2Page(res, HttpStatus.OK, layer);
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
|
}
|
}
|
|
@ApiOperation(value = "获取地形高度图")
|
@GetMapping("/{serviceName}/terrain")
|
public void getTerraMap(@PathVariable String serviceName, Integer width, Integer height, HttpServletResponse res) {
|
try {
|
if (!validate(serviceName, width, height, res)) {
|
return;
|
}
|
|
String file = waterService.getTerraMap(serviceName, width, height);
|
writeFile(file, res);
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
|
}
|
}
|
|
@ApiOperation(value = "获取水面高度图")
|
@GetMapping("/{serviceName}/waterMap")
|
public void getWaterMap(@PathVariable String serviceName, Integer width, Integer height, Long timestamp, HttpServletResponse res) {
|
try {
|
if (!validate(serviceName, width, height, timestamp, res)) {
|
return;
|
}
|
|
String file = waterService.getWaterMap(serviceName, width, height);
|
writeFile(file, res);
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
|
}
|
}
|
|
@ApiOperation(value = "获取水流向流速图")
|
@GetMapping("/{serviceName}/flowMap")
|
public void getFlowMap(@PathVariable String serviceName, Integer width, Integer height, Long timestamp, HttpServletResponse res) {
|
try {
|
if (!validate(serviceName, width, height, timestamp, res)) {
|
return;
|
}
|
|
String file = waterService.getFlowMap(serviceName, width, height, timestamp);
|
writeFile(file, res);
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
|
}
|
}
|
|
private boolean validate(String serviceName, HttpServletResponse res) {
|
if (StringHelper.isEmpty(serviceName)) {
|
return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "服务名不能为空");
|
}
|
|
return true;
|
}
|
|
private boolean validate(String serviceName, Integer width, Integer height, HttpServletResponse res) {
|
return validate(serviceName, width, height, Y2000, res);
|
}
|
|
/**
|
* 验证
|
*/
|
private boolean validate(String serviceName, Integer width, Integer height, Long timestamp, HttpServletResponse res) {
|
if (StringHelper.isEmpty(serviceName)) {
|
return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "服务名不能为空");
|
}
|
if (null == width || width < MIN_SIZE || width > MAX_SIZE) {
|
return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "图像宽度不能为空且取值范围为" + MIN_SIZE + "~" + MAX_SIZE + "之间");
|
}
|
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年");
|
}
|
|
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;
|
}
|
}
|