diff --git a/src/main/java/com/zhgd/xmgl/modules/bigdevice/controller/TowerNutController.java b/src/main/java/com/zhgd/xmgl/modules/bigdevice/controller/TowerNutController.java index b31527674..32e6ce426 100644 --- a/src/main/java/com/zhgd/xmgl/modules/bigdevice/controller/TowerNutController.java +++ b/src/main/java/com/zhgd/xmgl/modules/bigdevice/controller/TowerNutController.java @@ -172,111 +172,9 @@ public class TowerNutController { @ApiOperation(value = " 添加塔吊螺母信息", notes = "添加塔吊螺母信息", httpMethod = "POST") @PostMapping(value = "/saveNutData") - 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"); - JSONArray jsonArray = json.getJSONArray("values"); - List 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 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 Result saveNutData(HttpServletRequest request) { + towerNutService.saveNutData(request); + return Result.ok(); } - 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); - } - - } diff --git a/src/main/java/com/zhgd/xmgl/modules/bigdevice/service/ITowerNutService.java b/src/main/java/com/zhgd/xmgl/modules/bigdevice/service/ITowerNutService.java index 44a53e533..c6e77ff4a 100644 --- a/src/main/java/com/zhgd/xmgl/modules/bigdevice/service/ITowerNutService.java +++ b/src/main/java/com/zhgd/xmgl/modules/bigdevice/service/ITowerNutService.java @@ -6,20 +6,23 @@ import com.zhgd.jeecg.common.mybatis.EntityMap; import com.zhgd.xmgl.base.NutObj; import com.zhgd.xmgl.modules.bigdevice.entity.TowerNut; +import javax.servlet.http.HttpServletRequest; import java.util.List; import java.util.Map; /** * @Description: 塔吊螺母 * @author: pds - * @date: 2021-09-08 + * @date: 2021-09-08 * @version: V1.0 */ public interface ITowerNutService extends IService { - void saveNutData(TowerNut towerNut, Map data,int nutNum,int offLineNum,int alarmNum); + void saveNutData(TowerNut towerNut, Map data, int nutNum, int offLineNum, int alarmNum); TowerNut selectTowerNutByNutHostId(String nutHostId); List selectTowerNutList(Map map); + + void saveNutData(HttpServletRequest request); } diff --git a/src/main/java/com/zhgd/xmgl/modules/bigdevice/service/impl/TowerNutServiceImpl.java b/src/main/java/com/zhgd/xmgl/modules/bigdevice/service/impl/TowerNutServiceImpl.java index b6ce8ab22..a4ca742dc 100644 --- a/src/main/java/com/zhgd/xmgl/modules/bigdevice/service/impl/TowerNutServiceImpl.java +++ b/src/main/java/com/zhgd/xmgl/modules/bigdevice/service/impl/TowerNutServiceImpl.java @@ -3,16 +3,29 @@ package com.zhgd.xmgl.modules.bigdevice.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.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.mapper.TowerMapper; import com.zhgd.xmgl.modules.bigdevice.mapper.TowerNutMapper; 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.stereotype.Service; 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.Map; @@ -20,66 +33,69 @@ import java.util.Map; /** * @Description: 塔吊螺母 * @author: pds - * @date: 2021-09-08 + * @date: 2021-09-08 * @version: V1.0 */ @Service +@Slf4j @Transactional(rollbackFor = Exception.class) public class TowerNutServiceImpl extends ServiceImpl implements ITowerNutService { + @Autowired + private TowerMapper towerMapper; @Autowired private TowerNutMapper towerNutMapper; @Override - public void saveNutData(TowerNut towerNut,Map data, int nutNum, int offLineNum, int alarmNum) { - TowerNut oldTowerNut=towerNutMapper.selectTowerNutByHostId(towerNut.getNutHostId()); - if(oldTowerNut!=null){ - JSONObject jsonObject=JSONObject.parseObject(oldTowerNut.getAfterAnalysisData()); - if(offLineNum>0){ - jsonObject.put("offLineNum",offLineNum); + public void saveNutData(TowerNut towerNut, Map data, int nutNum, int offLineNum, int alarmNum) { + TowerNut oldTowerNut = towerNutMapper.selectTowerNutByHostId(towerNut.getNutHostId()); + if (oldTowerNut != null) { + JSONObject jsonObject = JSONObject.parseObject(oldTowerNut.getAfterAnalysisData()); + if (offLineNum > 0) { + jsonObject.put("offLineNum", offLineNum); } - if(alarmNum>0){ - jsonObject.put("alarmNum",alarmNum); + if (alarmNum > 0) { + jsonObject.put("alarmNum", alarmNum); } - if(nutNum>0){ - jsonObject.put("nutNum",nutNum); + if (nutNum > 0) { + jsonObject.put("nutNum", nutNum); } - JSONArray jsonArray=jsonObject.getJSONArray("list"); - Integer totalAlarmNum=0; - Integer totaloffLineNum=0; + JSONArray jsonArray = jsonObject.getJSONArray("list"); + Integer totalAlarmNum = 0; + Integer totaloffLineNum = 0; for (int i = 0; i < jsonArray.size(); i++) { - JSONObject object=jsonArray.getJSONObject(i); - NutObj nutObj=data.get(object.getInteger("nutNo")); - if(nutObj!=null){ - if(nutObj.getAlarm()>0){ - object.put("alarm",nutObj.getAlarm()); - object.put("offLine",nutObj.getAlarm()); + JSONObject object = jsonArray.getJSONObject(i); + NutObj nutObj = data.get(object.getInteger("nutNo")); + if (nutObj != null) { + if (nutObj.getAlarm() > 0) { + object.put("alarm", nutObj.getAlarm()); + object.put("offLine", nutObj.getAlarm()); } - if(nutObj.getOffLine()>0){ - object.put("offLine",nutObj.getOffLine()); - object.put("offLineTime",nutObj.getOffLineTime()); + if (nutObj.getOffLine() > 0) { + object.put("offLine", nutObj.getOffLine()); + object.put("offLineTime", nutObj.getOffLineTime()); } - if(nutObj.getOnLine()>0&&nutObj.getAlarm()==0&&nutObj.getOffLine()==0){ - object.put("alarm",0); - object.put("alarmTime",""); - object.put("offLine",0); - object.put("offLineTime",""); - object.put("onLine",nutObj.getOnLine()); - }else{ - if(nutObj.getOnLine()>0){ - object.put("onLine",nutObj.getOnLine()); + if (nutObj.getOnLine() > 0 && nutObj.getAlarm() == 0 && nutObj.getOffLine() == 0) { + object.put("alarm", 0); + object.put("alarmTime", ""); + object.put("offLine", 0); + object.put("offLineTime", ""); + object.put("onLine", nutObj.getOnLine()); + } else { + if (nutObj.getOnLine() > 0) { + object.put("onLine", nutObj.getOnLine()); } } - totalAlarmNum=totalAlarmNum+object.getInteger("alarm"); - totaloffLineNum=totaloffLineNum+object.getInteger("offLine"); + totalAlarmNum = totalAlarmNum + object.getInteger("alarm"); + totaloffLineNum = totaloffLineNum + object.getInteger("offLine"); } } - jsonObject.put("alarmNum",totalAlarmNum); - jsonObject.put("offLineNum",totaloffLineNum); - jsonObject.put("list",jsonArray); + jsonObject.put("alarmNum", totalAlarmNum); + jsonObject.put("offLineNum", totaloffLineNum); + jsonObject.put("list", jsonArray); towerNut.setAfterAnalysisData(jsonObject.toString()); towerNut.setId(oldTowerNut.getId()); towerNutMapper.updateById(towerNut); - }else{ + } else { towerNutMapper.insert(towerNut); } } @@ -94,4 +110,113 @@ public class TowerNutServiceImpl extends ServiceImpl i 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().eq(Tower::getNutHostId, nutHostId)); + if (StringUtils.isBlank(nutHostId) || tower == null) { + throw new OpenAlertException("螺母主机编号不存在"); + } + JSONArray jsonArray = json.getJSONArray("values"); + List 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 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); + } + }