添加塔吊螺母信息bug修改

This commit is contained in:
guo 2023-07-31 18:39:20 +08:00
parent b4fe3478b7
commit 3f3bf3e5a4
3 changed files with 171 additions and 145 deletions

View File

@ -172,111 +172,9 @@ public class TowerNutController {
@ApiOperation(value = " 添加塔吊螺母信息", notes = "添加塔吊螺母信息", httpMethod = "POST") @ApiOperation(value = " 添加塔吊螺母信息", notes = "添加塔吊螺母信息", httpMethod = "POST")
@PostMapping(value = "/saveNutData") @PostMapping(value = "/saveNutData")
public void saveNutData(HttpServletRequest request) { public Result saveNutData(HttpServletRequest request) {
BufferedReader reader = null; towerNutService.saveNutData(request);
try { return Result.ok();
reader = request.getReader();
char[] buf = new char[512];
int len = 0;
StringBuffer contentBuffer = new StringBuffer();
while ((len = reader.read(buf)) != -1) {
contentBuffer.append(buf, 0, len);
}
String content = contentBuffer.toString();
log.info("--------塔吊螺母接收数据------:" + content);
JSONObject json = JSONObject.parseObject(content);
//螺母主机ID
String nutHostId = json.getString("sn");
JSONArray jsonArray = json.getJSONArray("values");
List<NutValue> list = jsonArray.toJavaList(NutValue.class);
if (list != null && list.size() > 0) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int nutNum = 0;
int offLineNum = 0;
int alarmNum = 0;
Map<Integer, NutObj> data = new LinkedHashMap<>();
for (NutValue value : list) {
//质量码 0未初始化值 1有效值 2无效值 3设值值 4计算值
//去除无效数值
if (value.getQds() == 0 || value.getQds() == 2) {
continue;
}
//值类型 0浮点 1整形
//去除无法识别的值类型
if (value.getVt() != 0 && value.getVt() != 1) {
continue;
}
int paramValue = value.getPara();
if (paramValue == 1) {
nutNum = value.getValue();
} else if (paramValue == 2) {
offLineNum = value.getValue();
} else if (paramValue == 3) {
alarmNum = value.getValue();
} else {
int nutNo = calculateNut(paramValue);
NutObj nutObj = data.get(nutNo);
if (nutObj == null) {
nutObj = new NutObj();
}
nutObj.setNutNo(nutNo);
int temp = paramValue - 100;
int remainder = temp % 3;
if (value.getVt() == 1) {
if (remainder == 0) {
nutObj.setAlarm(value.getValue());
nutObj.setAlarmTime(sdf.format(new Date(value.getTime())));
} else if (remainder == 1) {
nutObj.setOnLine(value.getValue());
} else if (remainder == 2) {
nutObj.setOffLine(value.getValue());
nutObj.setOffLineTime(sdf.format(new Date(value.getTime())));
}
}
data.put(nutNo, nutObj);
}
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("nutNum", nutNum);
jsonObject.put("offLineNum", offLineNum);
jsonObject.put("alarmNum", alarmNum);
jsonObject.put("list", data.values());
//数据库存储
TowerNut smVbean = new TowerNut();
smVbean.setNutHostId(nutHostId);
smVbean.setBeforeAnalysisData(content);
smVbean.setAfterAnalysisData(jsonObject.toString());
smVbean.setUpdateTime(new Date());
towerNutService.saveNutData(smVbean, data, nutNum, offLineNum, alarmNum);
}
} catch (IOException e) {
e.printStackTrace();
}
return;
} }
public static int calculateNut(int paramValue) {
int temp = paramValue - 100;
int trunc = temp / 3;
int remainder = temp % 3;
if (remainder == 0) {
return trunc;
} else {
return trunc + 1;
}
}
public static void main(String[] args) {
long mid = 868089057238341L;
long nutHostId = (mid) >> 32;
log.info("" + nutHostId);
int paramValue = (int) mid & 0xFFFFFFFF;
log.info("" + paramValue);
}
} }

View File

@ -6,20 +6,23 @@ import com.zhgd.jeecg.common.mybatis.EntityMap;
import com.zhgd.xmgl.base.NutObj; import com.zhgd.xmgl.base.NutObj;
import com.zhgd.xmgl.modules.bigdevice.entity.TowerNut; import com.zhgd.xmgl.modules.bigdevice.entity.TowerNut;
import javax.servlet.http.HttpServletRequest;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* @Description: 塔吊螺母 * @Description: 塔吊螺母
* @author pds * @author pds
* @date 2021-09-08 * @date 2021-09-08
* @version V1.0 * @version V1.0
*/ */
public interface ITowerNutService extends IService<TowerNut> { public interface ITowerNutService extends IService<TowerNut> {
void saveNutData(TowerNut towerNut, Map<Integer, NutObj> data,int nutNum,int offLineNum,int alarmNum); void saveNutData(TowerNut towerNut, Map<Integer, NutObj> data, int nutNum, int offLineNum, int alarmNum);
TowerNut selectTowerNutByNutHostId(String nutHostId); TowerNut selectTowerNutByNutHostId(String nutHostId);
List<EntityMap> selectTowerNutList(Map<String, Object> map); List<EntityMap> selectTowerNutList(Map<String, Object> map);
void saveNutData(HttpServletRequest request);
} }

