13693261870
2025-07-02 6708810c4de34dfb9513061432d656f91d56ee3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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());
//        }
    }
}