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)));
|
}
|
}
|