Browse Source

优化天气预报返回

设备功能以及天气相关接口添加注解
develop
zhaoxianglong 9 months ago
parent
commit
44d935a51f
  1. 16
      zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java
  2. 80
      zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java
  3. 18
      zc-business/src/main/java/com/zc/business/enums/UniversalEnum.java

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

@ -536,7 +536,9 @@ public class DcDeviceController extends BaseController {
String iotDeviceId = device.getString("iotDeviceId");
String deviceType = device.getString("deviceType");
boolean continueToExecute = true;
//判断是否为需要执行回滚的设备类型
if (Objects.equals(deviceType, UniversalEnum.TEN.getValue())) {
//判断iotDeviceId是否为空 为空则直接整理封装错误数据 否则继续执行调用功能
if (iotDeviceId == null) {
for (Object function : functions) {
@ -572,23 +574,23 @@ public class DcDeviceController extends BaseController {
}
} else {
//获取要回滚的数据
JSONObject mdJSONObject = (JSONObject) JSON.toJSON(getDeviceLatestProperty(iotDeviceId, UniversalEnum.MD.getValue()).get("data"));
String mdValue = mdJSONObject.getString("value");
JSONObject tmJSONObject = (JSONObject) JSON.toJSON(getDeviceLatestProperty(iotDeviceId, UniversalEnum.TM.getValue()).get("data"));
String tmValue = tmJSONObject.getString("value");
//循环执行功能
for (Object function : functions) {
JSONObject functionJSONObject = (JSONObject) JSON.toJSON(function);
//JSONObject functionJSONObject = (JSONObject) JSON.toJSON(function.toString()) ;
String functionId = functionJSONObject.getString("functionId");
//判断此设备之前的数据是否失败过 没有失败过则正常执行设备功能调用 否则直接整理封装错误数据
if (continueToExecute) {
JSONObject jsonObject = functionJSONObject.getJSONObject("params") != null ? functionJSONObject.getJSONObject("params") : new JSONObject();
JSONObject result = getResult(device, iotDeviceId, functionId, jsonObject);
resultArray.add(result);
AjaxResult ajaxResult = (AjaxResult) result.get("result");
//判断是否成功 如果不成功则将 continueToExecute 设为失败过 并且判断设备类型是否为15 太阳能板 并且返回结束
if (!Objects.equals(String.valueOf(ajaxResult.get("code")), UniversalEnum.TWO_HUNDRED.getValue())) {
continueToExecute = false;
if (Objects.equals(device.getString("deviceType"), UniversalEnum.FIFTEEN.getValue())) {
@ -597,6 +599,7 @@ public class DcDeviceController extends BaseController {
}
} else {
List<Object> collect = resultArray.stream().filter(item -> Objects.equals(((JSONObject) JSON.toJSON(item)).getString("device"), device.getString("id"))).collect(Collectors.toList());
//判断返回结果中是否已经有此设备的数据 如果没有 则整理封装一个失败的结果并加入到返回集合中 否则替换数据
if (collect.size() == UniversalEnum.ZERO.getNumber()) {
JSONObject result = new JSONObject();
result.put("device", device.getString("id"));
@ -605,10 +608,13 @@ public class DcDeviceController extends BaseController {
result.put("result", AjaxResult.error());
resultArray.add(result);
} else {
//循环结果集合
for (int i = UniversalEnum.ZERO.getNumber(); i < resultArray.size(); i++) {
JSONObject jsonObject = (JSONObject) JSON.toJSON(resultArray.get(i));
//判断结果集合中是否存在此设备 不存在则结束此循环 存在则替换数据
if (!Objects.equals(jsonObject.getString("device"), device.getString("id"))) {
JSONObject result = jsonObject.getJSONObject("result");
//判断此内容是否为失败 为失败则代表是已经整理过的数据 无需处理直接结束此循环 否则替换数据 将code值200替换为500
if (!Objects.equals(result.getString("code"), UniversalEnum.TWO_HUNDRED.getValue())) {
break;
} else {
@ -622,12 +628,14 @@ public class DcDeviceController extends BaseController {
}
}
//判断此功能ID是否为SETMD 是则执行回滚数据
if (Objects.equals(functionId, UniversalEnum.SETMD.getValue())) {
JSONObject jsonObjectMD = new JSONObject();
jsonObjectMD.put(UniversalEnum.SET.getValue(), mdValue);
getAjaxResult(iotDeviceId, functionId, jsonObjectMD.toJavaObject(new TypeReference<HashMap<String, Object>>() {
}));
}
//判断此功能ID是否为SETTM 是则执行回滚数据
if (Objects.equals(functionId, UniversalEnum.SETTM.getValue())) {
JSONObject jsonObjectTM = new JSONObject();
jsonObjectTM.put(UniversalEnum.SET.getValue(), tmValue);

80
zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java

@ -25,10 +25,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@ -224,11 +221,13 @@ public class WeatherForecastController extends BaseController {
@PostMapping(value = "/currentWeatherAndForecastInformation")
public AjaxResult currentWeatherAndForecastInformation(@RequestBody HashMap<String, Object> parameter) {
//拿到获取到的桩号值并处理拆分
String stakeNum = String.valueOf(parameter.get("stakeNum"));
String[] split = stakeNum.split("[|]");
StringBuilder numStake = new StringBuilder();
//循环执行查询周围最近的5的倍数的桩号(集团接口需求 桩号从K50+000开始 K55+000 K60+000……以此类推)
for (int i = UniversalEnum.ZERO.getNumber(); i < split.length; i++) {
String[] split1 = split[i].split("[+]");
String k = split1[UniversalEnum.ZERO.getNumber()].replace(UniversalEnum.CAPITAL_K.getValue(), UniversalEnum.EMPTY_STRING.getValue());
@ -257,22 +256,28 @@ public class WeatherForecastController extends BaseController {
if (response.body() != null) {
JSONObject jsonResult = JSONObject.parseObject(response.body().string());
//判断请求是否成功 成功则判断data字段数组长度是否为0 否则获取redis中的缓存进行返回
if (jsonResult.getInteger("code") == UniversalEnum.TWO_HUNDRED.getNumber()) {
JSONArray data = jsonResult.getJSONArray("data");
;
JSONArray jsonArray = processingWeatherData(data);
//判断返回data是否不为0 不为0则将数据进行返回 为0则获取redis中的缓存进行返回
if (jsonArray.size() != UniversalEnum.ZERO.getNumber()) {
redisCache.deleteObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake);
redisCache.setCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake, jsonArray);
return AjaxResult.success(jsonArray);
} else {
JSONArray cacheObject = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake);
return AjaxResult.success(cacheObject);
if (cacheObject != null) {
return AjaxResult.success(cacheObject);
}
}
} else {
JSONArray cacheObject = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake);
return AjaxResult.success(cacheObject);
if (cacheObject != null) {
return AjaxResult.success(cacheObject);
}
}
}
} catch (Exception ignored) {
@ -280,8 +285,56 @@ public class WeatherForecastController extends BaseController {
logger.error(String.valueOf(ignored));
}
//如果出现错误直接返回redis缓存中的数据
JSONArray cacheObject = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake);
return AjaxResult.success(cacheObject);
//判断数据库中是否存在此桩号数据 存在直接返回 不存在则查询指定索引数据
if (cacheObject != null) {
return AjaxResult.success(cacheObject);
} else {
//获取执行索引的数据
JSONArray cacheObject1 = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_DISTRICTS_AND_COUNTIES.getValue());
//获取桩号
String s = String.valueOf(numStake);
//获取桩号指定字段
String replace = s.split(UniversalEnum.BARRE_PLUS.getValue())[UniversalEnum.ZERO.getNumber()].replace(UniversalEnum.CAPITAL_K.getValue(), UniversalEnum.EMPTY_STRING.getValue());
//将指定字段进行匹配相近的桩号指定字段
String s1 = queryCloseValue(Integer.parseInt(replace));
//指定字段替换为相近的桩号的指定字段 获取正确桩号
String replace1 = s.replace(replace, s1);
//筛选 获取相近桩号的指定字段的数据
List<Object> collect = cacheObject1.stream().filter(item -> {
JSONObject jsonObject = (JSONObject) JSON.toJSON(item);
return Objects.equals(jsonObject.getString("stakeNum"), replace1);
}).collect(Collectors.toList());
//JSONArray cacheObject2 = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + replace1);
//返回筛选好的数据
return AjaxResult.success(collect);
}
}
private static String queryCloseValue(int target) {
int[] array = new int[UniversalEnum.EIGHT.getNumber()];
array[UniversalEnum.ZERO.getNumber()] = UniversalEnum.SIXTY.getNumber();
array[UniversalEnum.ONE.getNumber()] = UniversalEnum.ONE_HUNDRED_AND_FIVE.getNumber();
array[UniversalEnum.TWO.getNumber()] = UniversalEnum.ONE_HUNDRED_AND_FORTY_FIVE.getNumber();
array[UniversalEnum.THREE.getNumber()] = UniversalEnum.ONE_HUNDRED_AND_SIXTY_FIVE.getNumber();
array[UniversalEnum.FOUR.getNumber()] = UniversalEnum.ONE_HUNDRED_SEVENTY_FIVE.getNumber();
array[UniversalEnum.FIVE.getNumber()] = UniversalEnum.ONE_HUNDRED_AND_NINETY.getNumber();
array[UniversalEnum.SIX.getNumber()] = UniversalEnum.TWO_HUNDRED.getNumber();
array[UniversalEnum.SEVEN.getNumber()] = UniversalEnum.TWO_HUNDRED_AND_FIVE.getNumber();
//int target = 88;
int closest = array[UniversalEnum.ZERO.getNumber()];
int minDiff = Math.abs(array[UniversalEnum.ZERO.getNumber()] - target);
for (int i = UniversalEnum.ONE.getNumber(); i < array.length; i++) {
int diff = Math.abs(array[i] - target);
if (diff < minDiff) {
minDiff = diff;
closest = array[i];
}
}
System.out.println(closest);
return String.valueOf(closest);
}
public JSONArray processingWeatherData(JSONArray data) {
@ -290,22 +343,27 @@ public class WeatherForecastController extends BaseController {
JSONObject jsonObject = (JSONObject) JSON.toJSON(data.get(i));
String pre = jsonObject.getString("pre");
//判断降水量是否为0 为0判断湿度以及能见度 否则判断降水等级以及降水类型
if (Objects.equals(pre, UniversalEnum.ZERO.getValue()) || Double.parseDouble(pre) == UniversalEnum.ZERO.getNumber()) {
String visLevel = jsonObject.getString("visLevel");
Double rhu = jsonObject.getDouble("rhu");
//判断湿度是否大于90 如果大于90咋返回阴天 否则判断能见度等级
if (rhu > UniversalEnum.NINETY.getNumber()) {
jsonObject.put("weatherCondition", UniversalEnum.OVERCAST_SKY.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.OVERCAST_SKY.getNumber());
} else {
//判断能见度等级为0则返回晴天
if (Objects.equals(visLevel, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
// 判断能见度等级为2、3则返回多云
} else if (Objects.equals(visLevel, UniversalEnum.TWO.getValue()) || Objects.equals(visLevel, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLOUDY.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLOUDY.getNumber());
// 判断能见度等级为4、5、6则返回阴天
} else if (Objects.equals(visLevel, UniversalEnum.FOUR.getValue()) || Objects.equals(visLevel, UniversalEnum.FIVE.getValue()) || Objects.equals(visLevel, UniversalEnum.SIX.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.OVERCAST_SKY.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.OVERCAST_SKY.getNumber());
@ -316,6 +374,7 @@ public class WeatherForecastController extends BaseController {
String preLevel = jsonObject.getString("preLevel");
String pph = jsonObject.getString("pph");
//判断降水等级
switch (preLevel) {
case "0":
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
@ -323,6 +382,7 @@ public class WeatherForecastController extends BaseController {
break;
case "1":
case "2":
//判断降水类型
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SPIT.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SPIT.getNumber());
@ -341,6 +401,7 @@ public class WeatherForecastController extends BaseController {
}
break;
case "3":
//判断降水类型
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.MODERATE_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.MODERATE_RAIN.getNumber());
@ -359,6 +420,7 @@ public class WeatherForecastController extends BaseController {
}
break;
case "4":
//判断降水类型
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.HEAVY_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.HEAVY_RAIN.getNumber());
@ -377,6 +439,7 @@ public class WeatherForecastController extends BaseController {
}
break;
case "5":
//判断降水类型
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.RAINSTORM.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.RAINSTORM.getNumber());
@ -395,6 +458,7 @@ public class WeatherForecastController extends BaseController {
}
break;
case "6":
//判断降水类型
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.BIG_RAINSTORM.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.BIG_RAINSTORM.getNumber());
@ -413,6 +477,7 @@ public class WeatherForecastController extends BaseController {
}
break;
case "7":
//判断降水类型
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.EXTREMELY_HEAVY_RAINSTORM.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.EXTREMELY_HEAVY_RAINSTORM.getNumber());
@ -434,6 +499,7 @@ public class WeatherForecastController extends BaseController {
}
JSONArray forecastList = jsonObject.getJSONArray("forecastList");
//判断是否存在预报内容 是则递归执行本方法 否则直接将整理好数据加入返回数组中
if (forecastList != null && forecastList.size() != UniversalEnum.ZERO.getNumber()) {
jsonObject.put("forecastList", processingWeatherData(forecastList));

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

@ -1420,6 +1420,9 @@ public enum UniversalEnum {
// 105
ONE_HUNDRED_AND_FIVE(105, "105"),
// 145
ONE_HUNDRED_AND_FORTY_FIVE(145, "145"),
// 150
ONE_HUNDRED_AND_FIFTY(150, "150"),
@ -1432,15 +1435,27 @@ public enum UniversalEnum {
// 154.585
ONE_HUNDRED_AND_FIFTY_FOUR_FIFTY_FIVE(154, "154.585"),
// 165
ONE_HUNDRED_AND_SIXTY_FIVE(165, "165"),
// 175
ONE_HUNDRED_SEVENTY_FIVE(175, "175"),
// 180
ONE_HUNDRED_AND_EIGHT(180, "180"),
// 190
ONE_HUNDRED_AND_NINETY(190, "190"),
// 200
TWO_HUNDRED(200, "200"),
// 204
TWO_HUNDRED_AND_FORTY(204, "204"),
// 205
TWO_HUNDRED_AND_FIVE(205, "205"),
// 206
TWO_HUNDRED_AND_SIX(206, "206"),
@ -1783,6 +1798,9 @@ public enum UniversalEnum {
// 当前气象及预报信息查询 currentWeatherAndForecastInformation
CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD(0, "currentWeatherAndForecastInformation"),
// 当前气象及预报信息查询 currentWeatherAndForecastInformationK60+000|K105+000|K145+000|K165+000|K175+000|K190+000|K200+000|K205+000
CURRENT_WEATHER_AND_FORECAST_INFORMATION_DISTRICTS_AND_COUNTIES(0, "currentWeatherAndForecastInformationK60+000|K105+000|K145+000|K165+000|K175+000|K190+000|K200+000|K205+000"),
// /preset/addPreset
NEW_PRESET_BIT(0, "/preset/addPreset"),

Loading…
Cancel
Save