恢复设备总用电量

This commit is contained in:
墨大叔 2024-10-07 09:25:18 +08:00
parent 903884cecb
commit 9f7aadd2c3
8 changed files with 87 additions and 10 deletions

View File

@ -94,7 +94,7 @@ public class IotConstants {
/**
* 命令 设置总用电量
*/
public static final String COMMAND_SET_TOTAL_ELE = "";
public static final String COMMAND_SET_TOTAL_ELE = "pow_set";
/**----------------------------命令end----------------------------*/

View File

@ -1,6 +1,7 @@
package com.ruoyi.iot.service;
import com.ruoyi.iot.domain.HistoryDeviceData;
import com.ruoyi.iot.domain.IotDeviceInfo;
import com.ruoyi.iot.domain.response.CommandResponse;
import com.ruoyi.iot.interfaces.IotDevice;
@ -158,7 +159,7 @@ public interface IotService {
/**
* 设置总用电量
* @param device 设备
* @param totalEle 总用电量
* @param totalEle 总用电量瓦时
*/
boolean setTotalEle(IotDevice device, BigDecimal totalEle);
@ -166,7 +167,12 @@ public interface IotService {
* 设置总用电量
* @param deviceName 设备MAC
* @param productId 产品ID
* @param totalEle 总用电量
* @param totalEle 总用电量瓦时
*/
boolean setTotalEle(String deviceName, String productId, BigDecimal totalEle);
/**
* 获取历史数据
*/
HistoryDeviceData getHistoryDataPoint(String deviceName, String productId);
}

View File

@ -4,9 +4,9 @@ import com.ruoyi.common.core.redis.enums.RedisLockKey;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.iot.constants.ReceiveConstants;
import com.ruoyi.iot.domain.IotDeviceDetail;
import com.ruoyi.iot.domain.ReceiveMsg;
import com.ruoyi.iot.domain.*;
import com.ruoyi.iot.domain.response.CommandResponse;
import com.ruoyi.iot.enums.ReceiveType;
import com.ruoyi.iot.service.IotReceiveService;
@ -60,9 +60,12 @@ public class IotReceiveServiceImpl implements IotReceiveService {
// 数据点推送
if (ReceiveType.DATA_POINT.getType().equals(msg.getType())) {
log.info("收到推送数据点:{},{},{}", msg.getAt(), msg.getDsId(), msg.getValue());
// 若推送数据点:CSQ,则恢复余额
// 若推送数据点:CSQ
if (ReceiveConstants.DS_CSQ.equals(msg.getDsId())) {
// 恢复余额
this.recoverBalance(msg);
// 恢复总用电量
this.recoverTotalEle(msg);
}
}
// 生命周期
@ -70,6 +73,52 @@ public class IotReceiveServiceImpl implements IotReceiveService {
}
}
/**
* 恢复异常的总用电量
* @param msg
*/
private void recoverTotalEle(ReceiveMsg msg) {
// 查询设备
DeviceVO device = deviceService.selectByAnyMac(msg.getDevName());
if (device == null) {
log.info("设备:{} 不存在", msg.getDevName());
return;
}
// 查询历史数据点
HistoryDeviceData historyDataPoint = iotService.getHistoryDataPoint(msg.getDevName(), device.getProductId());
if (historyDataPoint == null || CollectionUtils.isEmptyElement(historyDataPoint.getDatastreams()) ) {
log.info("设备:{} 没有历史数据点", msg.getDevName());
return;
}
// 获取总用电量数据点
HistoryDatastream pointW = historyDataPoint.getDatastreams()
.stream().filter(item -> item.getId().equals(ReceiveConstants.DS_W))
.findFirst().orElse(null);
if (pointW == null || CollectionUtils.isEmptyElement(pointW.getDatapoints())) {
log.info("设备:{} 没有数据点:{}", msg.getDevName(), ReceiveConstants.DS_W);
return;
}
// 遍历数据点查找异常数据并恢复
Datapoint lastPoint = null;
for (Datapoint dp : pointW.getDatapoints()) {
// 若当前数据点值大于上次数据点值则视为异常数据进行恢复
if (lastPoint != null && dp != null) {
BigDecimal lastEle = NumberUtils.nonNullDecimal(lastPoint.getValue().toString());
BigDecimal currEle = NumberUtils.nonNullDecimal(dp.getValue().toString());
if (currEle.compareTo(lastEle) > 0) {
log.info("设备:{} 数据点异常,恢复总电量:{} 瓦时, lastPoint = {}, dp = {}", msg.getDevName(), currEle, lastPoint, dp);
iotService.setTotalEle(device, currEle);
break;
}
}
lastPoint = dp;
}
}
// 恢复设备的余额
private void recoverBalance(ReceiveMsg msg) {
String lockKey = msg.getDevName();
@ -78,13 +127,15 @@ public class IotReceiveServiceImpl implements IotReceiveService {
}
try {
// 查询设备
DeviceVO device = deviceService.selectByMac(msg.getDevName());
DeviceVO device = deviceService.selectByAnyMac(msg.getDevName());
if (device == null) {
log.info("设备:{} 不存在", msg.getDevName());
return;
}
// 判断上次恢复余额的时间和本次恢复余额的时间是否相同若相同则视为重复推送则忽略
if (device.getLastRecoverTime() != null && msg.getAt().equals(device.getLastRecoverTime())) {
log.info("设备:{} 重复推送余额恢复,上次恢复时间:{}", msg.getDevName(), device.getLastRecoverTime());
return;
}

View File

@ -164,15 +164,14 @@ public class IotServiceImpl implements IotService {
}
// 获取历史设备数据点信息
private HistoryDeviceData getHistoryDataPoint(String deviceName, String productId) {
@Override
public HistoryDeviceData getHistoryDataPoint(String deviceName, String productId) {
String param = "device_name=" + deviceName + "&product_id=" + productId;
String sendUrl = iotHost + IotConstants.ADDS_HISTORY_DATAPOINTS + "?"+param;
String token = Token.getToken();
// log.info("IOT获取到Authorization:【{}】",token);
String result = HttpUtils.sendGetWithToken(sendUrl, null, token);
// log.info("IOT返回的结果【{}】",result);
if (!StringUtils.hasText(result)) {
log.error("与OneNet通信异常");
return null;

View File

@ -186,4 +186,9 @@ public interface DeviceMapper
* 续费时长
*/
int renewalRentTime(@Param("deviceId") Long deviceId, @Param("seconds") long seconds);
/**
* 根据任何一个MAC查询
*/
DeviceVO selectByAnyMac(@Param("mac") String mac);
}

View File

@ -280,6 +280,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
limit 1
</select>
<select id="selectByAnyMac" resultMap="SmDeviceResult">
<include refid="selectVo"/>
where sd.deleted = false and (sd.mac = #{mac} or sd.mac2 = #{mac})
</select>
<insert id="insertSmDevice" parameterType="Device" useGeneratedKeys="true" keyProperty="deviceId">
<selectKey resultType="Long" order="AFTER" keyProperty="deviceId">
select LAST_INSERT_ID()

View File

@ -352,4 +352,10 @@ public interface DeviceService
* 根据设备ID列表查询
*/
List<DeviceVO> selectByDeviceIds(List<Long> deviceIds);
/**
* 根据mac查询设备
*/
DeviceVO selectByAnyMac(String mac);
}

View File

@ -647,6 +647,11 @@ public class DeviceServiceImpl implements DeviceService
return this.selectSmDeviceList(query);
}
@Override
public DeviceVO selectByAnyMac(String mac) {
return deviceMapper.selectByAnyMac(mac);
}
@Override
public boolean addTime(Long deviceId, long seconds, boolean withIot) {