|
|
@@ -1,33 +1,31 @@
|
|
|
package com.yunfeiyun.agmp.iots.device.serviceImp;
|
|
|
|
|
|
-import com.alibaba.fastjson2.JSON;
|
|
|
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.common.utils.reflect.ReflectUtils;
|
|
|
-import com.yunfeiyun.agmp.iot.common.constant.cmd.CmdDef;
|
|
|
-import com.yunfeiyun.agmp.iot.common.constant.mqtt.IotMqttConstant;
|
|
|
+import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotBaseEntity;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotBzydata;
|
|
|
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.enums.IotDeviceStatusTypeEnum;
|
|
|
import com.yunfeiyun.agmp.iot.common.service.MongoService;
|
|
|
-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.domain.bzy.YfBzyReqMsg;
|
|
|
-import com.yunfeiyun.agmp.iots.device.domain.bzy.YfBzyTakephotoMsg;
|
|
|
-import com.yunfeiyun.agmp.iots.device.service.*;
|
|
|
-import com.yunfeiyun.agmp.iots.service.*;
|
|
|
+import com.yunfeiyun.agmp.iots.device.service.IBzyDevice;
|
|
|
+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.IotDeviceAddressService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.util.TextUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.*;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -38,103 +36,26 @@ import java.util.*;
|
|
|
@Component(ServiceNameConst.SERVICE_YF_BZY)
|
|
|
@Slf4j
|
|
|
public class BzyDeviceImpl extends DeviceAbstractImpl implements IBzyDevice {
|
|
|
-
|
|
|
- private static final String SERVICE_NAME = ServiceNameConst.SERVICE_YF_BZY;
|
|
|
-
|
|
|
- /**
|
|
|
- * 测试 BZY 编号
|
|
|
- */
|
|
|
- public final static String DEVICE_CODE_BZY_TEST = "861551058867599";
|
|
|
-
|
|
|
-
|
|
|
@Autowired
|
|
|
private MqttManager mqttManager;
|
|
|
|
|
|
@Autowired
|
|
|
- private IIotDeviceconfigService iIotDeviceconfigService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private IIotDevicelasteddataService iIotDevicelasteddataService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private MongoService<IotBaseEntity> mongoService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private IotAddressService iotAddressService;
|
|
|
+ private IIotDeviceService iIotDeviceService;
|
|
|
|
|
|
@Autowired
|
|
|
private IotBzyImgService iotBzyImgService;
|
|
|
|
|
|
@Autowired
|
|
|
- private IIotDeviceService iIotDeviceService;
|
|
|
+ private IotDeviceAddressService iotDeviceAddressService;
|
|
|
|
|
|
@Autowired
|
|
|
- private IIotCmdlogService iIotCmdlogService;
|
|
|
+ private MongoService<IotBaseEntity> mongoService;
|
|
|
|
|
|
@Autowired
|
|
|
- private IotDeviceAddressService iotDeviceAddressService;
|
|
|
-
|
|
|
- @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 = "发送指令成功";
|
|
|
- ;
|
|
|
-
|
|
|
- switch (methodName) {
|
|
|
- // 查询状态 ok
|
|
|
- case CmdDef.YfBzyCmdDef.CMD_REQ_DATA:
|
|
|
- mqttMsgContent = JSON.toJSON(new YfBzyReqMsg()).toString();
|
|
|
- log.info("【孢子仪】发送指令【reqData】 mqttMsgContent={}", mqttMsgContent);
|
|
|
- break;
|
|
|
- // 下发配置
|
|
|
- case CmdDef.YfBzyCmdDef.CMD_CONFIG:
|
|
|
- JSONObject jobjParam = cmdDistribution.getJsons();
|
|
|
- String culTime = jobjParam.getString("cul_time");
|
|
|
- jobjParam.put("cul_time", Integer.parseInt(culTime));
|
|
|
- //YfBzyConfigMsg yfBzyConfigMsg = new YfBzyConfigMsg();
|
|
|
- //YfBzyConfigMsg.Config config = new YfBzyConfigMsg.Config();
|
|
|
- JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.put("cmd", "paramconf");
|
|
|
- jsonObject.put("ext", jobjParam);
|
|
|
- //yfBzyConfigMsg.setExt(config);
|
|
|
- mqttMsgContent = jsonObject.toString();
|
|
|
- log.info("【孢子仪】发送指令【config】 mqttMsgContent={}", mqttMsgContent);
|
|
|
- break;
|
|
|
- // 拍照待定
|
|
|
- case CmdDef.YfBzyCmdDef.CMD_TAKEPHOTO:
|
|
|
- YfBzyTakephotoMsg yfBzyTakephotoMsg = new YfBzyTakephotoMsg();
|
|
|
- Map map = new HashMap();
|
|
|
- map.put("type", "takephoto");
|
|
|
- yfBzyTakephotoMsg.setExt(map);
|
|
|
- mqttMsgContent = JSON.toJSON(yfBzyTakephotoMsg).toString();
|
|
|
- log.info("【孢子仪】发送指令【takephoto】 mqttMsgContent={}", mqttMsgContent);
|
|
|
- break;
|
|
|
- case CmdDef.YfBzyCmdDef.CMD_REFRESH: {
|
|
|
- mqttMsgContent = JSONUtils.toJSONString(cmdDistribution.getJsons());
|
|
|
- log.info("【孢子仪】发送指令【refresh】 mqttMsgContent={}", mqttMsgContent);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- MqttPublisher mqttPublisher = mqttManager.getPublisherByDevConnBid(SERVICE_NAME);
|
|
|
- mqttPublisher.publish(IotMqttConstant.YFBzyTopic.TOPIC_BZY_CMD_PREFIX + cmdModel.getIotDevice().getDevCode(), mqttMsgContent);
|
|
|
- mqttPublisher.publish(IotMqttConstant.YFBzyTopic.TOPIC_BZY_CMD_PREFIX_2 + cmdModel.getIotDevice().getDevCode(), mqttMsgContent);
|
|
|
- log.info("【孢子仪】发送指令完毕!");
|
|
|
- cmdModel.setClogSendresult(clogSendresult);
|
|
|
- cmdModel.setClogDesc(mqttMsgContent);
|
|
|
- iIotCmdlogService.insertSuccessCmdlog(cmdModel);
|
|
|
-
|
|
|
- //返回值待定
|
|
|
- return true;
|
|
|
+ private IIotDeviceconfigService iIotDeviceconfigService;
|
|
|
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ private IIotDevicelasteddataService iIotDevicelasteddataService;
|
|
|
|
|
|
public String bzyData(JSONObject ext, String cId, IotDevice iotDevice) {
|
|
|
// 更新设备数据信息到数据库 mongodb
|
|
|
@@ -173,125 +94,120 @@ public class BzyDeviceImpl extends DeviceAbstractImpl implements IBzyDevice {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public void cmdData(JSONObject ext, String devUpdateddate, String topic, String connectionId) throws Exception {
|
|
|
+ log.info("【孢子仪】数据解析 {}", ext.toString());
|
|
|
+ String devBid = mqttManager.getDevIdByTopic(connectionId, topic);
|
|
|
+ // 设备不存在 就不在处理
|
|
|
+ IotDevice oldIotDevice = iIotDeviceService.selectIotDeviceByDevBid(devBid);
|
|
|
+ if (oldIotDevice == null) {
|
|
|
+ log.info("【孢子仪】oldIotDevice 空 {} ", devBid);
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- public void cmdData(JSONObject ext, String devUpdateddate) throws Exception {
|
|
|
- log.info("孢子仪数据解析 {}", ext.toString());
|
|
|
- String devtypeBid = mqttManager.getDeviceTypeBizId(SERVICE_NAME);
|
|
|
- String firmBid = mqttManager.getFirmBizId(SERVICE_NAME);
|
|
|
if (ext.containsKey("dat_f")) {
|
|
|
ext.put("datt", ext.getString("dat_f"));
|
|
|
}
|
|
|
|
|
|
- IotDevice iotDevice = new IotDevice();
|
|
|
- iotDevice.setDevtypeBid(devtypeBid);
|
|
|
- iotDevice.setFirmBid(firmBid);
|
|
|
- iotDevice.setDevUpdateddate(devUpdateddate);
|
|
|
- iotDevice.setDevStatus("1");
|
|
|
+ String devtypeBid = oldIotDevice.getDevtypeBid();
|
|
|
+ IotDevice newIotDevice = new IotDevice();
|
|
|
+ newIotDevice.setDevBid(oldIotDevice.getDevBid());
|
|
|
+ newIotDevice.setDevtypeBid(devtypeBid);
|
|
|
+ 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(iotDevice, k, v);
|
|
|
+ ReflectUtils.invokeSetter(newIotDevice, k, v);
|
|
|
}
|
|
|
}
|
|
|
- // 设备不存在 就不在处理
|
|
|
- IotDevice iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(iotDevice.getDevtypeBid(), iotDevice.getFirmBid(), iotDevice.getDevCode());
|
|
|
- if (iotDeviceOld == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- iotDevice.setDevBid(iotDeviceOld.getDevBid());
|
|
|
+
|
|
|
// 自动的可以更新数据库 0 手动
|
|
|
- if (!Objects.equals(iotDeviceOld.getDevPositionstatus(), "0")) {
|
|
|
- iotDeviceAddressService.setDeviceAddress(iotDevice, ext.getString("lng"), ext.getString("lat"));
|
|
|
+ if (!Objects.equals(oldIotDevice.getDevPositionstatus(), "0")) {
|
|
|
+ iotDeviceAddressService.setDeviceAddress(newIotDevice, ext.getString("lng"), ext.getString("lat"));
|
|
|
}
|
|
|
|
|
|
// 更新设备基础信息数据库 mysql
|
|
|
- iIotDeviceService.updateIotDevice(iotDevice);
|
|
|
+ iIotDeviceService.updateIotDevice(newIotDevice);
|
|
|
|
|
|
// 更新设备数据信息到数据库 mongodb
|
|
|
- String cId = iotDeviceOld.getTid();
|
|
|
- String devConfig = this.bzyData(ext, cId, iotDevice);
|
|
|
+ String cId = oldIotDevice.getTid();
|
|
|
+ String devConfig = this.bzyData(ext, cId, newIotDevice);
|
|
|
// 创建或更新设备配置信息
|
|
|
if (StringUtils.isNotEmpty(devConfig)) {
|
|
|
- iIotDeviceconfigService.createOrUpdateDevConfig(iotDeviceOld, devConfig, iotDevice.getDevUpdateddate());
|
|
|
+ iIotDeviceconfigService.createOrUpdateDevConfig(oldIotDevice, devConfig, devUpdateddate);
|
|
|
}
|
|
|
|
|
|
// 保存 设备最新数据 到redis
|
|
|
- iIotDevicelasteddataService.createOrUpdateDeviceLastedData(
|
|
|
- ext, iotDeviceOld, iotDevice.getDevUpdateddate(), 60 * 60 * 24L);
|
|
|
+ iIotDevicelasteddataService.createOrUpdateDeviceLastedData(ext, oldIotDevice, devUpdateddate, 60 * 60 * 24L);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public void cmdOffline(JSONObject ext) {
|
|
|
+ public void cmdOffline(JSONObject ext, String topic, String connectionId) {
|
|
|
log.info("孢子仪离线数据 {}", ext.toString());
|
|
|
- String devtypeBid = mqttManager.getDeviceTypeBizId(SERVICE_NAME);
|
|
|
- String firmBid = mqttManager.getFirmBizId(SERVICE_NAME);
|
|
|
- String devCode = ext.getString("imei");
|
|
|
+ String devBid = mqttManager.getDevIdByTopic(connectionId, topic);
|
|
|
// 设备不存在 就不在处理
|
|
|
- IotDevice iotDeviceOld = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
- if (iotDeviceOld == null) {
|
|
|
+ IotDevice oldIotDevice = iIotDeviceService.selectIotDeviceByDevBid(devBid);
|
|
|
+ if (oldIotDevice == null) {
|
|
|
+ log.info("【孢子仪】oldIotDevice 空 {} ", devBid);
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
IotDevice newIotDevice = new IotDevice();
|
|
|
- newIotDevice.setDevBid(iotDeviceOld.getDevBid());
|
|
|
+ newIotDevice.setDevBid(oldIotDevice.getDevBid());
|
|
|
newIotDevice.setDevStatus("0");
|
|
|
newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
|
|
|
iIotDeviceService.updateIotDevice(newIotDevice);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
- public Object receiveData(String topic, JSONObject dataJson,String connectionId) throws Exception {
|
|
|
- log.info("【孢子仪实现类】 【收到上报数据】 {}", dataJson.toString());
|
|
|
+ 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();
|
|
|
+ if (StringUtils.isEmpty(devUpdateddate)) {
|
|
|
+ devUpdateddate = DateUtils.dateTimeNow();
|
|
|
}
|
|
|
if (dataJson.containsKey("Image")) {
|
|
|
- Object result = this.receivePicData(dataJson, devUpdateddate);
|
|
|
- log.info("【孢子仪实现类】【收到上报数据】 Image:" + result);
|
|
|
+ Object result = this.receivePicData(dataJson, devUpdateddate, topic, connectionId);
|
|
|
+ log.info("孢子仪实现类 接收数据:" + result);
|
|
|
} else {
|
|
|
String cmd = dataJson.getString("cmd");
|
|
|
if (TextUtils.isEmpty(cmd)) {
|
|
|
log.error("未取到cmd");
|
|
|
return false;
|
|
|
}
|
|
|
- if ("read".equals(cmd)) {
|
|
|
- log.info("【【孢子仪实现类】【收到消息】 跳过 " + cmd + "类型");
|
|
|
- return true;
|
|
|
- }
|
|
|
JSONObject ext = dataJson.getJSONObject("ext");
|
|
|
if (ext == null) {
|
|
|
log.error("未取到ext");
|
|
|
return false;
|
|
|
}
|
|
|
- if (cmd.equals("data") || cmd.equals("status")) {
|
|
|
- this.cmdData(ext, devUpdateddate);
|
|
|
+
|
|
|
+ if (cmd.equals("data")) {
|
|
|
+ this.cmdData(ext, devUpdateddate, topic, connectionId);
|
|
|
} else if (cmd.equals("offline")) {
|
|
|
- this.cmdOffline(ext);
|
|
|
+ this.cmdOffline(ext, topic, connectionId);
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Object receivePicData(JSONObject jsonObject, String devUpdateddate) {
|
|
|
- String devtypeBid = mqttManager.getDeviceTypeBizId(SERVICE_NAME);
|
|
|
- String firmBid = mqttManager.getFirmBizId(SERVICE_NAME);
|
|
|
- String deviceTypeId = jsonObject.getString("device_type_id");
|
|
|
- String devCode = jsonObject.getString("imei");
|
|
|
- log.info("【解析孢子仪数据】:devtypeBid:{},firmBid:{},deviceTypeId,{},devCode:{}", devtypeBid, firmBid, deviceTypeId, devCode);
|
|
|
- // 设备不存在 就不在处理
|
|
|
- IotDevice iotDevice = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
+ public Object receivePicData(JSONObject jsonObject, String devUpdateddate, String topic, String connectionId) {
|
|
|
+ String devId = mqttManager.getDevIdByTopic(connectionId, topic);
|
|
|
+ IotDevice iotDevice = iIotDeviceService.selectIotDeviceByDevBid(devId);
|
|
|
if (iotDevice == null) {
|
|
|
return "设备不存在 就不在处理";
|
|
|
}
|
|
|
+ String devtypeBid = iotDevice.getDevtypeBid();
|
|
|
+ String deviceTypeId = jsonObject.getString("device_type_id");
|
|
|
+ String devCode = jsonObject.getString("imei");
|
|
|
+ log.info("【解析孢子仪图片数据】:devtypeBid:{},deviceTypeId,{},devCode:{}", devtypeBid, deviceTypeId, devCode);
|
|
|
String bzyimgAddr = jsonObject.getString("Image");
|
|
|
if (StringUtils.isEmpty(bzyimgAddr)) {
|
|
|
return "图片地址不存在,则不处理";
|
|
|
@@ -303,7 +219,6 @@ public class BzyDeviceImpl extends DeviceAbstractImpl implements IBzyDevice {
|
|
|
|
|
|
@Override
|
|
|
public boolean isDeviceProps(JSONObject jobjMsg) {
|
|
|
-
|
|
|
if ("data".equals(jobjMsg.getString("cmd"))) {
|
|
|
return true;
|
|
|
}
|
|
|
@@ -311,15 +226,10 @@ public class BzyDeviceImpl extends DeviceAbstractImpl implements IBzyDevice {
|
|
|
}
|
|
|
|
|
|
@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);
|
|
|
- //查询
|
|
|
- IotDevice ret = iIotDeviceService.selectIotDeviceByTypeFirmCode(devtypeBid, firmBid, devCode);
|
|
|
- log.info("查到了一个iotdevice {}", ret);
|
|
|
+ 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;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|