Browse Source

硬件实现了指纹数据上传

bfzgs 2 years ago
parent
commit
65fcee1026

+ 2 - 0
final-common/src/main/java/org/brynhild/graduation/common/constant/MqttConstant.java

@@ -14,6 +14,8 @@ public class MqttConstant {
 
     public static final String DEVICE_NOTIFY_RECORD = "mqtt_device_notify_record_";
 
+    public static final byte UPLOAD_FINGER_PRINT=1;
+
     private MqttConstant() {
     }
 

+ 9 - 0
final-device/src/main/java/org/brynhild/graduation/device/controller/AdminController.groovy

@@ -27,6 +27,15 @@ class AdminController {
         return adminService.bindDevice(request, token)
     }
 
+    @PostMapping("/device/enroll")
+    Result enrollFingerPrint(@RequestBody @Validated EnrollFingerPrint info,BindingResult result,
+                             @RequestHeader(AccountConstant.ACCOUNT_HEADER) String token){
+        if(result.hasErrors()){
+            return verifier.convertToErrors(result)
+        }
+        return adminService.enrollFingerPrint(info,token)
+    }
+
     @PostMapping("/device")
     Result findDevice(@RequestBody @Validated QueryDeviceRequest request, BindingResult result) {
         if (result.hasErrors()) {

+ 26 - 5
final-device/src/main/java/org/brynhild/graduation/device/event/MQTTHandler.java

@@ -12,7 +12,9 @@ import org.brynhild.graduation.device.event.callback.MqttDeviceCallback;
 import org.brynhild.graduation.device.util.MqttUtil;
 import org.brynhild.graduation.device.util.RedisUtil;
 import org.brynhild.graduation.persistence.device.entity.Device;
+import org.brynhild.graduation.persistence.device.entity.FingerPrintData;
 import org.brynhild.graduation.persistence.device.repository.DeviceRepository;
+import org.brynhild.graduation.persistence.device.repository.FingerPrintDataRepository;
 import org.eclipse.paho.client.mqttv3.MqttClient;
 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
 import org.eclipse.paho.client.mqttv3.MqttException;
@@ -21,6 +23,7 @@ import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -32,6 +35,8 @@ public class MQTTHandler {
     private final RedisUtil redisUtil;
     private final DeviceRepository deviceRepository;
 
+    private final FingerPrintDataRepository fingerPrintDataRepository;
+
     private final MQTTNotifyService notifyService;
     private MqttClient client;
     @Getter
@@ -49,6 +54,13 @@ public class MQTTHandler {
         }
         MqttMessage mqttMessage = new MqttMessage(message);
         mqttMessage.setQos(config.getQos());
+//        try{
+//            client.publish(MqttUtil.getServerPublish(macAddress), mqttMessage);
+//            return true;
+//        }catch (Exception e){
+//            e.printStackTrace();
+//            return false;
+//        }
         return instance.sendMessage(mqttMessage);
     }
 
@@ -103,9 +115,7 @@ public class MQTTHandler {
                 }
             });
             client.connect(options);
-
             client.subscribe(config.getListen());
-
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -128,6 +138,8 @@ public class MQTTHandler {
             client.setCallback(callback);
             client.connect(options);
             System.out.println("New handler listen on:" + MqttUtil.getServerListen(macAddress));
+            System.out.println("New handler connect state:"+client.isConnected());
+
             client.subscribe(MqttUtil.getServerListen(macAddress));
 
             MqttDeviceInstance instance = new MqttDeviceInstance(macAddress, client, persistence);
@@ -145,15 +157,22 @@ public class MQTTHandler {
 
     private void initDefaultCallbackMap() {
         defaultCallbackMap.put(MqttConstant.FINAL, (topic, message) -> {
-            String content = new String(message.getPayload());
+            byte[] payload = message.getPayload();
+            String content = new String(payload);
             String macAddress = MqttUtil.getMacAddressFromTopic(topic);
             System.out.println("content:" + content + ", mac:" + macAddress);
             final Device device = deviceRepository.findDeviceByMac(macAddress);
             if (device != null) {
                 device.setLastActive(TimeUtil.getCurrentTimeString());
                 deviceRepository.save(device);
-                if (!StringUtil.isEmpty(content)) {
-
+                if(payload[0]==MqttConstant.UPLOAD_FINGER_PRINT){
+                    System.out.println("UPLOAD FINGER PRINT ACTION");
+                    byte[] copy = Arrays.copyOfRange(payload, 1, payload.length);
+                    System.out.println(copy.length);
+                    if(copy.length==536){
+                        FingerPrintData data=new FingerPrintData(copy);
+                        fingerPrintDataRepository.save(data);
+                    }
                 }
             }
 
@@ -171,7 +190,9 @@ public class MQTTHandler {
 
         public boolean sendMessage(MqttMessage message) {
             try {
+                System.out.println("Send Message To:"+MqttUtil.getServerPublish(macAddress));
                 client.publish(MqttUtil.getServerPublish(macAddress), message);
+                System.out.println("Send Message Finished");
                 return true;
             } catch (MqttException me) {
                 System.out.println("reason " + me.getReasonCode());

+ 2 - 0
final-device/src/main/java/org/brynhild/graduation/device/service/IAdminService.groovy

@@ -34,4 +34,6 @@ interface IAdminService {
     Result findRelationship(QueryRelationshipRequest info)
 
     Result deleteRelationship(AdminDeleteRelationship info, String token)
+
+    Result enrollFingerPrint(EnrollFingerPrint info, String token)
 }

+ 31 - 0
final-device/src/main/java/org/brynhild/graduation/device/service/impl/AdminServiceImpl.groovy

@@ -7,6 +7,7 @@ import org.brynhild.graduation.common.constant.RedisConstant
 import org.brynhild.graduation.common.transfer.dto.Result
 import org.brynhild.graduation.common.utils.JwtUtil
 import org.brynhild.graduation.device.event.LogSender
+import org.brynhild.graduation.device.event.MQTTHandler
 import org.brynhild.graduation.device.factory.OperationLogFactory
 import org.brynhild.graduation.device.factory.SpecificationFactory
 import org.brynhild.graduation.device.service.IAdminService
@@ -14,6 +15,7 @@ import org.brynhild.graduation.persistence.device.entity.Area
 import org.brynhild.graduation.persistence.device.entity.Credential
 import org.brynhild.graduation.persistence.device.entity.Device
 import org.brynhild.graduation.persistence.device.entity.DeviceRelationship
+import org.brynhild.graduation.persistence.device.entity.FingerPrintData
 import org.brynhild.graduation.persistence.device.repository.*
 import org.brynhild.graduation.persistence.log.entity.OperationLog
 import org.brynhild.graduation.persistence.user.repository.UserRepository
@@ -30,6 +32,7 @@ class AdminServiceImpl implements IAdminService {
     private RedisTemplate<String, Object> redisTemplate
     private JwtUtil jwtUtil
     private LogSender logSender
+    private MQTTHandler mqttHandler
 
     private DeviceRepository deviceRepository
     private DeviceRelationshipRepository relationshipRepository
@@ -37,6 +40,7 @@ class AdminServiceImpl implements IAdminService {
     private CredentialRepository credentialRepository
     private SignInRecordRepository signInRecordRepository
     private UserRepository userRepository
+    private FingerPrintDataRepository fingerPrintDataRepository
 
     @Override
     Result bindDevice(BindDevice bindDevice, String token) {
@@ -66,6 +70,22 @@ class AdminServiceImpl implements IAdminService {
         return new Result(true, "绑定成功")
     }
 
+    @Override
+    Result enrollFingerPrint(EnrollFingerPrint info, String token) {
+        def data = fingerPrintDataRepository.findFingerPrintDataByUid(info.userId.toInteger())
+        if(data!=null){
+            return new Result(false,"该用户已录入指纹信息")
+        }
+
+        String message="enrollFingerPrint:"+info.userId
+        def sendResult = mqttHandler.sendMessageToDevice(info.mac, message)
+        if(sendResult){
+            return new Result(true,"已通知设备执行录入指纹操作")
+        }else{
+            return new Result(false,"出现异常,信息不可达")
+        }
+    }
+
     @Override
     Result findSignInRecord(QuerySignInRecordRequest request) {
         def specification = specificationFactory.getSignInRecordSpecification(request)
@@ -371,6 +391,12 @@ class AdminServiceImpl implements IAdminService {
         return new Result(true,"删除成功")
     }
 
+    @Autowired
+    @Resource
+    void setMqttHandler(MQTTHandler mqttHandler) {
+        this.mqttHandler = mqttHandler
+    }
+
     @Autowired
     @Resource
     void setDeviceRepository(DeviceRepository deviceRepository) {
@@ -436,4 +462,9 @@ class AdminServiceImpl implements IAdminService {
     void setRelationshipRepository(DeviceRelationshipRepository relationshipRepository) {
         this.relationshipRepository = relationshipRepository
     }
+    @Autowired
+    @Resource
+    void setFingerPrintDataRepository(FingerPrintDataRepository fingerPrintDataRepository) {
+        this.fingerPrintDataRepository = fingerPrintDataRepository
+    }
 }

+ 28 - 20
final-transfer/src/main/java/org/brynhild/graduation/transfer/device/vo/FingerPrintData.java → final-persistence/src/main/java/org/brynhild/graduation/persistence/device/entity/FingerPrintData.java

@@ -1,18 +1,31 @@
-package org.brynhild.graduation.transfer.device.vo;
+package org.brynhild.graduation.persistence.device.entity;
 
+import jakarta.persistence.Column;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Table;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.NoArgsConstructor;
 import org.brynhild.graduation.common.utils.ByteTransferUtil;
+import org.brynhild.graduation.persistence.common.constant.CommonDBStringDefinition;
+import org.brynhild.graduation.persistence.common.entity.LogicDeleteEntity;
+import org.hibernate.annotations.Where;
 
 import java.util.Arrays;
 
-public class FingerPrintData {
+@EqualsAndHashCode(callSuper = true)
+@Entity
+@Table(name = "finger_print_data")
+@Data
+@Where(clause = CommonDBStringDefinition.LOGIC_DELETE_WHERE_CLAUSE)
+@NoArgsConstructor
+public class FingerPrintData extends LogicDeleteEntity {
+
+    @Column(name = "mac")
     private String macAddress;
-    private Integer id;
+    private Integer uid;
     private byte[] data;
 
-    public FingerPrintData() {
-
-    }
-
     public FingerPrintData(byte[] buffer) {
         if (buffer == null || buffer.length != 536) {
             throw new IllegalArgumentException("buffer array is NULL or length mismatch!");
@@ -22,12 +35,7 @@ public class FingerPrintData {
         macAddress = new String(buffer, 0, 17);
         System.arraycopy(buffer, 20, idBuffer, 0, 4);
         System.arraycopy(buffer, 24, dataBuffer, 0, 4 * 128);
-        for (int i = 0; i < 2; i++) {
-            byte tmp = idBuffer[i];
-            idBuffer[i] = idBuffer[4 - 1 - i];
-            idBuffer[4 - 1 - i] = tmp;
-        }
-        id = ByteTransferUtil.byte2Int(idBuffer, true);
+        uid = ByteTransferUtil.byte2Int(idBuffer, true);
         data = dataBuffer;
     }
 
@@ -39,12 +47,12 @@ public class FingerPrintData {
         this.macAddress = macAddress;
     }
 
-    public Integer getId() {
-        return id;
+    public Integer getUid() {
+        return uid;
     }
 
-    public void setId(Integer id) {
-        this.id = id;
+    public void setUid(Integer uid) {
+        this.uid = uid;
     }
 
     public byte[] getData() {
@@ -55,10 +63,10 @@ public class FingerPrintData {
         this.data = data;
     }
 
-    public byte[] getBytes() {
+    public byte[] convertToBytes() {
         byte[] buffer = new byte[536];
         System.arraycopy(macAddress.getBytes(), 0, buffer, 0, 17);
-        System.arraycopy(ByteTransferUtil.int2Byte(id, true), 0, buffer, 20, 4);
+        System.arraycopy(ByteTransferUtil.int2Byte(uid, true), 0, buffer, 20, 4);
         System.arraycopy(data, 0, buffer, 24, 4 * 128);
         return buffer;
     }
@@ -67,7 +75,7 @@ public class FingerPrintData {
     public String toString() {
         return "FingerPrintData{" +
                 "macAddress='" + macAddress + '\'' +
-                ", id=" + id +
+                ", id=" + uid +
                 ", data=" + Arrays.toString(data) +
                 '}';
     }

+ 8 - 0
final-persistence/src/main/java/org/brynhild/graduation/persistence/device/repository/FingerPrintDataRepository.groovy

@@ -0,0 +1,8 @@
+package org.brynhild.graduation.persistence.device.repository
+
+import org.brynhild.graduation.persistence.common.repository.LogicDeleteRepository
+import org.brynhild.graduation.persistence.device.entity.FingerPrintData
+
+interface FingerPrintDataRepository extends LogicDeleteRepository<FingerPrintData>{
+    FingerPrintData findFingerPrintDataByUid(Integer uid)
+}

+ 14 - 0
final-transfer/src/main/java/org/brynhild/graduation/transfer/device/vo/EnrollFingerPrint.java

@@ -0,0 +1,14 @@
+package org.brynhild.graduation.transfer.device.vo;
+
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+
+@Data
+public class EnrollFingerPrint {
+    @NotNull
+    private Long userId;
+    @NotBlank
+    private String mac;
+}