|
|
@@ -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;
|
|
|
}
|
|
|
|