Commit a8248dce authored by 耿迪迪's avatar 耿迪迪

燃气设备修改 gengdidi

parent b3694a7e
......@@ -3,6 +3,9 @@ package com.zehong.gassdevicereport.netty;
import com.zehong.gassdevicereport.constant.Constant;
import com.zehong.gassdevicereport.entity.ReciveReportData;
import com.zehong.gassdevicereport.entity.TDeviceReportData;
import com.zehong.gassdevicereport.netty.decryptreported.flowmeter.DecryptReportedData;
import com.zehong.gassdevicereport.netty.decryptreported.flowmeter.FlowmeterDecryReportedDataStrategy;
import com.zehong.gassdevicereport.netty.decryptreported.PressureDecryptReportedData;
import com.zehong.gassdevicereport.service.ITDeviceReportDataService;
import com.zehong.gassdevicereport.utils.CrcUtil;
import com.zehong.gassdevicereport.utils.HexUtils;
......@@ -67,7 +70,7 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
* @throws UnsupportedEncodingException
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException{
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//msg为接收到的客户端传递的数据 个人这边直接传的json 数据
ByteBuf readMessage= (ByteBuf) msg;
String msgStr = readMessage.toString(CharsetUtil.UTF_8);
......@@ -83,12 +86,16 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
SocketChannel channel=(SocketChannel)ctx.channel();
int port = channel.localAddress().getPort();
TDeviceReportData tDeviceReportData;
TDeviceReportData tDeviceReportData = new TDeviceReportData();
if(7397 == port){
tDeviceReportData = new FlowmeterDecryptReportedData(msgStr,"nacangEVC300").getReportedData();
}else if (7398 == port){
tDeviceReportData = new FlowmeterDecryptReportedData(msgStr,"tianxinCPUcard").getReportedData();
}else{
DecryptReportedData decryptReportedData = FlowmeterDecryReportedDataStrategy.getDecryptReportedData(msgStr);
if(null == decryptReportedData){
logger.error("未获取到流量计类型");
throw new Exception("未获取到流量计类型");
}
tDeviceReportData = decryptReportedData.getReportedData();
}
if (7398 == port){
tDeviceReportData = new PressureDecryptReportedData(msgStr).getReportedData();
}
//缓存设备信息
......
package com.zehong.gassdevicereport.netty;
package com.zehong.gassdevicereport.netty.decryptreported;
import com.zehong.gassdevicereport.entity.TDeviceReportData;
import com.zehong.gassdevicereport.utils.HexUtils;
......@@ -6,6 +6,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.Date;
public class PressureDecryptReportedData {
......@@ -29,8 +30,11 @@ public class PressureDecryptReportedData {
//设备状态
String deviceStatus = getDeviceStatus();
tDeviceReportData.setDeviceStatus(deviceStatus);
//设备压力
float pressure = HexUtils.hexToFloat(reportedDataStr.substring(34, 42));
tDeviceReportData.setPressure(new BigDecimal(pressure));
//设备上报时间
tDeviceReportData.setReportTime(new Date());
return tDeviceReportData;
} catch (Exception e) {
logger.error("上报数据组装数据报错!"+e);
......@@ -50,7 +54,7 @@ public class PressureDecryptReportedData {
byte codeByte = HexUtils.hexToByte(code);
deviceNumStr.append(codeByte);
}
return deviceNum.toString();
return deviceNumStr.toString();
}
/**
......
package com.zehong.gassdevicereport.netty.decryptreported.flowmeter;
import com.zehong.gassdevicereport.entity.TDeviceReportData;
public interface DecryptReportedData {
TDeviceReportData getReportedData();
}
package com.zehong.gassdevicereport.netty.decryptreported.flowmeter;
public class FlowmeterDecryReportedDataStrategy {
public static DecryptReportedData getDecryptReportedData(String reportedDataStr){
return decryptReportedData(reportedDataStr.substring(reportedDataStr.length()-6,reportedDataStr.length()-4),reportedDataStr);
}
private static DecryptReportedData decryptReportedData(String hexStr,String reportedDataStr){
switch (hexStr){
case "B0":
return new FlowmeterDecryptReportedData(reportedDataStr,"苍南EVC300体积修正仪");
case "B1":
return new FlowmeterDecryptReportedData(reportedDataStr,"天信CPU卡式体积修正仪");
case "B2":
return new Tian5cFlowmeterDecryptReportedData(reportedDataStr,"天津市第五机床厂C型体积积算仪(天五C型)");
default:
return null;
}
}
}
package com.zehong.gassdevicereport.netty;
package com.zehong.gassdevicereport.netty.decryptreported.flowmeter;
import com.zehong.gassdevicereport.entity.TDeviceReportData;
import com.zehong.gassdevicereport.utils.HexUtils;
......@@ -10,7 +10,11 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FlowmeterDecryptReportedData {
/**
* @author zehong
* 接收苍南EVC300体积修正仪、天信CPU卡式体积修正仪上报数据
*/
public class FlowmeterDecryptReportedData implements DecryptReportedData{
private Logger logger = LoggerFactory.getLogger(FlowmeterDecryptReportedData.class);
......@@ -18,11 +22,12 @@ public class FlowmeterDecryptReportedData {
private String deviceType;
public FlowmeterDecryptReportedData(String reportedDataStr, String deviceType) {
public FlowmeterDecryptReportedData(String reportedDataStr,String deviceType) {
this.reportedDataStr = reportedDataStr;
this.deviceType = deviceType;
}
@Override
public TDeviceReportData getReportedData(){
try {
TDeviceReportData tDeviceReportData = new TDeviceReportData();
......@@ -64,7 +69,7 @@ public class FlowmeterDecryptReportedData {
/**
* 获取设备编号
* @return
* @return String
*/
private String getDeviceNum(){
String deviceNum = reportedDataStr.substring(0,36);
......@@ -74,12 +79,12 @@ public class FlowmeterDecryptReportedData {
byte codeByte = HexUtils.hexToByte(code);
deviceNumStr.append(codeByte);
}
return deviceNum.toString();
return deviceNumStr.toString();
}
/**
* 获取上报时间
* @return
* @return Date
* @throws ParseException
*/
private Date getReportedTime() throws ParseException {
......@@ -101,19 +106,19 @@ public class FlowmeterDecryptReportedData {
/**
* 通讯字
* @return
* @return String
*/
private String getCommunicationStatus(){
String communicationStatus = reportedDataStr.substring(112, 116);
StringBuilder communicationStatusStr = new StringBuilder("");
communicationStatusStr.append( HexUtils.hexToByte(communicationStatus.substring(0,2)));
communicationStatusStr.append( HexUtils.hexToByte(communicationStatus.substring(2,4)));
return communicationStatus.toString();
return communicationStatusStr.toString();
}
/**
* 设备状态字段 16进制转为二进制 高位或地位不足8位时左边补0
* @return
* @return String
*/
private String getDeviceStatus(){
String deviceStatus = reportedDataStr.substring(116, 120);
......
package com.zehong.gassdevicereport.netty.decryptreported.flowmeter;
import com.zehong.gassdevicereport.entity.TDeviceReportData;
import com.zehong.gassdevicereport.utils.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author zehong
* 接收天津市第五机床厂C型体积积算仪(天五C型)上报数据
*/
public class Tian5cFlowmeterDecryptReportedData implements DecryptReportedData{
private Logger logger = LoggerFactory.getLogger(Tian5cFlowmeterDecryptReportedData.class);
private String reportedDataStr;
private String deviceType;
public Tian5cFlowmeterDecryptReportedData(String reportedDataStr, String deviceType) {
this.reportedDataStr = reportedDataStr;
this.deviceType = deviceType;
}
@Override
public TDeviceReportData getReportedData(){
try {
TDeviceReportData tDeviceReportData = new TDeviceReportData();
//设备编号
tDeviceReportData.setDeviceNum(getDeviceNum());
//设备上报时间
Date reportedTime = getReportedTime();
tDeviceReportData.setReportTime(reportedTime);
// 标况累计流量
double standardConditionAccumulation = HexUtils.hexToFloat(reportedDataStr.substring(48, 56));
tDeviceReportData.setStandardConditionAccumulation(new BigDecimal(standardConditionAccumulation));
// 标况瞬时流量
float standardConditionFlow = HexUtils.hexToFloat(reportedDataStr.substring(56, 64));
tDeviceReportData.setStandardConditionFlow(new BigDecimal(standardConditionFlow));
// 压力
float pressure = HexUtils.hexToFloat(reportedDataStr.substring(64, 72));
tDeviceReportData.setPressure(new BigDecimal(pressure));
// 温度
float temperature = HexUtils.hexToFloat(reportedDataStr.substring(80, 88));
tDeviceReportData.setTemperature(new BigDecimal(temperature));
//通讯字段
String communicationStatus = getCommunicationStatus();
tDeviceReportData.setCommunicationStatus(communicationStatus);
tDeviceReportData.setDeviceStatus(deviceType);
return tDeviceReportData;
} catch (Exception e) {
logger.error("上报数据组装数据报错!"+e);
}
return null;
}
/**
* 获取设备编号
* @return
*/
private String getDeviceNum(){
String deviceNum = reportedDataStr.substring(0,36);
StringBuilder deviceNumStr = new StringBuilder("");
for(int i = 0; i < deviceNum.length(); i+=2){
String code = deviceNum.substring(i,i+2);
byte codeByte = HexUtils.hexToByte(code);
deviceNumStr.append(codeByte);
}
return deviceNumStr.toString();
}
/**
* 获取上报时间
* @return
* @throws ParseException
*/
private Date getReportedTime() throws ParseException {
String reportTime = reportedDataStr.substring(36,48);
StringBuilder timeStr = new StringBuilder("");
timeStr.append(reportTime.substring(0,2));
timeStr.append("-");
timeStr.append(reportTime.substring(2,4));
timeStr.append("-");
timeStr.append(reportTime.substring(4,6));
timeStr.append(" ");
timeStr.append(reportTime.substring(6,8));
timeStr.append(":");
timeStr.append(reportTime.substring(8,10));
timeStr.append(":");
timeStr.append(reportTime.substring(10,12));
return new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(timeStr.toString());
}
/**
* 通讯字
* @return
*/
private String getCommunicationStatus(){
String communicationStatus = reportedDataStr.substring(96, 100);
StringBuilder communicationStatusStr = new StringBuilder("");
communicationStatusStr.append( HexUtils.hexToByte(communicationStatus.substring(0,2)));
communicationStatusStr.append( HexUtils.hexToByte(communicationStatus.substring(2,4)));
return communicationStatusStr.toString();
}
}
......@@ -64,4 +64,4 @@ logging:
webSocektUrl: http://localhost:8903/gassafety/websocket/send
netty:
ports: 7397,7398,7399
ports: 7397,7398
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment