From b5a79dddd10d01a0ada82ae3a15273e4a00a6b16 Mon Sep 17 00:00:00 2001 From: zhaowg Date: Wed, 29 Aug 2018 17:52:30 +0800 Subject: [PATCH] 电信IOT插件 --- zteits-nbiot/VehicleDetector-ZTEITS-ZTEITS.iml | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/CRC16Util.java | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/ProtocolAdapterImpl.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/ZteitsReportProcess.java | 391 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 578 insertions(+), 0 deletions(-) create mode 100644 zteits-nbiot/VehicleDetector-ZTEITS-ZTEITS.iml create mode 100644 zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/CRC16Util.java create mode 100644 zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/ProtocolAdapterImpl.java create mode 100644 zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/ZteitsReportProcess.java diff --git a/zteits-nbiot/VehicleDetector-ZTEITS-ZTEITS.iml b/zteits-nbiot/VehicleDetector-ZTEITS-ZTEITS.iml new file mode 100644 index 0000000..5b07180 --- /dev/null +++ b/zteits-nbiot/VehicleDetector-ZTEITS-ZTEITS.iml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/CRC16Util.java b/zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/CRC16Util.java new file mode 100644 index 0000000..784f582 --- /dev/null +++ b/zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/CRC16Util.java @@ -0,0 +1,75 @@ +package com.zteits.nbiot.decoder; + +public class CRC16Util { + + /** + * 获取源数据和验证码的组合byte数组 + * + * @param strings 可变长度的十六进制字符串 + * @return + */ + public static byte[] appendCrc16(String... strings) { + byte[] data = new byte[]{}; + for (int i = 0; i < strings.length; i++) { + int x = Integer.parseInt(strings[i], 16); + byte n = (byte) x; + byte[] buffer = new byte[data.length + 1]; + byte[] aa = {n}; + System.arraycopy(data, 0, buffer, 0, data.length); + System.arraycopy(aa, 0, buffer, data.length, aa.length); + data = buffer; + } + return appendCrc16(data); + } + + /** + * 获取源数据和验证码的组合byte数组 + * + * @param aa 字节数组 + * @return + */ + public static byte[] appendCrc16(byte[] aa) { + byte[] bb = getCrc16(aa); + byte[] cc = new byte[aa.length + bb.length]; + System.arraycopy(aa, 0, cc, 0, aa.length); + System.arraycopy(bb, 0, cc, aa.length, bb.length); + return cc; + } + + /** + * 获取验证码byte数组,基于Modbus CRC16的校验算法 + */ + public static byte[] getCrc16(byte[] arr_buff) { + int len = arr_buff.length; + + // 预置 1 个 16 位的寄存器为十六进制FFFF, 称此寄存器为 CRC寄存器。 + int crc = 0xFFFF; + int i, j; + for (i = 0; i < len; i++) { + // 把第一个 8 位二进制数据 与 16 位的 CRC寄存器的低 8 位相异或, 把结果放于 CRC寄存器 + crc = ((crc & 0xFF00) | (crc & 0x00FF) ^ (arr_buff[i] & 0xFF)); + for (j = 0; j < 8; j++) { + // 把 CRC 寄存器的内容右移一位( 朝低位)用 0 填补最高位, 并检查右移后的移出位 + if ((crc & 0x0001) > 0) { + // 如果移出位为 1, CRC寄存器与多项式A001进行异或 + crc = crc >> 1; + crc = crc ^ 0xA001; + } else + // 如果移出位为 0,再次右移一位 + crc = crc >> 1; + } + } + return intToBytes(crc); + } + + /** + * 将int转换成byte数组,低位在前,高位在后 + * 改变高低位顺序只需调换数组序号 + */ + private static byte[] intToBytes(int value) { + byte[] src = new byte[2]; + src[1] = (byte) ((value >> 8) & 0xFF); + src[0] = (byte) (value & 0xFF); + return src; + } +} \ No newline at end of file diff --git a/zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/ProtocolAdapterImpl.java b/zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/ProtocolAdapterImpl.java new file mode 100644 index 0000000..e9b5afb --- /dev/null +++ b/zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/ProtocolAdapterImpl.java @@ -0,0 +1,59 @@ +package com.zteits.nbiot.decoder; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.huawei.m2m.cig.tup.modules.protocol_adapter.IProtocolAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class ProtocolAdapterImpl implements IProtocolAdapter { + + private static final Logger logger = LoggerFactory.getLogger(ProtocolAdapterImpl.class); + // 厂商名称 + private static final String MANU_FACTURERID = "ZTEITS"; + private static final String MODEL = "ZTEITS"; + + @Override + public String getManufacturerId() { + return MANU_FACTURERID; + } + + @Override + public String getModel() { + return MODEL; + } + + public void activate() { + logger.info("Codec demo HttpMessageHander activated."); + } + + public void deactivate() { + logger.info("Codec demo HttpMessageHander deactivated."); + } + + public byte[] encode(ObjectNode input) throws Exception { + logger.info("接收到平台的信息为 " + input.toString()); + try { + ZteitsCmdProcess cmdProcess = new ZteitsCmdProcess(input); + byte[] byteNode = cmdProcess.toByte(); + return byteNode; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public ObjectNode decode(byte[] binaryData) throws Exception { + try { + logger.info("接收到的报文:"+Utilty.parseByte2HexStr(binaryData)); + ZteitsReportProcess zteitsReportProcess = new ZteitsReportProcess(binaryData); + ObjectNode objectNode = zteitsReportProcess.toJsonNode(); + logger.info("dynamic zteitsReportProcess " + objectNode.toString()); + return objectNode; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + +} diff --git a/zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/ZteitsReportProcess.java b/zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/ZteitsReportProcess.java new file mode 100644 index 0000000..84d52f7 --- /dev/null +++ b/zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/ZteitsReportProcess.java @@ -0,0 +1,391 @@ +package com.zteits.nbiot.decoder; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import java.util.Arrays; + +public class ZteitsReportProcess { + + private String factoryCode; + private String model; + private int reportType; + private int nowTime1; + private int nowTime2; + private int state; + private int packCount; + private int voltage; + private int magId; + private int magRssi; + private int x; + private int y; + private int z; + private int version; + private int parkCnt; + private int sendCnt; + private int baseX; + private int baseY; + private int baseZ; + private int alarmInfo; + private int reserverd1; + private int reserverd2; + private int reserverd3; + private int reserverd4; + private int reserverd5; + private int reserverd6; + private int crc; + + public String getFactoryCode() + { + return this.factoryCode; + } + + public void setFactoryCode(String paramString) + { + this.factoryCode = paramString; + } + + public String getModel() + { + return this.model; + } + + public void setModel(String paramString) + { + this.model = paramString; + } + + public int getReportType() + { + return this.reportType; + } + + public void setReportType(int paramInt) + { + this.reportType = paramInt; + } + + public int getNowTime1() + { + return this.nowTime1; + } + + public void setNowTime1(int paramInt) + { + this.nowTime1 = paramInt; + } + + public int getNowTime2() + { + return this.nowTime2; + } + + public void setNowTime2(int paramInt) + { + this.nowTime2 = paramInt; + } + + public int getState() + { + return this.state; + } + + public void setState(int paramInt) + { + this.state = paramInt; + } + + public int getPackCount() + { + return this.packCount; + } + + public void setPackCount(int paramInt) + { + this.packCount = paramInt; + } + + public int getVoltage() + { + return this.voltage; + } + + public void setVoltage(int paramInt) + { + this.voltage = paramInt; + } + + public int getMagId() + { + return this.magId; + } + + public void setMagId(int paramInt) + { + this.magId = paramInt; + } + + public int getMagRssi() + { + return this.magRssi; + } + + public void setMagRssi(int paramInt) + { + this.magRssi = paramInt; + } + + public int getX() + { + return this.x; + } + + public void setX(int paramInt) + { + this.x = paramInt; + } + + public int getY() + { + return this.y; + } + + public void setY(int paramInt) + { + this.y = paramInt; + } + + public int getZ() + { + return this.z; + } + + public void setZ(int paramInt) + { + this.z = paramInt; + } + + public int getVersion() + { + return this.version; + } + + public void setVersion(int paramInt) + { + this.version = paramInt; + } + + public int getParkCnt() + { + return this.parkCnt; + } + + public void setParkCnt(int paramInt) + { + this.parkCnt = paramInt; + } + + public int getSendCnt() + { + return this.sendCnt; + } + + public void setSendCnt(int paramInt) + { + this.sendCnt = paramInt; + } + + public int getBaseX() + { + return this.baseX; + } + + public void setBaseX(int paramInt) + { + this.baseX = paramInt; + } + + public int getBaseY() + { + return this.baseY; + } + + public void setBaseY(int paramInt) + { + this.baseY = paramInt; + } + + public int getBaseZ() + { + return this.baseZ; + } + + public void setBaseZ(int paramInt) + { + this.baseZ = paramInt; + } + + public int getAlarmInfo() + { + return this.alarmInfo; + } + + public void setAlarmInfo(int paramInt) + { + this.alarmInfo = paramInt; + } + + public int getReserverd1() + { + return this.reserverd1; + } + + public void setReserverd1(int paramInt) + { + this.reserverd1 = paramInt; + } + + public int getReserverd2() + { + return this.reserverd2; + } + + public void setReserverd2(int paramInt) + { + this.reserverd2 = paramInt; + } + + public int getReserverd3() + { + return this.reserverd3; + } + + public void setReserverd3(int paramInt) + { + this.reserverd3 = paramInt; + } + + public int getReserverd4() + { + return this.reserverd4; + } + + public void setReserverd4(int paramInt) + { + this.reserverd4 = paramInt; + } + + public int getReserverd5() + { + return this.reserverd5; + } + + public void setReserverd5(int paramInt) + { + this.reserverd5 = paramInt; + } + + public int getReserverd6() + { + return this.reserverd6; + } + + public void setReserverd6(int paramInt) + { + this.reserverd6 = paramInt; + } + + public int getCrc() + { + return this.crc; + } + + public void setCrc(int paramInt) + { + this.crc = paramInt; + } + + public ZteitsReportProcess(byte[] binaryData) { + this.factoryCode = Utilty.hexStringToString(binaryData, 0, 4); + this.model = Utilty.hexStringToString(binaryData,4,8); + this.reportType = Utilty.hexStringToInteger(binaryData,8,9); + this.nowTime1 = Utilty.hexStringToInteger(binaryData,9,13); + this.nowTime2 = Utilty.hexStringToInteger(binaryData,13,17); + + this.state = Utilty.hexStringToInteger(binaryData,17,18); + + this.packCount = Utilty.hexStringToInteger(binaryData,18,20); + this.voltage = Utilty.hexStringToInteger(binaryData,20,22); + this.magId = Utilty.hexStringToInteger(binaryData,22,26); + this.magRssi = Utilty.hexStringToInteger(binaryData,26,27); + this.x = Utilty.hexStringToInteger(binaryData,27,29); + this.y = Utilty.hexStringToInteger(binaryData,29,31); + this.z = Utilty.hexStringToInteger(binaryData,31,33); + this.version = Utilty.hexStringToInteger(binaryData,33,35); + this.parkCnt = Utilty.hexStringToInteger(binaryData,35,37); + this.sendCnt = Utilty.hexStringToInteger(binaryData,37,40); + this.baseX = Utilty.hexStringToInteger(binaryData,40,42); + this.baseY = Utilty.hexStringToInteger(binaryData,42,44); + this.baseZ = Utilty.hexStringToInteger(binaryData,44,46); + this.alarmInfo = Utilty.hexStringToInteger(binaryData,46,47); + this.reserverd1 = Utilty.hexStringToInteger(binaryData,47,48); + this.reserverd2 = Utilty.hexStringToInteger(binaryData,48,49); + this.reserverd3 = Utilty.hexStringToInteger(binaryData,49,50); + this.reserverd4 = Utilty.hexStringToInteger(binaryData,50,52); + this.reserverd5 = Utilty.hexStringToInteger(binaryData,52,54); + this.reserverd6 = Utilty.hexStringToInteger(binaryData,54,58); + this.crc = Utilty.hexStringToInteger(binaryData,58,60); + } + + public ObjectNode toJsonNode() + { + ObjectMapper localObjectMapper = new ObjectMapper(); + ObjectNode localObjectNode1 = localObjectMapper.createObjectNode(); + localObjectNode1.put("msgType", "deviceReq"); + + ArrayNode localArrayNode = localObjectMapper.createArrayNode(); + + ObjectNode localObjectNode2 = localObjectMapper.createObjectNode(); + localObjectNode2 = localObjectMapper.createObjectNode(); + localObjectNode2.put("factoryCode", getFactoryCode()); + localObjectNode2.put("model", getModel()); + localObjectNode2.put("reportType", getReportType()); + localObjectNode2.put("reportTime1", getNowTime1()); + localObjectNode2.put("reportTime", getNowTime2()); + localObjectNode2.put("state", getState()); + localObjectNode2.put("packCount", getPackCount()); + localObjectNode2.put("voltage", getVoltage()); + localObjectNode2.put("magId", getMagId()); + localObjectNode2.put("deviceId", getMagId()); + localObjectNode2.put("rssi", getMagRssi()); + localObjectNode2.put("x", getX()); + localObjectNode2.put("y", getY()); + localObjectNode2.put("z", getZ()); + localObjectNode2.put("version", getVersion()); + localObjectNode2.put("parkCnt", getParkCnt()); + localObjectNode2.put("sendCnt", getSendCnt()); + localObjectNode2.put("baseX", getBaseX()); + localObjectNode2.put("baseY", getBaseY()); + localObjectNode2.put("baseZ", getBaseZ()); + localObjectNode2.put("alarmInfo", getAlarmInfo()); + localObjectNode2.put("reserverd1", getReserverd1()); + localObjectNode2.put("reserverd2", getReserverd2()); + localObjectNode2.put("reserverd3", getReserverd3()); + localObjectNode2.put("reserverd4", getReserverd4()); + localObjectNode2.put("reserverd5", getReserverd5()); + localObjectNode2.put("reserverd6", getReserverd6()); + localObjectNode2.put("crc", getCrc()); + + ObjectNode localObjectNode3 = localObjectMapper.createObjectNode(); + localObjectNode3.put("serviceId", "VehicleDetectorInfo"); + + localObjectNode3.set("serviceData", localObjectNode2); + + localArrayNode.add(localObjectNode3); + + localObjectNode1.set("data", localArrayNode); + + return localObjectNode1; + } +} \ No newline at end of file -- libgit2 0.21.4