张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.thhy.mobile.modules.biz.temdevice.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thhy.general.utils.SpringContextUtils;
import com.thhy.mobile.modules.biz.pipeinfo.mapper.PipeInfoMapper;
import com.thhy.mobile.modules.biz.temdevice.entity.DeviceInfo;
import com.thhy.mobile.modules.biz.temdevice.entity.RestDto;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
public class TimeTask extends TimerTask {
 
    private PipeInfoMapper pipeInfoMapper;
 
    public PipeInfoMapper getPipeInfoMapper() {
        return SpringContextUtils.getBean(PipeInfoMapper.class);
    }
 
    @Override
    public void run() {
        PipeInfoMapper pipeInfoMapper = getPipeInfoMapper();
        TemTask.login();
        Map<String,Object> dataMap = TemTask.getDeviceList();
        String result = TemTask.getRealTimeData();
        JSONObject jsonObject = JSONObject.parseObject(result);
        if(!jsonObject.containsKey("code")||!"1000".equals(jsonObject.getString("code")))return;
        JSONArray dataArrayJSON = JSON.parseArray(jsonObject.getString("data"));
 
        for(Object obj : dataArrayJSON){
            JSONObject deviceJson = JSONObject.parseObject(obj.toString());
            String deviceAddr = deviceJson.getString("deviceAddr");
 
            String deviceStatus = deviceJson.getString("deviceStatus"); //normal:正常alarming:报警preAlarming:预警offline:离线
 
            //查询设备表,没有添加
            Map<String,DeviceInfo> map = pipeInfoMapper.queryDeviceBySn(deviceAddr);
 
            if(map==null||map.size()<1){
                continue;
            }
 
            DeviceInfo deviceInfoFirst = map.get(map.keySet().toArray()[0]);
            if(!deviceStatus.equals("offline")){
                if(deviceInfoFirst.getDeviceStatus()!=1){
                    pipeInfoMapper.updateDeviceStatus(1,deviceAddr);
                }
            }else{
                //设备离线
                if(deviceInfoFirst.getDeviceStatus()!=2){
                    pipeInfoMapper.updateDeviceStatus(2,deviceAddr);
                }
                continue;
            }
 
            //解析数据
            String timestamp = deviceJson.getString("timeStamp");
            Date checkTime = new Date(Long.parseLong(timestamp));
            JSONArray dataArray = JSONArray.parseArray(deviceJson.getString("dataItem"));
 
 
            /*for(DeviceInfo deviceInfo : deviceInfoList){
                //更新设备
                if(!deviceStatus.equals("offline")){
                    if(deviceInfo.getDeviceStatus()!=1){
                        deviceInfo.setDeviceStatus(1);
                        pipeInfoMapper.updateDeviceStatus(deviceInfo);
                    }
 
                }else{
                    //离线
                    if(deviceInfo.getDeviceStatus()!=2){
                        deviceInfo.setDeviceStatus(2);
                        pipeInfoMapper.updateDeviceStatus(deviceInfo);
                    }
                }
 
            }*/
            for(Object node : dataArray){
                JSONObject nodeJSON = JSONObject.parseObject(node.toString());
                int nodeId = nodeJSON.getIntValue("nodeId");
                JSONArray nodeDataJSON = JSONArray.parseArray(nodeJSON.getString("registerItem"));
 
                DeviceInfo deviceInfo = (DeviceInfo) map.get(nodeId);
                if(deviceInfo==null)continue;
                if("静养".equals(deviceInfo.getProduceOrder())){
                    RestDto restDto = new RestDto();
                    restDto.setDeviceId(deviceInfo.getDeviceId());
                    restDto.setCheckTime(checkTime);
                    for(Object reg : nodeDataJSON){
                        JSONObject regJson = JSONObject.parseObject(reg.toString());
                        int registerId = regJson.getIntValue("registerId");
                        String desc = dataMap.get(deviceAddr+":"+nodeId+":"+registerId).toString();
                        if(desc.contains("温度")){
                            restDto.setTem(new BigDecimal(regJson.getDouble("value")).setScale(8,RoundingMode.HALF_UP));
                        }else if(desc.contains("湿度")){
                            restDto.setHum(new BigDecimal(regJson.getDouble("value")).setScale(8,RoundingMode.HALF_UP));
                        }
                    }
                    pipeInfoMapper.insertRestData(restDto);
 
                }else if("水养管理".equals(deviceInfo.getProduceOrder())){
                    RestDto restDto = new RestDto();
                    restDto.setDeviceId(deviceInfo.getDeviceId());
                    restDto.setCheckTime(checkTime);
                    for(Object reg : nodeDataJSON){
                        JSONObject regJson = JSONObject.parseObject(reg.toString());
                        int registerId = regJson.getIntValue("registerId");
                        String desc = dataMap.get(deviceAddr+":"+nodeId+":"+registerId).toString();
                        if(desc.contains("温度")){
                            restDto.setTem(new BigDecimal(regJson.getDouble("value")).setScale(8,RoundingMode.HALF_UP));
                        }else if(desc.contains("PH")){
                            restDto.setPhValue(new BigDecimal(regJson.getDouble("value")).setScale(8,RoundingMode.HALF_UP));
                        }
                    }
                    pipeInfoMapper.insertWaterData(restDto);
                }
 
            }
        }
    }
 
    public static void main(String[] args) {
        String time = "1694745809385";
        System.out.println(new Date(Long.parseLong(time)));
    }
}