|
@@ -1,15 +1,20 @@
|
|
|
package org.jeecg.modules.mqtt.component;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttCallback;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
+import org.jeecg.modules.iotedgeCollectData.entity.IotedgeCollectData;
|
|
|
+import org.jeecg.modules.iotedgeCollectData.service.IIotedgeCollectDataService;
|
|
|
+import org.jeecg.modules.iotedgeCollectData.service.impl.IotedgeCollectDataServiceImpl;
|
|
|
import org.jeecg.modules.mqtt.config.MqttConfig;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -42,24 +47,47 @@ public class PushCallback implements MqttCallback {
|
|
|
|
|
|
@Override
|
|
|
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
|
|
|
-// try {
|
|
|
- // subscribe后得到的消息会执行到这里面
|
|
|
logger.info("接收消息主题 : " + topic);
|
|
|
logger.info("接收消息Qos : " + mqttMessage.getQos());
|
|
|
logger.info("接收消息内容 : " + new String(mqttMessage.getPayload()));
|
|
|
-// JSONObject jsonObject = deviceDataService.analysisMessage(new String(mqttMessage.getPayload()));
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
-// map.put("map", jsonObject);
|
|
|
-// com.example.emqtt.service.WebsocketServer.sendInfo(map);
|
|
|
-// System.out.println(map);
|
|
|
-// }catch (Exception ex){
|
|
|
-// System.out.println(ex.getMessage());
|
|
|
-// }
|
|
|
+ saveDataInfo(new String(mqttMessage.getPayload()));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
|
|
logger.info("deliveryComplete---------" + iMqttDeliveryToken.isComplete());
|
|
|
}
|
|
|
+
|
|
|
+ private void saveDataInfo(String info) {
|
|
|
+ ArrayList<IotedgeCollectData> list = new ArrayList<>();
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(info);
|
|
|
+ Map<String,Object> services = (Map<String, Object>) jsonObject.get("services");
|
|
|
+ for (String key:services.keySet()) {
|
|
|
+ Map<String,Object> tag = (Map<String,Object>) services.get(key);
|
|
|
+ Map<String,Object> tagmap = (Map<String, Object>) tag.get("properties");
|
|
|
+ for (String item:tagmap.keySet()) {
|
|
|
+ Map<String,Object> valuemap = (Map<String, Object>) tagmap.get(item);
|
|
|
+ String value1;
|
|
|
+ if (valuemap.get("value") instanceof Integer){
|
|
|
+ value1 = String.valueOf(valuemap.get("value"));
|
|
|
+ }else {
|
|
|
+ value1 = (String) valuemap.get("value");
|
|
|
+ }
|
|
|
+
|
|
|
+ IotedgeCollectData iotedgeCollectData = new IotedgeCollectData();
|
|
|
+ iotedgeCollectData.setGroupid(jsonObject.get("g_id").toString());
|
|
|
+ iotedgeCollectData.setPid(jsonObject.get("p_id").toString());
|
|
|
+ iotedgeCollectData.setDevice(jsonObject.get("d_id").toString());
|
|
|
+ iotedgeCollectData.setTime(jsonObject.get("ts").toString());
|
|
|
+ iotedgeCollectData.setService(key);
|
|
|
+ iotedgeCollectData.setProperty(item);
|
|
|
+ iotedgeCollectData.setValue(value1);
|
|
|
+
|
|
|
+ list.add(iotedgeCollectData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ IIotedgeCollectDataService dataService = new IotedgeCollectDataServiceImpl();
|
|
|
+ dataService.saveBatch(list);
|
|
|
+ }
|
|
|
}
|
|
|
|