lihui001 3 年之前
父節點
當前提交
348396de97

+ 3 - 3
zoniot-common/zoniot-core-common/src/main/java/com/zcxk/core/common/exception/SystemExceptionHandler.java

@@ -133,7 +133,7 @@ public class SystemExceptionHandler implements InitializingBean {
      * @param req
      * @return
      */
-    private String getReqUrl(HttpServletRequest req) {
+    protected String getReqUrl(HttpServletRequest req) {
         String url = "uri";
         if (req != null && req.getRequestURL() != null) {
             url = req.getRequestURL().toString();
@@ -147,7 +147,7 @@ public class SystemExceptionHandler implements InitializingBean {
      * @return java.lang.String
      * @author Richard
      **/
-    private String getBindingResult(BindingResult bindingResult) {
+    protected String getBindingResult(BindingResult bindingResult) {
         List<ObjectError> errors = bindingResult.getAllErrors();
         StringBuilder errorMsg = new StringBuilder();
         for (ObjectError error : errors) {
@@ -156,7 +156,7 @@ public class SystemExceptionHandler implements InitializingBean {
         return errorMsg.toString();
     }
 
-    private String getRequestParam(HttpServletRequest request) {
+    protected String getRequestParam(HttpServletRequest request) {
         final Map<String, String[]> parameterMap = request.getParameterMap();
         return JSON.toJSONString(parameterMap);
     }

+ 39 - 3
zoniot-common/zoniot-iot-sync/src/main/java/com/zcxk/iot/sync/auth/AuthIotService.java

@@ -61,6 +61,17 @@ public class AuthIotService {
         return token;
     }
 
+    /**
+    * 清除token缓存
+    * @author Andy
+    * @date 10:13 2021/10/20
+    * @param :
+    * @return void
+    **/
+    public void cleanTokenCache(){
+        redisUtils.del(getCacheKey());
+    }
+
     /**
     * 获取物联网token
     * @author Andy
@@ -89,15 +100,40 @@ public class AuthIotService {
         return HttpUtil.post(authConfig.getAuthUrl(), params, AuthVo.class);
     }
 
+    /**
+    * 获取缓存的token
+    * @author Andy
+    * @date 10:18 2021/10/20
+    * @param :
+    * @return java.lang.String
+    **/
     private String getCacheToken(){
-        String key = String.format(REDIS_TOKEN_KEY, authConfig.getAppId());
-        return redisUtils.get(key);
+        return redisUtils.get(getCacheKey());
     }
 
+    /**
+    * 设值缓存token
+    * @author Andy
+    * @date 10:18 2021/10/20
+    * @param token:
+    * @param expires:
+    * @return void
+    **/
     private void setCacheToken(String token, int expires){
-        String key = String.format(REDIS_TOKEN_KEY, authConfig.getAppId());
+        String key = getCacheKey();
         redisUtils.set(key, token);
         redisUtils.setExpire(key, expires - 60);
     }
 
+    /**
+    * 获取缓存key
+    * @author Andy
+    * @date 10:18 2021/10/20
+    * @param :
+    * @return java.lang.String
+    **/
+    private String getCacheKey(){
+        return String.format(REDIS_TOKEN_KEY, authConfig.getAppId());
+    }
+
 }

+ 50 - 0
zoniot-common/zoniot-iot-sync/src/main/java/com/zcxk/iot/sync/enums/SyncErrorEnum.java

@@ -0,0 +1,50 @@
+package com.zcxk.iot.sync.enums;
+
+import com.zcxk.core.common.support.RespCode;
+import lombok.Getter;
+
+/**
+ * @author Andy
+ * @version V1.0
+ **/
+@Getter
+public enum SyncErrorEnum implements RespCode {
+
+    /**
+     * 访问失败
+     */
+    FAIL(204500, "访问失败!"),
+
+
+    /**
+     * 受权失败
+     */
+    AUTH_FAIL(204401, "访问物联网失败,没有权限访问"),
+
+    ;
+
+    /**
+     * 状态码
+     */
+    private int status;
+
+    /**
+     * 错误消息
+     */
+    private String message;
+
+    SyncErrorEnum(int status, String message){
+        this.status  = status;
+        this.message = message;
+    }
+
+    @Override
+    public int getStatus() {
+        return status;
+    }
+
+    @Override
+    public String getMessage() {
+        return message;
+    }
+}

+ 44 - 0
zoniot-common/zoniot-iot-sync/src/main/java/com/zcxk/iot/sync/exception/BusinessExceptionHandler.java

@@ -0,0 +1,44 @@
+package com.zcxk.iot.sync.exception;
+
+import com.zcxk.core.common.exception.BusinessException;
+import com.zcxk.core.common.exception.SystemExceptionHandler;
+import com.zcxk.core.common.pojo.AjaxMessage;
+import com.zcxk.iot.sync.auth.AuthIotService;
+import com.zcxk.iot.sync.enums.SyncErrorEnum;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @author Andy
+ * @version V1.0
+ * @description: TODO
+ * @date 2021/10/20
+ **/
+@RestControllerAdvice
+@Slf4j
+public class BusinessExceptionHandler extends SystemExceptionHandler{
+
+    @Autowired
+    private AuthIotService authIotService;
+
+    @ResponseBody
+    @ExceptionHandler(BusinessException.class)
+    @Override
+    public AjaxMessage<Void> handleBusinessException(HttpServletRequest req, BusinessException exception) {
+        String message = !StringUtils.isEmpty(exception.getMsg()) ? exception.getMsg()
+                : exception.getMessage();
+        log.info("【全局异常捕获提示】:uri: {} param:[{}] msg:{}", super.getReqUrl(req), super.getRequestParam(req),
+                message);
+        if (exception.getCode() == SyncErrorEnum.AUTH_FAIL.getStatus()) {
+            authIotService.cleanTokenCache();
+        }
+        return AjaxMessage.fail(exception.getCode(), message);
+    }
+
+}

+ 11 - 1
zoniot-common/zoniot-iot-sync/src/main/java/com/zcxk/iot/sync/utils/HttpUtil.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.zcxk.core.common.exception.BusinessException;
 import com.zcxk.core.utils.http.HttpClientPoolUtil;
+import com.zcxk.iot.sync.enums.SyncErrorEnum;
 import com.zcxk.iot.sync.vo.HttpContentVo;
 
 import java.util.Map;
@@ -16,6 +17,11 @@ import java.util.Map;
  **/
 public class HttpUtil {
 
+    /**
+     * 受权失败
+     */
+    private static  final int AUTH_FAIL = 401;
+
     /**
     * post请求,带body参数,头部带token
     * @author Andy
@@ -70,8 +76,12 @@ public class HttpUtil {
     **/
     private static <T> T convertObject(String result, Class<T> clazz){
         HttpContentVo<T> httpContentVo = JSONObject.parseObject(result, HttpContentVo.class);
+        // 没有权限
+        if (httpContentVo.getStatus() == AUTH_FAIL) {
+            throw BusinessException.builder(SyncErrorEnum.AUTH_FAIL.getStatus(), "访问繁忙,请稍后重试");
+        }
         if (httpContentVo.getStatus() != 0 ) {
-            throw BusinessException.builder(500, "同步物联网出错:" + httpContentVo.getMsg());
+            throw BusinessException.builder(SyncErrorEnum.FAIL.getStatus(), "同步物联网出错:" + httpContentVo.getMsg());
         }
         T t = httpContentVo.getData();
         if (t == null || clazz == null) {

+ 10 - 2
zoniot-water/zoniot-water-web/src/main/java/com/zcxk/water/web/controller/NotifyController.java

@@ -3,6 +3,7 @@ package com.zcxk.water.web.controller;
 import com.zcxk.core.common.pojo.AjaxMessage;
 import com.zcxk.water.api.dto.AlarmDetailsAddDto;
 import com.zcxk.water.api.dto.DeviceDataDto;
+import com.zcxk.water.api.dto.DeviceStateDto;
 import com.zcxk.water.web.service.AlarmDetailsService;
 import com.zcxk.water.web.service.NotifyService;
 import io.swagger.annotations.Api;
@@ -30,9 +31,9 @@ public class NotifyController {
     @Autowired
     private NotifyService notifyService;
 
-    @RequestMapping(value="alarm/add" , method = RequestMethod.POST)
+    @RequestMapping(value="add/alarm" , method = RequestMethod.POST)
     @ApiOperation(value = "告警信息添加")
-    public AjaxMessage<Integer> alarmAdd(@ApiParam(value = "报警详情信息") @RequestBody AlarmDetailsAddDto dto){
+    public AjaxMessage<Integer> addAlarm(@ApiParam(value = "报警详情信息") @RequestBody AlarmDetailsAddDto dto){
         return AjaxMessage.success(notifyService.insertAlarmInfo(dto));
     }
 
@@ -44,5 +45,12 @@ public class NotifyController {
         return AjaxMessage.success();
     }
 
+    @RequestMapping(value="update/device/state" , method = RequestMethod.POST)
+    @ApiOperation(value = "更新设备状态")
+    public AjaxMessage updateDeviceState(@ApiParam(value = "设备数据处理") @RequestBody DeviceStateDto dto){
+        notifyService.updateDeviceSate(dto);
+        return AjaxMessage.success();
+    }
+
 
 }

+ 1 - 1
zoniot-water/zoniot-water-web/src/main/java/com/zcxk/water/web/service/impl/NotifyServiceImpl.java

@@ -53,9 +53,9 @@ public class NotifyServiceImpl implements NotifyService {
     public void updateDeviceSate(DeviceStateDto dto) {
         // 修改设备状态
         deviceMapper.updateStateByIotDeviceId(dto.getDeviceId(), dto.getState());
-        Long deviceId = deviceMapper.findDeviceIdByIotDeviceId(dto.getDeviceId());
         // 如果正常就要变更报警为正常
         if (!DeviceStatusEnum.offline(dto.getState())) {
+            Long deviceId = deviceMapper.findDeviceIdByIotDeviceId(dto.getDeviceId());
             alarmTypeDetailsService.updateStateByDeviceId(deviceId, AlarmStateEnum.HISTORY_ALARM.getCode());
         }
     }