|
|
@@ -13,6 +13,8 @@ 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.IotBaseEntity;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotXctdata;
|
|
|
+import com.yunfeiyun.agmp.iot.common.enums.IotDeviceStatusTypeEnum;
|
|
|
import com.yunfeiyun.agmp.iot.common.model.cmd.CmdModel;
|
|
|
import com.yunfeiyun.agmp.iot.common.service.MongoService;
|
|
|
import com.yunfeiyun.agmp.iots.core.manager.MqttManager;
|
|
|
@@ -22,7 +24,10 @@ import com.yunfeiyun.agmp.iots.device.domain.cbd.YfCbdMsgUtil;
|
|
|
import com.yunfeiyun.agmp.iots.device.domain.cbd.YfCbdReqMsg;
|
|
|
import com.yunfeiyun.agmp.iots.device.domain.cbd.YfCbdTakephotoMsg;
|
|
|
import com.yunfeiyun.agmp.iots.device.service.IYfXctDevice;
|
|
|
-import com.yunfeiyun.agmp.iots.service.*;
|
|
|
+import com.yunfeiyun.agmp.iots.service.IIotCmdlogService;
|
|
|
+import com.yunfeiyun.agmp.iots.service.IIotDeviceService;
|
|
|
+import com.yunfeiyun.agmp.iots.service.IIotDeviceconfigService;
|
|
|
+import com.yunfeiyun.agmp.iots.service.IIotDevicelasteddataService;
|
|
|
import com.yunfeiyun.agmp.iots.service.impl.IotCbdImgService;
|
|
|
import com.yunfeiyun.agmp.iots.service.impl.IotDeviceAddressService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -34,7 +39,7 @@ import java.util.*;
|
|
|
|
|
|
@Component(ServiceNameConst.SERVICE_YF_XCT)
|
|
|
@Slf4j
|
|
|
-public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice {
|
|
|
+public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice {
|
|
|
@Autowired
|
|
|
private MqttManager mqttManager;
|
|
|
|
|
|
@@ -62,8 +67,6 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
|
|
|
@Autowired
|
|
|
private IotDeviceAddressService iotDeviceAddressService;
|
|
|
|
|
|
-
|
|
|
-
|
|
|
@Override
|
|
|
public Object sendCmd(CmdModel cmdModel) throws Exception {
|
|
|
log.info("【吸虫塔】发送指令 任务 cmdModel={}", cmdModel);
|
|
|
@@ -157,62 +160,88 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
|
|
|
*/
|
|
|
public void cmdData(JSONObject ext, String devUpdateddate, String topic, String connectionId) throws Exception {
|
|
|
log.info("【吸虫塔】数据解析 {}", ext.toString());
|
|
|
- String devId = mqttManager.getDevIdByTopic(connectionId, topic);
|
|
|
- IotDevice iotDeviceFromDb = iIotDeviceService.selectIotDeviceByDevBid(devId);
|
|
|
- if (iotDeviceFromDb == null) {
|
|
|
- log.info("【吸虫塔】iotDeviceFromDb 空 {} ", devId);
|
|
|
+ IotDevice oldIotDevice = findIotDevice(topic, ext, connectionId);
|
|
|
+ if (oldIotDevice == null) {
|
|
|
+ log.info("【吸虫塔】未取到设备:" + topic);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- String vtype = ext.getString("vtype");
|
|
|
if (ext.containsKey("dat_f")) {
|
|
|
ext.put("datt", ext.getString("dat_f"));
|
|
|
}
|
|
|
|
|
|
- iotDeviceFromDb.setDevUpdateddate(devUpdateddate);
|
|
|
- iotDeviceFromDb.setDevStatus("1");
|
|
|
+ IotDevice newIotDevice = new IotDevice();
|
|
|
+ newIotDevice.setDevBid(oldIotDevice.getDevBid());
|
|
|
+ newIotDevice.setDevUpdateddate(devUpdateddate);
|
|
|
+ newIotDevice.setDevStatus(IotDeviceStatusTypeEnum.ONLINE.getCode());
|
|
|
|
|
|
HashMap<String, String> keyMaps = new HashMap<>();
|
|
|
keyMaps.put("dver", "devVersion");
|
|
|
- keyMaps.put("imei", "devCode");
|
|
|
keyMaps.put("gps", "devPositiontype");
|
|
|
|
|
|
for (Map.Entry<String, String> entry : keyMaps.entrySet()) {
|
|
|
String k = entry.getValue();
|
|
|
String v = ext.getString(entry.getKey());
|
|
|
if (StringUtils.isNotEmpty(v)) {
|
|
|
- ReflectUtils.invokeSetter(iotDeviceFromDb, k, v);
|
|
|
+ ReflectUtils.invokeSetter(newIotDevice, k, v);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
String lat = ext.getString("lat");
|
|
|
String lng = ext.getString("lng");
|
|
|
|
|
|
- if (!Objects.equals(iotDeviceFromDb.getDevPositionstatus(), "0")) {
|
|
|
- iotDeviceAddressService.setDeviceAddress(iotDeviceFromDb, lng, lat);
|
|
|
+ if (!Objects.equals(oldIotDevice.getDevPositionstatus(), "0")) {
|
|
|
+ iotDeviceAddressService.setDeviceAddress(newIotDevice, lng, lat);
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- // 单独处理是否识别是否开启识别:0禁用(不带识别)1识别 2 计数
|
|
|
- String disable = ext.getString("disable");
|
|
|
- log.info("【吸虫塔-解析识别状态】disable:{}", disable);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("【吸虫塔-解析识别状态 异常】disable:{}", e.getMessage());
|
|
|
+ // 上报数据中没有disable字段,暂时不处理
|
|
|
+// try {
|
|
|
+// // 单独处理是否识别是否开启识别:0禁用(不带识别)1识别 2 计数
|
|
|
+// String disable = ext.getString("disable");
|
|
|
+// log.info("【吸虫塔-解析识别状态】disable:{}", disable);
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error("【吸虫塔-解析识别状态 异常】disable:{}", e.getMessage());
|
|
|
+// }
|
|
|
+
|
|
|
+ // 更新设备数据信息到数据库 mongodb
|
|
|
+ String[] keyArrays = {
|
|
|
+ "dat_f",
|
|
|
+ "shake_sec",
|
|
|
+ "shake",
|
|
|
+ "ds",
|
|
|
+ "dver",
|
|
|
+ "st",
|
|
|
+ "et"
|
|
|
+ };
|
|
|
+ JSONObject xctConfig = new JSONObject();
|
|
|
+ for (String k : keyArrays) {
|
|
|
+ String v = "0";
|
|
|
+ if (ext.containsKey(k)) {
|
|
|
+ v = ext.getString(k);
|
|
|
+ }
|
|
|
+ xctConfig.put(k, v);
|
|
|
}
|
|
|
+ String devConfig = JSONUtils.toJSONString(xctConfig);
|
|
|
+
|
|
|
+ IotXctdata iotXctdata = new IotXctdata();
|
|
|
+ iotXctdata.setTid(oldIotDevice.getTid());
|
|
|
+ iotXctdata.setCdbdataBid(iotXctdata.getUUId());
|
|
|
+ iotXctdata.setDevBid(oldIotDevice.getDevBid());
|
|
|
+ iotXctdata.setCbddataCreatedDate(devUpdateddate);
|
|
|
+ iotXctdata.setCdbdataContent(ext);
|
|
|
+
|
|
|
+ mongoService.saveOne(iotXctdata);
|
|
|
+
|
|
|
// 更新设备基础信息数据库 mysql
|
|
|
- iIotDeviceService.updateIotDevice(iotDeviceFromDb);
|
|
|
+ iIotDeviceService.updateIotDevice(newIotDevice);
|
|
|
|
|
|
- // 更新设备数据信息到数据库 mongodb
|
|
|
- String cId = iotDeviceFromDb.getTid();
|
|
|
- String devConfig = this.xctData(ext, cId, iotDeviceFromDb);
|
|
|
// 创建或更新设备配置信息
|
|
|
if (StringUtils.isNotEmpty(devConfig)) {
|
|
|
- iIotDeviceconfigService.createOrUpdateDevConfig(iotDeviceFromDb, devConfig, iotDeviceFromDb.getDevUpdateddate());
|
|
|
+ iIotDeviceconfigService.createOrUpdateDevConfig(oldIotDevice, devConfig, devUpdateddate);
|
|
|
}
|
|
|
|
|
|
// 保存 设备最新数据 到redis
|
|
|
- iIotDevicelasteddataService.createOrUpdateDeviceLastedData(
|
|
|
- ext, iotDeviceFromDb, iotDeviceFromDb.getDevUpdateddate(), 60 * 60 * 24L);
|
|
|
+ iIotDevicelasteddataService.createOrUpdateDeviceLastedData(ext, oldIotDevice, devUpdateddate, 60 * 60 * 24L);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -221,15 +250,14 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
|
|
|
* @param connectionId
|
|
|
*/
|
|
|
public void cmdOffline(JSONObject ext, String topic, String connectionId) {
|
|
|
- log.debug("测报灯离线数据 {}", ext.toString());
|
|
|
- String devId = mqttManager.getDevIdByTopic(connectionId, topic);
|
|
|
- IotDevice iotDevice = iIotDeviceService.selectIotDeviceByDevBid(devId);
|
|
|
+ log.debug("吸虫塔离线数据 {}", ext.toString());
|
|
|
+ IotDevice iotDevice = findIotDevice(topic, ext, connectionId);
|
|
|
if (iotDevice == null) {
|
|
|
return;
|
|
|
}
|
|
|
IotDevice newIotDevice = new IotDevice();
|
|
|
- newIotDevice.setDevBid(devId);
|
|
|
- newIotDevice.setDevStatus("0");
|
|
|
+ newIotDevice.setDevBid(iotDevice.getDevBid());
|
|
|
+ newIotDevice.setDevStatus(IotDeviceStatusTypeEnum.OFFLINE.getCode());
|
|
|
newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
|
|
|
iIotDeviceService.updateIotDevice(newIotDevice);
|
|
|
}
|
|
|
@@ -245,7 +273,10 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
|
|
|
public Object receiveData(String topic, JSONObject dataJson, String connectionId) throws Exception {
|
|
|
log.info("【吸虫塔】收到的 设备上报数据 {}", dataJson.toString());
|
|
|
// 接收设备上报数据后的处理逻辑
|
|
|
- String devUpdateddate = DateUtils.dateTimeNow();
|
|
|
+ String devUpdateddate = dataJson.getString("devUpdateddate");
|
|
|
+ if (StringUtils.isEmpty(devUpdateddate)) {
|
|
|
+ devUpdateddate = DateUtils.dateTimeNow();
|
|
|
+ }
|
|
|
if(topic.startsWith(IotMqttConstant.YFXctTopic.TOPIC_XCT_PEST_IMG)){
|
|
|
Object result = this.receivePicData(dataJson, devUpdateddate, topic, connectionId);
|
|
|
log.info("【吸虫塔】图片数据处理完成");
|
|
|
@@ -253,21 +284,20 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
|
|
|
}
|
|
|
if(topic.startsWith(IotMqttConstant.YFCbdTopic.TOPIC_CBD_REPORT_PREFIX)){
|
|
|
String cmd = dataJson.getString("cmd");
|
|
|
+ JSONObject ext = dataJson.getJSONObject("ext");
|
|
|
if (TextUtils.isEmpty(cmd)) {
|
|
|
log.error("未取到cmd");
|
|
|
return false;
|
|
|
}
|
|
|
- if("data".equals("cmd")){
|
|
|
- JSONObject ext = dataJson.getJSONObject("ext");
|
|
|
- if (ext == null) {
|
|
|
- log.error("未取到ext");
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (cmd.equals("data")) {
|
|
|
- this.cmdData(ext, devUpdateddate, topic, connectionId);
|
|
|
- } else if (cmd.equals("offline")) {
|
|
|
- this.cmdOffline(ext, topic, connectionId);
|
|
|
- }
|
|
|
+
|
|
|
+ if (ext == null) {
|
|
|
+ log.error("未取到ext");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if ("data".equals(cmd)) {
|
|
|
+ this.cmdData(ext, devUpdateddate, topic, connectionId);
|
|
|
+ } else if ("offline".equals(cmd)) {
|
|
|
+ this.cmdOffline(ext, topic, connectionId);
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
@@ -365,16 +395,15 @@ public class IYfXctDeviceImpl extends DeviceAbstractImpl implements IYfXctDevice
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 根据topic、设备发来的消息,查询对应设备实体
|
|
|
+ *
|
|
|
* @param topic
|
|
|
* @param jobjMsg
|
|
|
- * @param connectionId
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public IotDevice findIotDevice(String topic, JSONObject jobjMsg, String connectionId) {
|
|
|
- String devId = mqttManager.getDevIdByTopic(connectionId, topic);
|
|
|
- IotDevice ret = iIotDeviceService.selectIotDeviceByDevBid(devId);
|
|
|
- log.debug("查到了一个iotdevice {}", ret);
|
|
|
- return ret;
|
|
|
+ public IotDevice findIotDevice(String topic, JSONObject jobjMsg,String connectionId) {
|
|
|
+ String devId = mqttManager.getDevIdByTopic(connectionId,topic);
|
|
|
+ return iIotDeviceService.selectIotDeviceByDevBid(devId);
|
|
|
}
|
|
|
}
|