|
|
@@ -6,16 +6,15 @@ import com.yunfeiyun.agmp.common.utils.JSONUtils;
|
|
|
import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
import com.yunfeiyun.agmp.common.utils.reflect.ReflectUtils;
|
|
|
import com.yunfeiyun.agmp.iot.common.constant.cmd.CmdDef;
|
|
|
-import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictEnum;
|
|
|
+import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictConst;
|
|
|
+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.model.cmd.CmdModel;
|
|
|
-import com.yunfeiyun.agmp.iot.common.service.IotAddressService;
|
|
|
-import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
|
|
|
import com.yunfeiyun.agmp.iots.core.manager.MqttManager;
|
|
|
-import com.yunfeiyun.agmp.iots.core.mqtt.network.MqttPublisher;
|
|
|
import com.yunfeiyun.agmp.iots.device.common.DeviceAbstractImpl;
|
|
|
-import com.yunfeiyun.agmp.iots.device.service.*;
|
|
|
+import com.yunfeiyun.agmp.iots.device.service.IIotYfScddataService;
|
|
|
+import com.yunfeiyun.agmp.iots.device.service.IYfScdDevice;
|
|
|
import com.yunfeiyun.agmp.iots.service.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.util.TextUtils;
|
|
|
@@ -23,9 +22,7 @@ import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/** 云飞杀虫灯 */
|
|
|
@Component(ServiceNameConst.SERVICE_YF_SCD)
|
|
|
@@ -39,9 +36,6 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
|
|
|
private IIotDeviceService iIotDeviceService;
|
|
|
|
|
|
@Autowired
|
|
|
- private IotAddressService iotAddressService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
private IIotDeviceconfigService iIotDeviceconfigService;
|
|
|
|
|
|
@Autowired
|
|
|
@@ -56,7 +50,24 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
|
|
|
private IotDeviceAddressService iotDeviceAddressService;
|
|
|
|
|
|
|
|
|
- private static final String SERVICE_NAME = ServiceNameConst.SERVICE_YF_SCD;
|
|
|
+ private void publish(IotDevice iotDevice, String mqttMsgContent) {
|
|
|
+ String devCode = iotDevice.getDevCode();
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 下发指令
|
|
|
@@ -92,19 +103,12 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- IotDevice iotDevice = cmdModel.getIotDevice();
|
|
|
- String devCode = iotDevice.getDevCode();
|
|
|
- MqttPublisher mqttPublisher = mqttManager.getPublisherByService(SERVICE_NAME);
|
|
|
- mqttPublisher.publish(IotMqttConstant.YFScdTopic.TOPIC_SCD_CMD_PREFIX + devCode, mqttMsgContent);
|
|
|
- mqttPublisher.publish(IotMqttConstant.YFScdTopic.TOPIC_SCD_2_CMD_PREFIX + devCode, mqttMsgContent);
|
|
|
|
|
|
- if("3".equals(iotDevice.getDevTag())){
|
|
|
- //新款风吸式杀虫灯
|
|
|
- mqttPublisher.publish(IotMqttConstant.YFScdTopic.TOPIC_FXSSCD_CMD_PREFIX + devCode, mqttMsgContent);
|
|
|
+ if(StringUtils.isNotEmpty(mqttMsgContent)){
|
|
|
+ IotDevice iotDevice= iIotDeviceService.selectIotDeviceByDevBid(cmdModel.getIotDevice().getDevBid());
|
|
|
+ publish(iotDevice, mqttMsgContent);
|
|
|
}
|
|
|
|
|
|
- log.info("【杀虫灯】发送指令完毕!");
|
|
|
-
|
|
|
cmdModel.setClogSendresult(clogSendresult);
|
|
|
cmdModel.setClogDesc(mqttMsgContent);
|
|
|
|
|
|
@@ -112,50 +116,19 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public void cmdData(JSONObject ext, String topic, String devUpdateddate) throws Exception {
|
|
|
- log.info("杀虫灯数据解析 {},topic:{}", ext.toString(),topic);
|
|
|
+ public Object cmdData(JSONObject dataJson, String topic, String connectionId, String devUpdateddate) throws Exception {
|
|
|
+ log.info("杀虫灯数据解析 {},topic:{}", dataJson.toString(),topic);
|
|
|
|
|
|
- //区分 普通杀虫灯、风吸式杀虫灯:尝试查找,找到设备就继续处理
|
|
|
-
|
|
|
- //String devtypeBid = mqttManager.getDeviceTypeBizId(SERVICE_NAME);
|
|
|
- String firmBid = mqttManager.getFirmBizId(SERVICE_NAME);
|
|
|
-
|
|
|
-
|
|
|
- //尝试查找设备
|
|
|
-
|
|
|
- String devCode = topic.substring(topic.lastIndexOf("/") + 1);
|
|
|
- String devtypeBid = IotDeviceDictEnum.TYPE_YF_SCD.getCode();
|
|
|
-
|
|
|
- IotDevice iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
-
|
|
|
- if(iotDeviceOld==null){
|
|
|
- log.info("查不到设备(继续查找),devtypeid[{}],firmid[{}],devcode[{}],SERVICE_NAME[{}]",devtypeBid, firmBid, devCode,SERVICE_NAME);
|
|
|
- devtypeBid = IotDeviceDictEnum.TYPE_YF_FXSSCD.getCode();
|
|
|
- iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
- }
|
|
|
-
|
|
|
- if(iotDeviceOld==null){
|
|
|
- log.info("查不到设备(继续查找),devtypeid[{}],firmid[{}],devcode[{}],SERVICE_NAME[{}]",devtypeBid, firmBid, devCode,SERVICE_NAME);
|
|
|
- devtypeBid = IotDeviceDictEnum.TYPE_YF_JGFXSSCD.getCode();
|
|
|
- iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
- }
|
|
|
-
|
|
|
- // 设备不存在 就不再处理
|
|
|
- if (iotDeviceOld == null) {
|
|
|
- log.error("查不到设备,devtypeid[{}],firmid[{}],devcode[{}],SERVICE_NAME[{}]",devtypeBid, firmBid, devCode,SERVICE_NAME);
|
|
|
- return;
|
|
|
+ IotDevice oldIotDevice = findIotDevice(topic, dataJson, connectionId);
|
|
|
+ if (oldIotDevice == null) {
|
|
|
+ log.error("未取到 iotDevice");
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
IotDevice iotDevice = new IotDevice();
|
|
|
- iotDevice.setDevtypeBid(devtypeBid);
|
|
|
- iotDevice.setFirmBid(firmBid);
|
|
|
+ iotDevice.setDevBid(oldIotDevice.getDevBid());
|
|
|
iotDevice.setDevUpdateddate(devUpdateddate);
|
|
|
iotDevice.setDevStatus("1");//设备上线
|
|
|
- if(topic.startsWith(IotMqttConstant.YFScdTopic.TOPIC_SCD_2_REPORT_PREFIX)){
|
|
|
- iotDevice.setDevTag("2");
|
|
|
- }else if(topic.startsWith(IotMqttConstant.YFScdTopic.TOPIC_FXSSCD_REPORT_PREFIX)){//新风吸式
|
|
|
- iotDevice.setDevTag("3");
|
|
|
- }
|
|
|
|
|
|
HashMap<String, String> keyMaps = new HashMap<>();
|
|
|
keyMaps.put("dver", "devVersion");
|
|
|
@@ -164,16 +137,15 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
|
|
|
|
|
|
for (Map.Entry<String, String> entry : keyMaps.entrySet()) {
|
|
|
String k = entry.getValue();
|
|
|
- String v = ext.getString(entry.getKey());
|
|
|
+ String v = dataJson.getString(entry.getKey());
|
|
|
if (StringUtils.isNotEmpty(v)) {
|
|
|
ReflectUtils.invokeSetter(iotDevice, k, v);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- iotDevice.setDevBid(iotDeviceOld.getDevBid());
|
|
|
-
|
|
|
- if (!Objects.equals(iotDeviceOld.getDevPositionstatus(), "0")) {
|
|
|
- iotDeviceAddressService.setDeviceAddress(iotDevice, ext.getString("lng"), ext.getString("lat"));
|
|
|
+ String lng = dataJson.getString("lng");
|
|
|
+ String lat = dataJson.getString("lat");
|
|
|
+ if (!Objects.equals(oldIotDevice.getDevPositionstatus(), "0") && StringUtils.isNotEmpty(lng) && StringUtils.isNotEmpty(lat)) {
|
|
|
+ iotDeviceAddressService.setDeviceAddress(iotDevice, lng, lat);
|
|
|
}
|
|
|
|
|
|
String[] keyArrays = {
|
|
|
@@ -190,8 +162,8 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
|
|
|
JSONObject extConf = new JSONObject();
|
|
|
for (String k : keyArrays) {
|
|
|
String v = "0";
|
|
|
- if (ext.containsKey(k)) {
|
|
|
- v = ext.getString(k);
|
|
|
+ if (dataJson.containsKey(k)) {
|
|
|
+ v = dataJson.getString(k);
|
|
|
}
|
|
|
extConf.put(k, v);
|
|
|
}
|
|
|
@@ -202,61 +174,31 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
|
|
|
iIotDeviceService.updateIotDevice(iotDevice);
|
|
|
// 创建或更新设备配置信息
|
|
|
if (StringUtils.isNotEmpty(devConfig)) {
|
|
|
- iIotDeviceconfigService.createOrUpdateDevConfig(iotDeviceOld, devConfig, iotDevice.getDevUpdateddate());
|
|
|
+ iIotDeviceconfigService.createOrUpdateDevConfig(oldIotDevice, devConfig, iotDevice.getDevUpdateddate());
|
|
|
}
|
|
|
|
|
|
// 更新设备数据到mongodb
|
|
|
- iIotYfScddataService.insertScddata(iotDevice, ext);
|
|
|
- log.info("杀虫灯上报数据-》mongodb");
|
|
|
+ iIotYfScddataService.insertScddata(iotDevice, dataJson);
|
|
|
|
|
|
// 保存 设备最新数据 到redis
|
|
|
- iIotDevicelasteddataService.createOrUpdateDeviceLastedData(
|
|
|
- ext, iotDeviceOld, iotDevice.getDevUpdateddate(), 60 * 60 * 24L);
|
|
|
- log.info("杀虫灯上报数据-》redis");
|
|
|
-
|
|
|
+ iIotDevicelasteddataService.updateDeviceLastedData(oldIotDevice, String.valueOf(dataJson), devUpdateddate);
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- public void cmdOffline(JSONObject ext) throws MqttException {
|
|
|
- log.debug("杀虫灯离线数据 {}", ext.toString());
|
|
|
- //String devtypeBid = mqttManager.getDeviceTypeBizId(SERVICE_NAME);
|
|
|
- String firmBid = mqttManager.getFirmBizId(SERVICE_NAME);
|
|
|
- String devCode = ext.getString("imei");
|
|
|
-
|
|
|
- String devtypeBid = IotDeviceDictEnum.TYPE_YF_SCD.getCode();
|
|
|
-
|
|
|
- IotDevice iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
-
|
|
|
- if(iotDeviceOld==null){
|
|
|
- log.info("查不到设备(继续查找),devtypeid[{}],firmid[{}],devcode[{}],SERVICE_NAME[{}]",devtypeBid, firmBid, devCode,SERVICE_NAME);
|
|
|
- devtypeBid = IotDeviceDictEnum.TYPE_YF_FXSSCD.getCode();
|
|
|
- iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
- }
|
|
|
-
|
|
|
- if(iotDeviceOld==null){
|
|
|
- log.info("查不到设备(继续查找),devtypeid[{}],firmid[{}],devcode[{}],SERVICE_NAME[{}]",devtypeBid, firmBid, devCode,SERVICE_NAME);
|
|
|
- devtypeBid = IotDeviceDictEnum.TYPE_YF_JGFXSSCD.getCode();
|
|
|
- iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
- }
|
|
|
-
|
|
|
- // 设备不存在 就不再处理
|
|
|
- if (iotDeviceOld == null) {
|
|
|
- log.error("查不到设备,devtypeid[{}],firmid[{}],devcode[{}],SERVICE_NAME[{}]",devtypeBid, firmBid, devCode,SERVICE_NAME);
|
|
|
+ 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(iotDeviceOld.getDevBid());
|
|
|
+ newIotDevice.setDevBid(oldIotDevice.getDevBid());
|
|
|
newIotDevice.setDevStatus("0");
|
|
|
newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
|
|
|
iIotDeviceService.updateIotDevice(newIotDevice);
|
|
|
|
|
|
- String topic = IotMqttConstant.YFScdTopic.TOPIC_SCD_CMD_PREFIX;
|
|
|
- if(Objects.equals(iotDeviceOld.getDevTag(), "2")){
|
|
|
- topic = IotMqttConstant.YFScdTopic.TOPIC_SCD_2_CMD_PREFIX;
|
|
|
- }else if(Objects.equals(iotDeviceOld.getDevTag(), "3")){
|
|
|
- topic = IotMqttConstant.YFScdTopic.TOPIC_FXSSCD_CMD_PREFIX;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 下发刷新指令,检测设备是否真离线
|
|
|
*/
|
|
|
@@ -264,11 +206,9 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
|
|
|
payload.put("cmd", "read");
|
|
|
payload.put("ext", "data");
|
|
|
String mqttMsgContent = JSONUtils.toJSONString(payload);
|
|
|
+ publish(oldIotDevice, mqttMsgContent);
|
|
|
|
|
|
- MqttPublisher mqttPublisher = mqttManager.getPublisherByService(SERVICE_NAME);
|
|
|
- mqttPublisher.publish(topic + devCode, mqttMsgContent);
|
|
|
-
|
|
|
- log.info("[杀虫灯] 下发刷新指令,检测设备是否真离线: " + devCode);
|
|
|
+ log.info("[杀虫灯] 下发刷新指令,检测设备是否真离线: " + oldIotDevice.getDevCode());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -297,21 +237,17 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if (cmd.equals("data")) {
|
|
|
- this.cmdData(ext, topic, devUpdateddate);
|
|
|
- } else if (cmd.equals("offline")) {
|
|
|
- this.cmdOffline(ext);
|
|
|
+ if ("data".equals(cmd)) {
|
|
|
+ this.cmdData(ext, topic, connectionId, devUpdateddate);
|
|
|
+ } else if ("offline".equals(cmd)) {
|
|
|
+ this.cmdOffline(ext, topic, connectionId);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @param cmdJson
|
|
|
- * @return
|
|
|
- */
|
|
|
@Override
|
|
|
- public boolean isDeviceProps(JSONObject cmdJson) {
|
|
|
- return false;
|
|
|
+ public boolean isDeviceProps(JSONObject jobjMsg) {
|
|
|
+ return "data".equals(jobjMsg.getString("cmd"));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -323,33 +259,7 @@ public class YfScdDeviceImpl extends DeviceAbstractImpl implements IYfScdDevice
|
|
|
*/
|
|
|
@Override
|
|
|
public IotDevice findIotDevice(String topic, JSONObject jobjMsg,String connectionId) {
|
|
|
- String devCode = topic.substring(topic.lastIndexOf("/") + 1);
|
|
|
- //String devtypeBid = mqttManager.getDeviceTypeBizId(SERVICE_NAME);// 对于实现类复用的情况,目前这个写法会失效
|
|
|
- String firmBid = mqttManager.getFirmBizId(SERVICE_NAME);
|
|
|
-
|
|
|
-
|
|
|
- String devtypeBid = IotDeviceDictEnum.TYPE_YF_SCD.getCode();
|
|
|
-
|
|
|
- IotDevice iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
-
|
|
|
- if(iotDeviceOld==null){
|
|
|
- log.info("查不到设备(继续查找),devtypeid[{}],firmid[{}],devcode[{}],SERVICE_NAME[{}]",devtypeBid, firmBid, devCode,SERVICE_NAME);
|
|
|
- devtypeBid = IotDeviceDictEnum.TYPE_YF_FXSSCD.getCode();
|
|
|
- iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
- }
|
|
|
-
|
|
|
- if(iotDeviceOld==null){
|
|
|
- log.info("查不到设备(继续查找),devtypeid[{}],firmid[{}],devcode[{}],SERVICE_NAME[{}]",devtypeBid, firmBid, devCode,SERVICE_NAME);
|
|
|
- devtypeBid = IotDeviceDictEnum.TYPE_YF_JGFXSSCD.getCode();
|
|
|
- iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
- }
|
|
|
-
|
|
|
- // 设备不存在 就不再处理
|
|
|
- if (iotDeviceOld == null) {
|
|
|
- log.error("查不到设备,devtypeid[{}],firmid[{}],devcode[{}],SERVICE_NAME[{}]",devtypeBid, firmBid, devCode,SERVICE_NAME);
|
|
|
- return iotDeviceOld;
|
|
|
- }
|
|
|
-
|
|
|
- return iotDeviceOld;
|
|
|
+ String devId = mqttManager.getDevIdByTopic(connectionId,topic);
|
|
|
+ return iIotDeviceService.selectIotDeviceByDevBid(devId);
|
|
|
}
|
|
|
}
|