|
|
@@ -0,0 +1,589 @@
|
|
|
+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.domain.IotSfIrrigationRecord;
|
|
|
+import com.yunfeiyun.agmp.iot.common.enums.EnumIrrigationRecord;
|
|
|
+import com.yunfeiyun.agmp.iot.common.enums.EnumSfElementType;
|
|
|
+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.domain.IotSfIrrigationRecordListReqVo;
|
|
|
+import com.yunfeiyun.agmp.iots.service.*;
|
|
|
+import com.yunfeiyun.agmp.iots.service.impl.IotDeviceAddressService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+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.*;
|
|
|
+
|
|
|
+/** 润浩水肥机 */
|
|
|
+@Component(ServiceNameConst.SERVICE_RUNHAO_SF)
|
|
|
+@Slf4j
|
|
|
+public class RunHaoSfDeviceImpl extends DeviceAbstractImpl implements IRunHaoSfDevice {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MqttManager mqttManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IIotDeviceService iIotDeviceService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IIotDeviceconfigService iIotDeviceconfigService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IIotDevicelasteddataService iIotDevicelasteddataService;
|
|
|
+ @Autowired
|
|
|
+ private IIotRunHaoSfdataService iIotRunHaoSfdataService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IIotCmdlogService iIotCmdlogService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotDeviceAddressService iotDeviceAddressService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IIotSfElementfactorService iIotSfElementfactorService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IIotSfIrrigationRecordService iIotSfIrrigationRecordService;
|
|
|
+
|
|
|
+
|
|
|
+ private void publish(IotDevice iotDevice, String mqttMsgContent) {
|
|
|
+ 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{
|
|
|
+ // 延迟1秒 模拟设备上报延迟
|
|
|
+ Thread.sleep(1000);
|
|
|
+ String sendMsg = jsonObject.toString();
|
|
|
+ mqttManager.publishMsg(iotDevice.getDevconnBid(), topic, sendMsg);
|
|
|
+ log.info("【润浩水肥机】发送指令完毕!connectionId:{},topic :{} mqttMsgContent: {}",iotDevice.getDevconnBid(),topic, mqttMsgContent);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("【润浩水肥机】发送指令失败!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);
|
|
|
+// if(Objects.equals(iotDevice.getDevtypeBid(), IotDeviceDictConst.TYPE_YF_FXSSCD)){
|
|
|
+// //新款风吸式杀虫灯
|
|
|
+// topicList.add(IotMqttConstant.YFScdTopic.TOPIC_FXSSCD_CMD_PREFIX + devCode);
|
|
|
+// }
|
|
|
+// for(String topic:topicList){
|
|
|
+// try{
|
|
|
+// mqttManager.publishMsg(iotDevice.getDevconnBid(), topic, mqttMsgContent);
|
|
|
+// log.info("【YFSCD】发送指令完毕!connectionId:{},topic :{} mqttMsgContent: {}",iotDevice.getDevconnBid(),topic, mqttMsgContent);
|
|
|
+// }catch (Exception e){
|
|
|
+// log.error("【YFSCD】发送指令失败!connectionId:{},topic :{} mqttMsgContent: {}",iotDevice.getDevconnBid(),topic, mqttMsgContent);
|
|
|
+// }
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下发指令
|
|
|
+ *
|
|
|
+ * @param cmdModel
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Object sendCmd(CmdModel cmdModel) throws Exception {
|
|
|
+ log.info("【润浩水肥机】收到指令 任务 cmdModel={}", cmdModel);
|
|
|
+
|
|
|
+ // 获取执行的指令
|
|
|
+ CmdModel.Cmd cmdDistribution = cmdModel.getCmdDistribution();
|
|
|
+ // 获取执行的方法 ,方法可以通过反射获取执行,也可以临时case 匹配
|
|
|
+ String methodName = cmdModel.getCmdDistribution().getFunc();
|
|
|
+
|
|
|
+ String mqttMsgContent = "";
|
|
|
+ String clogSendresult = "发送指令成功";
|
|
|
+ JSONObject jobjParam = null;
|
|
|
+
|
|
|
+ switch (methodName) {
|
|
|
+ case CmdDef.RunHaoSfCmdDef.CMD_GROUP_CONFIG:
|
|
|
+ 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 = JSONUtils.toJSONString(payloadMap);
|
|
|
+ log.info("【润浩水肥机】发送指令【" + CmdDef.RunHaoSfCmdDef.CMD_GROUP_CONFIG + "】 mqttMsgContent={}", mqttMsgContent);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case CmdDef.RunHaoSfCmdDef.CMD_CONFIG:
|
|
|
+ jobjParam = cmdDistribution.getJsons();
|
|
|
+ mqttMsgContent = JSONUtils.toJSONString(jobjParam);
|
|
|
+ log.info("【润浩水肥机】发送指令【config】 mqttMsgContent={}", mqttMsgContent);
|
|
|
+ break;
|
|
|
+
|
|
|
+// case CmdDef.YfScdCmdDef.CMD_REFRESH:{
|
|
|
+// JSONObject jobjParam = cmdDistribution.getJsons();
|
|
|
+// mqttMsgContent = JSONUtils.toJSONString(jobjParam);
|
|
|
+// log.info("【杀虫灯】发送指令【refresh】 mqttMsgContent={}", mqttMsgContent);
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// case CmdDef.YfScdCmdDef.CMD_COMMON:{
|
|
|
+// JSONObject jobjParam = cmdDistribution.getJsons();
|
|
|
+// mqttMsgContent = JSONUtils.toJSONString(jobjParam);
|
|
|
+// 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);
|
|
|
+ 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)));
|
|
|
+ if(groupIndex == 0){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ 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();
|
|
|
+ if(valveEleList == null){
|
|
|
+ valveEleList = new ArrayList<>();
|
|
|
+ }
|
|
|
+ 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()){
|
|
|
+ if(ele.getChildrenList() != null && !ele.getChildrenList().isEmpty()){
|
|
|
+ deleteSfBidList.add(ele.getSfBid());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!deleteSfBidList.isEmpty()){
|
|
|
+ iIotSfElementfactorService.batchDeleteIotSfElementfactorBySfBidList(deleteSfBidList);
|
|
|
+ }
|
|
|
+ // 创建新的灌区配置
|
|
|
+ if(!createFactorList.isEmpty()){
|
|
|
+ iIotSfElementfactorService.batchInsertIotSfElementfactor(createFactorList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void syncIrrigationRecord(IotDevice iotDevice, JSONObject jsonObject){
|
|
|
+ String runMode = "Btn-zdsd";
|
|
|
+ String runStatus = "Btn-yjqd";
|
|
|
+
|
|
|
+ String zdsd = jsonObject.getString(runMode);
|
|
|
+ String yjqd = jsonObject.getString(runStatus);
|
|
|
+
|
|
|
+ // 如果进行中,则检测是否有未完成的灌溉记录,如果有,则不处理,如果没有,则创建新的灌溉记录
|
|
|
+ if("1".equals(yjqd)){
|
|
|
+ // 暂不处理
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ IotSfElementfactorListReqVo reqVo = new IotSfElementfactorListReqVo();
|
|
|
+ reqVo.setDevBid(iotDevice.getDevBid());
|
|
|
+ reqVo.setTid(iotDevice.getTid());
|
|
|
+ reqVo.setSfType(EnumSfElementType.SUCTION.getCode());
|
|
|
+ List<IotSfElementfactor> elementfactorList = iIotSfElementfactorService.selectIotSfElementfactorList(reqVo);
|
|
|
+ if(elementfactorList.isEmpty()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<IotSfElementfactor, Double> llMap = new HashMap<>();
|
|
|
+
|
|
|
+ for(IotSfElementfactor elementFactor: elementfactorList) {
|
|
|
+ String sfCode = elementFactor.getSfCode();
|
|
|
+ String key = sfCode.replace("Btn-fs", "Num-ls");
|
|
|
+ String v = jsonObject.getString(key);
|
|
|
+ Double ll = 0.0;
|
|
|
+ try{
|
|
|
+ ll = Double.parseDouble(v);
|
|
|
+ }catch (Exception e){}
|
|
|
+ llMap.put(elementFactor, ll);
|
|
|
+ }
|
|
|
+
|
|
|
+ IotSfIrrigationRecordListReqVo reqVo1 = new IotSfIrrigationRecordListReqVo();
|
|
|
+ reqVo1.setDevBid(iotDevice.getDevBid());
|
|
|
+ reqVo1.setTid(iotDevice.getTid());
|
|
|
+ reqVo1.setRcdMode(zdsd);
|
|
|
+ reqVo1.setRcdStatus(EnumIrrigationRecord.STATUS_RUNNING.getCode());
|
|
|
+
|
|
|
+ List<IotSfIrrigationRecord> recordList = iIotSfIrrigationRecordService.selectIrrigationRecordList(reqVo1);
|
|
|
+ if(recordList.isEmpty()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Set<String> sfdataBidSet = new HashSet<>();
|
|
|
+ for(IotSfIrrigationRecord record: recordList){
|
|
|
+ sfdataBidSet.add(record.getSfdataBid());
|
|
|
+ }
|
|
|
+ List<IotRunHaoSfdata> iotRunHaoSfdataList = iIotRunHaoSfdataService.selectDataList(new ArrayList<>(sfdataBidSet));
|
|
|
+ if(iotRunHaoSfdataList.isEmpty()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Map<String, IotRunHaoSfdata> iotRunHaoSfdataMap = new HashMap<>();
|
|
|
+ for(IotRunHaoSfdata iotRunHaoSfdata: iotRunHaoSfdataList){
|
|
|
+ iotRunHaoSfdataMap.put(iotRunHaoSfdata.getSfdataBid(), iotRunHaoSfdata);
|
|
|
+ }
|
|
|
+ String endDateStr = DateUtils.dateTimeNow();
|
|
|
+ Date endDate = DateUtils.parseDate(endDateStr);
|
|
|
+ for(IotSfIrrigationRecord record: recordList){
|
|
|
+ IotRunHaoSfdata iotRunHaoSfdata = iotRunHaoSfdataMap.get(record.getSfdataBid());
|
|
|
+ JSONObject sfdataContent = iotRunHaoSfdata.getSfdataContent();
|
|
|
+ double totalLL = 0.0;
|
|
|
+ for(Map.Entry<IotSfElementfactor, Double> entry: llMap.entrySet()) {
|
|
|
+ IotSfElementfactor elementFactor = entry.getKey();
|
|
|
+ String sfCode = elementFactor.getSfCode();
|
|
|
+ String key = sfCode.replace("Btn-fs", "Num-ls");
|
|
|
+
|
|
|
+ Double oldLL = entry.getValue();
|
|
|
+ double newLL = 0.0;
|
|
|
+ try{
|
|
|
+ newLL = Double.parseDouble(sfdataContent.getString(key));
|
|
|
+ }catch (Exception e){}
|
|
|
+
|
|
|
+ double diffLL = newLL - oldLL;
|
|
|
+ if(diffLL < 0){
|
|
|
+ diffLL = 0.0;
|
|
|
+ }
|
|
|
+ llMap.put(elementFactor, diffLL);
|
|
|
+ totalLL += diffLL;
|
|
|
+ }
|
|
|
+ StringBuilder rcdContent = new StringBuilder(record.getRcdContent());
|
|
|
+ rcdContent = new StringBuilder(rcdContent.toString().replace(EnumIrrigationRecord.STATUS_RUNNING.getName(), " 灌溉完成. "));
|
|
|
+ String rcdFertilizer = "";
|
|
|
+ for(Map.Entry<IotSfElementfactor, Double> entry: llMap.entrySet()) {
|
|
|
+ IotSfElementfactor elementFactor = entry.getKey();
|
|
|
+ String sfDisplayname = elementFactor.getSfDisplayname();
|
|
|
+ Double diffLL = entry.getValue();
|
|
|
+ String msg = sfDisplayname + "用量:" + diffLL + "L ";
|
|
|
+ rcdFertilizer += msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ Date startDate = DateUtils.parseDate(record.getRcdStartdate());
|
|
|
+ long diff = endDate.getTime() - startDate.getTime();
|
|
|
+ int diffMinutes = (int) (diff / (60 * 1000));
|
|
|
+ if(diffMinutes < 0){
|
|
|
+ diffMinutes = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ record.setRcdFertilizer(rcdFertilizer);
|
|
|
+ record.setRcdTime(diffMinutes);
|
|
|
+ record.setRcdContent(rcdContent.toString());
|
|
|
+ record.setRcdStatus(EnumIrrigationRecord.STATUS_FINISHED.getCode());
|
|
|
+ record.setRcdFlow(totalLL);
|
|
|
+ record.setRcdEnddate(endDateStr);
|
|
|
+ }
|
|
|
+ iIotSfIrrigationRecordService.batchUpdateIrrigationRecord(recordList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Object cmdData(JSONObject dataJson, String topic, String connectionId, String devUpdateddate) throws Exception {
|
|
|
+ log.info("润浩水肥 数据解析 {},topic:{}", dataJson.toString(),topic);
|
|
|
+
|
|
|
+ IotDevice oldIotDevice = findIotDevice(topic, dataJson, connectionId);
|
|
|
+ if (oldIotDevice == null) {
|
|
|
+ log.error("未取到 iotDevice");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ IotDevice iotDevice = new IotDevice();
|
|
|
+ iotDevice.setTid(oldIotDevice.getTid());
|
|
|
+ iotDevice.setDevtypeBid(oldIotDevice.getDevtypeBid());
|
|
|
+ iotDevice.setDevBid(oldIotDevice.getDevBid());
|
|
|
+ iotDevice.setDevUpdateddate(devUpdateddate);
|
|
|
+ iotDevice.setDevStatus("1");//设备上线
|
|
|
+
|
|
|
+ String[] keyArrays = {
|
|
|
+ "Btn-dsdl", // 施肥模式 1 定时模式 0 定量模式
|
|
|
+ "Btn-zfxz", // 注肥开关 1 开 0 关
|
|
|
+ "Btn-jbms", // 搅拌模式 1 联动模式 0 搅拌模式
|
|
|
+ "Num-jbsjA", // 搅拌时间 A搅拌机 单位 分钟
|
|
|
+ "Num-jbsjB",
|
|
|
+ "Num-jbsjC",
|
|
|
+ "Num-jbsjD",
|
|
|
+ "Btn-jbA", // 搅拌开关 A搅拌机 1 开 0 关
|
|
|
+ "Btn-jbB",
|
|
|
+ "Btn-jbC",
|
|
|
+ "Btn-jbD",
|
|
|
+ "Num-jbsyA", // 剩余搅拌时间 A搅拌机 单位 分钟
|
|
|
+ "Num-jbsyB",
|
|
|
+ "Num-jbsyC",
|
|
|
+ "Num-jbsyD",
|
|
|
+ "Num-lgcs", // 轮灌次数 单位 次 0 - 10
|
|
|
+ "Num-lgjg", // 轮灌间隔 单位 分钟 0 - 1000
|
|
|
+ "Btn-zdsd" // 运行模式 1 自动 0 手动
|
|
|
+ };
|
|
|
+//
|
|
|
+ JSONObject extConf = new JSONObject();
|
|
|
+ for (String k : keyArrays) {
|
|
|
+ String v = "0";
|
|
|
+ if (dataJson.containsKey(k)) {
|
|
|
+ v = dataJson.getString(k);
|
|
|
+ if("true".equals(v)){
|
|
|
+ v = "1";
|
|
|
+ }else if("false".equals(v)){
|
|
|
+ v = "0";
|
|
|
+ }
|
|
|
+ v = String.valueOf((int) Math.floor(Double.parseDouble(v)));
|
|
|
+ }
|
|
|
+ extConf.put(k, v);
|
|
|
+ }
|
|
|
+ String devConfig = JSONUtils.toJSONString(extConf);
|
|
|
+
|
|
|
+ // 更新设备基础信息数据库 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);
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ // 同步灌溉记录
|
|
|
+ syncIrrigationRecord(oldIotDevice, dataJson);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("润浩水肥机 同步灌溉记录失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void cmdOffline(JSONObject dataJson, String topic, String connectionId) throws MqttException {
|
|
|
+ log.debug("杀虫灯离线数据 {}", dataJson.toString());
|
|
|
+// IotDevice oldIotDevice = findIotDevice(topic, dataJson, connectionId);
|
|
|
+// if (oldIotDevice == null) {
|
|
|
+// log.error("未取到 iotDevice");
|
|
|
+// return;
|
|
|
+// }
|
|
|
+//
|
|
|
+// IotDevice newIotDevice = new IotDevice();
|
|
|
+// newIotDevice.setDevBid(oldIotDevice.getDevBid());
|
|
|
+// newIotDevice.setDevStatus("0");
|
|
|
+// newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
|
|
|
+// newIotDevice.setTid(oldIotDevice.getTid());
|
|
|
+// newIotDevice.setDevtypeBid(oldIotDevice.getDevtypeBid());
|
|
|
+// newIotDevice.setDevCreateddate(oldIotDevice.getDevCreateddate());
|
|
|
+// newIotDevice.setDevUpdateddate(oldIotDevice.getDevUpdateddate());
|
|
|
+// newIotDevice.setDevCode(oldIotDevice.getDevCode());
|
|
|
+// newIotDevice.setDevOriginalStatus(oldIotDevice.getDevOriginalStatus());
|
|
|
+// iIotDeviceService.updateIotDevice(newIotDevice);
|
|
|
+//
|
|
|
+//
|
|
|
+// //发送离线预警
|
|
|
+// SpringUtils.getBean(WarnService.class).processWarningOfflineData(newIotDevice, dataJson);
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 下发刷新指令,检测设备是否真离线
|
|
|
+// */
|
|
|
+// JSONObject payload = new JSONObject();
|
|
|
+// payload.put("cmd", "read");
|
|
|
+// payload.put("ext", "data");
|
|
|
+// String mqttMsgContent = JSONUtils.toJSONString(payload);
|
|
|
+// publish(oldIotDevice, mqttMsgContent);
|
|
|
+//
|
|
|
+// log.info("[杀虫灯] 下发刷新指令,检测设备是否真离线: " + oldIotDevice.getDevCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 接收上报数据
|
|
|
+ *
|
|
|
+ * @param topic
|
|
|
+ * @param dataJson
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Object receiveData(String topic, JSONObject dataJson,String connectionId) throws Exception {
|
|
|
+ log.info("润浩水肥机实现类 处理收到的 设备上报数据 " + dataJson.toString());
|
|
|
+ // 接收设备上报数据后的处理逻辑
|
|
|
+ String devUpdateddate = dataJson.getString("devUpdateddate");
|
|
|
+ if(StringUtils.isEmpty(devUpdateddate)){
|
|
|
+ devUpdateddate= DateUtils.dateTimeNow();
|
|
|
+ }
|
|
|
+ this.cmdData(dataJson, topic, connectionId, devUpdateddate);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isDeviceProps(JSONObject jobjMsg) {
|
|
|
+ return "data".equals(jobjMsg.getString("cmd"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据topic、设备发来的消息,查询对应设备实体
|
|
|
+ *
|
|
|
+ * @param topic
|
|
|
+ * @param jobjMsg
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public IotDevice findIotDevice(String topic, JSONObject jobjMsg,String connectionId) {
|
|
|
+ String devId = mqttManager.getDevIdByTopic(connectionId,topic);
|
|
|
+ return iIotDeviceService.selectIotDeviceByDevBid(devId);
|
|
|
+ }
|
|
|
+}
|