Browse Source

添加入库后的自检

su-lmstack 3 years ago
parent
commit
77846ad845

+ 62 - 13
src/main/java/cn/fastfun/service/impl/AppDeviceLogServiceImpl.java

@@ -7,6 +7,7 @@ import cn.fastfun.service.UtilService;
 import cn.fastfun.service.entity.AppDevice;
 import cn.fastfun.service.entity.AppDeviceLog;
 import cn.fastfun.service.entity.AppImeiHistory;
+import cn.fastfun.util.ObjectUtil;
 import com.bridge.dto.ApiDTO;
 import com.bridge.dto.ApiPageDTO;
 import com.bridge.dto.QueryParam;
@@ -19,6 +20,8 @@ import io.swagger.annotations.ApiParam;
 import io.swagger.models.auth.In;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.BooleanUtils;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
@@ -30,6 +33,8 @@ import javax.annotation.Resource;
 import javax.persistence.Query;
 import javax.transaction.Transactional;
 import java.math.BigInteger;
+//import java.sql.Timestamp;
+import java.sql.Timestamp;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -45,6 +50,10 @@ public class AppDeviceLogServiceImpl extends JpaServiceImp<AppDeviceLog, String>
 
     @Resource
     UtilService utilService;
+
+    @Resource
+    JdbcTemplate jdbcTemplate;
+
     //业务类
     @Resource(name = "appImeiHistoryService")
     JpaService<AppImeiHistory, String> appImeiHistoryService;
@@ -88,13 +97,11 @@ public class AppDeviceLogServiceImpl extends JpaServiceImp<AppDeviceLog, String>
                     AppDeviceLog appDeviceLog = new AppDeviceLog(p, appDevice.getImei()).toInStorage(time, batchNum, finalOperateID);
                     appDeviceLog.setOperator(utilService.getUserName());
                     save(appDeviceLog); // 记录入库日志
-                }
-                else if(deviceList.size() == 1) {  //如果只有1个,认为是单个入库,检测是否重复入库;批次入库只跳过
+                } else if (deviceList.size() == 1) {  //如果只有1个,认为是单个入库,检测是否重复入库;批次入库只跳过
                     throw new ApiRuntimeException(p + "设备已入库,不能重复入库!");
                 }
             });
-        }
-        else{
+        } else {
             throw new ApiRuntimeException("设备未录入!");
         }
     }
@@ -104,15 +111,57 @@ public class AppDeviceLogServiceImpl extends JpaServiceImp<AppDeviceLog, String>
     public Map<String, Boolean> selfCheck(String sn) {
         //TODO 添加自检
         Map<String, Boolean> res = new HashMap<>();
-        Boolean checkResult = false;
-        Boolean checkDataConnectResult = false;
-        Boolean checkLocationResult = false;
-        Boolean checkLockResult = false;
-        Boolean checkFaultResult = false;
-        Boolean checkVoltageResult = false;
-        if (true){
-            checkResult = true;
+        Boolean checkResult = true;
+        Boolean checkDataConnectResult = true;
+        Boolean checkLocationResult = true;
+        Boolean checkLockResult = true;
+        Boolean checkFaultResult = true;
+        Boolean checkVoltageResult = true;
+
+        Map<String, Object> bmsData = new HashMap<>();
+        Map<String, Object> gpsData = new HashMap<>();
+        try {
+            bmsData = jdbcTemplate.queryForMap("select devcode,error_code, locked_state, status_time, cell_voltages from ff_battery_status where devcode='" + sn + "'");
+        } catch (EmptyResultDataAccessException e) {
+            bmsData.put("devcode", null);
         }
+        try {
+            gpsData = jdbcTemplate.queryForMap("select devcode from ff_location where devcode='" + sn + "'");
+        } catch (EmptyResultDataAccessException e) {
+            gpsData.put("devcode", null);
+        }
+        if (StringUtils.isEmpty(bmsData.get("devcode"))) {
+            checkLockResult = false;
+            checkFaultResult = false;
+            checkVoltageResult = false;
+            checkDataConnectResult = false;
+        } else {
+//            if (!StringUtils.isEmpty(bmsData.get("locked_state")) && bmsData.get("locked_state").equals(0)) { //如果该值为null, 则放过
+//                checkLockResult = false;
+//            }
+            if (!StringUtils.isEmpty(bmsData.get("error_code")) && bmsData.get("error_code").equals(0)) {
+                checkFaultResult = false;
+            }
+            List<Double> voltage = ObjectUtil.StringToArrayList((String) bmsData.get("cell_voltages"), ",");
+            if (!StringUtils.isEmpty(bmsData.get("cell_voltages")) && (Collections.max(voltage) > 4.5 || Collections.min(voltage) < 2.5)) {
+                checkVoltageResult = false;
+            }
+
+            Calendar cal = Calendar.getInstance();
+            cal.setTime(new Date());
+            Long time1 = cal.getTimeInMillis();
+            cal.setTime((Date) bmsData.get("status_time"));
+            Long time2 = cal.getTimeInMillis();
+            if (!StringUtils.isEmpty(bmsData.get("status_time")) && (time1 - time2) / 1000.0 / 3600 > 6) {
+                checkDataConnectResult = false;
+            }
+            new Timestamp(System.currentTimeMillis());
+        }
+        if (StringUtils.isEmpty(gpsData.get("devcode"))) {
+            checkLocationResult = false;
+        }
+
+        checkResult = checkDataConnectResult && checkLocationResult && checkLockResult && checkFaultResult && checkVoltageResult;
         res.put("checkResult", checkResult);
         res.put("checkDataConnectResult", checkDataConnectResult);
         res.put("checkLocationResult", checkLocationResult);
@@ -233,7 +282,7 @@ public class AppDeviceLogServiceImpl extends JpaServiceImp<AppDeviceLog, String>
                 if (null == appDevice) {
                     new ApiRuntimeException(p + "设备信息不存在!");
                 }
-                if ( 2 != appDevice.getStatus() && 3 != appDevice.getStatus()) {
+                if (2 != appDevice.getStatus() && 3 != appDevice.getStatus()) {
                     new ApiRuntimeException(p + "不是调拨状态,不能出库!");
                 }
             });

+ 14 - 0
src/main/java/cn/fastfun/util/ObjectUtil.java

@@ -1,6 +1,8 @@
 package cn.fastfun.util;
 
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.StringTokenizer;
 
 /**
  * 对象工具类
@@ -26,4 +28,16 @@ public final class ObjectUtil {
     public static int obj2Integer(Object object) {
         return null == object ? 0 : Integer.parseInt(obj2String(object));
     }
+
+    public static ArrayList<Double> StringToArrayList(String str, String separator) {
+        ArrayList<Double> arr = new ArrayList<Double>();
+        if ((str == null) || (separator == null)) {
+            return arr;
+        }
+        StringTokenizer st = new StringTokenizer(str, separator);
+        while (st.hasMoreTokens()) {
+            arr.add(Double.parseDouble(st.nextToken()));
+        }
+        return arr;
+    }
 }