Przeglądaj źródła

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

zhaiyifei 8 miesięcy temu
rodzic
commit
792be9cfed

+ 35 - 0
src/main/java/com/yunfeiyun/agmp/iotm/device/sf/controller/IotDeviceSfController.java

@@ -2,16 +2,21 @@ package com.yunfeiyun.agmp.iotm.device.sf.controller;
 
 import cn.hutool.core.exceptions.ValidateException;
 import com.alibaba.fastjson2.JSONObject;
+import com.yunfeiyun.agmp.common.constant.ErrorCode;
 import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
 import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
+import com.yunfeiyun.agmp.common.log.LogCore;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
 import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
 import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictConst;
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
 import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
+import com.yunfeiyun.agmp.iot.common.util.dev.DevOperateUtil;
 import com.yunfeiyun.agmp.iotm.common.controller.BaseController;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceBaseFunReqVo;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceDataListReqVo;
 import com.yunfeiyun.agmp.iotm.device.common.service.IotDeviceCommonService;
+import com.yunfeiyun.agmp.iotm.device.sf.domain.IotSfConfigCmdReqVo;
 import com.yunfeiyun.agmp.iotm.device.sf.domain.IotSfElementfactorAddReqVo;
 import com.yunfeiyun.agmp.iotm.device.sf.domain.IotSfElementfactorListReqVo;
 import com.yunfeiyun.agmp.iotm.util.ValidateUtil;
@@ -261,4 +266,34 @@ public class IotDeviceSfController extends BaseController {
         return AjaxResult.success(jsonObject);
     }
 
+    /**
+     * 下发杀虫灯配置指令
+     *
+     * @return
+     * @throws Exception
+     */
+    @PostMapping("/config/edit")
+    public AjaxResult sendConfigCmd(@RequestBody IotSfConfigCmdReqVo reqVo) {
+        log.info("【{}】【水肥机】【下发指令配置】客户id {}", LogCore.getSeq(), getCustomerId());
+        String devBid = reqVo.getDevBid();
+        ValidateUtil.validateDevBid(devBid);
+        JSONObject devConfig = reqVo.getData();
+        if(StringUtils.isEmpty(devBid) || devConfig == null){
+            throw new IotBizException(IotErrorCode.PARAM_INVALID.getCode(),"参数不能为空");
+        }
+        IotDevice iotDevice = iIotDeviceService.selectIotDeviceByDevBid(devBid);
+
+        DevOperateUtil.sendConfigCmdCheck(iotDevice);
+
+        reqVo.setIotDevice(iotDevice);
+
+        IotDeviceBaseFunReqVo iotDeviceBaseFunReqVo = new IotDeviceBaseFunReqVo();
+        iotDeviceBaseFunReqVo.setDevBid(reqVo.getDevBid());
+        iotDeviceBaseFunReqVo.setMethodName("sendConfigCmd");
+        iotDeviceBaseFunReqVo.setParam(reqVo);
+        String taskId = iotDeviceCommonService.func(iotDeviceBaseFunReqVo);
+
+        return new AjaxResult(ErrorCode.SUCCESS.getCode(), "下发成功,等待设备响应结果", taskId);
+    }
+
 }

+ 13 - 0
src/main/java/com/yunfeiyun/agmp/iotm/device/sf/domain/IotSfConfigCmdReqVo.java

@@ -0,0 +1,13 @@
+package com.yunfeiyun.agmp.iotm.device.sf.domain;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
+import lombok.Data;
+
+@Data
+public class IotSfConfigCmdReqVo {
+    private String devBid;
+    private String cmd;
+    private JSONObject data;
+    private IotDevice iotDevice;
+}

+ 41 - 64
src/main/java/com/yunfeiyun/agmp/iotm/device/sf/service/IIotSfCommService.java

@@ -4,35 +4,42 @@ import cn.hutool.core.exceptions.ValidateException;
 import com.alibaba.fastjson2.JSONObject;
 import com.yunfeiyun.agmp.common.constant.ErrorCode;
 import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
-import com.yunfeiyun.agmp.common.utils.DateUtils;
+import com.yunfeiyun.agmp.common.log.LogCore;
+import com.yunfeiyun.agmp.common.utils.JSONUtils;
 import com.yunfeiyun.agmp.common.utils.SecurityUtils;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
