Browse Source

优化魔法值

优化设备状态定时任务
develop
zhaoxianglong 5 months ago
parent
commit
669b517cd9
  1. 102
      zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java
  2. 60
      zc-business/src/main/java/com/zc/business/controller/DeviceStatus.java
  3. 6
      zc-business/src/main/java/com/zc/business/enums/UniversalEnum.java
  4. 2
      zc-business/src/main/java/com/zc/business/message/device/handler/DeviceMessageHandler.java
  5. 38
      zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

102
zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java

@ -71,7 +71,7 @@ public class DcDeviceController extends BaseController {
* @return 分页查询结果
*/
@ApiOperation("分页查询设备列表")
// @PreAuthorize("@ss.hasPermi('iot:device:list')")
// @PreAuthorize("@ss.hasPermi('iot:device:list')")
@GetMapping("list")
public TableDataInfo listDevice(DcDevice dcDevice) {
return getDataTable(dcDeviceService.pageDevice(dcDevice));
@ -83,7 +83,7 @@ public class DcDeviceController extends BaseController {
* @return 查询结果
*/
@ApiOperation("统计异常设备")
// @PreAuthorize("@ss.hasPermi('iot:device:list')")
// @PreAuthorize("@ss.hasPermi('iot:device:list')")
@GetMapping("abnormalDevice")
public AjaxResult statisticalAnomalyDevice() {
return AjaxResult.success(dcDeviceService.statisticalAnomalyDevice());
@ -96,7 +96,7 @@ public class DcDeviceController extends BaseController {
* @return 查询结果
*/
@ApiOperation("无分页查询设备列表")
// @PreAuthorize("@ss.hasPermi('iot:device:query')")
// @PreAuthorize("@ss.hasPermi('iot:device:query')")
@GetMapping("query")
public AjaxResult queryDevice(DcDevice dcDevice) {
return AjaxResult.success(dcDeviceService.listDevice(dcDevice));
@ -122,7 +122,7 @@ public class DcDeviceController extends BaseController {
* @return 查询结果
*/
@ApiOperation("根据id查询设备信息")
// @PreAuthorize("@ss.hasPermi('iot:device:query')")
// @PreAuthorize("@ss.hasPermi('iot:device:query')")
@GetMapping("{id}")
public AjaxResult getDevice(@PathVariable String id) {
return AjaxResult.success(dcDeviceService.getDevice(id));
@ -154,7 +154,7 @@ public class DcDeviceController extends BaseController {
* @return 新增操作结果
*/
@ApiOperation("新增")
// @PreAuthorize("@ss.hasPermi('iot:device:add')")
// @PreAuthorize("@ss.hasPermi('iot:device:add')")
@Log(title = "新增设备", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult addDevice(@Valid @RequestBody DcDevice dcDevice) {
@ -168,7 +168,7 @@ public class DcDeviceController extends BaseController {
* @return 修改操作结果
*/
@ApiOperation("修改")
// @PreAuthorize("@ss.hasPermi('iot:device:edit')")
// @PreAuthorize("@ss.hasPermi('iot:device:edit')")
@Log(title = "修改设备", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult editDevice(@Valid @RequestBody DcDevice dcDevice) {
@ -182,7 +182,7 @@ public class DcDeviceController extends BaseController {
* @return 删除操作结果
*/
@ApiOperation("删除")
// @PreAuthorize("@ss.hasPermi('iot:device:remove')")
// @PreAuthorize("@ss.hasPermi('iot:device:remove')")
@Log(title = "删除", businessType = BusinessType.DELETE)
@DeleteMapping("{ids}")
public AjaxResult removeDevice(@PathVariable List<String> ids) {
@ -192,10 +192,11 @@ public class DcDeviceController extends BaseController {
/**
* 导出
* @param response 响应
*
* @param response 响应
* @param iotDevice 导入数据结果
*/
// @PreAuthorize("@ss.hasPermi('iot:device:export')")
// @PreAuthorize("@ss.hasPermi('iot:device:export')")
@Log(title = "导出设备", businessType = BusinessType.EXPORT)
@PostMapping("export")
public void exportDevice(HttpServletResponse response, DcDevice iotDevice) {
@ -481,7 +482,7 @@ public class DcDeviceController extends BaseController {
@PathVariable String deviceId,
@PathVariable String functionId,
@RequestBody HashMap<String, Object> props) throws HttpException, IOException {
//public AjaxResult invokedFunction(@PathVariable String deviceId,@PathVariable String functionId,@RequestBody HashMap<String, Object> props,int operType) throws HttpException, IOException { todo
//public AjaxResult invokedFunction(@PathVariable String deviceId,@PathVariable String functionId,@RequestBody HashMap<String, Object> props,int operType) throws HttpException, IOException { todo
return getAjaxResult(deviceId, functionId, props);
@ -591,8 +592,8 @@ public class DcDeviceController extends BaseController {
resultArray.add(getAjaxResult(deviceId, functionId, (HashMap<String, Object>) param));
}
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("SET", UniversalEnum.SEVEN.getValue());
getAjaxResult(deviceId, "SETMD", hashMap);
hashMap.put(UniversalEnum.SET.getValue(), UniversalEnum.SEVEN.getValue());
getAjaxResult(deviceId, UniversalEnum.SETMD.getValue(), hashMap);
return AjaxResult.success(resultArray);
}
@ -633,7 +634,7 @@ public class DcDeviceController extends BaseController {
* 查询物联设备指定事件数据
*
* @param deviceId 物联设备id
* @param type 类型
* @param type 类型
* @param queryParam 查询条件
* @return 查询事件结果
*/
@ -648,7 +649,7 @@ public class DcDeviceController extends BaseController {
if (!StringUtils.hasText(deviceId) || !StringUtils.hasText(type)) {
return AjaxResult.error(UniversalEnum.DEVICE_NOT_CONNECTED.getValue());
}
if (type.equals(UniversalEnum.ALL.getValue())){
if (type.equals(UniversalEnum.ALL.getValue())) {
OkHttp okHttp = new OkHttp();
@ -656,11 +657,11 @@ public class DcDeviceController extends BaseController {
Response response // 请求响应
= okHttp
.url(iotAddress + UniversalEnum.QUERY_EVENT_DATA_OF_IOT_DEVICES.getValue()+deviceId) // 请求地址
.url(iotAddress + UniversalEnum.QUERY_EVENT_DATA_OF_IOT_DEVICES.getValue() + deviceId) // 请求地址
.data(requestParams)
.get(); // 请求方法
return JSON.parseObject(response.body().string(), AjaxResult.class);
}else {
} else {
return queryPagerDeviceEvents(deviceId, type, queryParam);
}
}
@ -678,38 +679,37 @@ public class DcDeviceController extends BaseController {
OkHttp okHttp = new OkHttp();
Response response // 请求响应
= okHttp
.url(iotAddress + UniversalEnum.GET_A_PHYSICAL_MODEL_OF_AN_IOT_DEVICE.getValue()+ id) // 请求地址
.url(iotAddress + UniversalEnum.GET_A_PHYSICAL_MODEL_OF_AN_IOT_DEVICE.getValue() + id) // 请求地址
.get(); // 请求方法
return JSON.parseObject(response.body().string(), AjaxResult.class);
}
/**
* @Description 查询上游10公里内的情报板
*
* @author liuwenge
* @date 2024/4/15 14:22
* @param stakeMark
* @param direction
* @return com.ruoyi.common.core.domain.AjaxResult
* @Description 查询上游10公里内的情报板
* @author liuwenge
* @date 2024/4/15 14:22
*/
@ApiOperation("查询上游10公里内的情报板")
@PostMapping("/selectNearBoard")
public AjaxResult selectNearBoard(@ApiParam(value="桩号", name="stakeMark", required=true) @RequestParam ("stakeMark") String stakeMark,
@ApiParam(value="方向", name="direction", required=true) @RequestParam ("direction") String direction){
return dcDeviceService.selectNearBoard(stakeMark,direction);
public AjaxResult selectNearBoard(@ApiParam(value = "桩号", name = "stakeMark", required = true) @RequestParam("stakeMark") String stakeMark,
@ApiParam(value = "方向", name = "direction", required = true) @RequestParam("direction") String direction) {
return dcDeviceService.selectNearBoard(stakeMark, direction);
}
@OperationLog(operUrl = "/business/device/batchFunctions")
public AjaxResult batchInvokedFunction(Object object) throws HttpException, IOException, InterruptedException {
//public AjaxResult batchInvokedFunction(Object object,int operType) throws HttpException, IOException, InterruptedException { todo
//public AjaxResult batchInvokedFunction(Object object,int operType) throws HttpException, IOException, InterruptedException { todo
Map<String, Object> map = new ObjectMapper().convertValue(object, Map.class);
return batchInvokedFunction(map);
}
/**
* 分页查询设备名称列表
*
* @param dcDevice 请求参数
* @return 分页查询结果
*/
@ -718,9 +718,10 @@ public class DcDeviceController extends BaseController {
startPage();
return getDataTable(dcDeviceService.selectDeviceNameList(dcDevice));
}
@GetMapping("/deviceParameter")
public TableDataInfo selectDeviceParameterProperties(DcDevice dcDevice) {
if (dcDevice==null||dcDevice.getDeviceType()==null){
if (dcDevice == null || dcDevice.getDeviceType() == null) {
TableDataInfo tableDataInfo = new TableDataInfo();
tableDataInfo.setCode(UniversalEnum.FIVE_HUNDRED.getNumber());
tableDataInfo.setMsg(UniversalEnum.PARAMETER_ERROR.getValue());
@ -729,29 +730,31 @@ public class DcDeviceController extends BaseController {
startPage();
return getDataTable(dcDeviceService.selectDeviceParameterProperties(dcDevice));
}
/**
* 查询设备指定属性属性列表
* param参数
* deviceId 设备id
* propertyId 属性id
* propertyName 属性名称
* deviceType 设备类型
* dateTime 时间
* param参数
* deviceId 设备id
* propertyId 属性id
* propertyName 属性名称
* deviceType 设备类型
* dateTime 时间
*
* @return 属性列表
*/
@PostMapping("/properties/deviceNameData")
public AjaxResult queryDevice(@RequestBody HashMap map) throws HttpException, IOException {
if (map==null||!map.containsKey("deviceId")||!map.containsKey("deviceType")
||!map.containsKey("propertyName")||!map.containsKey("propertyId")
||!map.containsKey("dateTime")){
return AjaxResult.error(UniversalEnum.PARAMETER_ERROR.getValue());
}
if (map == null || !map.containsKey("deviceId") || !map.containsKey("deviceType")
|| !map.containsKey("propertyName") || !map.containsKey("propertyId")
|| !map.containsKey("dateTime")) {
return AjaxResult.error(UniversalEnum.PARAMETER_ERROR.getValue());
}
String deviceType = map.get("deviceType").toString();//设备类型
String deviceId = map.get("deviceId").toString();//设备id
String propertyId = map.get("propertyId").toString();//属性id
String propertyName = map.get("propertyName").toString();//属性名称
HashMap<String, Object> props = new HashMap<>();
// 设置查询条件的键为“timestamp$BTW”,表示时间戳在一定范围内
// 设置查询条件的键为“timestamp$BTW”,表示时间戳在一定范围内
props.put("terms[0].column", "timestamp$BTW");
ArrayList<String> dateList = new ArrayList<>();
// 添加当前日期的开始和结束时间到列表,用于设定时间范围
@ -770,7 +773,7 @@ public class DcDeviceController extends BaseController {
props.put("sorts[0].name", "timestamp");
AjaxResult ajaxResult = queryDeviceProperties(deviceId, propertyId, props);
String code = ajaxResult.get("code").toString();
if ("500".equals(code)){
if (UniversalEnum.FIVE_HUNDRED.getValue().equals(code)) {
return ajaxResult;
}
if (UniversalEnum.THIRTEEN.getValue().equals(deviceType) || UniversalEnum.SIXTEEN.getValue().equals(deviceType)) { //判断是否为设备箱/远端机
@ -820,6 +823,7 @@ public class DcDeviceController extends BaseController {
}
return null;
}
/**
* 设备箱-设备操作口令校验
*/
@ -829,4 +833,22 @@ public class DcDeviceController extends BaseController {
return AjaxResult.success(key.equals(secretKey));
}
///***
// * 合流区预警设备设备电量不足警告
// */
//@Scheduled(cron = "0 0/1 * * * ?")
//public void ConfluenceAreaEarlyWarningDeviceBatteryLowWarning() throws HttpException, IOException {
// DcDevice dcDevice = new DcDevice();
// dcDevice.setDeviceType("8");
// List<DcDevice> devices = dcDeviceService.listDevice(dcDevice);
// for (DcDevice device : devices) {
// String iotDeviceId = device.getIotDeviceId();
// if (iotDeviceId != null && !iotDeviceId.equals("")) {
// AjaxResult deviceLatestProperty = queryDeviceProperties(iotDeviceId, "batteryCapacity",new HashMap<>());
// System.out.println(deviceLatestProperty.get("code"));
// System.out.println(deviceLatestProperty.get("msg"));
// }
// }
//}
}

60
zc-business/src/main/java/com/zc/business/controller/DeviceStatus.java

@ -63,13 +63,19 @@ public class DeviceStatus {
AjaxResult deviceByIotDeviceId = deviceService.getDeviceByIotDeviceId(device.getIotDeviceId());
String deviceState = ((JSONObject) JSON.toJSON(deviceByIotDeviceId.get("data"))).getString("deviceState");
if (Objects.equals(deviceState, UniversalEnum.ON_LINE.getValue())) {
extracted(statusService, device, UniversalEnum.ONE_HUNDRED_PERCENT.getValue());
extracted(statusService, device, true, UniversalEnum.ZERO_PERCENT.getValue());
} else {
extracted(statusService, device, UniversalEnum.ZERO_PERCENT.getValue());
extracted(statusService, device, false, UniversalEnum.ONE_HUNDRED_PERCENT.getValue());
}
} else {
extracted(statusService, device, getPingPacketLossRate(device.getDeviceIp()));
if (StringUtil.isNotEmpty(device.getDeviceIp())) {
InetAddress address = InetAddress.getByName(device.getDeviceIp());
boolean reachable = address.isReachable(UniversalEnum.FIVE_THOUSAND.getNumber()); // Timeout: 5 seconds
extracted(statusService, device, reachable, getPingPacketLossRate(device.getDeviceIp()));
} else {
extracted(statusService, device, false, UniversalEnum.ONE_HUNDRED_PERCENT.getValue());
}
}
return null;
};
@ -88,35 +94,23 @@ public class DeviceStatus {
executor.shutdown();
}
private void extracted(StatusService statusService, DcDevice device, String lostRate) {
try {
Status status = new Status();
status.setDeviceNo(device.getStakeMark());
status.setDeviceName(device.getDeviceName());
status.setDeviceIp(device.getDeviceIp());
status.setDeviceId(device.getId());
LocalDateTime localDateTime = LocalDateTime.now();
status.setTime(localDateTime);
if (StringUtil.isNotEmpty(device.getDeviceIp())) {
InetAddress address = InetAddress.getByName(device.getDeviceIp());
boolean reachable = address.isReachable(UniversalEnum.FIVE_THOUSAND.getNumber()); // Timeout: 5 seconds
status.setLostRate(lostRate);
if (reachable) {
status.setDeviceStatus(UniversalEnum.ONE.getValue());
status.setSuccessRate(UniversalEnum.ONE_HUNDRED_PERCENT.getValue());
} else {
status.setDeviceStatus(UniversalEnum.ZERO.getValue());
status.setSuccessRate(UniversalEnum.ZERO_PERCENT.getValue());
}
} else {
status.setDeviceStatus(UniversalEnum.ZERO.getValue());
status.setSuccessRate(UniversalEnum.ZERO_PERCENT.getValue());
status.setLostRate(UniversalEnum.ONE_HUNDRED_PERCENT.getValue());
}
statusService.Add(status);
} catch (IOException e) {
System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage());
private void extracted(StatusService statusService, DcDevice device, boolean reachable, String packetLossRate) {
Status status = new Status();
status.setDeviceNo(device.getStakeMark());
status.setDeviceName(device.getDeviceName());
status.setDeviceIp(device.getDeviceIp());
status.setDeviceId(device.getId());
LocalDateTime localDateTime = LocalDateTime.now();
status.setTime(localDateTime);
status.setLostRate(packetLossRate);
if (reachable) {
status.setDeviceStatus(UniversalEnum.ONE.getValue());
status.setSuccessRate(UniversalEnum.ONE_HUNDRED_PERCENT.getValue());
} else {
status.setDeviceStatus(UniversalEnum.ZERO.getValue());
status.setSuccessRate(UniversalEnum.ZERO_PERCENT.getValue());
}
statusService.Add(status);
}
@ -139,8 +133,8 @@ public class DeviceStatus {
if (line.contains("packets transmitted")) {
// Extract the number of packets sent and lost
String[] stats = line.split(", ");
packetsSent = Integer.parseInt(stats[UniversalEnum.ZERO.getNumber()].split(" ")[UniversalEnum.ZERO.getNumber()]);
packetsLost = Integer.parseInt(stats[UniversalEnum.ZERO.getNumber()].split(" ")[UniversalEnum.ZERO.getNumber()]) - Integer.parseInt(stats[UniversalEnum.ONE.getNumber()].split(" ")[UniversalEnum.ZERO.getNumber()]);
packetsSent = Integer.parseInt(stats[UniversalEnum.ZERO.getNumber()].split(UniversalEnum.BLANK_SPACE.getValue())[UniversalEnum.ZERO.getNumber()]);
packetsLost = Integer.parseInt(stats[UniversalEnum.ZERO.getNumber()].split(UniversalEnum.BLANK_SPACE.getValue())[UniversalEnum.ZERO.getNumber()]) - Integer.parseInt(stats[UniversalEnum.ONE.getNumber()].split(UniversalEnum.BLANK_SPACE.getValue())[UniversalEnum.ZERO.getNumber()]);
}
}

6
zc-business/src/main/java/com/zc/business/enums/UniversalEnum.java

@ -2077,6 +2077,12 @@ public enum UniversalEnum {
// 中冒号
IN_THE_COLON(0, ":"),
// 左小括号
OPEN_PARENTHESIS(0, "("),
// 右小括号
CLOSING_PARENTHESIS(0, ")"),
// 左中括号
LEFT_CENTER_BRACKET(0, "["),

2
zc-business/src/main/java/com/zc/business/message/device/handler/DeviceMessageHandler.java

@ -587,7 +587,7 @@ public class DeviceMessageHandler {
//设备名称
meteorologicalDetectorData.setDeviceName(deviceName+"("+stakeMark+")");
meteorologicalDetectorData.setDeviceName(deviceName+UniversalEnum.OPEN_PARENTHESIS.getValue()+stakeMark+UniversalEnum.CLOSING_PARENTHESIS.getValue());
//位置
meteorologicalDetectorData.setStakeMark(stakeMark);
//方向

38
zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

@ -920,45 +920,45 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
Response response // 请求响应
= okHttp
.headers(headers)
.url(baseUrl + "/api/dc/query/rd_tr_congestion_jh") // 请求地址
.url(baseUrl + UniversalEnum.GETS_THE_URI_OF_THE_CURRENT_CONGESTION_EVENT.getValue()) // 请求地址
.data(requestParams) // 请求参数
.post(); // 请求方法
ResponseBody body = response.body();
if (body != null) {
JSONArray jsonArray = JSON.parseArray(body.string());
JSONObject jsonObject = (JSONObject) jsonArray.get(0);
JSONObject jsonObject = (JSONObject) jsonArray.get(UniversalEnum.ZERO.getNumber());
Integer asOneRoad = jsonObject.getInteger("as_one_road");
if (asOneRoad == UniversalEnum.ONE.getNumber()){
String string = jsonObject.getString("list_link_info");
JSONArray jsonlist = JSON.parseArray(string);
JSONObject o = (JSONObject) jsonlist.get(0);
JSONObject o = (JSONObject) jsonlist.get(UniversalEnum.ZERO.getNumber());
String string1 = o.getString("firstPoint");
String string2 = o.getString("lastPoint");
String lngLats = o.getString("lngLats");
String[] lngLatsString = lngLats.split(";");
double s = Double.parseDouble(lngLatsString[0].split(",")[0]);
double s2 = Double.parseDouble(lngLatsString[0].split(",")[1]);
double s3 = Double.parseDouble(lngLatsString[lngLatsString.length-1].split(",")[0]);
double s4 = Double.parseDouble(lngLatsString[lngLatsString.length-1].split(",")[1]);
double distanceM = calculateDistance(s2, s, s4, s3)* 1000;
String[] lngLatsString = lngLats.split(UniversalEnum.COLON.getValue());
double s = Double.parseDouble(lngLatsString[UniversalEnum.ZERO.getNumber()].split(UniversalEnum.COMMA.getValue())[UniversalEnum.ZERO.getNumber()]);
double s2 = Double.parseDouble(lngLatsString[UniversalEnum.ZERO.getNumber()].split(UniversalEnum.COMMA.getValue())[UniversalEnum.ONE.getNumber()]);
double s3 = Double.parseDouble(lngLatsString[lngLatsString.length-UniversalEnum.ONE.getNumber()].split(UniversalEnum.COMMA.getValue())[UniversalEnum.ZERO.getNumber()]);
double s4 = Double.parseDouble(lngLatsString[lngLatsString.length-UniversalEnum.ONE.getNumber()].split(UniversalEnum.COMMA.getValue())[UniversalEnum.ONE.getNumber()]);
double distanceM = calculateDistance(s2, s, s4, s3)* UniversalEnum.THOUSAND.getNumber();
// System.out.printf("最大距离为: %.2f 米%n", distanceM);
jsonObject.put("max_jam_dist",distanceM);//最大拥堵距离
String[] split = string1.split(",");
double firstLon = Double.parseDouble(split[0]);
double firstLat = Double.parseDouble(split[1]);
String[] split2 = string2.split(",");
double lastLon = Double.parseDouble(split2[0]);
double lastLat = Double.parseDouble(split2[1]);
String[] split = string1.split(UniversalEnum.COMMA.getValue());
double firstLon = Double.parseDouble(split[UniversalEnum.ZERO.getNumber()]);
double firstLat = Double.parseDouble(split[UniversalEnum.ONE.getNumber()]);
String[] split2 = string2.split(UniversalEnum.COMMA.getValue());
double lastLon = Double.parseDouble(split2[UniversalEnum.ZERO.getNumber()]);
double lastLat = Double.parseDouble(split2[UniversalEnum.ONE.getNumber()]);
//double firstLat = 35.38524527319016;
// double firstLon = 118.39808642864227;
//double lastLat = 35.386351346969604;
// double lastLon = 118.4038907289505;
double distanceKm = calculateDistance(firstLat, firstLon, lastLat, lastLon);
double distanceMeters = distanceKm * 1000; // 将距离转换为米
double distanceMeters = distanceKm * UniversalEnum.THOUSAND.getNumber(); // 将距离转换为米
//System.out.printf("两点之间的距离为: %.2f 米%n", distanceMeters);
jsonObject.put("jam_dist",distanceMeters);//当前拥堵距离
@ -1012,10 +1012,10 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
double dLon = endLonRad - startLonRad;
// Haversine公式
double a = Math.pow(Math.sin(dLat / 2), 2) +
double a = Math.pow(Math.sin(dLat / UniversalEnum.TWO.getNumber()), UniversalEnum.TWO.getNumber()) +
Math.cos(startLatRad) * Math.cos(endLatRad) *
Math.pow(Math.sin(dLon / 2), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
Math.pow(Math.sin(dLon / UniversalEnum.TWO.getNumber()), UniversalEnum.TWO.getNumber());
double c = UniversalEnum.TWO.getNumber() * Math.atan2(Math.sqrt(a), Math.sqrt(UniversalEnum.ONE.getNumber() - a));
// 计算两点间的距离
double distance = EARTH_RADIUS * c;

Loading…
Cancel
Save