package com.ruoyi.fuzhou.service.impl;
|
|
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.fuzhou.domain.DpEquipment;
|
import com.ruoyi.fuzhou.domain.ReceiveElectricityInfo;
|
import com.ruoyi.fuzhou.domain.ReceiveElectricityValue;
|
import com.ruoyi.fuzhou.domain.ReceiveOilValue;
|
import com.ruoyi.fuzhou.mapper.ReceiveElectricityInfoMapper;
|
import com.ruoyi.fuzhou.service.ReceiveElectricityInfoService;
|
import com.ruoyi.fuzhou.service.ReceiveElectricityValueService;
|
import com.ruoyi.fuzhou.utils.electricitymodbus.ModbusElectricityUtils;
|
import com.ruoyi.fuzhou.websocket.WebSocketOilServer;
|
import jakarta.annotation.Resource;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.stereotype.Service;
|
|
import java.util.Date;
|
import java.util.List;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* <p>
|
* 电数据接收管理 服务实现类
|
* </p>
|
*
|
* @author zhangyy
|
* @since 2025-03-14
|
*/
|
@Service
|
public class ReceiveElectricityInfoServiceImpl extends ServiceImpl<ReceiveElectricityInfoMapper, ReceiveElectricityInfo> implements ReceiveElectricityInfoService {
|
|
@Resource
|
private ReceiveElectricityValueService receiveElectricityValueService;
|
|
@Resource
|
private RedisTemplate redisTemplate;
|
|
@Override
|
public void saveElectricity(List<ReceiveElectricityInfo> list, DpEquipment dpEquipment) {
|
JSONObject jsonObject = ModbusElectricityUtils.getValue(list.get(0).getIp(), list.get(0).getPort(), list);
|
if (jsonObject != null && jsonObject.size() > 0) {
|
ReceiveElectricityValue receiveElectricityValue = JSON.toJavaObject(jsonObject, ReceiveElectricityValue.class);
|
receiveElectricityValue.setCreateTime(new Date());
|
receiveElectricityValue.setDeviceName(list.get(0).getDeviceName());
|
if (Math.abs(Double.valueOf(receiveElectricityValue.getCurrentA())) > 0 || Math.abs(Double.valueOf(receiveElectricityValue.getCurrentB())) > 0 ||
|
Math.abs(Double.valueOf(receiveElectricityValue.getCurrentC())) > 0) {
|
redisTemplate.opsForValue().set("VALUE@" + receiveElectricityValue.getDeviceName(), receiveElectricityValue.getCurrentA()
|
+ receiveElectricityValue.getCurrentB()
|
+ receiveElectricityValue.getCurrentC(), 30, TimeUnit.MINUTES);
|
receiveElectricityValueService.save(receiveElectricityValue);
|
} else {
|
Object obj = redisTemplate.opsForValue().get("VALUE@" + receiveElectricityValue.getDeviceName());
|
if (obj == null) {
|
redisTemplate.opsForValue().set("VALUE@" + receiveElectricityValue.getDeviceName(), receiveElectricityValue.getCurrentA()
|
+ receiveElectricityValue.getCurrentB()
|
+ receiveElectricityValue.getCurrentC(), 30, TimeUnit.MINUTES);
|
receiveElectricityValueService.save(receiveElectricityValue);
|
} else if (!obj.toString().equals(receiveElectricityValue.getCurrentA() + receiveElectricityValue.getCurrentB() + receiveElectricityValue.getCurrentC())) {
|
redisTemplate.opsForValue().set("VALUE@" + receiveElectricityValue.getDeviceName(), receiveElectricityValue.getCurrentA()
|
+ receiveElectricityValue.getCurrentB()
|
+ receiveElectricityValue.getCurrentC(), 30, TimeUnit.MINUTES);
|
receiveElectricityValueService.save(receiveElectricityValue);
|
}
|
}
|
}
|
//制造socket数据
|
// JSONArray header = new JSONArray();
|
// list.forEach(item -> {
|
// JSONObject head = new JSONObject();
|
// head.put("param", item.getParam());
|
// head.put("paramCode", item.getParamCode());
|
// head.put("unit", item.getUnit());
|
// header.add(head);
|
// });
|
// ReceiveElectricityValue receiveOilValue = receiveElectricityValueService.getOne(new LambdaQueryWrapper<ReceiveElectricityValue>() {{
|
// or().eq(ReceiveElectricityValue::getDeviceName, list.get(0).getDeviceName()).orderByDesc(ReceiveElectricityValue::getCreateTime).last("limit 1");
|
// }});
|
// if (receiveOilValue != null) {
|
// JSONObject object = new JSONObject();
|
// object.put("header", header);
|
// object.put("body", JSONObject.parseObject(JSON.toJSONString(receiveOilValue)));
|
// object.put("info", JSONObject.parseObject(JSON.toJSONString(dpEquipment)));
|
// object.put("status", "1");
|
// WebSocketOilServer.sendAllMessage(object.toJSONString());
|
// }
|
}
|
}
|