+import com.yunfeiyun.agmp.common.utils.uuid.IdUtils;
 import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
 import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictConst;
 import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
 import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconfig;
 import com.yunfeiyun.agmp.iot.common.domain.IotRunHaoSfdata;
 import com.yunfeiyun.agmp.iot.common.domain.IotSfElementfactor;
-import com.yunfeiyun.agmp.iot.common.enums.EnumSfElementType;
 import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
+import com.yunfeiyun.agmp.iot.common.model.cmd.CmdGroupModel;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceDataListReqVo;
 import com.yunfeiyun.agmp.iotm.device.common.service.IotDeviceBaseService;
 import com.yunfeiyun.agmp.iotm.device.common.service.impl.IotDeviceBaseServiceImpl;
+import com.yunfeiyun.agmp.iotm.device.sf.domain.IotSfConfigCmdReqVo;
 import com.yunfeiyun.agmp.iotm.device.sf.domain.IotSfElementfactorAddReqVo;
 import com.yunfeiyun.agmp.iotm.device.sf.domain.IotSfElementfactorAlreadyListResVo;
 import com.yunfeiyun.agmp.iotm.device.sf.domain.IotSfElementfactorListReqVo;
+import com.yunfeiyun.agmp.iotm.web.service.IIotCmdtaskService;
 import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceService;
 import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceconfigService;
 import com.yunfeiyun.agmp.iotm.web.service.IIotSfElementfactorService;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import javax.annotation.Resource;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * 水肥机通用服务
  */
+@Slf4j
 public class IIotSfCommService extends IotDeviceBaseServiceImpl implements IotDeviceBaseService {
 
     @Autowired
@@ -44,6 +51,9 @@ public class IIotSfCommService extends IotDeviceBaseServiceImpl implements IotDe
     @Resource
     private IIotDeviceconfigService iIotDeviceconfigService;
 
+    @Autowired
+    private IIotCmdtaskService iIotCmdtaskService;
+
 
     public Class getTableClass(String devtypeBid) {
         Class tableClass = null;
@@ -98,7 +108,7 @@ public class IIotSfCommService extends IotDeviceBaseServiceImpl implements IotDe
         return null;
     }
 
-    private IotSfElementfactor makeIotSfElementfactor(IotSfElementfactorAddReqVo reqVo, String date){
+    public IotSfElementfactor makeIotSfElementfactor(IotSfElementfactorAddReqVo reqVo, String date){
         String tid = SecurityUtils.getTid();
         String userId = SecurityUtils.getUserId();
 
@@ -121,65 +131,8 @@ public class IIotSfCommService extends IotDeviceBaseServiceImpl implements IotDe
      * @param reqVo 包含设备添加请求的参数,包括设备标识(devBid)等
      */
     public int elementAdd(IotSfElementfactorAddReqVo reqVo) {
-        String devBid = reqVo.getDevBid();
-        if (StringUtils.isEmpty(devBid)) {
-            throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "设备标识不能为空");
-        }
-
-        IotDevice findDevice = iotDeviceService.selectIotDeviceByDevBid(reqVo.getDevBid());
-        if (findDevice == null) {
-            throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "设备不存在");
-        }
-        String sfType = reqVo.getSfType();
-        String sfCode = reqVo.getSfCode();
-        // 肥料桶是虚拟的,不需要检测code重复问题
-        if(!Objects.equals(sfType, EnumSfElementType.FERTILIZER_BUCKET.getCode())){
-            if (StringUtils.isEmpty(sfCode)) {
-                throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "要素编码不能为空");
-            }
-        }
-        String sfDisplayname = reqVo.getSfDisplayname();
-        if (StringUtils.isEmpty(sfType)) {
-            throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "要素类型不能为空");
-        }
-
-        if (StringUtils.isEmpty(sfDisplayname)) {
-            throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "要素名称不能为空");
-        }
-
-        IotSfElementfactorListReqVo reqVo1 = new IotSfElementfactorListReqVo();
-        reqVo1.setDevBid(devBid);
-        List<IotSfElementfactor> elementList = iotSfElementfactorService.selectIotSfElementfactorList(reqVo1);
-        Set<String> sfCodeSet = new HashSet<>();
-        for (IotSfElementfactor element : elementList) {
-            if(StringUtils.isNotEmpty(element.getSfCode())){
-                sfCodeSet.add(element.getSfCode());
-            }
-        }
-
-        String createDate = DateUtils.dateTimeNow();
-        List<IotSfElementfactor> sfElementfactorList = new ArrayList<>();
-
-        IotSfElementfactor elementfactor = makeIotSfElementfactor(reqVo, createDate);
-        sfElementfactorList.add(elementfactor);
-
-        List<IotSfElementfactorAddReqVo> childrenList = reqVo.getChildrenList();
-        if (childrenList!= null &&!childrenList.isEmpty()){
-            for (IotSfElementfactorAddReqVo child : childrenList) {
-                child.setDevBid(devBid);
-                IotSfElementfactor childElementfactor = makeIotSfElementfactor(child, createDate);
-                childElementfactor.setSfParentBid(elementfactor.getSfBid());
-                sfElementfactorList.add(childElementfactor);
-            }
-        }
-
-        for(IotSfElementfactor element : sfElementfactorList){
-            if(sfCodeSet.contains(element.getSfCode())){
-                throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "要素编码重复");
-            }
-        }
-
-        return iotSfElementfactorService.batchInsertIotSfElementfactor(sfElementfactorList);
+        // 由具体子类实现
+        return 0;
     }
 
     /**
@@ -283,7 +236,7 @@ public class IIotSfCommService extends IotDeviceBaseServiceImpl implements IotDe
     /**
      * 获取水肥机状态信息
      *
-     * @param reqVo 请求对象,包含设备标识等信息
+     * @param devBid 请求对象,包含设备标识等信息
      * @return TableDataInfo 包含响应码、消息、数据和总数
      * @throws IotBizException 抛出业务异常,包含错误码和错误信息
      */
