Prechádzať zdrojové kódy

闸机设备增加访问类型绑定

yingp 6 rokov pred
rodič
commit
4418ce9eb4

+ 12 - 2
applications/device/device-client/src/main/java/com/usoftchina/smartschool/device/client/listener/AccessControlListener.java

@@ -1,6 +1,8 @@
 package com.usoftchina.smartschool.device.client.listener;
 
+import com.usoftchina.smartschool.device.client.po.AccessControl;
 import com.usoftchina.smartschool.device.client.service.AccessControlService;
+import com.usoftchina.smartschool.device.dto.AccessControlInfo;
 import com.usoftchina.smartschool.device.event.AccessControlEvent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -28,9 +30,17 @@ public class AccessControlListener {
     @Async
     @EventListener(AccessControlEvent.class)
     public void onAccessControlEvent(AccessControlEvent event) {
+        AccessControlInfo info = event.getAccessControlInfo();
+        AccessControl accessControl = accessControlService.findById(event.getDeviceId());
+        // 通过门禁设备绑定的访问类型,来设置本次事件的访问类型
+        if (accessControl.isEntryType()) {
+            info.setEventType(AccessControlInfo.EventType.ENTRY);
+        } else {
+            info.setEventType(AccessControlInfo.EventType.EXIT);
+        }
         if(logger.isDebugEnabled()) {
-            logger.debug(event.getAccessControlInfo().toString());
+            logger.debug(info.toString());
         }
-        accessControlService.saveRecord(event.getAccessControlInfo());
+        accessControlService.saveRecord(info);
     }
 }

+ 48 - 1
applications/device/device-client/src/main/java/com/usoftchina/smartschool/device/client/po/AccessControl.java

@@ -1,12 +1,15 @@
 package com.usoftchina.smartschool.device.client.po;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.usoftchina.smartschool.device.dto.DeviceInfo;
 
+import java.util.Arrays;
+
 /**
  * @author yingp
  * @date 2019/3/11
  */
