第三方API(刷鞋机)

This commit is contained in:
磷叶 2025-04-16 10:08:02 +08:00
parent 37571fc2de
commit a54b1b1d72
7 changed files with 149 additions and 155 deletions

View File

@ -61,16 +61,22 @@ public class IotConstants {
* 命令 打开
*/
public static final String COMMAND_OPEN = "open";
// 命令打开和上面功能一样特殊情况使用
public static final String COMMAND_PPPP = "pppp";
/**
* 命令 关闭
*/
public static final String COMMAND_CLOSE = "close";
// 命令关闭和上面功能一样特殊情况使用
public static final String COMMAND_CCCCC = "ccccc";
/**
* 命令 电量充值
* 命令 时长充值
*/
public static final String COMMAND_RECHARGE = "time";
// 命令时长充值和上面一样特殊情况使用
public static final String COMMAND_TTTT = "tttt";
/**
* 命令 设置通断电方式

View File

@ -43,17 +43,17 @@ public interface IotService {
/**
* 通电
*/
boolean open(IotDevice device, String reason);
boolean open(IotDevice device, String reason, String command);
/**
* 断电
*/
boolean close(IotDevice device, String reason);
boolean close(IotDevice device, String reason, String command);
/**
* 设置剩余时长
*/
CommandResponse setTime(IotDevice device, long seconds, String reason);
CommandResponse setTime(IotDevice device, long seconds, String reason, String command);
/**
* 获取设备信息并转为IotDeviceInfo

View File

@ -175,11 +175,11 @@ public class IotServiceImpl implements IotService {
}
// 通电
private boolean open(String deviceName, String productId, String reason) {
private boolean open(String deviceName, String productId, String reason, String command) {
if (StringUtils.hasBlank(deviceName, productId)) {
return false;
}
CommandResponse response = sendCommand(deviceName, IotConstants.COMMAND_OPEN, productId, reason);
CommandResponse response = sendCommand(deviceName, command, productId, reason);
IotHttpStatus status = IotHttpStatus.convertByCode(response.getCode());
if (!IotHttpStatus.SUCCESS.equals(status)) {
log.error("{}通电异常:{}", deviceName, status.getMsg());
@ -189,7 +189,7 @@ public class IotServiceImpl implements IotService {
}
@Override
public boolean open(IotDevice device, String reason) {
public boolean open(IotDevice device, String reason, String command) {
boolean result = false;
if (device == null || StringUtils.isBlank(device.getProductId())) {
return result;
@ -197,7 +197,7 @@ public class IotServiceImpl implements IotService {
// 尝试用mac2通电
try {
result = this.open(device.iotMac2(), device.getProductId(), reason);
result = this.open(device.iotMac2(), device.getProductId(), reason, IotConstants.COMMAND_OPEN);
if (!result) {
log.error("mac2通电失败{}", device.iotMac2());
throw new ServiceException("mac2通电失败");
@ -205,7 +205,7 @@ public class IotServiceImpl implements IotService {
} catch (Exception e) {
log.info("mac2通电失败尝试用mac1通电");
if (!StringUtils.isAnyBlank(device.iotMac1(), device.getProductId())) {
result = this.open(device.iotMac1(), device.getProductId(), reason);
result = this.open(device.iotMac1(), device.getProductId(), reason, IotConstants.COMMAND_OPEN);
} else {
throw e;
}
@ -215,11 +215,11 @@ public class IotServiceImpl implements IotService {
}
// 断电
private boolean close(String deviceName, String productId, String reason) {
private boolean close(String deviceName, String productId, String reason, String command) {
if (StringUtils.isAnyBlank(deviceName, productId)) {
return false;
}
CommandResponse response = sendCommand(deviceName, IotConstants.COMMAND_CLOSE, productId, reason);
CommandResponse response = sendCommand(deviceName, command, productId, reason);
IotHttpStatus status = IotHttpStatus.convertByCode(response.getCode());
if (!IotHttpStatus.SUCCESS.equals(status)) {
log.error("断电发生异常:{}, {}", deviceName, status.getMsg());
@ -229,7 +229,7 @@ public class IotServiceImpl implements IotService {
}
@Override
public boolean close(IotDevice device, String reason) {
public boolean close(IotDevice device, String reason, String command) {
boolean result = false;
if (device == null || StringUtils.isBlank(device.getProductId())) {
return result;
@ -237,14 +237,14 @@ public class IotServiceImpl implements IotService {
// 尝试用mac2断电
try {
result = this.close(device.iotMac2(), device.getProductId(), reason);
result = this.close(device.iotMac2(), device.getProductId(), reason, command);
if (!result) {
throw new ServiceException("mac2断电失败");
}
} catch (Exception e) {
log.info("mac2断电失败:{}尝试用mac1断电", e.getMessage());
if (!StringUtils.isAnyBlank(device.iotMac1(), device.getProductId())) {
result = this.close(device.iotMac1(), device.getProductId(), reason);
result = this.close(device.iotMac1(), device.getProductId(), reason, command);
} else {
throw e;
}
@ -405,17 +405,18 @@ public class IotServiceImpl implements IotService {
* @param seconds 时长
* @param productId
* @param reason
* @param command
* @return 是否成功
*/
private CommandResponse setTime(String deviceName, long seconds, String productId, String reason) {
private CommandResponse setTime(String deviceName, long seconds, String productId, String reason, String command) {
if (seconds < 0) {
throw new ServiceException("设置剩余时长参数错误读数不允许小于0");
}
return sendCommand(deviceName, IotConstants.COMMAND_RECHARGE + seconds + IotConstants.COMMAND_SEPARATOR, 5, productId, reason);
return sendCommand(deviceName, command + seconds + IotConstants.COMMAND_SEPARATOR, 5, productId, reason);
}
@Override
public CommandResponse setTime(IotDevice device, long seconds, String reason) {
public CommandResponse setTime(IotDevice device, long seconds, String reason, String command) {
if (device == null || StringUtils.isBlank(device.getProductId())) {
// throw new ServiceException("设置时长错误:参数不允许为空");
return null;
@ -423,10 +424,10 @@ public class IotServiceImpl implements IotService {
CommandResponse res = null;
if (StringUtils.hasText(device.iotMac2())) {
res = this.setTime(device.iotMac2(), seconds, device.getProductId(), reason);
res = this.setTime(device.iotMac2(), seconds, device.getProductId(), reason, command);
}
if ((res == null || !res.isSuccess()) && StringUtils.hasText(device.iotMac1())) {
res = this.setTime(device.iotMac1(), seconds, device.getProductId(), reason);
res = this.setTime(device.iotMac1(), seconds, device.getProductId(), reason, command);
}
return res;
@ -607,7 +608,7 @@ public class IotServiceImpl implements IotService {
@Override
public CommandResponse trySetTime(IotDevice device, long seconds, int tryCount, String reason) {
CommandResponse res = this.setTime(device, seconds, reason);
CommandResponse res = this.setTime(device, seconds, reason, IotConstants.COMMAND_RECHARGE);
if (res == null || !res.isSuccess()) {
if (tryCount > 0) {
try {

View File

@ -7,6 +7,7 @@ import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import com.ruoyi.common.constant.IotConstants;
import com.ruoyi.iot.domain.IotDeviceInfo;
import com.ruoyi.iot.domain.response.CommandResponse;
import com.ruoyi.ss.device.domain.Device;
@ -111,7 +112,20 @@ public interface DeviceService
* @param status 通电状态
* @param reason
*/
int switchDevice(DeviceVO device, DevicePowerStatus status, String reason);
default int switchDevice(DeviceVO device, DevicePowerStatus status, String reason) {
return switchDevice(device, status, reason, IotConstants.COMMAND_OPEN, IotConstants.COMMAND_CLOSE);
}
/**
* 切换设备通电状态/断电
*
* @param device 设备id
* @param status 通电状态
* @param reason
* @param openCommand
* @param closeCommand
*/
int switchDevice(DeviceVO device, DevicePowerStatus status, String reason, String openCommand, String closeCommand);
/**
* 拉取设备信息并保存到数据库
@ -156,7 +170,22 @@ public interface DeviceService
* @param withTime
* @param withEle
*/
int resetWithBill(Long deviceId, boolean requiredIot, BigDecimal totalEle, String reason, boolean withTime, boolean withEle);
default int resetWithBill(Long deviceId, boolean requiredIot, BigDecimal totalEle, String reason, boolean withTime, boolean withEle) {
return resetWithBill(deviceId, requiredIot, totalEle, reason, withTime, withEle, IotConstants.COMMAND_RECHARGE);
}
/**
* 归零电表并关闭订单
*
* @param deviceId 设备id
* @param requiredIot
* @param totalEle
* @param reason
* @param withTime
* @param withEle
* @param timeCommand
*/
int resetWithBill(Long deviceId, boolean requiredIot, BigDecimal totalEle, String reason, boolean withTime, boolean withEle, String timeCommand);
/**
* 设备是否已经被绑定
@ -287,20 +316,6 @@ public interface DeviceService
*/
void pullDeviceInfo(Long deviceId, String onlineType);
/**
* 设备归零时长
*
* @param device 设备
* @param required 是否必须成功
* @param reason
*/
int resetTime(DeviceVO device, boolean required, String reason);
/**
* 设备归零电量
*/
int resetEle(DeviceVO device, boolean required, String reason);
/**
* 添加电量
*/
@ -418,7 +433,14 @@ public interface DeviceService
/**
* 同步时间
*/
CommandResponse syncTime(Long deviceId, LocalDateTime now, String reason);
default CommandResponse syncTime(Long deviceId, LocalDateTime now, String reason) {
return syncTime(deviceId, now, reason, IotConstants.COMMAND_RECHARGE);
}
/**
* 同步时间
*/
CommandResponse syncTime(Long deviceId, LocalDateTime now, String reason, String command);
/**
* 同步电量

View File

@ -64,7 +64,6 @@ import com.ruoyi.ss.deviceSuit.service.DeviceSuitService;
import com.ruoyi.ss.model.domain.SmModelVO;
import com.ruoyi.ss.model.domain.enums.ModelTag;
import com.ruoyi.ss.model.service.ModelService;
import com.ruoyi.ss.record.time.domain.enums.RecordTimeType;
import com.ruoyi.ss.record.time.service.IRecordTimeService;
import com.ruoyi.ss.record.time.service.RecordTimeConverter;
import com.ruoyi.ss.store.domain.StoreVo;
@ -436,111 +435,6 @@ public class DeviceServiceImpl implements DeviceService
pullDeviceInfo(device, onlineType);
}
@Override
public int resetTime(DeviceVO device, boolean required, String reason) {
Integer result = transactionTemplate.execute(status -> {
// 归零时间将过期时间设置为现在
int update = this.resetUpdateDbTime(device.getDeviceId());
ServiceUtil.assertion(update != 1, "修改设备时间失败");
// 物联网设备归零
try {
CommandResponse res = iotService.setTime(device, 0L, reason);
ServiceUtil.assertion(res == null, "设备%s归零失败返回值为空", device.getDeviceNo());
ServiceUtil.assertion(!res.isSuccess(), "设备%s归零失败请检查设备是否在线或联系管理员", device.getDeviceNo());
} catch (Exception e) {
log.warn("设备归零失败:{}, {}, {}", device.getDeviceId(), device.getDeviceNo(), e.getMessage());
if (required) {
throw e;
}
}
return update;
});
// 归零记录
// if (result != null && result == 1 ) {
// this.resetRecord(device, RecordTimeType.TIME, reason);
// }
return result == null ? 0 : result;
}
private int resetUpdateDbTime(Long deviceId) {
Device form = new Device();
form.setDeviceId(deviceId);
form.setExpireTime(LocalDateTime.now());
form.setStatus(DeviceStatus.NORMAL.getStatus());
return deviceMapper.updateSmDevice(form);
}
@Override
public int resetEle(DeviceVO device, boolean required, String reason) {
if (device == null) {
return 0;
}
Integer result = transactionTemplate.execute(status -> {
// 更新剩余电量
int update = this.resetUpdateDbEle(device.getDeviceId(), device.getTotalElectriQuantity());
ServiceUtil.assertion(update != 1, "更新剩余电量失败");
// 直接设置电量为0
try {
CommandResponse res = iotService.setEle(device, BigDecimal.ZERO, reason);
ServiceUtil.assertion(res == null, "归零电量失败,返回值为空");
ServiceUtil.assertion(!res.isSuccess(), "归零电量失败,请检查设备是否在线或联系管理员");
} catch (Exception e) {
if (required) {
throw e;
}
}
return update;
});
// 归零记录
// if (result != null && result == 1 ) {
// this.resetRecord(device, RecordTimeType.ELE, reason);
// }
return result == null ? 0 : result;
}
private void resetRecord(DeviceVO device, RecordTimeType type, String reason) {
// // 电量
// if (RecordTimeType.ELE.equals(type)) {
// int amount = device.getSurplusEle().negate().intValue();
// RecordTime record = recordTimeConverter.toRecordTime(device.getDeviceId(), amount, reason, RecordTimeType.ELE.getType());
// scheduledExecutorService.schedule(() -> {
// recordTimeService.insertRecordTime(record);
// }, 0, TimeUnit.SECONDS);
// }
// // 时长
// else if (RecordTimeType.TIME.equals(type)) {
// LocalDateTime expireTime = device.getExpireTime();
// long seconds = 0;
// if (expireTime != null) {
// Duration duration = Duration.between(LocalDateTime.now(), expireTime);
// seconds = duration.getSeconds() > 0 ? duration.getSeconds() : 0;
// }
// if (seconds > 0) {
// RecordTime record = recordTimeConverter.toRecordTime(device.getDeviceId(), -seconds, reason, RecordTimeType.TIME.getType());
// scheduledExecutorService.schedule(() -> {
// recordTimeService.insertRecordTime(record);
// }, 0, TimeUnit.SECONDS);
// }
// }
}
private int resetUpdateDbEle(Long deviceId, BigDecimal expireEle) {
Device data = new Device();
data.setDeviceId(deviceId);
data.setSurplusEle(BigDecimal.ZERO);
data.setExpireEle(expireEle);
return deviceMapper.updateSmDevice(data);
}
@Override
public boolean addEle(Long deviceId, BigDecimal ele, String reason, boolean requiredIot) {
DeviceVO device = selectById(deviceId);
@ -885,12 +779,14 @@ public class DeviceServiceImpl implements DeviceService
/**
* 切换设备通电状态/断电
*
* @param device 设备id
* @param powerStatus 通电状态
* @param device 设备id
* @param powerStatus 通电状态
* @param reason
* @param openCommand
* @param closeCommand
*/
@Override
public int switchDevice(DeviceVO device, DevicePowerStatus powerStatus, String reason) {
public int switchDevice(DeviceVO device, DevicePowerStatus powerStatus, String reason, String openCommand, String closeCommand) {
ServiceUtil.assertion(device == null || device.getDeleted(), "设备不存在");
Integer result = transactionTemplate.execute(status -> {
@ -910,10 +806,10 @@ public class DeviceServiceImpl implements DeviceService
// 操作设备
if (update == 1) {
if (DevicePowerStatus.ON.equals(powerStatus)) {
boolean open = iotService.open(device, reason);
boolean open = iotService.open(device, reason, openCommand);
ServiceUtil.assertion(!open, "开启设备失败");
} else if (DevicePowerStatus.OFF.equals(powerStatus)) {
boolean close = iotService.close(device, reason);
boolean close = iotService.close(device, reason, closeCommand);
ServiceUtil.assertion(!close, "关闭设备失败");
} else {
throw new ServiceException("不支持的操作");
@ -1185,7 +1081,7 @@ public class DeviceServiceImpl implements DeviceService
}
@Override
public int resetWithBill(Long deviceId, boolean requiredIot, BigDecimal totalEle, String reason, boolean withTime, boolean withEle) {
public int resetWithBill(Long deviceId, boolean requiredIot, BigDecimal totalEle, String reason, boolean withTime, boolean withEle, String timeCommand) {
ServiceUtil.assertion(deviceId == null, "设备id不能为空");
// 获取设备信息
@ -1217,7 +1113,7 @@ public class DeviceServiceImpl implements DeviceService
// 发送命令
if (withTime) {
CommandResponse res = iotService.setTime(device, 0L, reason);
CommandResponse res = iotService.setTime(device, 0L, reason, timeCommand);
if (requiredIot) {
ServiceUtil.assertion(res == null, "归零电量失败,返回值为空");
ServiceUtil.assertion(!res.isSuccess(), "归零电量失败,请检查设备是否在线或联系管理员");
@ -1335,7 +1231,7 @@ public class DeviceServiceImpl implements DeviceService
}
@Override
public CommandResponse syncTime(Long deviceId, LocalDateTime now, String reason) {
public CommandResponse syncTime(Long deviceId, LocalDateTime now, String reason, String command) {
if (deviceId == null) {
return null;
}
@ -1346,7 +1242,7 @@ public class DeviceServiceImpl implements DeviceService
long syncSeconds = DeviceUtils.getSyncSeconds(device.getExpireTime(), now);
if (syncSeconds > 0) {
return iotService.setTime(device, syncSeconds, reason);
return iotService.setTime(device, syncSeconds, reason, command);
}
return null;
}

View File

@ -59,8 +59,7 @@ public class Store extends BaseEntity
/** 门店地址 */
@Excel(name = "门店地址")
@NotNull(message = "门店地址不能为空", groups = {ValidGroup.Create.class, ValidGroup.FrontCreate.class})
@Size(min = 1, max = 200, message = "门店地址长度必须在1~200之间")
@Size(max = 200, message = "门店地址长度不允许超过200")
@JsonView(JsonViewProfile.App.class)
private String address;

View File

@ -0,0 +1,70 @@
package com.ruoyi.web.controller.mch;
import com.ruoyi.common.constant.IotConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.device.domain.enums.DevicePowerStatus;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceAssembler;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.device.service.DeviceValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 设备操作接口专门给刷鞋机客户用的特殊接口
* @author wjh
* 2025/4/16
*/
@RestController
@RequestMapping("/mch/device/iot/sp")
public class MchDeviceIotSpController extends BaseController {
@Autowired
private DeviceService deviceService;
@Autowired
private DeviceValidator deviceValidator;
@Autowired
private DeviceAssembler deviceAssembler;
// 设备开关
@PutMapping("/switch")
public AjaxResult switchDevice(Long deviceId, String status) {
DeviceVO device = deviceService.selectById(deviceId);
ServiceUtil.assertion(device == null, "设备不存在");
// 判断是否有权限更改
ServiceUtil.assertion(!deviceValidator.canOpera(deviceId, getUserId()), "您无权操作此设备");
deviceAssembler.assembleAllowSwitch(device, getUserId());
ServiceUtil.assertion(!device.getAllowSwitch(), "您无权操作此设备");
// 获取最新数据
deviceService.pullDeviceInfo(device, null);
DevicePowerStatus powerStatus = DevicePowerStatus.parse(status);
return toAjax(deviceService.switchDevice(device, powerStatus, "刷鞋机商户开关设备", IotConstants.COMMAND_PPPP, IotConstants.COMMAND_CCCCC));
}
// 设备增加时间
@PutMapping("/time")
public AjaxResult setTime(Long deviceId, Long seconds) {
DeviceVO device = deviceService.selectById(deviceId);
ServiceUtil.assertion(device == null, "设备不存在");
deviceAssembler.assembleAllowSwitch(device, getUserId());
ServiceUtil.assertion(!device.getAllowSwitch(), "您无权操作此设备");
return toAjax(deviceService.addTime(deviceId, seconds, "刷鞋机商户手动充值", true));
}
// 设备归零
@PutMapping("/reset")
public AjaxResult reset(Long deviceId) {
if (!deviceValidator.canOpera(deviceId, getUserId()) ) {
return error("您无权操作此设备");
}
return toAjax(deviceService.resetWithBill(deviceId, false, null, "刷鞋机商户设备归零", true, false, IotConstants.COMMAND_TTTT));
}
}