Bläddra i källkod

新增 吸虫塔设备对接

zhaiyifei 11 månader sedan
förälder
incheckning
243897f313

+ 0 - 2
src/main/java/com/yunfeiyun/agmp/iots/core/mqtt/DeviceTopicService.java

@@ -145,9 +145,7 @@ public class DeviceTopicService {
         String[] topicArray = {
                 IotMqttConstant.YFCbdTopic.TOPIC_CBD_REPORT_PREFIX,
                 IotMqttConstant.YFCbdTopic.TOPIC_CBD_OFFLINE_PREFIX,
-                IotMqttConstant.YFCbdTopic.TOPIC_CBD_PHOTO_PREFIX,
                 IotMqttConstant.YFCbdTopic.TOPIC_CBD_HISTORY_REPORT_PREFIX,
-                IotMqttConstant.YFCbdTopic.TOPIC_CBD_PHOTO_HISTORY_REPORT_PREFIX,
                 IotMqttConstant.YFXctTopic.TOPIC_XCT_PEST_IMG
         };
         return getTopics(deviceId, topicArray);

+ 0 - 1
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/CqCbdDeviceImpl.java

@@ -231,7 +231,6 @@ public class CqCbdDeviceImpl extends DeviceAbstractImpl implements ICbdDevice {
 
         HashMap<String, String> keyMaps = new HashMap<>();
         keyMaps.put("dver", "devVersion");
-        keyMaps.put("imei", "devCode");
         keyMaps.put("gps", "devPositiontype");
 
         for (Map.Entry<String, String> entry : keyMaps.entrySet()) {

+ 80 - 51
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/IYfXctDeviceImpl.java

@@ -13,6 +13,8 @@ import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
 import com.yunfeiyun.agmp.iot.common.constant.mqtt.IotMqttConstant;
 import com.yunfeiyun.agmp.iot.common.domain.IotBaseEntity;
 import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
+import com.yunfeiyun.agmp.iot.common.domain.IotXctdata;
+import com.yunfeiyun.agmp.iot.common.enums.IotDeviceStatusTypeEnum;
 import com.yunfeiyun.agmp.iot.common.model.cmd.CmdModel;
 import com.yunfeiyun.agmp.iot.common.service.MongoService;
 import com.yunfeiyun.agmp.iots.core.manager.MqttManager;
@@ -22,7 +24,10 @@ import com.yunfeiyun.agmp.iots.device.domain.cbd.YfCbdMsgUtil;
 import com.yunfeiyun.agmp.iots.device.domain.cbd.YfCbdReqMsg;
 import com.yunfeiyun.agmp.iots.device.domain.cbd.YfCbdTakephotoMsg;
 import com.yunfeiyun.agmp.iots.device.service.IYfXctDevice;
-import com.yunfeiyun.agmp.iots.service.*;
+import com.yunfeiyun.agmp.iots.service.IIotCmdlogService;
+import com.yunfeiyun.agmp.iots.service.IIotDeviceService;
+import com.yunfeiyun.agmp.iots.service.IIotDeviceconfigService;
+import com.yunfeiyun.agmp.iots.service.IIotDevicelasteddataService;
 import com.yunfeiyun.agmp.iots.service.impl.IotCbdImgService;
 import com.yunfeiyun.agmp.iots.service.impl.IotDeviceAddressService;
 import lombok.extern.slf4j.Slf4j;
@@ -34,7 +39,7 @@ import java.util.*;
 
 @Component(ServiceNameConst.SERVICE_YF_XCT)
 @Slf4j
-public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice {
+public class  IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice {
     @Autowired
     private MqttManager mqttManager;
 
@@ -62,8 +67,6 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
     @Autowired
     private IotDeviceAddressService iotDeviceAddressService;
 
-
-
     @Override
     public Object sendCmd(CmdModel cmdModel) throws Exception {
         log.info("【吸虫塔】发送指令 任务 cmdModel={}", cmdModel);
@@ -157,62 +160,88 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
      */
     public void cmdData(JSONObject ext, String devUpdateddate, String topic, String connectionId) throws Exception {
         log.info("【吸虫塔】数据解析 {}", ext.toString());
-        String devId = mqttManager.getDevIdByTopic(connectionId, topic);
-        IotDevice iotDeviceFromDb = iIotDeviceService.selectIotDeviceByDevBid(devId);
-        if (iotDeviceFromDb == null) {
-            log.info("【吸虫塔】iotDeviceFromDb 空 {} ", devId);
+        IotDevice oldIotDevice = findIotDevice(topic, ext, connectionId);
+        if (oldIotDevice == null) {
+            log.info("【吸虫塔】未取到设备:" + topic);
             return;
         }
 
-        String vtype = ext.getString("vtype");
         if (ext.containsKey("dat_f")) {
             ext.put("datt", ext.getString("dat_f"));
         }
 
-        iotDeviceFromDb.setDevUpdateddate(devUpdateddate);
-        iotDeviceFromDb.setDevStatus("1");
+        IotDevice newIotDevice = new IotDevice();
+        newIotDevice.setDevBid(oldIotDevice.getDevBid());
+        newIotDevice.setDevUpdateddate(devUpdateddate);
+        newIotDevice.setDevStatus(IotDeviceStatusTypeEnum.ONLINE.getCode());
 
         HashMap<String, String> keyMaps = new HashMap<>();
         keyMaps.put("dver", "devVersion");
-        keyMaps.put("imei", "devCode");
         keyMaps.put("gps", "devPositiontype");
 
         for (Map.Entry<String, String> entry : keyMaps.entrySet()) {
             String k = entry.getValue();
             String v = ext.getString(entry.getKey());
             if (StringUtils.isNotEmpty(v)) {
-                ReflectUtils.invokeSetter(iotDeviceFromDb, k, v);
+                ReflectUtils.invokeSetter(newIotDevice, k, v);
             }
         }
 
         String lat = ext.getString("lat");
         String lng = ext.getString("lng");
 
-        if (!Objects.equals(iotDeviceFromDb.getDevPositionstatus(), "0")) {
-            iotDeviceAddressService.setDeviceAddress(iotDeviceFromDb, lng, lat);
+        if (!Objects.equals(oldIotDevice.getDevPositionstatus(), "0")) {
+            iotDeviceAddressService.setDeviceAddress(newIotDevice, lng, lat);
         }
 
-        try {
-            // 单独处理是否识别是否开启识别:0禁用(不带识别)1识别 2 计数
-            String disable = ext.getString("disable");
-            log.info("【吸虫塔-解析识别状态】disable:{}", disable);
-        } catch (Exception e) {
-            log.error("【吸虫塔-解析识别状态 异常】disable:{}", e.getMessage());
+        // 上报数据中没有disable字段,暂时不处理
+//        try {
+//            // 单独处理是否识别是否开启识别:0禁用(不带识别)1识别 2 计数
+//            String disable = ext.getString("disable");
+//            log.info("【吸虫塔-解析识别状态】disable:{}", disable);
+//        } catch (Exception e) {
+//            log.error("【吸虫塔-解析识别状态 异常】disable:{}", e.getMessage());
+//        }
+
+        // 更新设备数据信息到数据库 mongodb
+        String[] keyArrays = {
+                "dat_f",
+                "shake_sec",
+                "shake",
+                "ds",
+                "dver",
+                "st",
+                "et"
+        };
+        JSONObject xctConfig = new JSONObject();
+        for (String k : keyArrays) {
+            String v = "0";
+            if (ext.containsKey(k)) {
+                v = ext.getString(k);
+            }
+            xctConfig.put(k, v);
         }
+        String devConfig = JSONUtils.toJSONString(xctConfig);
+
+        IotXctdata iotXctdata = new IotXctdata();
+        iotXctdata.setTid(oldIotDevice.getTid());
+        iotXctdata.setCdbdataBid(iotXctdata.getUUId());
+        iotXctdata.setDevBid(oldIotDevice.getDevBid());
+        iotXctdata.setCbddataCreatedDate(devUpdateddate);
+        iotXctdata.setCdbdataContent(ext);
+
+        mongoService.saveOne(iotXctdata);
+
         // 更新设备基础信息数据库 mysql
-        iIotDeviceService.updateIotDevice(iotDeviceFromDb);
+        iIotDeviceService.updateIotDevice(newIotDevice);
 
-        // 更新设备数据信息到数据库 mongodb
-        String cId = iotDeviceFromDb.getTid();
-        String devConfig = this.xctData(ext, cId, iotDeviceFromDb);
         // 创建或更新设备配置信息
         if (StringUtils.isNotEmpty(devConfig)) {
-            iIotDeviceconfigService.createOrUpdateDevConfig(iotDeviceFromDb, devConfig, iotDeviceFromDb.getDevUpdateddate());
+            iIotDeviceconfigService.createOrUpdateDevConfig(oldIotDevice, devConfig, devUpdateddate);
         }
 
         // 保存 设备最新数据 到redis
-        iIotDevicelasteddataService.createOrUpdateDeviceLastedData(
-                ext, iotDeviceFromDb, iotDeviceFromDb.getDevUpdateddate(), 60 * 60 * 24L);
+        iIotDevicelasteddataService.createOrUpdateDeviceLastedData(ext, oldIotDevice, devUpdateddate, 60 * 60 * 24L);
     }
 
     /**
@@ -221,15 +250,14 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
      * @param connectionId
      */
     public void cmdOffline(JSONObject ext, String topic, String connectionId) {
-        log.debug("测报灯离线数据 {}", ext.toString());
-        String devId = mqttManager.getDevIdByTopic(connectionId, topic);
-        IotDevice iotDevice = iIotDeviceService.selectIotDeviceByDevBid(devId);
+        log.debug("吸虫塔离线数据 {}", ext.toString());
+        IotDevice iotDevice = findIotDevice(topic, ext, connectionId);
         if (iotDevice == null) {
             return;
         }
         IotDevice newIotDevice = new IotDevice();
-        newIotDevice.setDevBid(devId);
-        newIotDevice.setDevStatus("0");
+        newIotDevice.setDevBid(iotDevice.getDevBid());
+        newIotDevice.setDevStatus(IotDeviceStatusTypeEnum.OFFLINE.getCode());
         newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
         iIotDeviceService.updateIotDevice(newIotDevice);
     }
@@ -245,7 +273,10 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
     public Object receiveData(String topic, JSONObject dataJson, String connectionId) throws Exception {
         log.info("【吸虫塔】收到的 设备上报数据 {}", dataJson.toString());
         // 接收设备上报数据后的处理逻辑
-        String devUpdateddate = DateUtils.dateTimeNow();
+        String devUpdateddate = dataJson.getString("devUpdateddate");
+        if (StringUtils.isEmpty(devUpdateddate)) {
+            devUpdateddate = DateUtils.dateTimeNow();
+        }
         if(topic.startsWith(IotMqttConstant.YFXctTopic.TOPIC_XCT_PEST_IMG)){
             Object result = this.receivePicData(dataJson, devUpdateddate, topic, connectionId);
             log.info("【吸虫塔】图片数据处理完成");
@@ -253,21 +284,20 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
         }
         if(topic.startsWith(IotMqttConstant.YFCbdTopic.TOPIC_CBD_REPORT_PREFIX)){
             String cmd = dataJson.getString("cmd");
+            JSONObject ext = dataJson.getJSONObject("ext");
             if (TextUtils.isEmpty(cmd)) {
                 log.error("未取到cmd");
                 return false;
             }
-            if("data".equals("cmd")){
-                JSONObject ext = dataJson.getJSONObject("ext");
-                if (ext == null) {
-                    log.error("未取到ext");
-                    return false;
-                }
-                if (cmd.equals("data")) {
-                    this.cmdData(ext, devUpdateddate, topic, connectionId);
-                } else if (cmd.equals("offline")) {
-                    this.cmdOffline(ext, topic, connectionId);
-                }
+
+            if (ext == null) {
+                log.error("未取到ext");
+                return false;
+            }
+            if ("data".equals(cmd)) {
+                this.cmdData(ext, devUpdateddate, topic, connectionId);
+            } else if ("offline".equals(cmd)) {
+                this.cmdOffline(ext, topic, connectionId);
             }
         }
         return true;
@@ -365,16 +395,15 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
     }
 
     /**
+     * 根据topic、设备发来的消息,查询对应设备实体
+     *
      * @param topic
      * @param jobjMsg
-     * @param connectionId
      * @return
      */
     @Override
-    public IotDevice findIotDevice(String topic, JSONObject jobjMsg, String connectionId) {
-        String devId = mqttManager.getDevIdByTopic(connectionId, topic);
-        IotDevice ret = iIotDeviceService.selectIotDeviceByDevBid(devId);
-        log.debug("查到了一个iotdevice {}", ret);
-        return ret;
+    public IotDevice findIotDevice(String topic, JSONObject jobjMsg,String connectionId) {
+        String devId = mqttManager.getDevIdByTopic(connectionId,topic);
+        return iIotDeviceService.selectIotDeviceByDevBid(devId);
     }
 }

+ 0 - 2
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/YfXycb2DeviceImpl.java

@@ -86,8 +86,6 @@ public class YfXycb2DeviceImpl extends DeviceAbstractImpl implements IYfXycb2Dev
         IotDevice newIotDevice = new IotDevice();
         newIotDevice.setDevBid(oldIotDevice.getDevBid());
         newIotDevice.setDevVersion("YF-XY-2.0");
-        newIotDevice.setDevPositionstatus("0");
-        newIotDevice.setDevPositiontype("0");
         newIotDevice.setDevUpdateddate(devUpdateddate);
         newIotDevice.setDevStatus(IotDeviceStatusTypeEnum.ONLINE.getCode());
 

+ 0 - 2
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/YfXycbIIIDeviceImpl.java

@@ -83,8 +83,6 @@ public class YfXycbIIIDeviceImpl extends DeviceAbstractImpl implements IYfXycbII
         IotDevice newIotDevice = new IotDevice();
         newIotDevice.setDevBid(oldIotDevice.getDevBid());
         newIotDevice.setDevVersion("YF-XY-III");
-        newIotDevice.setDevPositionstatus("0");
-        newIotDevice.setDevPositiontype("0");
         newIotDevice.setDevUpdateddate(devUpdateddate);
         newIotDevice.setDevStatus(IotDeviceStatusTypeEnum.ONLINE.getCode());
 

+ 0 - 14
src/main/java/com/yunfeiyun/agmp/iots/service/impl/IotBzyPestRecogService.java

@@ -150,19 +150,5 @@ public class IotBzyPestRecogService {
         pestRecogResult.setBzyimgRecognumTotal(bzyimgRecognumTotal);
         return pestRecogResult;
     }
-
-    public void savePestRecog(PestRecogResult pestRecogResult, IotDevice iotDevice, IotBzyrecog iotBzyrecog) {
-        for (IotPestrecog iotPestrecog : pestRecogResult.getIotPestrecogList()) {
-            mongoService.saveOne(iotPestrecog);
-
-            // 保存 测报灯图片识别和虫情信息关联信息
-            IotCbdpest iotCbdpest = new IotCbdpest();
-            iotCbdpest.setTid(iotDevice.getTid());
-            iotCbdpest.setCbdpestBid(iotCbdpest.getUUId());
-            iotCbdpest.setPestrecogBid(iotPestrecog.getPestrecogBid());
-            iotCbdpest.setCbdrecogBid(iotBzyrecog.getBzyrecogBid());
-            mongoService.saveOne(iotCbdpest);
-        }
-    }
 }
 

+ 0 - 30
src/main/java/com/yunfeiyun/agmp/iots/startup/MongoStartup.java

@@ -58,36 +58,6 @@ public class MongoStartup {
         return mongodbIndexEntity;
     }
 
-    public MongodbIndexEntity cbdpestCreateIndex() {
-        log.info("开始创建IotCbdpest索引");
-        List<String[]> indexNameList = new ArrayList<>();
-        indexNameList.add(new String[]{"cbdrecogBid"});
-        indexNameList.add(new String[]{"pestrecogBid"});
-
-        MongodbIndexEntity mongodbIndexEntity = new MongodbIndexEntity();
-        mongodbIndexEntity.setIotBaseEntity(IotCbdpest.class);
-        mongodbIndexEntity.setIndexNameList(indexNameList);
-
-        return mongodbIndexEntity;
-    }
-
-    public MongodbIndexEntity cbdrecogCreateIndex() {
-        log.info("开始创建IotCbdrecog索引");
-        List<String[]> indexNameList = new ArrayList<>();
-        indexNameList.add(new String[]{"cbdimgBid"});
-        indexNameList.add(new String[]{"cbdrecogBid"});
-        indexNameList.add(new String[]{"cbdrecogCreatedDate"});
-        indexNameList.add(new String[]{"cbdrecogMarktype"});
-        indexNameList.add(new String[]{"cbdrecogType"});
-        indexNameList.add(new String[]{"cbdimgBid", "cbdrecogType"});
-
-        MongodbIndexEntity mongodbIndexEntity = new MongodbIndexEntity();
-        mongodbIndexEntity.setIotBaseEntity(IotCbdrecog.class);
-        mongodbIndexEntity.setIndexNameList(indexNameList);
-
-        return mongodbIndexEntity;
-    }
-
     public MongodbIndexEntity pestrecogCreateIndex() {
         log.info("开始创建IotPestrecog索引");
         List<String[]> indexNameList = new ArrayList<>();