debug 蓝牙充值、设备是否在线

This commit is contained in:
墨大叔 2024-05-24 16:20:30 +08:00
parent 8a1ad7c929
commit 36d1c75a55
10 changed files with 146 additions and 117 deletions

View File

@ -6,7 +6,7 @@ package com.ruoyi.iot.constants;
*/
public class ReceiveConstants {
// 数据点IDSSID
// 数据点IDSSIDWIFI
public static final String DS_SSID = "ssid";
// 数据点ID电压
public static final String DS_V = "V";

View File

@ -1,7 +1,9 @@
package com.ruoyi.iot.domain;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import java.util.Date;
import java.util.List;
@ -42,7 +44,7 @@ public class IotDeviceDetail {
// 设备状态
@JsonProperty("status")
private int status;
private Integer status;
// 设备创建时间
@JsonProperty("create_time")
@ -86,7 +88,7 @@ public class IotDeviceDetail {
// 设备启用状态
@JsonProperty("enable_status")
private boolean enableStatus;
private Boolean enableStatus;
// 设备标签列表
@JsonProperty("tags")
@ -106,15 +108,15 @@ public class IotDeviceDetail {
// 设备资源自动订阅是否启用
@JsonProperty("obsv")
private boolean obsv;
private Boolean obsv;
// 设备资源自动订阅状态
@JsonProperty("obsv_st")
private boolean obsvSt;
private Boolean obsvSt;
// 设备私密性
@JsonProperty("private")
private boolean privateFlag;
private Boolean privateFlag;
// 设备imsi历史变更记录
@JsonProperty("imsi_old")
@ -123,4 +125,14 @@ public class IotDeviceDetail {
// 设备imsi最近一次修改时间
@JsonProperty("imsi_mt")
private Date imsiMt;
@Getter
@AllArgsConstructor
public enum Status {
OFFLINE(0), // 离线
ONLINE(1), // 在线
NO_ACTIVE(2); // 未激活
private final Integer status;
}
}

View File

@ -29,8 +29,7 @@ public class IotReceiveServiceImpl implements IotReceiveService{
@Override
public void handleReceive(ReceiveMsg msg) {
log.info("handleReceive {}", msg.toString());
if (ReceiveType.DATA_POINT.getType().equals(msg.getType())
) {
if (ReceiveType.DATA_POINT.getType().equals(msg.getType())) {
if (ReceiveConstants.DS_SSID.equals(msg.getDsId())
&& ReceiveConstants.DSV_SSID_CT.equals((String) msg.getValue())) {
// 当数据点推送ssid的值为ChangteA时录入设备

View File

@ -39,7 +39,6 @@ import java.util.stream.Collectors;
@Service
@Slf4j
public class IotServiceImpl implements IotService {
@Value("${sm.iotHost}")
private String iotHost;
@ -70,8 +69,15 @@ public class IotServiceImpl implements IotService {
if (StringUtils.isBlank(deviceName)) {
return DeviceOnlineStatus.OFFLINE;
}
CommandResponse response = sendCommand(deviceName, "111");
return HttpStatus.IOT_SUCCESS.equals(response.getCode()) ? DeviceOnlineStatus.ONLINE : DeviceOnlineStatus.OFFLINE;
// CommandResponse response = sendCommand(deviceName, "111");
// return HttpStatus.IOT_SUCCESS.equals(response.getCode()) ? DeviceOnlineStatus.ONLINE : DeviceOnlineStatus.OFFLINE;
IotDeviceDetail detail = this.getDeviceDetail(deviceName);
if (detail == null) {
return DeviceOnlineStatus.OFFLINE;
}
return IotDeviceDetail.Status.ONLINE.getStatus().equals(detail.getStatus()) ? DeviceOnlineStatus.ONLINE : DeviceOnlineStatus.OFFLINE;
}
// 通电
@ -160,7 +166,7 @@ public class IotServiceImpl implements IotService {
}
DetailResponse response = JSONObject.parseObject(result, DetailResponse.class);
if (!HttpStatus.IOT_SUCCESS.equals(response.getCode())) {
log.error("获取当前设备数据点信息出错:" + response.getMsg());
log.error("获取当前设备数据点信息出错:{}", response.getMsg());
return null;
}

View File

@ -61,7 +61,35 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<sql id="selectVo">
select
sd.*,
sd.device_id,
sd.device_no,
sd.store_id,
sd.device_name,
sd.model_id,
sd.mac,
sd.activation_time,
sd.total_electri_quantity,
sd.online_status,
sd.status,
sd.real_time_power,
sd.electricity,
sd.voltage,
sd.nick_name,
sd.create_by,
sd.create_time,
sd.update_by,
sd.update_time,
sd.remark,
sd.outage_way,
sd.wifi,
sd.deleted,
sd.last_pull_time,
sd.init_reading,
sd.power_status,
sd.expire_time,
sd.custom_picture,
sd.service_rate,
sd.remain_time,
sm.model_name as model,
sm.picture as picture,
sm.tags as model_tags,

View File

@ -31,4 +31,10 @@ public interface DeviceAssembler {
* 拼接订单统计信息订单数量订单金额
*/
void assembleOrderCountInfo(List<SmDeviceVo> list);
/**
* 拼接物联网数据
* @param list
*/
void assembleIotDeviceInfo(List<SmDeviceVo> list);
}

View File

@ -1,7 +1,11 @@
package com.ruoyi.ss.device.service.impl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.iot.domain.IotDeviceInfo;
import com.ruoyi.iot.service.IotService;
import com.ruoyi.ss.dashboard.BillCountVo;
import com.ruoyi.ss.device.domain.enums.DeviceOnlineStatus;
import com.ruoyi.ss.device.domain.vo.SmDeviceVo;
import com.ruoyi.ss.device.service.DeviceAssembler;
import com.ruoyi.ss.deviceTenant.domain.SmDeviceTenant;
@ -45,6 +49,9 @@ public class DeviceAssemblerImpl implements DeviceAssembler {
@Autowired
private TransactionBillService transactionBillService;
@Autowired
private IotService iotService;
@Override
public void assembleTenant(List<SmDeviceVo> list) {
if (CollectionUtils.isEmpty(list)) {
@ -128,4 +135,38 @@ public class DeviceAssemblerImpl implements DeviceAssembler {
device.setOrderAmount(count == null ? BigDecimal.ZERO : count.getRecharge());
}
}
@Override
public void assembleIotDeviceInfo(List<SmDeviceVo> list) {
if (CollectionUtils.isEmptyElement(list)) {
return;
}
// 获取当前设备数据
List<String> deviceNames = list.stream().map(SmDeviceVo::getMac).filter(StringUtils::hasText).collect(Collectors.toList());
if (CollectionUtils.isEmpty(deviceNames)) {
return;
}
Map<String, IotDeviceInfo> deviceInfoMap = iotService.getDeviceInfo(deviceNames)
.stream().collect(Collectors.toMap(IotDeviceInfo::getMac, item -> item));
// 拼接数据
for (SmDeviceVo device : list) {
if (StringUtils.hasText(device.getMac())) {
// 在线状态
device.setOnlineStatus(iotService.getOnlineStatus(device.getMac()).getStatus());
// 其他信息
IotDeviceInfo deviceInfo = deviceInfoMap.get(device.getMac());
if (deviceInfo != null) {
device.setElectricity(deviceInfo.getA());
device.setVoltage(deviceInfo.getV());
device.setRealTimePower(deviceInfo.getP());
device.setTotalElectriQuantity(deviceInfo.getW()); // 电量
device.setRemainTime(deviceInfo.getTime());
}
} else {
device.setOnlineStatus(DeviceOnlineStatus.OFFLINE.getStatus());
}
}
}
}

View File

@ -86,12 +86,7 @@ public class SmDeviceServiceImpl implements ISmDeviceService
@Override
public SmDeviceVo selectSmDeviceByDeviceId(Long deviceId)
{
SmDeviceVo device = smDeviceMapper.selectSmDeviceByDeviceId(deviceId);
if (device == null) {
return null;
}
assembleDeviceInfo(Collections.singletonList(device));
return device;
return smDeviceMapper.selectSmDeviceByDeviceId(deviceId);
}
/**
@ -103,48 +98,7 @@ public class SmDeviceServiceImpl implements ISmDeviceService
@Override
public List<SmDeviceVo> selectSmDeviceList(SmDeviceQuery smDevice)
{
smDevice.setDeleted(false);
List<SmDeviceVo> list = smDeviceMapper.selectSmDeviceList(smDevice);
assembleDeviceInfo(list);
return list;
}
/**
* 拼接设备信息
* @param list 设备列表
*/
private void assembleDeviceInfo(List<SmDeviceVo> list) {
if (CollectionUtils.isEmpty(list)) {
return;
}
// 获取当前设备数据
List<String> deviceNames = list.stream().map(SmDeviceVo::getMac).filter(StringUtils::hasText).collect(Collectors.toList());
if (CollectionUtils.isEmpty(deviceNames)) {
return;
}
Map<String, IotDeviceInfo> deviceInfoMap = iotService.getDeviceInfo(deviceNames)
.stream().collect(Collectors.toMap(IotDeviceInfo::getMac, item -> item));
// 拼接数据
for (SmDeviceVo device : list) {
if (StringUtils.hasText(device.getMac())) {
// 在线状态
device.setOnlineStatus(iotService.getOnlineStatus(device.getMac()).getStatus());
// 其他信息
IotDeviceInfo deviceInfo = deviceInfoMap.get(device.getMac());
if (deviceInfo != null) {
device.setElectricity(deviceInfo.getA());
device.setVoltage(deviceInfo.getV());
device.setRealTimePower(deviceInfo.getP());
device.setTotalElectriQuantity(deviceInfo.getW()); // 电量
device.setRemainTime(deviceInfo.getTime());
}
} else {
device.setOnlineStatus(DeviceOnlineStatus.OFFLINE.getStatus());
}
}
return smDeviceMapper.selectSmDeviceList(smDevice);
}
/**
@ -177,11 +131,11 @@ public class SmDeviceServiceImpl implements ISmDeviceService
int updateCount = smDeviceMapper.updateSmDevice(smDevice);
// 设置物联网设备信息
SmDeviceVo device = smDeviceMapper.selectSmDeviceByDeviceId(smDevice.getDeviceId());
if (!StringUtils.isBlank(device.getMac())) {
boolean updateDevice = iotService.updateDevice(device);
ServiceUtil.assertion(!updateDevice, "设备修改失败,请刷新后重试");
}
// SmDeviceVo device = smDeviceMapper.selectSmDeviceByDeviceId(smDevice.getDeviceId());
// if (!StringUtils.isBlank(device.getMac())) {
// boolean updateDevice = iotService.updateDevice(device);
// ServiceUtil.assertion(!updateDevice, "设备修改失败,请刷新后重试");
// }
return updateCount;
}
@ -264,6 +218,7 @@ public class SmDeviceServiceImpl implements ISmDeviceService
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public boolean addTime(Long deviceId, BigDecimal num) {
ServiceUtil.assertion(num.compareTo(BigDecimal.ZERO) < 0, "增加的时长不允许小于0");
@ -272,38 +227,30 @@ public class SmDeviceServiceImpl implements ISmDeviceService
ServiceUtil.assertion(!StringUtils.hasText(device.getMac()), "设备MAC号为空");
ServiceUtil.assertion(DeviceStatus.FIXING.getStatus().equals(device.getStatus()), "设备正在维修中,无法使用");
// 开启一个新的事务
TransactionTemplate newTransaction = new TransactionTemplate();
Boolean result = newTransaction.execute(status -> {
// 更新数据库时长
int updateCount = smDeviceMapper.addTime(deviceId, num);
ServiceUtil.assertion(updateCount != 1, "增加时长失败,请刷新后重试");
// 更新数据库时长
int updateCount = smDeviceMapper.addTime(deviceId, num);
ServiceUtil.assertion(updateCount != 1, "增加时长失败,请刷新后重试");
// 修改状态为使用中
changeStatus(deviceId, DeviceStatus.USING);
// 修改状态为使用中
changeStatus(deviceId, DeviceStatus.USING);
// 物联网设备增加时长
SmDeviceVo newDevice = selectSmDeviceByDeviceId(deviceId);
long seconds = Duration.between(LocalDateTime.now(), newDevice.getExpireTime()).getSeconds();
if (seconds > 0) {
boolean rechargeResult = iotService.setTime(device.getMac(), new BigDecimal(seconds));
ServiceUtil.assertion(!rechargeResult, "设备充值失败");
}
return Boolean.TRUE;
});
// 拉取设备信息
if (result != null && result) {
this.pullDeviceInfoAsync(Collections.singletonList(deviceId), 3, TimeUnit.SECONDS);
// 时长结束后修改设备状态
scheduledExecutorService.schedule(()-> {
freshStatus(deviceId);
}, num.intValue(), TimeUnit.MINUTES);
// 物联网设备增加时长
SmDeviceVo newDevice = selectSmDeviceByDeviceId(deviceId);
long seconds = Duration.between(LocalDateTime.now(), newDevice.getExpireTime()).getSeconds();
if (seconds > 0) {
boolean rechargeResult = iotService.setTime(device.getMac(), new BigDecimal(seconds));
ServiceUtil.assertion(!rechargeResult, "设备充值失败");
}
return result != null && result;
// 拉取设备信息
this.pullDeviceInfoAsync(Collections.singletonList(deviceId), 3, TimeUnit.SECONDS);
// 时长结束后修改设备状态
scheduledExecutorService.schedule(()-> {
freshStatus(deviceId);
}, num.intValue(), TimeUnit.MINUTES);
return true;
}
private void freshStatus(Long deviceId) {
@ -341,25 +288,6 @@ public class SmDeviceServiceImpl implements ISmDeviceService
return smDeviceMapper.updateSmDevice(device);
}
/**
* 获取租户设备列表
* @param tenantId 租户id
* @return
*/
private List<SmDeviceVo> selectTenantDeviceList(Long tenantId) {
if (tenantId == null) {
return Collections.emptyList();
}
SmDeviceQuery dto = new SmDeviceQuery();
dto.setDeleted(false);
List<SmDeviceVo> list = smDeviceMapper.selectSmDeviceList(dto);
assembleDeviceInfo(list);
if (CollectionUtils.isEmpty(list)) {
return Collections.emptyList();
}
return list;
}
@Override
public boolean logicDel(List<Long> deviceIds) {
this.validatePreLogicDelete(deviceIds);
@ -520,7 +448,6 @@ public class SmDeviceServiceImpl implements ISmDeviceService
SmDeviceQuery dto = new SmDeviceQuery();
dto.setMac(mac);
List<SmDeviceVo> list = smDeviceMapper.selectSmDeviceList(dto);
assembleDeviceInfo(list);
if (CollectionUtils.isEmpty(list)) {
return null;
}
@ -613,7 +540,6 @@ public class SmDeviceServiceImpl implements ISmDeviceService
@Override
public int updateByIot(SmDevice smDevice) {
smDevice.setLastPullTime(DateUtils.getNowDate());
smDevice.setOnlineStatus(smDevice.getOnlineStatus());
return smDeviceMapper.updateSmDeviceByIot(smDevice);
}

View File

@ -66,7 +66,9 @@ public class AppDeviceController extends BaseController {
public TableDataInfo list(SmDeviceQuery smDevice) {
startPage();
smDevice.setUserId(getUserId());
return getDataTable(smDeviceService.selectSmDeviceList(smDevice));
List<SmDeviceVo> list = smDeviceService.selectSmDeviceList(smDevice);
deviceAssembler.assembleIotDeviceInfo(list);
return getDataTable(list);
}
@ApiOperation("查询店铺设备列表")
@ -77,6 +79,7 @@ public class AppDeviceController extends BaseController {
query.setStoreId(storeId);
List<SmDeviceVo> list = smDeviceService.selectSmDeviceList(query);
deviceAssembler.assembleBusinessTime(list); // 店铺营业时间
deviceAssembler.assembleIotDeviceInfo(list);
return getDataTable(list);
}
@ -87,6 +90,7 @@ public class AppDeviceController extends BaseController {
SmDeviceVo device = smDeviceService.selectSmDeviceByDeviceId(deviceId);
List<SmDeviceVo> list = Collections.singletonList(device);
deviceAssembler.assembleOrderCountInfo(list); // 订单统计信息
deviceAssembler.assembleIotDeviceInfo(list);
return success(device);
}

View File

@ -50,6 +50,9 @@ public class SmDeviceController extends BaseController
@Autowired
private DeviceValidator deviceValidator;
@Autowired
private DeviceAssembler deviceAssembler;
/**
* 查询设备列表
*/
@ -59,6 +62,7 @@ public class SmDeviceController extends BaseController
{
startPage();
List<SmDeviceVo> list = smDeviceService.selectSmDeviceList(smDevice);
deviceAssembler.assembleIotDeviceInfo(list);
return getDataTable(list);
}
@ -94,7 +98,10 @@ public class SmDeviceController extends BaseController
@GetMapping(value = "/{deviceId}")
public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
{
return success(smDeviceService.selectSmDeviceByDeviceId(deviceId));
SmDeviceVo device = smDeviceService.selectSmDeviceByDeviceId(deviceId);
List<SmDeviceVo> list = Collections.singletonList(device);
deviceAssembler.assembleIotDeviceInfo(list);
return success(device);
}
/**