@@ -324,4 +277,28 @@ public class IIotSfCommService extends IotDeviceBaseServiceImpl implements IotDe
         return null;
     }
 
+    public String sendConfigCmd(IotSfConfigCmdReqVo reqVo) {
+        IotDevice iotDevice = reqVo.getIotDevice();
+        String devBid = reqVo.getDevBid();
+        JSONObject data = reqVo.getData();
+        IotDeviceconfig iotDeviceconfig = new IotDeviceconfig();
+        iotDeviceconfig.setDevBid(devBid);
+        iotDeviceconfig.setDevcfgContext(data.toJSONString());
+        iotDeviceconfig.setTid(iotDevice.getTid());
+
+        String func = reqVo.getCmd();
+        CmdGroupModel cmdGroupModel = iIotDeviceconfigService.createCmd(iotDeviceconfig, func);
+        log.info("【{}】【水肥机 [下发配置]】【构建 cmdGroupModel】{}", LogCore.getSeq(), JSONUtils.toJSONString(cmdGroupModel));
+        cmdGroupModel.setRequestId(IdUtils.fastUUID());
+
+        // 适配增加详细日志使用
+        cmdGroupModel.setDevCode(iotDevice.getDevCode());
+        cmdGroupModel.setCtBiztitle(iotDevice.getDevtypeName() + ":" + iotDevice.getDevCode());
+        cmdGroupModel.setCtBiztype("3");
+        cmdGroupModel.setCtDevtype(iotDevice.getDevtypeBid());
+        cmdGroupModel.setCtParam(JSONUtils.toJSONString(reqVo));
+        iIotCmdtaskService.handInternalCmd(cmdGroupModel);
+        return cmdGroupModel.getTaskUuid();
+    }
+
 }

+ 89 - 32
src/main/java/com/yunfeiyun/agmp/iotm/device/sf/service/impl/IotRunHaoSfServiceImpl.java

@@ -4,8 +4,10 @@ import cn.hutool.core.exceptions.ValidateException;
 import com.alibaba.fastjson2.JSONObject;
 import com.yunfeiyun.agmp.common.constant.ErrorCode;
 import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
+import com.yunfeiyun.agmp.common.utils.DateUtils;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
 import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
+import com.yunfeiyun.agmp.iot.common.constant.cmd.CmdDef;
 import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
 import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
 import com.yunfeiyun.agmp.iot.common.domain.IotRunHaoSfdata;