-public class AccessControl implements DeviceInfo{
+public class AccessControl implements DeviceInfo {
     private String id;
     /**
      * 名称
@@ -21,6 +24,11 @@ public class AccessControl implements DeviceInfo{
 
     private String password;
 
+    /**
+     * 闸机头绑定访问类型 1 进入 / 2 出去
+     */
+    private int accessType;
+
     public String getName() {
         return name;
     }
@@ -29,6 +37,7 @@ public class AccessControl implements DeviceInfo{
         this.name = name;
     }
 
+    @Override
     public String getId() {
         return id;
     }
@@ -72,4 +81,42 @@ public class AccessControl implements DeviceInfo{
     public void setPassword(String password) {
         this.password = password;
     }
+
+    public int getAccessType() {
+        return accessType;
+    }
+
+    public void setAccessType(int accessType) {
+        this.accessType = accessType;
+    }
+
+    /**
+     * 是否进门类型
+     *
+     * @return
+     */
+    @JsonIgnore
+    public boolean isEntryType() {
+        return AccessType.of(accessType) == AccessType.ENTRY;
+    }
+
+    public enum AccessType {
+        ENTRY(1),
+        EXIT(2);
+
+        private final int code;
+
+        AccessType(int code) {
+            this.code = code;
+        }
+
+        public static AccessType of(int code) {
+            for (AccessType type : values()) {
+                if (type.code == code) {
+                    return type;
+                }
+            }
+            return ENTRY;
+        }
+    }
 }

+ 10 - 4
applications/device/device-client/src/main/java/com/usoftchina/smartschool/device/client/repository/AccessControlRepository.java

@@ -2,6 +2,8 @@ package com.usoftchina.smartschool.device.client.repository;
 
 import com.usoftchina.smartschool.device.client.po.AccessControl;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.dao.EmptyResultDataAccessException;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -38,6 +40,7 @@ public class AccessControlRepository {
         }
     }
 
+    @Cacheable(value = "accessControl", key = "#id")
     public AccessControl findById(String id) {
         try {
             return jdbcTemplate.queryForObject("select * from access_control where id=?",
@@ -47,20 +50,23 @@ public class AccessControlRepository {
         }
     }
 
+    @CacheEvict(value = "accessControl", key = "#accessControl.id")
     public boolean save(AccessControl accessControl) {
-        int ret = jdbcTemplate.update("insert into access_control(id, name,ip,port,username,password) values " +
+        int ret = jdbcTemplate.update("insert into access_control(id, name,ip,port,username,password,accessType) values " +
                         "(?,?,?,?,?,?)", accessControl.getId(), accessControl.getName(), accessControl.getIp(), accessControl.getPort(),
-                accessControl.getUsername(), accessControl.getPassword());
+                accessControl.getUsername(), accessControl.getPassword(), accessControl.getAccessType());
         return ret > 0;
     }
 
+    @CacheEvict(value = "accessControl", key = "#accessControl.id")
     public boolean update(AccessControl accessControl) {
-        int ret = jdbcTemplate.update("update access_control set name=?,ip=?,port=?,username=?,password=? where " +
+        int ret = jdbcTemplate.update("update access_control set name=?,ip=?,port=?,username=?,password=?,accessType=? where " +
                         "id=?", accessControl.getName(), accessControl.getIp(), accessControl.getPort(),
-                accessControl.getUsername(), accessControl.getPassword(), accessControl.getId());
+                accessControl.getUsername(), accessControl.getPassword(), accessControl.getAccessType(), accessControl.getId());
         return ret > 0;
     }
 
+    @CacheEvict(value = "accessControl", key = "#id")
     public boolean delete(String id) {
         return jdbcTemplate.update("delete from access_control where id=?", id) > 0;
     }

+ 4 - 0
applications/device/device-client/src/main/java/com/usoftchina/smartschool/device/client/service/AccessControlService.java

@@ -94,6 +94,10 @@ public class AccessControlService {
         }
     }
 
+    public AccessControl findById(String id) {
+        return accessControlRepository.findById(id);
+    }
+
     /**
      * 远程保存门禁记录
      *

+ 3 - 1
applications/device/device-client/src/main/resources/schema.sql

@@ -22,4 +22,6 @@ port int not null,
 username varchar(30) not null,
 password varchar(50) not null,
 databaseName varchar(30) not null
-);
+);
+
+alter table access_control add column if not exists accessType int default 1;

+ 9 - 0
applications/device/device-client/src/main/resources/static/index.html

@@ -68,6 +68,13 @@
                         <input type="password" class="form-control" id="passwordInput" name="password"
                                required aria-describedby="passwordHelp" placeholder="登录密码">
                     </div>
+                    <div class="form-group">
+                        <label for="accessTypeInput">访问类型</label>
+                        <select class="form-control" id="accessTypeInput" name="accessType">
+                            <option value="1">进门</option>
+                            <option value="2">出门</option>
+                        </select>
+                    </div>
                 </form>
             </div>
             <div class="modal-footer">
@@ -83,6 +90,7 @@
         <th scope="col">名称</th>
         <th scope="col">IP</th>
         <th scope="col">端口</th>
+        <th scope="col">访问类型</th>
         <th scope="col">操作</th>
     </tr>
     </thead>
@@ -95,6 +103,7 @@
         <td><%=data[i].name%></td>
         <td><%=data[i].ip%></td>
         <td><%=data[i].port%></td>
+        <td><%=data[i].accessType == 1 ? '进门' : '出门'%></td>
         <td>
             <button type="button" class="btn btn-link btn-edit" data-index="<%=i%>" title="编辑"
                     data-toggle="modal" data-target="#formModal">

+ 1 - 0
applications/device/device-client/src/main/resources/static/js/index.js

@@ -21,6 +21,7 @@ $(document).ready(function () {
                                 $('#portInput').val(accessControl.port);
                                 $('#usernameInput').val(accessControl.username);
                                 $('#passwordInput').val(accessControl.password);
+                                $('#accessTypeInput').val(accessControl.accessType);
                             });
                             $('.btn-delete').click(function(){
                                 var index = $(this).data('index'),

+ 12 - 3
applications/device/device-sdk-dahua/src/main/java/com/usoftchina/smartschool/device/dahua/service/DahuaDataAnalyzeService.java

@@ -39,13 +39,15 @@ public class DahuaDataAnalyzeService {
     /**
      * 启动智能数据监听
      *
+     * @param deviceId 门禁设备ID
      * @param loginHandle 登录句柄
      */
-    public void startListen(NativeLong loginHandle) {
+    public void startListen(String deviceId, NativeLong loginHandle) {
         long key = loginHandle.longValue();
         if (key != 0 && !analyzerHandles.containsKey(key)) {
             NativeLong lAnalyzerHandle = sdk.getInstance().CLIENT_RealLoadPictureEx(loginHandle, 0,
-                    DahuaEvents.EVENT_IVS_ALL, true, new AnalyzerDataCallBack(), new NativeLong(), null);
+                    DahuaEvents.EVENT_IVS_ALL, true, new AnalyzerDataCallBack(deviceId),
+                    new NativeLong(), null);
             analyzerHandles.put(key, lAnalyzerHandle);
         }
     }
@@ -54,6 +56,13 @@ public class DahuaDataAnalyzeService {
      * 智能分析数据回调
      */
     class AnalyzerDataCallBack implements fAnalyzerDataCallBack {
+        private final String deviceId;
+
+        public AnalyzerDataCallBack(String deviceId) {
+            super();
+            this.deviceId = deviceId;
+        }
+
         @Override
         public void invoke(NativeLong lAnalyzerHandle, int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer,
                            int dwBufSize, NativeLong dwUser, int nSequence, Pointer reserved) {
@@ -75,7 +84,7 @@ public class DahuaDataAnalyzeService {
                     accessControlInfo.setOpenMethod(convertOpenMethod(info.emOpenMethod));
                     accessControlInfo.setEventTime(info.UTC.toDate());
                     SpringContextHolder.getContext().publishEvent(new AccessControlEvent(this,
-                            accessControlInfo));
+                            deviceId, accessControlInfo));
                     // 其他地方可通过监听 AccessControlEvent 来扩展
                     break;
                 /**

+ 1 - 1
applications/device/device-sdk-dahua/src/main/java/com/usoftchina/smartschool/device/dahua/service/DahuaDeviceService.java

@@ -44,7 +44,7 @@ public class DahuaDeviceService implements DeviceApi {
             NativeLong loginHandle = login(info.getIp(), info.getPort(), info.getUsername(), info.getPassword());
             loginHandles.put(id, loginHandle);
             // 开启智能事件监听
-            dataAnalyzeService.startListen(loginHandle);
+            dataAnalyzeService.startListen(info.getId(), loginHandle);
         }
     }
 

+ 8 - 0
applications/device/device-sdk/src/main/java/com/usoftchina/smartschool/device/dto/DeviceInfo.java

@@ -7,6 +7,14 @@ package com.usoftchina.smartschool.device.dto;
  * @date 2019/3/8
  */
 public interface DeviceInfo {
+
+    /**
+     * ID
+     *
+     * @return
+     */
+    String getId();
+
     /**
      * ip
      *

+ 12 - 1
applications/device/device-sdk/src/main/java/com/usoftchina/smartschool/device/event/AccessControlEvent.java

@@ -11,13 +11,24 @@ import org.springframework.context.ApplicationEvent;
  */
 public class AccessControlEvent extends ApplicationEvent {
 
+    private final String deviceId;
     private final AccessControlInfo accessControlInfo;
 
-    public AccessControlEvent(Object source, AccessControlInfo accessControlInfo) {
+    public AccessControlEvent(Object source, String deviceId, AccessControlInfo accessControlInfo) {
         super(source);
+        this.deviceId = deviceId;
         this.accessControlInfo = accessControlInfo;
     }
 
+    /**
+     * 产生事件的门禁设备ID
+     *
+     * @return
+     */
+    public String getDeviceId() {
+        return deviceId;
+    }
+
     public AccessControlInfo getAccessControlInfo() {
         return accessControlInfo;
     }