Browse Source

新增 同步灌区配置,以设备为准,解决平台与设备双边不一致的问题

zhaiyifei 8 tháng trước cách đây
mục cha
commit
59ad5e32d5

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

@@ -98,6 +98,8 @@ public class DeviceTopicService {
                 return getYfXycbIIIBatchSubTopic(deviceId);
             case ServiceNameConst.SERVICE_YF_XCT:
                 return getYfXctDeviceBatchSubTopic(deviceId);
+            case ServiceNameConst.SERVICE_RUNHAO_SF:
+                return getRunHaoSfBatchSubTopic(deviceId);
             default: {
                 throw new IotBizException(IotErrorCode.FAILURE.getCode(), serviceName + "不存在对应topic 解析");
             }
@@ -266,6 +268,19 @@ public class DeviceTopicService {
      * @param deviceId
      * @return
      */
+    private String[] getRunHaoSfBatchSubTopic(String[] deviceId) {
+        String[] topicArray = {
+                IotMqttConstant.RunHaoSfTopic.TOPIC_RUNHAO_SF_REPORT_PREFIX
+        };
+        return getTopics(deviceId, topicArray);
+    }
+
+    /**
+     * 海普发智能开关设备 topic
+     *
+     * @param deviceId
+     * @return
+     */
     private String[] getYrSfDeviceBatchSubTopic(String[] deviceId) {
         String[] topicArray = {
                 IotMqttConstant.YrSfTopic.TOPIC_YR_SF_REPORT_PREFIX,

+ 242 - 55
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/RunHaoSfDeviceImpl.java

@@ -1,23 +1,36 @@
 package com.yunfeiyun.agmp.iots.device.serviceImp;
 
+import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.yunfeiyun.agmp.common.utils.DateUtils;
 import com.yunfeiyun.agmp.common.utils.JSONUtils;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
+import com.yunfeiyun.agmp.iot.common.constant.cmd.CmdDef;
 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.IotDevice;
+import com.yunfeiyun.agmp.iot.common.domain.IotRunHaoSfdata;
+import com.yunfeiyun.agmp.iot.common.domain.IotSfElementfactor;
 import com.yunfeiyun.agmp.iot.common.model.cmd.CmdModel;
+import com.yunfeiyun.agmp.iot.common.util.dev.RunHaoSfElementUtil;
 import com.yunfeiyun.agmp.iots.core.manager.MqttManager;
 import com.yunfeiyun.agmp.iots.device.common.DeviceAbstractImpl;
 import com.yunfeiyun.agmp.iots.device.service.IRunHaoSfDevice;
+import com.yunfeiyun.agmp.iots.domain.IotSfElementfactorAlreadyListResVo;
+import com.yunfeiyun.agmp.iots.domain.IotSfElementfactorListReqVo;
 import com.yunfeiyun.agmp.iots.service.*;
 import com.yunfeiyun.agmp.iots.service.impl.IotDeviceAddressService;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.http.util.TextUtils;
 import org.eclipse.paho.client.mqttv3.MqttException;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 /** 润浩水肥机 */
 @Component(ServiceNameConst.SERVICE_RUNHAO_SF)
 @Slf4j
@@ -35,7 +48,7 @@ public class RunHaoSfDeviceImpl extends DeviceAbstractImpl implements IRunHaoSfD
     @Autowired
     private IIotDevicelasteddataService iIotDevicelasteddataService;
     @Autowired
-    private IIotYfScddataService iIotYfScddataService;
+    private IIotRunHaoSfdataService iIotRunHaoSfdataService;
 
     @Autowired
     private IIotCmdlogService iIotCmdlogService;
@@ -43,9 +56,33 @@ public class RunHaoSfDeviceImpl extends DeviceAbstractImpl implements IRunHaoSfD
     @Autowired
     private IotDeviceAddressService iotDeviceAddressService;
 
+    @Autowired
+    private IIotSfElementfactorService iIotSfElementfactorService;
+
 
     private void publish(IotDevice iotDevice, String mqttMsgContent) {
-//        String devCode = iotDevice.getDevCode();
+        String devCode = iotDevice.getDevCode();
+        String devBid = iotDevice.getDevBid();
+
+        IotRunHaoSfdata iotRunHaoSfdata = iIotRunHaoSfdataService.selectData(devBid);
+        if (iotRunHaoSfdata != null) {
+            JSONObject msgJson = JSONObject.parseObject(mqttMsgContent);
+            JSONObject jsonObject = iotRunHaoSfdata.getSfdataContent();
+            for(String key: msgJson.keySet()){
+                jsonObject.put(key, msgJson.get(key));
+            }
+            String topic = IotMqttConstant.RunHaoSfTopic.TOPIC_RUNHAO_SF_REPORT_PREFIX + devCode;
+            try{
+                String sendMsg = jsonObject.toString();
+                mqttManager.publishMsg(iotDevice.getDevconnBid(), topic, sendMsg);
+                log.info("【YFSCD】发送指令完毕!connectionId:{},topic :{} mqttMsgContent: {}",iotDevice.getDevconnBid(),topic, mqttMsgContent);
+            }catch (Exception e){
+                log.error("【YFSCD】发送指令失败!connectionId:{},topic :{} mqttMsgContent: {}",iotDevice.getDevconnBid(),topic, mqttMsgContent);
+            }
+        }
+        //设备暂未对接mqtt,先注释掉
+        // 先手动插入数据库新数据
+
 //        List<String> topicList = new ArrayList<>();
 //        topicList.add(IotMqttConstant.YFScdTopic.TOPIC_SCD_CMD_PREFIX + devCode);
 //        topicList.add(IotMqttConstant.YFScdTopic.TOPIC_SCD_2_CMD_PREFIX + devCode);
@@ -71,17 +108,36 @@ public class RunHaoSfDeviceImpl extends DeviceAbstractImpl implements IRunHaoSfD
      */
     @Override
     public Object sendCmd(CmdModel cmdModel) throws Exception {
-        log.info("【杀虫灯】收到指令 任务 cmdModel={}", cmdModel);
+        log.info("【润浩水肥机】收到指令 任务 cmdModel={}", cmdModel);
 
-//        // 获取执行的指令
-//        CmdModel.Cmd cmdDistribution = cmdModel.getCmdDistribution();
-//        // 获取执行的方法 ,方法可以通过反射获取执行,也可以临时case 匹配
-//        String methodName = cmdModel.getCmdDistribution().getFunc();
-//
-//        String mqttMsgContent = "";
-//        String clogSendresult = "发送指令成功";
-//        switch (methodName) {
-//            case CmdDef.YfScdCmdDef.CMD_CONFIG:{
+        // 获取执行的指令
+        CmdModel.Cmd cmdDistribution = cmdModel.getCmdDistribution();
+        // 获取执行的方法 ,方法可以通过反射获取执行,也可以临时case 匹配
+        String methodName = cmdModel.getCmdDistribution().getFunc();
+
+        String mqttMsgContent = "";
+        String clogSendresult = "发送指令成功";
+        switch (methodName) {
+            case CmdDef.RunHaoSfCmdDef.CMD_GROUP_CONFIG:
+                JSONObject jobjParam = cmdDistribution.getJsons();
+                String groupCode = jobjParam.getString("sfCode");
+                int groupIndex = Integer.parseInt(groupCode.replace("Btn-qx", ""));
+                String groupIndexStr = String.valueOf(groupIndex);
+                Map<String, String> payloadMap = new HashMap<>();
+                JSONArray valveArray = jobjParam.getJSONArray("childrenList");
+                for (int i = 0; i < valveArray.size(); i++) {
+                    JSONObject valveObj = valveArray.getJSONObject(i);
+                    String valveCode = valveObj.getString("sfCode");
+                    int valveIndex = Integer.parseInt(valveCode.replace("Btn-fa", ""));
+                    String key = "Btn-fx" + String.format("%02d", valveIndex);
+                    payloadMap.put(key, groupIndexStr);
+                }
+
+                mqttMsgContent = payloadMap.toString();
+                log.info("【润浩水肥机】发送指令【" + CmdDef.RunHaoSfCmdDef.CMD_GROUP_CONFIG + "】 mqttMsgContent={}", mqttMsgContent);
+                break;
+
+//            case CmdDef.YfScdCmdDef.CMD_CONFIG: {
 //                JSONObject jobjParam = cmdDistribution.getJsons();
 //                Map<String, String> keyMap = new HashMap<>();
 //                keyMap.put("ds", "ds");
@@ -92,7 +148,7 @@ public class RunHaoSfDeviceImpl extends DeviceAbstractImpl implements IRunHaoSfD
 //                keyMap.put("et", "et");
 //
 //                JSONObject configObj = new JSONObject();
-//                for(Map.Entry<String, String> entry : keyMap.entrySet()) {
+//                for (Map.Entry<String, String> entry : keyMap.entrySet()) {
 //                    String key = entry.getKey();
 //                    String keyName = entry.getValue();
 //
@@ -122,22 +178,161 @@ public class RunHaoSfDeviceImpl extends DeviceAbstractImpl implements IRunHaoSfD
 //                log.info("【杀虫灯】发送指令【report】 mqttMsgContent={}", mqttMsgContent);
 //                break;
 //            }
-//        }
-//
-//        if(StringUtils.isNotEmpty(mqttMsgContent)){
-//            IotDevice iotDevice= iIotDeviceService.selectIotDeviceByDevBid(cmdModel.getIotDevice().getDevBid());
-//            publish(iotDevice, mqttMsgContent);
-//        }
-//
-//        cmdModel.setClogSendresult(clogSendresult);
-//        cmdModel.setClogDesc(mqttMsgContent);
-//
-//        iIotCmdlogService.insertSuccessCmdlog(cmdModel);
+        }
+
+
+        if(StringUtils.isNotEmpty(mqttMsgContent)){
+            IotDevice iotDevice= iIotDeviceService.selectIotDeviceByDevBid(cmdModel.getIotDevice().getDevBid());
+            publish(iotDevice, mqttMsgContent);
+        }
+
+        cmdModel.setClogSendresult(clogSendresult);
+        cmdModel.setClogDesc(mqttMsgContent);
+
+        iIotCmdlogService.insertSuccessCmdlog(cmdModel);
         return null;
     }
 
+    private IotSfElementfactor getValveElementFactor(IotDevice iotDevice, String valveCode, IotSfElementfactor parentFactor) {
+        IotSfElementfactor valveFactor = RunHaoSfElementUtil.getValveElementFactor(valveCode);
+        if(valveFactor == null){
+            return null;
+        }
+        String sfCreatedDate = parentFactor.getSfCreatedDate();
+        String devBid = iotDevice.getDevBid();
+        String tid = iotDevice.getTid();
+
+        String valveSfBid = valveFactor.getUUId();
+        valveFactor.setSfBid(valveSfBid);
+        valveFactor.setTid(tid);
+        valveFactor.setDevBid(devBid);
+        valveFactor.setSfCreatedDate(sfCreatedDate);
+        valveFactor.setSfModifieddate(sfCreatedDate);
+        valveFactor.setSfParentBid(parentFactor.getSfBid());
+        valveFactor.setSfCreator(iotDevice.getDevCreator());
+        valveFactor.setSfModifier(iotDevice.getDevCreator());
+
+        return valveFactor;
+    }
+
+    private List<IotSfElementfactor> getCreateGroupFactorList(IotDevice iotDevice, String groupCode, List<String> valveList) {
+        List<IotSfElementfactor> createFactorList = new ArrayList<>();
+        IotSfElementfactor groupFactor = RunHaoSfElementUtil.getGroupElementFactor(groupCode);
+        if(groupFactor == null){
+            return createFactorList;
+        }
+
+        String sfCreatedDate = DateUtils.dateTimeNow();
+        String devBid = iotDevice.getDevBid();
+
+        String sfBid = groupFactor.getUUId();
+        groupFactor.setSfBid(sfBid);
+        groupFactor.setTid(iotDevice.getTid());
+        groupFactor.setDevBid(devBid);
+        groupFactor.setSfCreatedDate(sfCreatedDate);
+        groupFactor.setSfModifieddate(sfCreatedDate);
+        groupFactor.setSfCreator(iotDevice.getDevCreator());
+        groupFactor.setSfModifier(iotDevice.getDevCreator());
+
+        createFactorList.add(groupFactor);
+
+        for(String valveCode: valveList){
+            IotSfElementfactor valveFactor = getValveElementFactor(iotDevice, valveCode, groupFactor);
+            if(valveFactor == null){
+                continue;
+            }
+            createFactorList.add(valveFactor);
+        }
+        return createFactorList;
+    }
+
+    public void syncGroupValveConfig(IotDevice iotDevice, JSONObject jsonObject) {
+
+        Map<String, List<String>> groupMap = new HashMap<>();
+        for(String key:jsonObject.keySet()){
+            if(key.startsWith("Btn-fx")){
+                try{
+                    int groupIndex = (int)Math.floor(Double.parseDouble(jsonObject.getString(key)));
+                    String groupCode = String.format("Btn-qx%02d", groupIndex);
+                    int valveIndex = Integer.parseInt(key.replace("Btn-fx", ""));
+                    String valveCode = String.format("Btn-fa%d", valveIndex);
+                    if(!groupMap.containsKey(groupCode)){
+                        groupMap.put(groupCode, new ArrayList<>());
+                    }
+                    groupMap.get(groupCode).add(valveCode);
+                }catch (Exception e){
+                    continue;
+                }
+            }
+        }
+
+        String devBid = iotDevice.getDevBid();
+        IotSfElementfactorListReqVo reqVo = new IotSfElementfactorListReqVo();
+        reqVo.setDevBid(devBid);
+        reqVo.setTid(iotDevice.getTid());
+        List<IotSfElementfactorAlreadyListResVo> elementList = iIotSfElementfactorService.getGroupAlreadyElementList(reqVo);
+        Map<String, IotSfElementfactorAlreadyListResVo> eleMap = new HashMap<>();
+        for(IotSfElementfactorAlreadyListResVo ele: elementList){
+            eleMap.put(ele.getSfCode(), ele);
+        }
+
+        List<IotSfElementfactor> createFactorList = new ArrayList<>();
+        String sfCreatedDate = DateUtils.dateTimeNow();
+        List<String> deleteSfBidList = new ArrayList<>();
+        for(Map.Entry<String, List<String>> entry: groupMap.entrySet()){
+            String groupCode = entry.getKey();
+            List<String> valveList = entry.getValue();
+            // 先删除已存在的灌区配置,如果最后灌区仍有剩余,则表示设备已经删除,平台未删除,需要删除平台的 与设备同步
+            IotSfElementfactorAlreadyListResVo groupEle = eleMap.remove(groupCode);
+            // 如果不存在,则创建新的灌区配置
+            if(groupEle == null){
+                List<IotSfElementfactor> createGroupList = getCreateGroupFactorList(iotDevice, groupCode, valveList);
+                createFactorList.addAll(createGroupList);
+            }else{
+                // 如果存在,则比较灌区内的电磁阀配置,如果不同,则删除原有的电磁阀配置,创建新的电磁阀配置
+                List<IotSfElementfactorAlreadyListResVo> valveEleList = groupEle.getChildrenList();
+                Map<String, IotSfElementfactorAlreadyListResVo> valveEleMap = new HashMap<>();
+                for(IotSfElementfactorAlreadyListResVo valveEle: valveEleList){
+                    valveEleMap.put(valveEle.getSfCode(), valveEle);
+                }
+                for(String valveCode: valveList){
+                    IotSfElementfactorAlreadyListResVo valveEle = valveEleMap.remove(valveCode);
+                    // 如果存在,则不处理
+                    // 如果不存在,则创建当前灌区下的新电磁阀配置
+                    if(valveEle == null){
+                        IotSfElementfactor parentFactor = new IotSfElementfactor();
+                        BeanUtils.copyProperties(groupEle, parentFactor);
+                        parentFactor.setSfCreatedDate(sfCreatedDate);
+
+                        IotSfElementfactor valveFactor = getValveElementFactor(iotDevice, valveCode, parentFactor);
+                        if(valveFactor == null){
+                            continue;
+                        }
+                        createFactorList.add(valveFactor);
+                    }
+                }
+                // 剩余的元素为需要删除的元素
+                for(IotSfElementfactorAlreadyListResVo valveEle: valveEleMap.values()){
+                    deleteSfBidList.add(valveEle.getSfBid());
+                }
+            }
+        }
+
+        // 剩余的元素为需要删除的元素
+        for(IotSfElementfactorAlreadyListResVo ele: eleMap.values()){
+            deleteSfBidList.add(ele.getSfBid());
+        }
+        if(!deleteSfBidList.isEmpty()){
+            iIotSfElementfactorService.batchDeleteIotSfElementfactorBySfBidList(deleteSfBidList);
+        }
+        // 创建新的灌区配置
+        if(!createFactorList.isEmpty()){
+            iIotSfElementfactorService.batchInsertIotSfElementfactor(createFactorList);
+        }
+    }
+
     public Object cmdData(JSONObject dataJson, String topic, String connectionId, String devUpdateddate) throws Exception {
-        log.info("杀虫灯数据解析 {},topic:{}", dataJson.toString(),topic);
+        log.info("润浩水肥 数据解析 {},topic:{}", dataJson.toString(),topic);
 
         IotDevice oldIotDevice = findIotDevice(topic, dataJson, connectionId);
         if (oldIotDevice == null) {
@@ -189,18 +384,25 @@ public class RunHaoSfDeviceImpl extends DeviceAbstractImpl implements IRunHaoSfD
         }
         String devConfig = JSONUtils.toJSONString(extConf);
 
-//        // 更新设备基础信息数据库 mysql
-//        iIotDeviceService.updateIotDevice(iotDevice);
-//        // 创建或更新设备配置信息
-//        if (StringUtils.isNotEmpty(devConfig)) {
-//            iIotDeviceconfigService.createOrUpdateDevConfig(oldIotDevice, devConfig, iotDevice.getDevUpdateddate());
-//        }
-//
-//        // 更新设备数据到mongodb
-//        iIotYfScddataService.insertScddata(iotDevice, dataJson);
-//
-//        // 保存 设备最新数据 到redis
-//        iIotDevicelasteddataService.updateDeviceLastedData(oldIotDevice, String.valueOf(dataJson), devUpdateddate);
+        // 更新设备基础信息数据库 mysql
+        iIotDeviceService.updateIotDevice(iotDevice);
+        // 创建或更新设备配置信息
+        if (StringUtils.isNotEmpty(devConfig)) {
+            iIotDeviceconfigService.createOrUpdateDevConfig(oldIotDevice, devConfig, iotDevice.getDevUpdateddate());
+        }
+
+        // 更新设备数据到mongodb
+        iIotRunHaoSfdataService.insertData(iotDevice, dataJson);
+
+        // 保存 设备最新数据 到redis
+        iIotDevicelasteddataService.updateDeviceLastedData(oldIotDevice, String.valueOf(dataJson), devUpdateddate);
+
+        try{
+            // 同步灌区配置
+            syncGroupValveConfig(oldIotDevice, dataJson);
+        }catch (Exception e){
+            log.error("润浩水肥机 同步灌区配置失败", e);
+        }
         return null;
     }
 
@@ -249,28 +451,13 @@ public class RunHaoSfDeviceImpl extends DeviceAbstractImpl implements IRunHaoSfD
      */
     @Override
     public Object receiveData(String topic, JSONObject dataJson,String connectionId) throws Exception {
-        log.info("杀虫灯实现类  处理收到的 设备上报数据 " + dataJson.toString());
+        log.info("润浩水肥机实现类  处理收到的 设备上报数据 " + dataJson.toString());
         // 接收设备上报数据后的处理逻辑
-        String cmd = dataJson.getString("cmd");
-        if (TextUtils.isEmpty(cmd)) {
-            log.error("未取到cmd");
-            return false;
-        }
-        JSONObject ext = dataJson.getJSONObject("ext");
         String devUpdateddate = dataJson.getString("devUpdateddate");
         if(StringUtils.isEmpty(devUpdateddate)){
             devUpdateddate= DateUtils.dateTimeNow();
         }
-        if (ext == null) {
-            log.error("未取到ext");
-            return false;
-        }
-
-        if ("data".equals(cmd)) {
-            this.cmdData(ext, topic, connectionId, devUpdateddate);
-        } else if ("offline".equals(cmd)) {
-            this.cmdOffline(ext, topic, connectionId);
-        }
+        this.cmdData(dataJson, topic, connectionId, devUpdateddate);
         return null;
     }
 

+ 58 - 0
src/main/java/com/yunfeiyun/agmp/iots/domain/IotSfElementfactorAlreadyListResVo.java

@@ -0,0 +1,58 @@
+package com.yunfeiyun.agmp.iots.domain;
+
+import com.yunfeiyun.agmp.iot.common.domain.IotBaseEntity;
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * 水肥机要素
+ */
+@Data
+public class IotSfElementfactorAlreadyListResVo extends IotBaseEntity {
+    private static final long serialVersionUID = 1L;
+
+    /** 自增主键 */
+    private Long id;
+
+    /** 业务标识 */
+    private String sfBid;
+
+    /** 设备编号 */
+    private String devBid;
+
+    /** 水肥要素类型 */
+    private String sfType;
+
+    /** 原始名称 */
+    private String sfName;
+
+    /** 显示名称 */
+    private String sfDisplayname;
+
+    /** 要素编码 */
+    private String sfCode;
+
+    /** 父类id */
+    private String sfParentBid;
+
+    /** 排序字段 默认0 */
+    private int sfSequence;
+
+    /** 租户ID */
+    private String tid;
+
+    /** 创建时间 */
+    private String sfCreatedDate;
+
+    /** 创建人 */
+    private String sfCreator;
+
+    /** 修改时间 */
+    private String sfModifieddate;
+    /** 修改人 */
+    private String sfModifier;
+
+    private List<IotSfElementfactorAlreadyListResVo> childrenList;
+
+}

+ 57 - 0
src/main/java/com/yunfeiyun/agmp/iots/domain/IotSfElementfactorListReqVo.java

@@ -0,0 +1,57 @@
+package com.yunfeiyun.agmp.iots.domain;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * 水肥机要素
+ */
+@Data
+public class IotSfElementfactorListReqVo {
+    private static final long serialVersionUID = 1L;
+
+    /** 自增主键 */
+    private Long id;
+
+    /** 业务标识 */
+    private String sfBid;
+
+    /** 设备编号 */
+    private String devBid;
+
+    /** 水肥要素类型 */
+    private String sfType;
+
+    /** 原始名称 */
+    private String sfName;
+
+    /** 显示名称 */
+    private String sfDisplayname;
+
+    /** 要素编码 */
+    private String sfCode;
+
+    /** 父类id */
+    private String sfParentBid;
+
+    /** 排序字段 默认0 */
+    private int sfSequence;
+
+    /** 租户ID */
+    private String tid;
+
+    /** 创建时间 */
+    private String sfCreatedDate;
+
+    /** 创建人 */
+    private String sfCreator;
+
+    /** 修改时间 */
+    private String sfModifieddate;
+    /** 修改人 */
+    private String sfModifier;
+
+    private List<String> sfTypeList;
+
+}

+ 41 - 0
src/main/java/com/yunfeiyun/agmp/iots/mapper/IotSfElementfactorMapper.java

@@ -0,0 +1,41 @@
+package com.yunfeiyun.agmp.iots.mapper;
+
+import com.yunfeiyun.agmp.iot.common.domain.IotSfElementfactor;
+import com.yunfeiyun.agmp.iots.domain.IotSfElementfactorListReqVo;
+
+import java.util.List;
+
+/**
+ * Mapper接口
+ *
+ */
+public interface IotSfElementfactorMapper {
+
+    /**
+     * 查询水肥机要素列表
+     *
+     * @param iotSfElementfactor 水肥机要素
+     * @return 水肥机要素集合
+     */
+    public List<IotSfElementfactor> selectIotSfElementfactorList(IotSfElementfactorListReqVo reqVo);
+
+    /**
+     * 批量插入水肥机要素
+     *
+     * @param factorList 水肥机要素列表
+     * @return 结果
+     */
+    public int batchInsertIotSfElementfactor(List<IotSfElementfactor> factorList);
+
+
+    /**
+     * 删除水肥机要素
+     *
+     * @param sfBid 水肥机要素ID
+     * @return 结果
+     */
+    public int deleteIotSfElementfactorBySfBid(String sfBid);
+
+    public int batchDeleteIotSfElementfactorBySfBidList(List<String> sfBidList);
+
+}

+ 11 - 0
src/main/java/com/yunfeiyun/agmp/iots/service/IIotRunHaoSfdataService.java

@@ -0,0 +1,11 @@
+package com.yunfeiyun.agmp.iots.service;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
+import com.yunfeiyun.agmp.iot.common.domain.IotRunHaoSfdata;
+
+public interface IIotRunHaoSfdataService {
+    public void insertData(IotDevice iotDevice, JSONObject jsonObject);
+
+    public IotRunHaoSfdata selectData(String devBid);
+}

+ 111 - 0
src/main/java/com/yunfeiyun/agmp/iots/service/IIotSfElementfactorService.java

@@ -0,0 +1,111 @@
+package com.yunfeiyun.agmp.iots.service;
+
+import com.yunfeiyun.agmp.iot.common.domain.IotSfElementfactor;
+import com.yunfeiyun.agmp.iots.domain.IotSfElementfactorAlreadyListResVo;
+import com.yunfeiyun.agmp.iots.domain.IotSfElementfactorListReqVo;
+
+import java.util.List;
+
+/**
+ * 水肥机要素Service
+ */
+public interface IIotSfElementfactorService {
+    /**
+     * 查询水肥机要素列表
+     *
+     * @param iotSfElementfactor 水肥机要素
+     * @return 水肥机要素集合
+     */
+    public List<IotSfElementfactor> selectIotSfElementfactorList(IotSfElementfactorListReqVo reqVo);
+
+    /**
+     * 查询泵类已配置原始要素列表
+     * @param reqVo
+     * @return
+     */
+
+    public List<IotSfElementfactor> selectIotSfElementfactorListByPump(IotSfElementfactorListReqVo reqVo);
+
+    /**
+     * 查询灌区已配置原始要素列表
+     * @param reqVo
+     * @return
+     */
+
+    public List<IotSfElementfactor> selectIotSfElementfactorListByGroup(IotSfElementfactorListReqVo reqVo);
+
+    /**
+     * 查询电磁阀已配置原始要素列表
+     * @param reqVo
+     * @return
+     */
+
+    public List<IotSfElementfactor> selectIotSfElementfactorListByValve(IotSfElementfactorListReqVo reqVo);
+
+
+
+    /**
+     * 批量插入水肥机要素
+     *
+     * @param factorList 水肥机要素列表
+     * @return 结果
+     */
+    public int batchInsertIotSfElementfactor(List<IotSfElementfactor> factorList);
+
+    /**
+     * 删除水肥机要素
+     *
+     * @param sfBid 水肥机要素ID
+     * @return 结果
+     */
+    public int deleteIotSfElementfactorBySfBid(String sfBid);
+
+    /**
+     * 批量删除水肥机要素
+     *
+     * @param sfBid 水肥机要素ID
+     * @return 结果
+     */
+    public int batchDeleteIotSfElementfactorBySfBidList(List<String> sfBidList);
+
+//    /**
+//     * 查询已配置要素列表
+//     * @param reqVo
+//     * @return
+//     */
+//
+//    public List<IotSfElementfactorAlreadyListResVo> getAlreadyElementList(IotSfElementfactorListReqVo reqVo);
+
+    /**
+     * 查询已配置要素列表
+     * @param reqVo
+     * @return
+     */
+
+    public List<IotSfElementfactorAlreadyListResVo> getAlreadyElementList(List<IotSfElementfactor> factorList);
+
+    /**
+     * 查询泵类已配置要素列表
+     * @param reqVo
+     * @return
+     */
+
+    public List<IotSfElementfactorAlreadyListResVo> getPumpAlreadyElementList(IotSfElementfactorListReqVo reqVo);
+
+    /**
+     * 查询灌区已配置要素列表
+     * @param reqVo
+     * @return
+     */
+
+    public List<IotSfElementfactorAlreadyListResVo> getGroupAlreadyElementList(IotSfElementfactorListReqVo reqVo);
+
+    /**
+     * 查询电磁阀已配置要素列表
+     * @param reqVo
+     * @return
+     */
+
+    public List<IotSfElementfactorAlreadyListResVo> getValveAlreadyElementList(IotSfElementfactorListReqVo reqVo);
+}
+

+ 39 - 0
src/main/java/com/yunfeiyun/agmp/iots/service/impl/IotRunHaoSfdataServiceImpl.java

@@ -0,0 +1,39 @@
+package com.yunfeiyun.agmp.iots.service.impl;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
+import com.yunfeiyun.agmp.iot.common.domain.IotRunHaoSfdata;
+import com.yunfeiyun.agmp.iot.common.service.MongoService;
+import com.yunfeiyun.agmp.iots.service.IIotRunHaoSfdataService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Service
+@Slf4j
+public class IotRunHaoSfdataServiceImpl implements IIotRunHaoSfdataService {
+    @Autowired
+    private MongoService mongoService;
+
+    @Override
+    public void insertData(IotDevice iotDevice, JSONObject jsonObject) {
+        IotRunHaoSfdata iotRunHaoSfdata = new IotRunHaoSfdata();
+        iotRunHaoSfdata.setCId(iotDevice.getTid());
+        iotRunHaoSfdata.setSfdataBid(iotRunHaoSfdata.getUUId());
+        iotRunHaoSfdata.setDevBid(iotDevice.getDevBid());
+        iotRunHaoSfdata.setSfdataCreatedDate(iotDevice.getDevUpdateddate());
+        iotRunHaoSfdata.setSfdataContent(jsonObject);
+
+        mongoService.saveOne(iotRunHaoSfdata);
+    }
+
+    @Override
+    public IotRunHaoSfdata selectData(String devBid) {
+        Map<String, String> params = new HashMap<>();
+        params.put("devBid", devBid);
+        return (IotRunHaoSfdata) mongoService.findOne(IotRunHaoSfdata.class, params, "sfdataCreatedDate", "desc");
+    }
+}

+ 213 - 0
src/main/java/com/yunfeiyun/agmp/iots/service/impl/IotSfElementfactorServiceImpl.java

@@ -0,0 +1,213 @@
+package com.yunfeiyun.agmp.iots.service.impl;
+
+import com.yunfeiyun.agmp.iot.common.domain.IotSfElementfactor;
+import com.yunfeiyun.agmp.iot.common.enums.EnumSfElementType;
+import com.yunfeiyun.agmp.iots.domain.IotSfElementfactorAlreadyListResVo;
+import com.yunfeiyun.agmp.iots.domain.IotSfElementfactorListReqVo;
+import com.yunfeiyun.agmp.iots.mapper.IotSfElementfactorMapper;
+import com.yunfeiyun.agmp.iots.service.IIotSfElementfactorService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+
+@Slf4j
+@Service
+public class IotSfElementfactorServiceImpl implements IIotSfElementfactorService {
+    @Autowired
+    private IotSfElementfactorMapper iotSfElementfactorMapper;
+    /**
+     * 查询水肥机要素列表
+     *
+     * @param  水肥机要素
+     * @return 水肥机要素集合
+     */
+    @Override
+    public List<IotSfElementfactor> selectIotSfElementfactorList(IotSfElementfactorListReqVo reqVo) {
+        return iotSfElementfactorMapper.selectIotSfElementfactorList(reqVo);
+    }
+
+
+    /**
+     * 批量插入水肥机要素
+     *
+     * @param factorList 水肥机要素列表
+     * @return 结果
+     */
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public int batchInsertIotSfElementfactor(List<IotSfElementfactor> factorList) {
+        return iotSfElementfactorMapper.batchInsertIotSfElementfactor(factorList);
+    }
+
+    /**
+     * 删除水肥机要素
+     *
+     * @param sfBid 水肥机要素ID
+     * @return 结果
+     */
+    @Override
+    public int deleteIotSfElementfactorBySfBid(String sfBid) {
+        return iotSfElementfactorMapper.deleteIotSfElementfactorBySfBid(sfBid);
+    }
+
+    /**
+     * 批量删除水肥机要素
+     *
+     * @param sfBidList@return 结果
+     */
+
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public int batchDeleteIotSfElementfactorBySfBidList(List<String> sfBidList) {
+        return iotSfElementfactorMapper.batchDeleteIotSfElementfactorBySfBidList(sfBidList);
+    }
+
+//    /**
+//     * 查询已配置要素列表
+//     *
+//     * @param reqVo
+//     * @return
+//     */
+//    @Override
+//    public List<IotSfElementfactorAlreadyListResVo> getAlreadyElementList(IotSfElementfactorListReqVo reqVo) {
+//        List<IotSfElementfactor> elementfactorList = selectIotSfElementfactorList(reqVo);
+//        Map<String, IotSfElementfactorAlreadyListResVo> eleMap = new LinkedHashMap<>();
+//        for (IotSfElementfactor elementfactor : elementfactorList) {
+//            String sfBid = elementfactor.getSfBid();
+//            String sfParentBid = elementfactor.getSfParentBid();
+//
+//            IotSfElementfactorAlreadyListResVo eleResVo = new IotSfElementfactorAlreadyListResVo();
+//            BeanUtils.copyProperties(elementfactor, eleResVo);
+//
+//            IotSfElementfactorAlreadyListResVo parentInfo = eleMap.get(sfParentBid);
+//            if(parentInfo == null){
+//                eleResVo.setChildrenList(new ArrayList<>());
+//                eleMap.put(sfBid, eleResVo);
+//            }else{
+//                parentInfo.getChildrenList().add(eleResVo);
+//            }
+//        }
+//        return new ArrayList<>(eleMap.values());
+//    }
+
+    /**
+     * 查询已配置要素列表
+     *
+     * @param factorList@return
+     */
+    @Override
+    public List<IotSfElementfactorAlreadyListResVo> getAlreadyElementList(List<IotSfElementfactor> factorList) {
+        Map<String, IotSfElementfactorAlreadyListResVo> eleMap = new LinkedHashMap<>();
+        for (IotSfElementfactor elementfactor : factorList) {
+            String sfBid = elementfactor.getSfBid();
+            String sfParentBid = elementfactor.getSfParentBid();
+
+            IotSfElementfactorAlreadyListResVo eleResVo = new IotSfElementfactorAlreadyListResVo();
+            BeanUtils.copyProperties(elementfactor, eleResVo);
+
+            IotSfElementfactorAlreadyListResVo parentInfo = eleMap.get(sfParentBid);
+            if(parentInfo == null){
+                eleResVo.setChildrenList(new ArrayList<>());
+                eleMap.put(sfBid, eleResVo);
+            }else{
+                parentInfo.getChildrenList().add(eleResVo);
+            }
+        }
+        return new ArrayList<>(eleMap.values());
+    }
+
+    /**
+     * 查询泵类已配置原始要素列表
+     *
+     * @param reqVo
+     * @return
+     */
+    @Override
+    public List<IotSfElementfactor> selectIotSfElementfactorListByPump(IotSfElementfactorListReqVo reqVo) {
+        List<String> sfTypeList = new ArrayList<>();
+        sfTypeList.add(EnumSfElementType.WATER_SOURCE.getCode());
+        sfTypeList.add(EnumSfElementType.FERTILIZER.getCode());
+        sfTypeList.add(EnumSfElementType.SUCTION.getCode());
+        sfTypeList.add(EnumSfElementType.MIXING.getCode());
+        sfTypeList.add(EnumSfElementType.FERTILIZER_BUCKET.getCode());
+        reqVo.setSfTypeList(sfTypeList);
+        return selectIotSfElementfactorList(reqVo);
+    }
+
+    /**
+     * 查询灌区已配置原始要素列表
+     *
+     * @param reqVo
+     * @return
+     */
+    @Override
+    public List<IotSfElementfactor> selectIotSfElementfactorListByGroup(IotSfElementfactorListReqVo reqVo) {
+        List<String> sfTypeList = new ArrayList<>();
+        sfTypeList.add(EnumSfElementType.GROUP.getCode());
+        reqVo.setSfTypeList(sfTypeList);
+        return selectIotSfElementfactorList(reqVo);
+    }
+
+    /**
+     * 查询电磁阀已配置原始要素列表
+     *
+     * @param reqVo
+     * @return
+     */
+    @Override
+    public List<IotSfElementfactor> selectIotSfElementfactorListByValve(IotSfElementfactorListReqVo reqVo) {
+        List<String> sfTypeList = new ArrayList<>();
+        sfTypeList.add(EnumSfElementType.SOLENOID_VALVE.getCode());
+        reqVo.setSfTypeList(sfTypeList);
+        return selectIotSfElementfactorList(reqVo);
+    }
+
+    /**
+     * 查询泵类已配置要素列表
+     *
+     * @param reqVo
+     * @return
+     */
+    @Override
+    public List<IotSfElementfactorAlreadyListResVo> getPumpAlreadyElementList(IotSfElementfactorListReqVo reqVo) {
+        List<IotSfElementfactor> elementfactorList = selectIotSfElementfactorListByPump(reqVo);
+        return getAlreadyElementList(elementfactorList);
+    }
+
+    /**
+     * 查询灌区已配置要素列表
+     *
+     * @param reqVo
+     * @return
+     */
+    @Override
+    public List<IotSfElementfactorAlreadyListResVo> getGroupAlreadyElementList(IotSfElementfactorListReqVo reqVo) {
+        List<String> sfTypeList = new ArrayList<>();
+        sfTypeList.add(EnumSfElementType.GROUP.getCode());
+        sfTypeList.add(EnumSfElementType.SOLENOID_VALVE.getCode());
+        reqVo.setSfTypeList(sfTypeList);
+
+        List<IotSfElementfactor> elementfactorList = selectIotSfElementfactorList(reqVo);
+        return getAlreadyElementList(elementfactorList);
+    }
+
+    /**
+     * 查询电磁阀已配置要素列表
+     *
+     * @param reqVo
+     * @return
+     */
+    @Override
+    public List<IotSfElementfactorAlreadyListResVo> getValveAlreadyElementList(IotSfElementfactorListReqVo reqVo) {
+        List<IotSfElementfactor> elementfactorList = selectIotSfElementfactorListByValve(reqVo);
+        return getAlreadyElementList(elementfactorList);
+    }
+}

+ 55 - 0
src/main/resources/mapper/IotSfElementfactorMapper.xml

@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.yunfeiyun.agmp.iots.mapper.IotSfElementfactorMapper">
+
+    <select id="selectIotSfElementfactorList" parameterType="IotSfElementfactorListReqVo"
+            resultType="com.yunfeiyun.agmp.iot.common.domain.IotSfElementfactor" >
+        SELECT sf.id, sf.sfBid, sf.devBid, sf.sfType, sf.sfCode, sf.sfName, sf.sfDisplayname, sf.sfParentBid, sf.sfSequence,
+            sf.tid, sf.sfCreatedDate, sf.sfCreator, sf.sfModifieddate, sf.sfModifier
+        FROM IotSfElementfactor AS sf
+        <where>
+            sf.tid = #{tid}
+            <if test="sfBid!= null and sfBid!= ''"> and sf.sfBid = #{sfBid}</if>
+            <if test="sfType!= null and sfType!= ''"> and sf.sfType = #{sfType}</if>
+            <if test="devBid != null and devBid != ''"> and sf.devBid = #{devBid}</if>
+            <if test="sfCode!= null and sfCode!= ''"> and sf.sfCode = #{sfCode}</if>
+            <if test="sfName!= null and sfName!= ''"> and sf.sfName like concat('%', #{sfName}, '%')</if>
+            <if test="sfDisplayname!= null and sfDisplayname!= ''"> and sf.sfDisplayname like concat('%', #{sfDisplayname}, '%')</if>
+            <if test="sfParentBid!= null and sfParentBid!= ''"> and sf.sfParentBid = #{sfParentBid}</if>
+            <if test="sfSequence!= null and sfSequence!= ''"> and sf.sfSequence = #{sfSequence}</if>
+            <if test="sfCreatedDate!= null and sfCreatedDate!= ''"> and sf.sfCreatedDate = #{sfCreatedDate}</if>
+            <if test="sfCreator!= null and sfCreator!= ''"> and sf.sfCreator = #{sfCreator}</if>
+            <if test="sfModifieddate!= null and sfModifieddate!= ''"> and sf.sfModifieddate = #{sfModifieddate}</if>
+            <if test="sfModifier!= null and sfModifier!= ''"> and sf.sfModifier = #{sfModifier}</if>
+            <if test="sfTypeList != null and sfTypeList.size() > 0">
+                and sf.sfType in
+                <foreach collection="sfTypeList" item="item" index="index" open="(" separator="," close=")">
+                    #{item}
+                </foreach>
+            </if>
+        </where>
+    </select>
+
+    <insert id="batchInsertIotSfElementfactor" parameterType="IotSfElementfactor">
+        INSERT INTO IotSfElementfactor (id, sfBid, devBid, sfType, sfCode, sfName, sfDisplayname, sfParentBid, sfSequence,
+            tid, sfCreatedDate, sfCreator, sfModifieddate, sfModifier)
+        VALUES
+        <foreach collection="list" item="item" index="index" separator=",">
+            (#{item.id}, #{item.sfBid}, #{item.devBid}, #{item.sfType}, #{item.sfCode}, #{item.sfName}, #{item.sfDisplayname}, #{item.sfParentBid}, #{item.sfSequence},
+            #{item.tid}, #{item.sfCreatedDate}, #{item.sfCreator}, #{item.sfModifieddate}, #{item.sfModifier})
+        </foreach>
+    </insert>
+
+    <delete id="deleteIotSfElementfactorBySfBid" parameterType="string">
+        DELETE FROM IotSfElementfactor WHERE sfBid = #{sfBid} OR sfParentBid = #{sfBid}
+    </delete>
+
+    <delete id="batchDeleteIotSfElementfactorBySfBidList" parameterType="string">
+        <foreach collection="list" item="item" index="index" separator=";">
+            DELETE FROM IotSfElementfactor WHERE sfBid = #{item} OR sfParentBid = #{item}
+        </foreach>
+    </delete>
+
+</mapper>