@@ -14,9 +16,7 @@ import com.yunfeiyun.agmp.iot.common.enums.EnumSfElementType;
 import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
 import com.yunfeiyun.agmp.iot.common.service.MongoService;
 import com.yunfeiyun.agmp.iot.common.util.dev.RunHaoSfElementUtil;
-import com.yunfeiyun.agmp.iotm.device.sf.domain.IotSfElementfactorAlreadyListResVo;
-import com.yunfeiyun.agmp.iotm.device.sf.domain.IotSfElementfactorInfoVo;
-import com.yunfeiyun.agmp.iotm.device.sf.domain.IotSfElementfactorListReqVo;
+import com.yunfeiyun.agmp.iotm.device.sf.domain.*;
 import com.yunfeiyun.agmp.iotm.device.sf.service.IIotSfCommService;
 import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceService;
 import com.yunfeiyun.agmp.iotm.web.service.IIotSfElementfactorService;
@@ -133,7 +133,6 @@ public class IotRunHaoSfServiceImpl extends IIotSfCommService {
             }catch (Exception e){}
         }
         return groupValveMap;
-
     }
 
     /**
@@ -179,9 +178,6 @@ public class IotRunHaoSfServiceImpl extends IIotSfCommService {
         List<IotSfElementfactorAlreadyListResVo> resultList = new ArrayList<>();
         if (iotRunHaoSfdata != null) {
             JSONObject dataJson = iotRunHaoSfdata.getSfdataContent();
-            //获取灌区和电磁阀的绑定关系
-            Map<String, List<String>> groupValveMap = getGroupValveMap(dataJson);
-
             for (String key : dataJson.keySet()) {
                 if(sfCodeSet.contains(key)){
                     continue;
@@ -192,24 +188,6 @@ public class IotRunHaoSfServiceImpl extends IIotSfCommService {
                 }
                 IotSfElementfactorAlreadyListResVo resVo = new IotSfElementfactorAlreadyListResVo();
                 BeanUtils.copyProperties(elementFactor, resVo);
-
-                List<String> valveList = groupValveMap.get(key);
-                // 灌区内配置过电磁阀
-                if(valveList != null && !valveList.isEmpty()){
-                    List<IotSfElementfactorAlreadyListResVo> childrenList = new ArrayList<>();
-                    for(String valve : valveList){
-                        IotSfElementfactor valveFactor = RunHaoSfElementUtil.getValveElementFactor(valve);
-                        if(valveFactor == null){
-                            continue;
-                        }
-                        IotSfElementfactorAlreadyListResVo valveVo = new IotSfElementfactorAlreadyListResVo();
-                        BeanUtils.copyProperties(valveFactor, valveVo);
-                        childrenList.add(valveVo);
-                    }
-                    if(!childrenList.isEmpty()){
-                        resVo.setChildrenList(childrenList);
-                    }
-                }
                 resultList.add(resVo);
             }
         }
@@ -260,15 +238,9 @@ public class IotRunHaoSfServiceImpl extends IIotSfCommService {
         List<IotSfElementfactor> resultList = new ArrayList<>();
         if (iotRunHaoSfdata != null) {
             JSONObject dataJson = iotRunHaoSfdata.getSfdataContent();
-            //获取灌区和电磁阀的绑定关系 获取已经配置的阀门
-            Map<String, List<String>> groupValveMap = getGroupValveMap(dataJson);
-            Set<String> valveSet = new HashSet<>();
-            for (List<String> valveList : groupValveMap.values()) {
-                valveSet.addAll(valveList);
-            }
 
             for (String key : dataJson.keySet()) {
-                if(sfCodeSet.contains(key) || valveSet.contains(key)){
+                if(sfCodeSet.contains(key)){
                     continue;
                 }
                 IotSfElementfactor elementFactor = RunHaoSfElementUtil.getValveElementFactor(key);
@@ -417,4 +389,89 @@ public class IotRunHaoSfServiceImpl extends IIotSfCommService {
         }
         return runStatus;
     }
+
+    /**
+     * 新增要素
+     * 该接口用于添加新的泵类要素,接收相关参数并调用通用设备功能服务进行处理。
+     *
+     * @param reqVo 包含设备添加请求的参数,包括设备标识(devBid)等
+     */
+    @Override
+    public int elementAdd(IotSfElementfactorAddReqVo reqVo) {
+        String devBid = reqVo.getDevBid();
+        if (StringUtils.isEmpty(devBid)) {
+            throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "设备标识不能为空");
+        }
+
+        IotDevice findDevice = iotDeviceService.selectIotDeviceByDevBid(reqVo.getDevBid());
+        if (findDevice == null) {
+            throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "设备不存在");
+        }
+        String sfType = reqVo.getSfType();
+        String sfCode = reqVo.getSfCode();
+        // 肥料桶是虚拟的,不需要检测code重复问题
+        if(!Objects.equals(sfType, EnumSfElementType.FERTILIZER_BUCKET.getCode())){
+            if (StringUtils.isEmpty(sfCode)) {
+                throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "要素编码不能为空");
+            }
+        }
+        String sfDisplayname = reqVo.getSfDisplayname();
+        if (StringUtils.isEmpty(sfType)) {
+            throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "要素类型不能为空");
+        }
+
+        if (StringUtils.isEmpty(sfDisplayname)) {
+            throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "要素名称不能为空");
+        }
+        List<IotSfElementfactorAddReqVo> childrenList = reqVo.getChildrenList();
+        if(Objects.equals(sfType, EnumSfElementType.GROUP.getCode())){
+            if(childrenList == null || childrenList.isEmpty()) {
+                throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "灌区未配置阀");
+            }
+        }
+
+        IotSfElementfactorListReqVo reqVo1 = new IotSfElementfactorListReqVo();
+        reqVo1.setDevBid(devBid);
+        List<IotSfElementfactor> elementList = iotSfElementfactorService.selectIotSfElementfactorList(reqVo1);
+        Set<String> sfCodeSet = new HashSet<>();
+        for (IotSfElementfactor element : elementList) {
+            if(StringUtils.isNotEmpty(element.getSfCode())){
+                sfCodeSet.add(element.getSfCode());
+            }
+        }
+
+        String createDate = DateUtils.dateTimeNow();
+        List<IotSfElementfactor> sfElementfactorList = new ArrayList<>();
+
+        IotSfElementfactor elementfactor = makeIotSfElementfactor(reqVo, createDate);
+        sfElementfactorList.add(elementfactor);
+
+
+        if (childrenList!= null &&!childrenList.isEmpty()){
+            for (IotSfElementfactorAddReqVo child : childrenList) {
+                child.setDevBid(devBid);
+                IotSfElementfactor childElementfactor = makeIotSfElementfactor(child, createDate);
+                childElementfactor.setSfParentBid(elementfactor.getSfBid());
+                sfElementfactorList.add(childElementfactor);
+            }
+        }
+
+        for(IotSfElementfactor element : sfElementfactorList){
+            if(sfCodeSet.contains(element.getSfCode())){
+                throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "要素编码重复");
+            }
+        }
+        int status = iotSfElementfactorService.batchInsertIotSfElementfactor(sfElementfactorList);
+
+        if(Objects.equals(sfType, EnumSfElementType.GROUP.getCode())){
+            IotSfConfigCmdReqVo configCmdReqVo = new IotSfConfigCmdReqVo();
+            configCmdReqVo.setDevBid(devBid);
+            configCmdReqVo.setCmd(CmdDef.RunHaoSfCmdDef.CMD_GROUP_CONFIG);
+            configCmdReqVo.setIotDevice(findDevice);
+            configCmdReqVo.setData(JSONObject.from(reqVo));
+
+            sendConfigCmd(configCmdReqVo);
+        }
+        return status;
+    }
 }

+ 2 - 0
src/main/java/com/yunfeiyun/agmp/iotm/web/service/IIotDeviceconfigService.java

@@ -71,6 +71,8 @@ public interface IIotDeviceconfigService {
      */
     public CmdGroupModel createConfigCmd(IotDeviceconfig iotDeviceconfig);
 
+    public CmdGroupModel createCmd(IotDeviceconfig iotDeviceconfig, String func);
+
     CmdGroupModel createTakePhotoCmd(IotDeviceconfig iotDeviceconfig);
 
     List<IotDeviceconfig> selectIotDeviceConfigByDevBidList(List<String> devBidList);