2024-05-06 18:06:58 +08:00
|
|
|
|
package com.ruoyi.iot.service;
|
2024-05-20 16:04:35 +08:00
|
|
|
|
import com.ruoyi.common.utils.NumberUtils;
|
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
2024-05-06 18:06:58 +08:00
|
|
|
|
import com.ruoyi.iot.constants.ReceiveConstants;
|
2024-06-04 15:13:00 +08:00
|
|
|
|
import com.ruoyi.iot.domain.IotDeviceDetail;
|
2024-05-06 18:06:58 +08:00
|
|
|
|
import com.ruoyi.iot.domain.ReceiveMsg;
|
|
|
|
|
import com.ruoyi.iot.enums.ReceiveType;
|
|
|
|
|
import com.ruoyi.ss.device.domain.SmDevice;
|
2024-05-20 16:04:35 +08:00
|
|
|
|
import com.ruoyi.ss.device.domain.enums.DeviceOutageWay;
|
2024-07-15 15:26:48 +08:00
|
|
|
|
import com.ruoyi.ss.device.service.DeviceService;
|
2024-05-06 18:06:58 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
2024-05-20 16:04:35 +08:00
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.math.RoundingMode;
|
|
|
|
|
|
2024-05-06 18:06:58 +08:00
|
|
|
|
/**
|
|
|
|
|
* @author wjh
|
|
|
|
|
* 2024/5/6
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class IotReceiveServiceImpl implements IotReceiveService{
|
|
|
|
|
|
|
|
|
|
@Autowired
|
2024-07-15 15:26:48 +08:00
|
|
|
|
private DeviceService deviceService;
|
2024-05-06 18:06:58 +08:00
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void handleReceive(ReceiveMsg msg) {
|
|
|
|
|
log.info("handleReceive {}", msg.toString());
|
2024-06-04 15:13:00 +08:00
|
|
|
|
// 数据点推送
|
2024-05-24 16:20:30 +08:00
|
|
|
|
if (ReceiveType.DATA_POINT.getType().equals(msg.getType())) {
|
2024-05-20 16:04:35 +08:00
|
|
|
|
if (ReceiveConstants.DS_SSID.equals(msg.getDsId())
|
|
|
|
|
&& ReceiveConstants.DSV_SSID_CT.equals((String) msg.getValue())) {
|
|
|
|
|
// 当数据点推送ssid的值为ChangteA时,录入设备
|
2024-05-22 15:50:24 +08:00
|
|
|
|
SmDevice device = this.parseToDevice(msg);
|
|
|
|
|
device.setDeviceName("未命名");
|
2024-07-09 11:17:20 +08:00
|
|
|
|
// deviceService.addInitDevice(device);
|
2024-05-20 16:04:35 +08:00
|
|
|
|
} else {
|
|
|
|
|
// 其他情况,更新设备信息
|
|
|
|
|
deviceService.updateByIot(this.parseToDevice(msg));
|
|
|
|
|
}
|
2024-05-06 18:06:58 +08:00
|
|
|
|
}
|
2024-06-04 15:13:00 +08:00
|
|
|
|
// 生命周期
|
|
|
|
|
else if (ReceiveType.DEVICE_STATUS.getType().equals(msg.getType())) {
|
|
|
|
|
deviceService.updateByIot(this.parseToDeviceByLife(msg));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生命周期转设备信息
|
|
|
|
|
private SmDevice parseToDeviceByLife(ReceiveMsg msg) {
|
|
|
|
|
SmDevice device = new SmDevice();
|
|
|
|
|
device.setMac(msg.getDevName());
|
|
|
|
|
device.setOnlineStatus(IotDeviceDetail.Status.toDeviceOnlineStatus(msg.getStatus()).getStatus());
|
|
|
|
|
return device;
|
2024-05-06 18:06:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 15:13:00 +08:00
|
|
|
|
// 数据点数据转设备信息
|
2024-05-06 18:06:58 +08:00
|
|
|
|
private SmDevice parseToDevice(ReceiveMsg msg) {
|
|
|
|
|
SmDevice device = new SmDevice();
|
|
|
|
|
device.setMac(msg.getDevName());
|
2024-05-20 16:04:35 +08:00
|
|
|
|
|
|
|
|
|
if (StringUtils.hasText(msg.getDsId())) {
|
|
|
|
|
String value = msg.getValue().toString();
|
|
|
|
|
switch(msg.getDsId()) {
|
|
|
|
|
case ReceiveConstants.DS_SSID:
|
|
|
|
|
device.setWifi(value);
|
|
|
|
|
break;
|
|
|
|
|
case ReceiveConstants.DS_V:
|
|
|
|
|
device.setVoltage(NumberUtils.nonNullDecimal(value));
|
|
|
|
|
break;
|
|
|
|
|
case ReceiveConstants.DS_P:
|
|
|
|
|
device.setRealTimePower(NumberUtils.nonNullDecimal(value));
|
|
|
|
|
break;
|
|
|
|
|
case ReceiveConstants.DS_A:
|
|
|
|
|
device.setElectricity(NumberUtils.nonNullDecimal(value));
|
|
|
|
|
break;
|
|
|
|
|
case ReceiveConstants.DS_W:
|
|
|
|
|
device.setTotalElectriQuantity(NumberUtils.nonNullDecimal(value).divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP));
|
|
|
|
|
break;
|
|
|
|
|
case ReceiveConstants.DS_S:
|
|
|
|
|
device.setPowerStatus(value);
|
|
|
|
|
break;
|
|
|
|
|
case ReceiveConstants.DS_SET:
|
|
|
|
|
DeviceOutageWay deviceOutageWay = DeviceOutageWay.parse(value);
|
|
|
|
|
device.setOutageWay(deviceOutageWay.getValue());
|
|
|
|
|
break;
|
|
|
|
|
case ReceiveConstants.DS_TIME:
|
|
|
|
|
device.setRemainTime(NumberUtils.nonNullDecimal(value));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 18:06:58 +08:00
|
|
|
|
return device;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|