|
|
@@ -0,0 +1,157 @@
|
|
|
+package com.yunfeiyun.agmp.iots.device.serviceImp;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.yunfeiyun.agmp.common.utils.DateUtils;
|
|
|
+import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
+import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.IotYfXycb2data;
|
|
|
+import com.yunfeiyun.agmp.iot.common.enums.IotDeviceStatusTypeEnum;
|
|
|
+import com.yunfeiyun.agmp.iot.common.model.cmd.CmdModel;
|
|
|
+import com.yunfeiyun.agmp.iots.core.manager.MqttManager;
|
|
|
+import com.yunfeiyun.agmp.iots.device.common.DeviceAbstractImpl;
|
|
|
+import com.yunfeiyun.agmp.iots.device.service.IIotYfXycb2dataService;
|
|
|
+import com.yunfeiyun.agmp.iots.device.service.IYfXycb2Device;
|
|
|
+import com.yunfeiyun.agmp.iots.service.IIotDeviceService;
|
|
|
+import com.yunfeiyun.agmp.iots.service.IIotDevicelasteddataService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 云飞-性诱2.0
|
|
|
+ * 宁录性诱设备
|
|
|
+ * */
|
|
|
+@Component(ServiceNameConst.SERVICE_YF_XYCB_2)
|
|
|
+@Slf4j
|
|
|
+public class YfXycb2DeviceImpl extends DeviceAbstractImpl implements IYfXycb2Device {
|
|
|
+ private static final String TAG = "【云飞-性诱2.0】 ";
|
|
|
+
|
|
|
+ private static final String SERVICE_NAME = ServiceNameConst.SERVICE_YF_XYCB_2;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MqttManager mqttManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IIotDeviceService iIotDeviceService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IIotYfXycb2dataService iIotYfXycb2dataService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IIotDevicelasteddataService iIotDevicelasteddataService;
|
|
|
+
|
|
|
+
|
|
|
+ private void logger(String level, String msg){
|
|
|
+ if(Objects.equals(level, "info")){
|
|
|
+ log.info(TAG + msg);
|
|
|
+ } else if (Objects.equals(level, "error")) {
|
|
|
+ log.error(TAG + msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object sendCmd(CmdModel cmdModel) throws Exception {
|
|
|
+ logger("info", "不支持设备操控");
|
|
|
+ //返回值待定
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object cmdData(JSONObject dataJson, String topic, String connectionId, String devUpdateddate) {
|
|
|
+ logger("info", "数据解析 " + dataJson.toString() + " topic: " + topic);
|
|
|
+ IotDevice oldIotDevice = findIotDevice(topic, dataJson, connectionId);
|
|
|
+ if (oldIotDevice == null) {
|
|
|
+ logger("error", "未取到设备:" + topic);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ IotDevice newIotDevice = new IotDevice();
|
|
|
+ newIotDevice.setDevBid(oldIotDevice.getDevBid());
|
|
|
+ newIotDevice.setDevVersion("YF-XY-2.0");
|
|
|
+ newIotDevice.setDevPositionstatus("0");
|
|
|
+ newIotDevice.setDevPositiontype("0");
|
|
|
+ newIotDevice.setDevUpdateddate(devUpdateddate);
|
|
|
+ newIotDevice.setDevStatus(IotDeviceStatusTypeEnum.ONLINE.getCode());
|
|
|
+
|
|
|
+ // 更新设备基础信息数据库 mysql
|
|
|
+ iIotDeviceService.updateIotDevice(newIotDevice);
|
|
|
+ // 设备不可控制,没有配置信息
|
|
|
+
|
|
|
+ // 更新设备数据到mongodb
|
|
|
+ // ct 击虫数量,默认0,由上报击虫信息时候更新 ct 值
|
|
|
+ dataJson.put("ct", "0");
|
|
|
+ iIotYfXycb2dataService.insertXycb2data(newIotDevice, dataJson);
|
|
|
+
|
|
|
+ // 保存 设备最新数据 到redis
|
|
|
+ iIotDevicelasteddataService.createOrUpdateDeviceLastedData(
|
|
|
+ dataJson, oldIotDevice, devUpdateddate, 60 * 60 * 24L);
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object cmdPestCount(JSONObject dataJson, String topic, String connectionId){
|
|
|
+ logger("info", "性诱测报2.0虫情数据 " + dataJson.toString() + " topic: " + topic);
|
|
|
+ IotDevice oldIotDevice = findIotDevice(topic, dataJson, connectionId);
|
|
|
+ if (oldIotDevice == null) {
|
|
|
+ logger("error", "未取到设备:" + topic);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String devBid = oldIotDevice.getDevBid();
|
|
|
+
|
|
|
+ IotYfXycb2data iotYfXycb2data = iIotYfXycb2dataService.selectIotYfXycb2dataByDevBid(devBid);
|
|
|
+ if(iotYfXycb2data == null){
|
|
|
+ logger("info", "没有上报数据,跳过此次虫情统计数据:" + devBid);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer pestCount = dataJson.getInteger("pest");
|
|
|
+
|
|
|
+ JSONObject jsonObject = iotYfXycb2data.getXycb2dataContent();
|
|
|
+ Long oldPestNum = Long.valueOf((String) jsonObject.getOrDefault("ct", "0"));
|
|
|
+ long newPestNum = pestCount + oldPestNum;
|
|
|
+ jsonObject.put("ct", Long.toString(newPestNum));
|
|
|
+
|
|
|
+ Map<String, Object> updateField = new HashMap<>();
|
|
|
+ updateField.put("id", iotYfXycb2data.getId());
|
|
|
+ updateField.put("xycb2dataContent", jsonObject);
|
|
|
+ iIotYfXycb2dataService.updateIotYfXycb2data(updateField);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object receiveData(String topic, JSONObject dataJson, String connectionId) throws Exception {
|
|
|
+ logger("info", "处理收到的 设备上报数据 " + dataJson.toString());
|
|
|
+
|
|
|
+ // 接收设备上报数据后的处理逻辑
|
|
|
+ String protocoltype = dataJson.getString("protocoltype");
|
|
|
+ String devUpdateddate = dataJson.getString("devUpdateddate");
|
|
|
+ if(StringUtils.isEmpty(devUpdateddate)){
|
|
|
+ devUpdateddate = DateUtils.dateTimeNow();
|
|
|
+ }
|
|
|
+ if("2".equals(protocoltype)){
|
|
|
+ cmdData(dataJson, topic, connectionId, devUpdateddate);
|
|
|
+ } else if ("3".equals(protocoltype)){
|
|
|
+ cmdPestCount(dataJson, topic, connectionId);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|