Browse Source

优化魔法值

优化设备状态定时任务
develop
zhaoxianglong 5 months ago
parent
commit
669b517cd9
  1. 60
      zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java
  2. 32
      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

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

@ -192,6 +192,7 @@ public class DcDeviceController extends BaseController {
/**
* 导出
*
* @param response 响应
* @param iotDevice 导入数据结果
*/
@ -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);
}
@ -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,27 +679,25 @@ 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")
@ -710,6 +709,7 @@ public class DcDeviceController extends BaseController {
/**
* 分页查询设备名称列表
*
* @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,6 +730,7 @@ public class DcDeviceController extends BaseController {
startPage();
return getDataTable(dcDeviceService.selectDeviceParameterProperties(dcDevice));
}
/**
* 查询设备指定属性属性列表
* param参数
@ -737,13 +739,14 @@ public class DcDeviceController extends BaseController {
* 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")){
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();//设备类型
@ -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"));
// }
// }
//}
}

32
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,8 +94,7 @@ public class DeviceStatus {
executor.shutdown();
}
private void extracted(StatusService statusService, DcDevice device, String lostRate) {
try {
private void extracted(StatusService statusService, DcDevice device, boolean reachable, String packetLossRate) {
Status status = new Status();
status.setDeviceNo(device.getStakeMark());
status.setDeviceName(device.getDeviceName());
@ -97,10 +102,7 @@ public class DeviceStatus {
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);
status.setLostRate(packetLossRate);
if (reachable) {
status.setDeviceStatus(UniversalEnum.ONE.getValue());
status.setSuccessRate(UniversalEnum.ONE_HUNDRED_PERCENT.getValue());
@ -108,15 +110,7 @@ public class DeviceStatus {
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());
}
}
@ -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