Browse Source

设备人脸库逻辑修改,设备在线和离线逻辑修改

M墨鱼—_mo 5 years ago
parent
commit
d120a27232

+ 8 - 0
common/src/main/java/com/jpsoft/smart/modules/base/entity/DeviceInfo.java

@@ -149,6 +149,14 @@ public class DeviceInfo {
 	@ApiModelProperty(value = "默认人脸库id")
 	private String defaultFaceLibraryId;
 
+
+	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone ="GMT+8")
+	@ApiModelProperty(value = "心跳更新时间")
+	private Date heartbeatUpdateTime;
+
+	@ApiModelProperty(value = "异常原因")
+	private String abnormalReason;
+
 	public String getIsOnlineN() {
 		if(isOnline!=null) {
 			if (isOnline) {

+ 1 - 0
common/src/main/java/com/jpsoft/smart/modules/base/service/DeviceInfoService.java

@@ -18,4 +18,5 @@ public interface DeviceInfoService {
 	List<DeviceInfo> findByCompanyId(String companyId);
 	Page<DeviceInfo> pageSearch(Map<String, Object> searchParams, int pageNum, int pageSize,boolean count, List<Sort> sortList);
 	void updateByDeviceNo(String deviceNo, String ip);
+
 }

+ 10 - 1
common/src/main/java/com/jpsoft/smart/modules/base/service/impl/DeviceInfoServiceImpl.java

@@ -1,5 +1,6 @@
 package com.jpsoft.smart.modules.base.service.impl;
 
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import javax.annotation.Resource;
@@ -91,6 +92,14 @@ public class DeviceInfoServiceImpl implements DeviceInfoService {
 			DeviceInfo deviceInfo= deviceInfoDAO.findByDeviceNo(deviceNo);
 			String oldIp = deviceInfo.getIpAddress();
 
+			if (deviceInfo != null){
+				deviceInfo.setIsOnline(true);
+				deviceInfo.setHeartbeatUpdateTime(new Date());
+				deviceInfo.setAbnormalReason("");
+				deviceInfoDAO.update(deviceInfo);
+			}
+
+
 			if (deviceInfo.getIsAutoUpdate()!=null &&
 			deviceInfo.getIsAutoUpdate() && !deviceInfo.getIpAddress().equals(ip)){
 				deviceInfo.setIpAddress(ip);
@@ -98,7 +107,7 @@ public class DeviceInfoServiceImpl implements DeviceInfoService {
 				log.warn(deviceNo+"的ip由原来的:"+oldIp+"自动更新为:"+ip);
 			}
 		}catch (Exception e){
-			log.error(deviceNo +":" +e.getMessage());
+			log.error("ip:"+ip+",deviceNo:"+deviceNo +":" +e.getMessage());
 		}
 
 	}

+ 58 - 14
common/src/main/java/com/jpsoft/smart/modules/lapi/service/impl/LapiServiceImpl.java

@@ -92,7 +92,7 @@ public class LapiServiceImpl implements ILapiService {
             throw new Exception("人员与设备绑定错误");
         }
         String base64Data = OSSUtil.downloadToBase64(personInfo.getFaceImageUrl());
-        if (base64Data.length()>512*1024 || base64Data.length()<10*1024){
+        if (base64Data.length() > 512 * 1024 || base64Data.length() < 10 * 1024) {
             throw new Exception("人员照片大小限定为10k~512k");
         }
         List<LapiMsgResult> lapiMsgResults = new ArrayList<>();
@@ -170,19 +170,32 @@ public class LapiServiceImpl implements ILapiService {
         JSONObject response = LApiUtil.getResponse(dataJson);
         List listData = response.getJSONArray("LibList");
         if (listData.size() <= 0) {
-            throw new Exception(deviceInfo.getAliasName()+"设备没有配置人脸库");
+            throw new Exception(deviceInfo.getAliasName() + "设备没有配置人脸库");
         }
         for (int i = 0; i < listData.size(); i++) {
             JSONObject jsonObject1 = (JSONObject) JSONArray.toJSON(listData.get(i));
             String name = jsonObject1.getString("Name");
             if (StringUtils.isNotBlank(name) && companyName.equals(name)) {
                 faceDbId = jsonObject1.getString("ID");
+                break;
             }
         }
         //人员所属的公司库不存在,保存到默认库
         if (StringUtils.isBlank(faceDbId)) {
-            throw new Exception(deviceInfo.getAliasName()+"设备中未配置"+companyName+"人脸库");
-       //    faceDbId = deviceInfo.getDefaultFaceLibraryId();
+            for (int i = 0; i < listData.size(); i++) {
+                JSONObject jsonObject1 = (JSONObject) JSONArray.toJSON(listData.get(i));
+                String name = jsonObject1.getString("Name");
+                if ("默认员工库".equals(name)) {
+                    faceDbId = jsonObject1.getString("ID");
+                    break;
+                }
+            }
+            if (StringUtils.isBlank(faceDbId)){
+                faceDbId = getDefaultLib(deviceInfo.getId());
+            }
+
+
+
         }
         return faceDbId;
     }
@@ -255,7 +268,7 @@ public class LapiServiceImpl implements ILapiService {
 
             } catch (Exception e) {
 
-                log.error(e.getMessage(),e);
+                log.error(e.getMessage(), e);
                 LapiMsgResult lapiMsgResult = new LapiMsgResult();
                 lapiMsgResult.setPersonName(personInfo.getName());
                 lapiMsgResult.setSuccess(false);
@@ -272,23 +285,23 @@ public class LapiServiceImpl implements ILapiService {
     }
 
     @Override
-    public List<LapiMsgResult> deleteDevicePerson(Long id,String deviceIds) throws Exception {
-        if (StringUtils.isBlank(deviceIds)){
+    public List<LapiMsgResult> deleteDevicePerson(Long id, String deviceIds) throws Exception {
+        if (StringUtils.isBlank(deviceIds)) {
             throw new Exception("相关设备不能为空");
         }
         PersonInfo personInfo = personInfoService.get(id);
-        if (personInfo == null){
+        if (personInfo == null) {
             throw new Exception("人员信息不存在");
         }
         CompanyInfo companyInfo = companyInfoService.get(personInfo.getCompanyId());
-        if (companyInfo == null){
+        if (companyInfo == null) {
             throw new Exception("人员没有相应公司");
         }
         List<LapiMsgResult> lapiMsgResults = new ArrayList<>();
         List<String> deviceIdList = Arrays.asList(deviceIds.split(","));
-        for (String deviceId:deviceIdList ){
+        for (String deviceId : deviceIdList) {
             DeviceInfo deviceInfo = deviceInfoService.get(deviceId);
-            try{
+            try {
                 String faceDbId = getFaceDbId(deviceInfo, companyInfo.getName());
                 JSONObject jsonObject = LApiUtil.DeleteRequest(deviceInfo.getIpAddress() + ":" + deviceInfo.getPort() + LApiConstant.DELETEPERSON + faceDbId + "/People/" + personInfo.getId() + "?LastChange=" + new Date().getTime());
                 JSONObject dataJson = jsonObject.getJSONObject("Response");
@@ -300,8 +313,8 @@ public class LapiServiceImpl implements ILapiService {
                     lapiMsgResult.setAliasName(deviceInfo.getAliasName());
                     lapiMsgResults.add(lapiMsgResult);
                 }
-            }catch (Exception e){
-                log.error(e.getMessage(),e);
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
                 LapiMsgResult lapiMsgResult = new LapiMsgResult();
                 lapiMsgResult.setPersonName(personInfo.getName());
                 lapiMsgResult.setSuccess(false);
@@ -384,7 +397,7 @@ public class LapiServiceImpl implements ILapiService {
                 HashMap dataMap = new HashMap<>();
                 dataMap.put("CompanyId", companyInfoId);
                 dataMap.put("CompanyName", companyInfo.getName());
-                dataMap.put("deviceId",deviceInfoId);
+                dataMap.put("deviceId", deviceInfoId);
                 dataMap.put("LibraryId", response.getString("ID"));
                 lapiResult.setData(dataMap);
 
@@ -404,6 +417,37 @@ public class LapiServiceImpl implements ILapiService {
         return lapiResultList;
     }
 
+    public String getDefaultLib(String deviceInfoId) throws Exception {
+
+        if (StringUtils.isBlank(deviceInfoId)) {
+            throw new Exception("请选择设备");
+        }
+        DeviceInfo deviceInfo = deviceInfoService.get(deviceInfoId);
+        try {
+            if (deviceInfo == null) {
+                throw new Exception("设备不存在");
+            }
+
+
+
+            HashMap map = new HashMap();
+            map.put("BelongIndex", "");
+            map.put("LastChange", new Date().getTime());
+            map.put("MemberNum", 0);
+            map.put("Name", "默认员工库");
+            map.put("Type", 3);
+            JSONObject jsonObject = LApiUtil.PostRequest(deviceInfo.getIpAddress() + ":" + deviceInfo.getPort() + LApiConstant.ADDLIBRARY, map);
+            JSONObject dataJson = jsonObject.getJSONObject("Response");
+            JSONObject response = LApiUtil.getResponse(dataJson);
+            return response.getString("ID");
+
+        } catch (Exception e) {
+            log.error(e.getMessage());
+
+        }
+        return "";
+    }
+
     @Override
     public LapiResult deleteCompanyLib(String deviceInfoId, String libraryId) throws Exception {
         if (StringUtils.isBlank(deviceInfoId)) {

+ 12 - 2
common/src/main/resources/mapper/base/DeviceInfo.xml

@@ -24,6 +24,8 @@
 			<result property="updateBy" column="update_by" />
 			<result property="updateTime" column="update_time" />
 		    <result property="defaultFaceLibraryId" column="default_face_library_id" />
+		    <result property="heartbeatUpdateTime" column="heartbeat_update_time" />
+		    <result property="abnormalReason" column="abnormal_reason" />
 			</resultMap>
 	<insert id="insert" parameterType="com.jpsoft.smart.modules.base.entity.DeviceInfo">
 	<!--
@@ -33,7 +35,7 @@
 	-->
 	<![CDATA[
 		insert into base_device_info
-	    (id_,header_,device_no,alias_name,address_,is_online,company_id,unlock_password,code_address,ip_address,port_,type_,is_auto_update,del_flag,create_by,create_time,update_by,update_time,default_face_library_id)
+	    (id_,header_,device_no,alias_name,address_,is_online,company_id,unlock_password,code_address,ip_address,port_,type_,is_auto_update,del_flag,create_by,create_time,update_by,update_time,default_face_library_id,heartbeat_update_time,abnormal_reason)
 		values
 		(
 			#{id,jdbcType=VARCHAR}
@@ -55,6 +57,8 @@
 			,#{updateBy,jdbcType=VARCHAR}
 			,#{updateTime,jdbcType= TIMESTAMP }
 			,#{defaultFaceLibraryId,jdbcType=VARCHAR}
+            ,#{heartbeatUpdateTime,jdbcType=TIMESTAMP}
+            ,#{abnormalReason,jdbcType=VARCHAR}
 		)
 	]]>
 	</insert>
@@ -118,6 +122,12 @@
 			<if test="defaultFaceLibraryId!=null">
 				default_face_library_id=#{defaultFaceLibraryId,jdbcType= VARCHAR },
 			</if>
+			<if test="heartbeatUpdateTime!=null">
+				heartbeat_update_time=#{heartbeatUpdateTime,jdbcType= TIMESTAMP },
+			</if>
+			<if test="abnormalReason!=null">
+				abnormal_reason=#{abnormalReason,jdbcType= VARCHAR },
+			</if>
 		</set>
 	where id_=#{id}
 	</update>
@@ -127,7 +137,7 @@ id_, header_,device_no,alias_name,address_,is_online,company_id,unlock_password,
 	</select>
 	<select id="getByDeviceNo" parameterType="string" resultMap="DeviceInfoMap">
 		select
-id_, header_,device_no,alias_name,address_,is_online,company_id,unlock_password,code_address,ip_address,port_,type_,is_auto_update,del_flag,create_by,create_time,update_by,update_time,default_face_library_id		from base_device_info where device_no=#{0} and del_flag=false
+id_, header_,device_no,alias_name,address_,is_online,company_id,unlock_password,code_address,ip_address,port_,type_,is_auto_update,del_flag,create_by,create_time,update_by,update_time,default_face_library_id,heartbeat_update_time,abnormal_reason		from base_device_info where device_no=#{0} and del_flag=false
 	</select>
 	<select id="exist" parameterType="string" resultType="int">
 		select count(*) from base_device_info where id_=#{0} and  del_flag=false

+ 59 - 0
web/src/main/java/com/jpsoft/smart/schduled/HeartbeatDetectionTask.java

@@ -0,0 +1,59 @@
+package com.jpsoft.smart.schduled;
+
+import com.jpsoft.smart.modules.base.entity.DeviceInfo;
+import com.jpsoft.smart.modules.base.service.DeviceInfoService;
+import com.jpsoft.smart.modules.lapi.service.ILapiService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+import java.util.List;
+
+/**
+ * @author 墨鱼_mo
+ * @date 2020-3-25 11:11
+ */
+@Component
+@Slf4j
+public class HeartbeatDetectionTask {
+
+    @Autowired
+    private DeviceInfoService deviceInfoService;
+
+    @Autowired
+    private ILapiService lapiService;
+
+    //每2分钟执行一次
+    @Scheduled(cron="0 0/1 * * * ?")
+    public void run() {
+
+        List<DeviceInfo> deviceInfoList = deviceInfoService.list();
+
+        for (DeviceInfo deviceInfo : deviceInfoList){
+            if (deviceInfo.getIsOnline() && deviceInfo.getHeartbeatUpdateTime() != null){
+                Long heartbeatUpdateTime = deviceInfo.getHeartbeatUpdateTime().getTime();
+                if (new Date().getTime() - heartbeatUpdateTime >2*60*1000){
+                    deviceInfo.setIsOnline(false);
+                    try{
+                        Boolean isKeepAlive = lapiService.keepAlive(deviceInfo.getId());
+                        if (isKeepAlive){
+                            deviceInfo.setAbnormalReason("设备可正常访问,但无心跳返回,请查看设备服务器配置");
+                        }else {
+                            deviceInfo.setAbnormalReason("设备离线");
+                        }
+                        deviceInfoService.update(deviceInfo);
+                    }catch (Exception e){
+                        deviceInfo.setAbnormalReason("设备离线");
+                        deviceInfoService.update(deviceInfo);
+                    }
+
+                }
+            }
+        }
+
+
+        }
+
+}