|
@@ -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());
|