View File

@ -3,16 +3,29 @@ package com.zhgd.xmgl.modules.bigdevice.service.impl;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhgd.jeecg.common.execption.OpenAlertException;
import com.zhgd.jeecg.common.mybatis.EntityMap; import com.zhgd.jeecg.common.mybatis.EntityMap;
import com.zhgd.xmgl.base.NutObj; import com.zhgd.xmgl.base.NutObj;
import com.zhgd.xmgl.base.NutValue;
import com.zhgd.xmgl.modules.bigdevice.entity.Tower;
import com.zhgd.xmgl.modules.bigdevice.entity.TowerNut; import com.zhgd.xmgl.modules.bigdevice.entity.TowerNut;
import com.zhgd.xmgl.modules.bigdevice.mapper.TowerMapper;
import com.zhgd.xmgl.modules.bigdevice.mapper.TowerNutMapper; import com.zhgd.xmgl.modules.bigdevice.mapper.TowerNutMapper;
import com.zhgd.xmgl.modules.bigdevice.service.ITowerNutService; import com.zhgd.xmgl.modules.bigdevice.service.ITowerNutService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -20,66 +33,69 @@ import java.util.Map;
/** /**
* @Description: 塔吊螺母 * @Description: 塔吊螺母
* @author pds * @author pds
* @date 2021-09-08 * @date 2021-09-08
* @version V1.0 * @version V1.0
*/ */
@Service @Service
@Slf4j
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public class TowerNutServiceImpl extends ServiceImpl<TowerNutMapper, TowerNut> implements ITowerNutService { public class TowerNutServiceImpl extends ServiceImpl<TowerNutMapper, TowerNut> implements ITowerNutService {
@Autowired
private TowerMapper towerMapper;
@Autowired @Autowired
private TowerNutMapper towerNutMapper; private TowerNutMapper towerNutMapper;
@Override @Override
public void saveNutData(TowerNut towerNut,Map<Integer, NutObj> data, int nutNum, int offLineNum, int alarmNum) { public void saveNutData(TowerNut towerNut, Map<Integer, NutObj> data, int nutNum, int offLineNum, int alarmNum) {
TowerNut oldTowerNut=towerNutMapper.selectTowerNutByHostId(towerNut.getNutHostId()); TowerNut oldTowerNut = towerNutMapper.selectTowerNutByHostId(towerNut.getNutHostId());
if(oldTowerNut!=null){ if (oldTowerNut != null) {
JSONObject jsonObject=JSONObject.parseObject(oldTowerNut.getAfterAnalysisData()); JSONObject jsonObject = JSONObject.parseObject(oldTowerNut.getAfterAnalysisData());
if(offLineNum>0){ if (offLineNum > 0) {
jsonObject.put("offLineNum",offLineNum); jsonObject.put("offLineNum", offLineNum);
} }
if(alarmNum>0){ if (alarmNum > 0) {
jsonObject.put("alarmNum",alarmNum); jsonObject.put("alarmNum", alarmNum);
} }
if(nutNum>0){ if (nutNum > 0) {
jsonObject.put("nutNum",nutNum); jsonObject.put("nutNum", nutNum);
} }
JSONArray jsonArray=jsonObject.getJSONArray("list"); JSONArray jsonArray = jsonObject.getJSONArray("list");
Integer totalAlarmNum=0; Integer totalAlarmNum = 0;
Integer totaloffLineNum=0; Integer totaloffLineNum = 0;
for (int i = 0; i < jsonArray.size(); i++) { for (int i = 0; i < jsonArray.size(); i++) {
JSONObject object=jsonArray.getJSONObject(i); JSONObject object = jsonArray.getJSONObject(i);
NutObj nutObj=data.get(object.getInteger("nutNo")); NutObj nutObj = data.get(object.getInteger("nutNo"));
if(nutObj!=null){ if (nutObj != null) {
if(nutObj.getAlarm()>0){ if (nutObj.getAlarm() > 0) {
object.put("alarm",nutObj.getAlarm()); object.put("alarm", nutObj.getAlarm());
object.put("offLine",nutObj.getAlarm()); object.put("offLine", nutObj.getAlarm());
} }
if(nutObj.getOffLine()>0){ if (nutObj.getOffLine() > 0) {
object.put("offLine",nutObj.getOffLine()); object.put("offLine", nutObj.getOffLine());
object.put("offLineTime",nutObj.getOffLineTime()); object.put("offLineTime", nutObj.getOffLineTime());
} }
if(nutObj.getOnLine()>0&&nutObj.getAlarm()==0&&nutObj.getOffLine()==0){ if (nutObj.getOnLine() > 0 && nutObj.getAlarm() == 0 && nutObj.getOffLine() == 0) {
object.put("alarm",0); object.put("alarm", 0);
object.put("alarmTime",""); object.put("alarmTime", "");
object.put("offLine",0); object.put("offLine", 0);
object.put("offLineTime",""); object.put("offLineTime", "");
object.put("onLine",nutObj.getOnLine()); object.put("onLine", nutObj.getOnLine());
}else{ } else {
if(nutObj.getOnLine()>0){ if (nutObj.getOnLine() > 0) {
object.put("onLine",nutObj.getOnLine()); object.put("onLine", nutObj.getOnLine());
} }
} }
totalAlarmNum=totalAlarmNum+object.getInteger("alarm"); totalAlarmNum = totalAlarmNum + object.getInteger("alarm");
totaloffLineNum=totaloffLineNum+object.getInteger("offLine"); totaloffLineNum = totaloffLineNum + object.getInteger("offLine");
} }
} }
jsonObject.put("alarmNum",totalAlarmNum); jsonObject.put("alarmNum", totalAlarmNum);
jsonObject.put("offLineNum",totaloffLineNum); jsonObject.put("offLineNum", totaloffLineNum);
jsonObject.put("list",jsonArray); jsonObject.put("list", jsonArray);
towerNut.setAfterAnalysisData(jsonObject.toString()); towerNut.setAfterAnalysisData(jsonObject.toString());
towerNut.setId(oldTowerNut.getId()); towerNut.setId(oldTowerNut.getId());
towerNutMapper.updateById(towerNut); towerNutMapper.updateById(towerNut);
}else{ } else {
towerNutMapper.insert(towerNut); towerNutMapper.insert(towerNut);
} }
} }
@ -94,4 +110,113 @@ public class TowerNutServiceImpl extends ServiceImpl<TowerNutMapper, TowerNut> i
return towerNutMapper.selectTowerNutList(map); return towerNutMapper.selectTowerNutList(map);
} }
@Override
public void saveNutData(HttpServletRequest request) {
BufferedReader reader = null;
try {
reader = request.getReader();
char[] buf = new char[512];
int len = 0;
StringBuffer contentBuffer = new StringBuffer();
while ((len = reader.read(buf)) != -1) {
contentBuffer.append(buf, 0, len);
}
String content = contentBuffer.toString();
log.info("--------塔吊螺母接收数据------:" + content);
JSONObject json = JSONObject.parseObject(content);
//螺母主机ID
String nutHostId = json.getString("sn");
Tower tower = towerMapper.selectOne(new LambdaQueryWrapper<Tower>().eq(Tower::getNutHostId, nutHostId));
if (StringUtils.isBlank(nutHostId) || tower == null) {
throw new OpenAlertException("螺母主机编号不存在");
}
JSONArray jsonArray = json.getJSONArray("values");
List<NutValue> list = jsonArray.toJavaList(NutValue.class);
if (list != null && list.size() > 0) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int nutNum = 0;
int offLineNum = 0;
int alarmNum = 0;
Map<Integer, NutObj> data = new LinkedHashMap<>();
for (NutValue value : list) {
//质量码 0未初始化值 1有效值 2无效值 3设值值 4计算值
//去除无效数值
if (value.getQds() == 0 || value.getQds() == 2) {
continue;
}
//值类型 0浮点 1整形
//去除无法识别的值类型
if (value.getVt() != 0 && value.getVt() != 1) {
continue;
}
int paramValue = value.getPara();
if (paramValue == 1) {
nutNum = value.getValue();
} else if (paramValue == 2) {
offLineNum = value.getValue();
} else if (paramValue == 3) {
alarmNum = value.getValue();
} else {
int nutNo = calculateNut(paramValue);
NutObj nutObj = data.get(nutNo);
if (nutObj == null) {
nutObj = new NutObj();
}
nutObj.setNutNo(nutNo);
int temp = paramValue - 100;
int remainder = temp % 3;
if (value.getVt() == 1) {
if (remainder == 0) {
nutObj.setAlarm(value.getValue());
nutObj.setAlarmTime(sdf.format(new Date(value.getTime())));
} else if (remainder == 1) {
nutObj.setOnLine(value.getValue());
} else if (remainder == 2) {
nutObj.setOffLine(value.getValue());
nutObj.setOffLineTime(sdf.format(new Date(value.getTime())));
}
}
data.put(nutNo, nutObj);
}
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("nutNum", nutNum);
jsonObject.put("offLineNum", offLineNum);
jsonObject.put("alarmNum", alarmNum);
jsonObject.put("list", data.values());
//数据库存储
TowerNut smVbean = new TowerNut();
smVbean.setNutHostId(nutHostId);
smVbean.setBeforeAnalysisData(content);
smVbean.setAfterAnalysisData(jsonObject.toString());
smVbean.setUpdateTime(new Date());
saveNutData(smVbean, data, nutNum, offLineNum, alarmNum);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static int calculateNut(int paramValue) {
int temp = paramValue - 100;
int trunc = temp / 3;
int remainder = temp % 3;
if (remainder == 0) {
return trunc;
} else {
return trunc + 1;
}
}
public static void main(String[] args) {
long mid = 868089057238341L;
long nutHostId = (mid) >> 32;
log.info("" + nutHostId);
int paramValue = (int) mid & 0xFFFFFFFF;
log.info("" + paramValue);
}
} }