From 8435cf1d25d07b09e2a9937d39bed1c1a422e4f0 Mon Sep 17 00:00:00 2001 From: jiazx0107@163.com <jiazx0107@163.com> Date: 星期四, 14 十二月 2023 14:10:45 +0800 Subject: [PATCH] 提交正来电子协议解析-1 --- src/main/java/com/fzzy/protocol/zldz/data/ReMessageBody.java | 18 src/main/java/com/fzzy/protocol/zldz/package-info.java | 4 src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java | 15 src/main/java/com/fzzy/protocol/zldz/data/Message.java | 52 + src/main/java/com/fzzy/protocol/zldz/util/CRC8.java | 182 ++++ src/main/java/com/fzzy/api/data/ApiCommonDevice.java | 2 src/main/java/com/fzzy/protocol/zldz/server/ZldzServerEngine.java | 85 ++ src/main/java/com/fzzy/gateway/entity/GatewayDevice.java | 28 src/main/java/com/fzzy/protocol/fzzy/service/FzzyGatewayGrainService.java | 3 src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisService.java | 169 ++++ src/main/java/com/fzzy/protocol/bhzn/service/Bhzn2GatewayGrainService.java | 7 src/main/java/com/fzzy/protocol/zldz/service/ZldzGatewayGrainService.java | 147 +++ src/main/java/com/fzzy/protocol/fzzy/server/ServerUtils.java | 19 src/main/java/com/fzzy/protocol/zldz/data/MessageBody.java | 13 src/main/java/com/fzzy/api/utils/ContextUtil.java | 6 src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder8815.java | 41 + src/main/java/com/fzzy/protocol/zldz/server/HeartbeatProviderImpl.java | 25 src/main/java/com/fzzy/protocol/zldz/data/Metadata.java | 18 src/main/java/com/fzzy/protocol/zldz/data/ReMessage.java | 74 + src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder1115.java | 85 ++ src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java | 11 src/main/java/com/fzzy/protocol/zldz/cmd/ReMessageBuilder.java | 131 +++ src/main/java/com/fzzy/protocol/data/THDto.java | 6 src/main/java/com/fzzy/protocol/fzzy/analysis/AnalysisGrain.java | 3 src/main/java/com/fzzy/protocol/zldz/util/CRC16.java | 129 +++ src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder.java | 144 +++ src/main/java/com/fzzy/protocol/zldz/server/MsgConsumer.java | 44 + src/main/java/com/fzzy/protocol/zldz/server/SessionListener.java | 59 + /dev/null | 15 src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder8817.java | 54 + src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisTH.java | 87 ++ src/main/java/com/fzzy/api/utils/BytesUtil.java | 8 src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java | 384 +++++++++ src/main/java/com/fzzy/protocol/zldz/server/ZldzServerUtil.java | 58 + src/main/java/com/fzzy/protocol/ProtocolUtils.java | 55 + src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java | 4 src/main/java/com/fzzy/protocol/bhzn/server/BhznGrainV2ServerEngine.java | 2 src/main/java/com/fzzy/protocol/bhzn/server/BhznGrainV2ServerUtils.java | 19 src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisConf.java | 184 ++++ 39 files changed, 2,319 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/fzzy/api/data/ApiCommonDevice.java b/src/main/java/com/fzzy/api/data/ApiCommonDevice.java index 52240c4..9cde079 100644 --- a/src/main/java/com/fzzy/api/data/ApiCommonDevice.java +++ b/src/main/java/com/fzzy/api/data/ApiCommonDevice.java @@ -20,6 +20,8 @@ private Integer port; + private String orgId; + //鐘舵�佹槸鍚﹀湪绾� private String status = Constant.YN_Y; diff --git a/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java b/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java index 99631d8..1cd7427 100644 --- a/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java +++ b/src/main/java/com/fzzy/api/data/GatewayDeviceProtocol.java @@ -13,7 +13,8 @@ TCP_FZZY_V3("TCP_FZZY_V3", "绮儏-椋庢鑷磋繙鎺у埗鏌滃崗璁甐3"), GRAIN_FZZY_IGDS_V40("GRAIN_FZZY_IGDS_V40", "绮儏-椋庢鑷磋繙绯荤粺V40"), - GRAIN_FZZY_BHZN_WEB("GRAIN_FZZY_BHZN_WEB", "绮儏-椋庢鑷磋繙閭︽捣缃戝彛鍗忚"), + GRAIN_FZZY_BHZN_WEB("GRAIN_FZZY_BHZN_WEB", "绮儏-FZZY-閭︽捣鏅鸿兘缃戝彛鍗忚"), + GRAIN_FZZY_ZLDZ_WEB("GRAIN_FZZY_ZLDZ_WEB", "绮儏-FZZY-姝f潵鐢靛瓙缃戝彛鍗忚"), DEVICE_WEIGHT_HTTP("DEVICE_WEIGHT_HTTP", "鍦扮-HTTP鍗忚"), DEVICE_WEIGHT_TCP_YH("DEVICE_WEIGHT_TCP_YH", "鍦扮-鑰�鍗嶵CP鍗忚"), DEVICE_IDCARD_HTTP("DEVICE_IDCARD_HTTP", "韬唤璇�-HTTP鍗忚"), @@ -30,6 +31,7 @@ list.add(new ApiTrigger(TCP_FZZY_V3.getCode(), TCP_FZZY_V3.getName())); list.add(new ApiTrigger(GRAIN_FZZY_IGDS_V40.getCode(), GRAIN_FZZY_IGDS_V40.getName())); list.add(new ApiTrigger(GRAIN_FZZY_BHZN_WEB.getCode(), GRAIN_FZZY_BHZN_WEB.getName())); + list.add(new ApiTrigger(GRAIN_FZZY_ZLDZ_WEB.getCode(), GRAIN_FZZY_ZLDZ_WEB.getName())); list.add(new ApiTrigger(DEVICE_WEIGHT_HTTP.getCode(), DEVICE_WEIGHT_HTTP.getName())); list.add(new ApiTrigger(DEVICE_WEIGHT_TCP_YH.getCode(), DEVICE_WEIGHT_TCP_YH.getName())); list.add(new ApiTrigger(DEVICE_IDCARD_HTTP.getCode(), DEVICE_IDCARD_HTTP.getName())); diff --git a/src/main/java/com/fzzy/api/utils/BytesUtil.java b/src/main/java/com/fzzy/api/utils/BytesUtil.java index 37a1ee6..b62f453 100644 --- a/src/main/java/com/fzzy/api/utils/BytesUtil.java +++ b/src/main/java/com/fzzy/api/utils/BytesUtil.java @@ -461,4 +461,12 @@ } return biannary2Decimal(two) + 1; } + + public static String byteToHex(byte b) { + String hex = Integer.toHexString(b & 0xFF); + if (hex.length() < 2) { + hex = "0" + hex; + } + return hex; + } } diff --git a/src/main/java/com/fzzy/api/utils/ContextUtil.java b/src/main/java/com/fzzy/api/utils/ContextUtil.java index 287452f..b2233a5 100644 --- a/src/main/java/com/fzzy/api/utils/ContextUtil.java +++ b/src/main/java/com/fzzy/api/utils/ContextUtil.java @@ -3,6 +3,7 @@ import com.fzzy.api.Constant; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.DateUtils; +import org.apache.commons.lang3.time.DateFormatUtils; import java.util.*; @@ -189,4 +190,9 @@ public static String buildDeviceStatusKey(String companyId, String serId, int passCode) { return companyId + "_" + serId + "_STATUS_" + passCode; } + + + public static String getDefaultBatchId() { + return DateFormatUtils.format(new Date(), "yyyyMMddHHmm"); + } } diff --git a/src/main/java/com/fzzy/gateway/entity/GatewayDevice.java b/src/main/java/com/fzzy/gateway/entity/GatewayDevice.java index 4670371..b69ac87 100644 --- a/src/main/java/com/fzzy/gateway/entity/GatewayDevice.java +++ b/src/main/java/com/fzzy/gateway/entity/GatewayDevice.java @@ -107,9 +107,7 @@ @PropertyDef(label = "浣嶇疆") private String position; - @Column(name = "REMARK_", length = 200) - @PropertyDef(label = "澶囨敞", description = "澶囨敞淇℃伅") - private String remark; + @Column(name = "CABLE_RULE_", length = 20) @PropertyDef(label = "甯冪嚎瑙勫垯", description = "骞虫柟浠撹〃绀哄眰琛屽垪锛岀瓛浠撹〃绀烘瘡鍦堢殑鍒楁暟") @@ -123,8 +121,32 @@ @PropertyDef(label = "鍦ㄧ嚎鐘舵��") private String status = Constant.YN_Y; + @Column(name = "CABLE_START_") + @PropertyDef(label = "鐢电紗寮�濮�") + private int cableStart; + + @Column(name = "CABLE_END_") + @PropertyDef(label = "鐢电紗鎴嚦") + private int cableEnd; + + @Column(name = "TH_ADDR_") + @PropertyDef(label = "娓╂箍搴﹀湴鍧�") + private String thAddr; + + @Column(name = "CABLE_FORMAT", length = 4) + @PropertyDef(label = "鐢电紗鍒跺紡") + private String cableFormat; + + @Column(name = "CABLE_TYPE", length = 4) + @PropertyDef(label = "鐢电紗绫诲瀷") + private String cableType; + @Transient @PropertyDef(label = "璁惧涓婄嚎鏃堕棿") private Date onlineTime; + @Column(name = "REMARK_", length = 200) + @PropertyDef(label = "澶囨敞", description = "澶囨敞淇℃伅") + private String remark; + } diff --git a/src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java b/src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java index 96f40ed..4bbe59a 100644 --- a/src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java +++ b/src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java @@ -92,6 +92,15 @@ flushCache(); } + public void updateData(GatewayDevice device) { + device.setStatus(Constant.YN_Y); + + gatewayDeviceRep.save(device); + + flushCacheOne(device); + } + + /** * gatewayDeviceService#delData * @@ -115,6 +124,10 @@ for (GatewayDevice device : list) { GatewayUtils.add2Cache(device); } + } + + private void flushCacheOne(GatewayDevice device) { + GatewayUtils.add2Cache(device); } @@ -154,4 +167,6 @@ GatewayUtils.add2Cache(device); } } + + } diff --git a/src/main/java/com/fzzy/protocol/ConstantProtocol.java b/src/main/java/com/fzzy/protocol/ConstantProtocol.java deleted file mode 100644 index c1f168d..0000000 --- a/src/main/java/com/fzzy/protocol/ConstantProtocol.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.fzzy.protocol; - - -/** - * 鍗忚瑙f瀽鍏ㄥ眬甯搁噺 - */ -public class ConstantProtocol { - - //-绮俯澶囩敤鐐� - public static final double ERROR_TEMP = -100; - //-绮俯鏁呴殰鐐� - public static final double FAULT_TEMP = -101; - //-绮俯琛ュ伩鐐�-閽堝閿ュ舰浠撹ˉ鐐逛娇鐢� - public static final double ADD_TEMP = -102; -} diff --git a/src/main/java/com/fzzy/protocol/ProtocolUtils.java b/src/main/java/com/fzzy/protocol/ProtocolUtils.java new file mode 100644 index 0000000..6630c7c --- /dev/null +++ b/src/main/java/com/fzzy/protocol/ProtocolUtils.java @@ -0,0 +1,55 @@ +package com.fzzy.protocol; + + +import com.fzzy.gateway.data.BaseReqData; +import com.fzzy.protocol.data.THDto; + +import java.util.HashMap; +import java.util.Map; + +/** + * 鍗忚瑙f瀽鍏ㄥ眬甯搁噺 + */ +public class ProtocolUtils { + + /** + * 璇锋眰缂撳瓨 key = deviceId,value = requData + */ + private static Map<String, BaseReqData> contextSyncReqMap = new HashMap<>(); + + /** + * 璇锋眰缂撳瓨 key = deviceId,value = thDto + */ + private static Map<String, THDto> contextThMap = new HashMap<>(); + + + //-绮俯澶囩敤鐐� + public static final double ERROR_TEMP = -100; + //-绮俯鏁呴殰鐐� + public static final double FAULT_TEMP = -101; + //-绮俯琛ュ伩鐐�-閽堝閿ュ舰浠撹ˉ鐐逛娇鐢� + public static final double ADD_TEMP = -102; + + + /** + * 灏嗘墽琛屽懡浠や俊鎭瓨鏀惧湪缂撳瓨 + * + * @param depotId 浠撳簱缂栫爜 + * @param reqData 璇锋眰鍙傛暟淇℃伅 + */ + public static void addSyncReq2Map(String depotId, BaseReqData reqData) { + contextSyncReqMap.put(depotId, reqData); + } + + public static BaseReqData getSyncReq(String depotId) { + return contextSyncReqMap.get(depotId); + } + + public static void addTh2Map(String deviceId, THDto thDto) { + contextThMap.put(deviceId, thDto); + } + + public static THDto getCacheTh(String deviceId) { + return contextThMap.get(deviceId); + } +} diff --git a/src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java b/src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java index 7aca558..fb8b9ba 100644 --- a/src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java +++ b/src/main/java/com/fzzy/protocol/bhzn/analysis/AnalysisService.java @@ -14,14 +14,13 @@ import com.fzzy.gateway.entity.GatewayDevice; import com.fzzy.gateway.hx2023.ScConstant; import com.fzzy.gateway.hx2023.data.*; -import com.fzzy.protocol.ConstantProtocol; +import com.fzzy.protocol.ProtocolUtils; import com.fzzy.protocol.bhzn.cmd.CommandBuild; import com.fzzy.protocol.bhzn.cmd.ReMessageBuilder; import com.fzzy.protocol.bhzn.data.IoMessage; -import com.fzzy.protocol.bhzn.data.THDto; +import com.fzzy.protocol.data.THDto; import com.fzzy.protocol.bhzn.server.BhznGrainV2ServerEngine; import com.fzzy.protocol.bhzn.server.BhznGrainV2ServerUtils; -import com.fzzy.protocol.fzzy.server.ServerUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -116,7 +115,7 @@ } //鑾峰彇璇锋眰淇℃伅 - BaseReqData reqData = BhznGrainV2ServerUtils.getSyncReq(gatewayDevice.getDepotIdSys()); + BaseReqData reqData = ProtocolUtils.getSyncReq(gatewayDevice.getDepotIdSys()); if (null == reqData) { replayGrain(message); log.error("涓绘満-------->>骞冲彴锛岃В鏋愮伯鎯呭け璐ワ紝鏈幏鍙栧埌绮儏璇锋眰淇℃伅锛�" + message.getAddr()); @@ -216,13 +215,13 @@ temp = "0000"; } if (ReMessageBuilder.ERROR_TAG.equals(temp)) { - tempValue = ConstantProtocol.ERROR_TEMP; + tempValue = ProtocolUtils.ERROR_TEMP; } else { tempValue = BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) / 10.0; } // 鏁呴殰鍊煎鐞� if (tempValue >= ReMessageBuilder.FAULT_CHECK_TAG) { - tempValue = ConstantProtocol.FAULT_TEMP; + tempValue = ProtocolUtils.FAULT_TEMP; } temps.add(tempValue); } diff --git a/src/main/java/com/fzzy/protocol/bhzn/server/BhznGrainV2ServerEngine.java b/src/main/java/com/fzzy/protocol/bhzn/server/BhznGrainV2ServerEngine.java index f56c482..2a6ee94 100644 --- a/src/main/java/com/fzzy/protocol/bhzn/server/BhznGrainV2ServerEngine.java +++ b/src/main/java/com/fzzy/protocol/bhzn/server/BhznGrainV2ServerEngine.java @@ -17,7 +17,7 @@ @Service public class BhznGrainV2ServerEngine { - public static final Integer PORT = 19309; + public static final Integer PORT = 19301; public static NettyServer ioServer = null; diff --git a/src/main/java/com/fzzy/protocol/bhzn/server/BhznGrainV2ServerUtils.java b/src/main/java/com/fzzy/protocol/bhzn/server/BhznGrainV2ServerUtils.java index 68a7acd..2bec80a 100644 --- a/src/main/java/com/fzzy/protocol/bhzn/server/BhznGrainV2ServerUtils.java +++ b/src/main/java/com/fzzy/protocol/bhzn/server/BhznGrainV2ServerUtils.java @@ -21,10 +21,6 @@ */ public static Map<String, Long> contextMapHeart = new HashMap<>(); - /** - * 璇锋眰缂撳瓨 key = deviceId,value = requData - */ - private static Map<String, BaseReqData> contextSyncReqMap = new HashMap<>(); @@ -49,21 +45,6 @@ public static String FUNCTION_ID_83 = "83"; public static String FUNCTION_ID_93 = "93"; public static String FUNCTION_ID_92 = "92"; - - - /** - * 灏嗘墽琛屽懡浠や俊鎭瓨鏀惧湪缂撳瓨 - * - * @param depotId 浠撳簱缂栫爜 - * @param reqData 璇锋眰鍙傛暟淇℃伅 - */ - public static void addSyncReq2Map(String depotId, BaseReqData reqData) { - contextSyncReqMap.put(depotId, reqData); - } - - public static BaseReqData getSyncReq(String depotId) { - return contextSyncReqMap.get(depotId); - } /** diff --git a/src/main/java/com/fzzy/protocol/bhzn/service/Bhzn2GatewayGrainService.java b/src/main/java/com/fzzy/protocol/bhzn/service/Bhzn2GatewayGrainService.java index 0cde399..76d3852 100644 --- a/src/main/java/com/fzzy/protocol/bhzn/service/Bhzn2GatewayGrainService.java +++ b/src/main/java/com/fzzy/protocol/bhzn/service/Bhzn2GatewayGrainService.java @@ -8,15 +8,18 @@ import com.fzzy.gateway.data.BaseReqData; import com.fzzy.gateway.data.BaseResp; import com.fzzy.gateway.entity.GatewayDevice; +import com.fzzy.io.request.BaseRequest; +import com.fzzy.protocol.ProtocolUtils; import com.fzzy.protocol.bhzn.cmd.CommandBuild; import com.fzzy.protocol.bhzn.server.BhznGrainV2ServerEngine; import com.fzzy.protocol.bhzn.server.BhznGrainV2ServerUtils; +import com.fzzy.protocol.zldz.data.Message; import com.ld.io.api.InvokeResult; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; /** - * 椋庢鑷磋繙鎺у埗鏌滃崗璁� + * 閭︽捣鏅鸿兘绮儏鍗忚-缃戝彛璁惧 */ @Slf4j @Component @@ -58,7 +61,7 @@ try { //Step 璇锋眰淇℃伅鏀惧叆鍐呭瓨 - BhznGrainV2ServerUtils.addSyncReq2Map(device.getDepotIdSys(), reqData); + ProtocolUtils.addSyncReq2Map(device.getDepotIdSys(), reqData); // 鐢熸垚绮儏淇℃伅 String hexStr = CommandBuild.getInstance().getMsgCheck(device.getDeviceSn(), device.getDepotIdSys()); diff --git a/src/main/java/com/fzzy/protocol/bhzn/data/THDto.java b/src/main/java/com/fzzy/protocol/data/THDto.java similarity index 83% rename from src/main/java/com/fzzy/protocol/bhzn/data/THDto.java rename to src/main/java/com/fzzy/protocol/data/THDto.java index b7fdfbf..894a9d6 100644 --- a/src/main/java/com/fzzy/protocol/bhzn/data/THDto.java +++ b/src/main/java/com/fzzy/protocol/data/THDto.java @@ -1,4 +1,4 @@ -package com.fzzy.protocol.bhzn.data; +package com.fzzy.protocol.data; import com.bstek.dorado.annotation.PropertyDef; import lombok.Data; @@ -20,7 +20,7 @@ private String serId;//鎵�灞炲垎鏈� - private String thConf = "1";//鎵�灞為�氶亾 + private String thAddr = "1";//鎵�灞為�氶亾 @PropertyDef(label = "鎵�灞炰粨搴�", description = "浠撳簱缂栧彿") private String depotId; @@ -31,7 +31,7 @@ @PropertyDef(label = "浠撳唴婀垮害", description = "鍗曚綅%RH") private Double humidityIn; - @PropertyDef(label = "鎺ユ敹鏃堕棿", description = "") + @PropertyDef(label = "鎺ユ敹鏃堕棿") private Date receiveDate = new Date(); private String remark; diff --git a/src/main/java/com/fzzy/protocol/fzzy/analysis/AnalysisGrain.java b/src/main/java/com/fzzy/protocol/fzzy/analysis/AnalysisGrain.java index 0c70f31..0c8346d 100644 --- a/src/main/java/com/fzzy/protocol/fzzy/analysis/AnalysisGrain.java +++ b/src/main/java/com/fzzy/protocol/fzzy/analysis/AnalysisGrain.java @@ -13,6 +13,7 @@ import com.fzzy.gateway.entity.GatewayDevice; import com.fzzy.gateway.hx2023.ScConstant; import com.fzzy.gateway.hx2023.data.*; +import com.fzzy.protocol.ProtocolUtils; import com.fzzy.protocol.fzzy.builder.SimpleCommandBuilder; import com.fzzy.protocol.fzzy.cmd.BaseRemoteImpl; import com.fzzy.protocol.fzzy.data.ReMessage; @@ -93,7 +94,7 @@ private void buildBizInfo1(Response2102 response) { //鑾峰彇璇锋眰淇℃伅 - BaseReqData reqData = ServerUtils.getSyncReq(response.getHouseId()); + BaseReqData reqData = ProtocolUtils.getSyncReq(response.getHouseId()); if (null == reqData) { log.error("--绮儏灏佽瑙f瀽锛屾湭鑾峰彇鍒拌姹傜浉鍏冲弬鏁�----"); diff --git a/src/main/java/com/fzzy/protocol/fzzy/server/ServerUtils.java b/src/main/java/com/fzzy/protocol/fzzy/server/ServerUtils.java index f6d8633..a4e3755 100644 --- a/src/main/java/com/fzzy/protocol/fzzy/server/ServerUtils.java +++ b/src/main/java/com/fzzy/protocol/fzzy/server/ServerUtils.java @@ -16,25 +16,6 @@ private static Map<String, Integer> contextOrder = new HashMap<>(); - /** - * 璇锋眰缂撳瓨 key = deviceId,value = requData - */ - private static Map<String, BaseReqData> contextSyncReqMap = new HashMap<>(); - - /** - * 灏嗘墽琛屽懡浠や俊鎭瓨鏀惧湪缂撳瓨 - * - * @param depotId 浠撳簱缂栫爜 - * @param reqData 璇锋眰鍙傛暟淇℃伅 - */ - public static void addSyncReq2Map(String depotId, BaseReqData reqData) { - contextSyncReqMap.put(depotId, reqData); - } - - public static BaseReqData getSyncReq(String depotId) { - return contextSyncReqMap.get(depotId); - } - public static final String MSG_BEGIN = "<FZZY>"; public static final String MSG_END = "<END>"; diff --git a/src/main/java/com/fzzy/protocol/fzzy/service/FzzyGatewayGrainService.java b/src/main/java/com/fzzy/protocol/fzzy/service/FzzyGatewayGrainService.java index 38e74e7..bf7e2c3 100644 --- a/src/main/java/com/fzzy/protocol/fzzy/service/FzzyGatewayGrainService.java +++ b/src/main/java/com/fzzy/protocol/fzzy/service/FzzyGatewayGrainService.java @@ -7,6 +7,7 @@ import com.fzzy.gateway.data.BaseReqData; import com.fzzy.gateway.data.BaseResp; import com.fzzy.gateway.entity.GatewayDevice; +import com.fzzy.protocol.ProtocolUtils; import com.fzzy.protocol.fzzy.builder.GrainCommandBuilder; import com.fzzy.protocol.fzzy.cmd.BaseRemoteImpl; import com.fzzy.protocol.fzzy.data.SendMessage; @@ -60,7 +61,7 @@ try { //Step 璇锋眰淇℃伅鏀惧叆鍐呭瓨 - ServerUtils.addSyncReq2Map(device.getDepotIdSys(), reqData); + ProtocolUtils.addSyncReq2Map(device.getDepotIdSys(), reqData); // Step1 鐢熸垚绮儏淇℃伅 SendMessage message = GrainCommandBuilder.getInstance().buildMessage(device,apiCommonDevice); diff --git a/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisConf.java b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisConf.java new file mode 100644 index 0000000..ed5d024 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisConf.java @@ -0,0 +1,184 @@ +package com.fzzy.protocol.zldz.analysis; + +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.gateway.entity.GatewayDevice; +import com.fzzy.gateway.service.GatewayDeviceService; +import com.fzzy.protocol.zldz.data.ReMessage; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +/** + * @author vince + */ +@Slf4j +@Component(AnalysisConf.BEAN_ID) +public class AnalysisConf { + + public static final String BEAN_ID = "zldz.analysisConf"; + + @Resource + private GatewayDeviceService gatewayDeviceService; + + /** + * 瑙f瀽鍒嗘満閰嶇疆淇℃伅 + * + * @param message + * @param ser + */ + public void analysis8816(ReMessage message, GatewayDevice ser) { + + log.debug("鍒嗘満---->>>骞冲彴锛氭秷鎭被鍨�=8816-----{}", ser.getDeviceName()); + + String content = message.getBody().getContent(); + + // 璇诲啓鏍囧織 + int start = 0, len = 1; + String temp = content.substring(start * 2, start * 2 + len * 2); + int readTag = BytesUtil.hexToInt(temp); + + // 璁惧ID锛屽叏灞�鍞竴 + start = 1; + len = 2; + temp = content.substring(start * 2, start * 2 + len * 2); + + // 缃戠粶ID + start = 3; + len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + + // 淇¢亾 + start = 4; + len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + + // 閫熺巼 + start = 5; + len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + + // 灞傛暟 + String cable = ""; + start = 6; + len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + cable += BytesUtil.hexToInt(temp); + + // 琛屾暟 + start = 7; + len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + cable += "-" + BytesUtil.hexToInt(temp); + + // 鍒楁暟 + start = 8; + len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + cable += "-" + BytesUtil.hexToInt(temp); + + //灞� - 琛� - 鍒� 閰嶇疆 + ser.setCableRule(cable); + + // 鐢电紗鍒跺紡 + start = 9; + len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + ser.setCableFormat("0" + BytesUtil.hexToInt(temp)); + + // 鐢电紗绫诲瀷 + start = 10; + len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + ser.setCableType("0" + BytesUtil.hexToInt(temp)); + + // 渚涚數妯″紡 + start = 11; + len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + //ser.setPowerModel("0" + BytesUtil.hexToInt(temp)); + + String msg = ser.getDeviceName() + " 杩滅▼鑾峰彇閰嶇疆鎴愬姛锛岃鍒锋柊鏁版嵁鏌ョ湅锛�"; + + // 缁勭粐缂栫爜 + start = 12; + temp = content.substring(start * 2); + if (readTag == 1) {// 琛ㄧず鍐欑殑鎴愬姛杩斿洖 + ser.setOrgId(ser.getOrgId()); + msg = ser.getDeviceName() + " 杩滅▼鍐欏叆鎴愬姛锛岃鍒锋柊鏁版嵁鏌ョ湅锛�"; + } else { + ser.setOrgId(BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) + ""); + } + + if (null == ser.getDeviceSn()) { + ser.setDeviceSn(ser.getId()); + } + ser.setIp(message.getIp()); + ser.setPort(message.getPort()); + + // 寮�濮嬪垪榛樿=1 + if (ser.getCableStart() == 0) { + ser.setCableStart(1); + } + + log.info("鍒嗘満---->>>骞冲彴锛氬垎鏈洪厤缃В鏋愬畬鎴�---{}锛屾洿鏂板埌鏈嶆湇鍔″櫒", + ser.getDeviceName()); + + //鏇存柊璁惧淇℃伅 + gatewayDeviceService.updateData(ser); + } + + /** + * 鐢电紗鍒濆鍖栨垚鍔� + * + * @param reMessage + * @param ser + */ + public void analysis8822(ReMessage reMessage, GatewayDevice ser) { + log.info("--------鐢电紗鍒濆鍖栨垚鍔�------------{}", ser.getDeviceName()); + } + + /** + * 鐢电紗淇敼杩斿洖锛� + * + * @param reMessage + * @param ser + */ + public void analysis8826(ReMessage reMessage, GatewayDevice ser) { + log.info("--------淇敼鐢电紗淇℃伅杩斿洖------------{}", ser.getDeviceName()); + } + + /** + * 閫氶亾鐢电紗鑾峰彇杩斿洖锛屽垎鏈鸿繑鍥炴敹鍒板懡浠や俊鎭紝瀹為檯杩斿洖鍊煎湪8829涓鏄� + * + * @param reMessage + * @param ser + */ + public void analysis8825(ReMessage reMessage, GatewayDevice ser) { + log.info("--------閫氶亾鐢电紗鑾峰彇杩斿洖------------{}", ser.getDeviceName()); + } + + /** + * 缁堢鍙戦�侀�氶亾鐢电紗鏁版嵁缁欏悗鍙帮紝鍒嗘満涓诲姩鎺ㄩ�� + * + * @param reMessage + * @param ser + */ + @SuppressWarnings("unchecked") + public void analysis1129(ReMessage reMessage, GatewayDevice ser) { + log.info("--------缁堢鍙戦�侀�氶亾鐢电紗鏁版嵁缁欏悗鍙�------------{}", ser.getDeviceName()); + } + + /** + * 鐢电紗姹囨�诲簲绛� + * + * @param reMessage + * @param ser + */ + public void analysis8823(ReMessage reMessage, GatewayDevice ser) { + // 寮�濮嬭В鏋� + String content = reMessage.getBody().getContent(); + + //DO NOTHING + } +} diff --git a/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java new file mode 100644 index 0000000..99ffdd7 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java @@ -0,0 +1,384 @@ +package com.fzzy.protocol.zldz.analysis; + +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.api.utils.ContextUtil; +import com.fzzy.gateway.data.BaseReqData; +import com.fzzy.gateway.entity.GatewayDevice; +import com.fzzy.io.request.BaseRequest; +import com.fzzy.protocol.ProtocolUtils; +import com.fzzy.protocol.zldz.data.ReMessage; +import com.fzzy.protocol.zldz.service.ZldzGatewayGrainService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 绮儏瑙f瀽 + * + * @author Andy + */ +@Slf4j +@Component(AnalysisGrain.BEAN_ID) +public class AnalysisGrain { + + public static final String BEAN_ID = "zldz.analysisGrain"; + + @Resource + private ZldzGatewayGrainService zldzGatewayGrainService; + + public static Map<String, Map<String, String>> contextMap = new HashMap<>(); + + public static double ERROR_CHECK_TAG = -100.0; + + public static double FAULT_CHECK_TAG = 85.0; + + public static double ERROR_CHECK_TAG2 = 50; + + public static double MAX_TEMP = -50.0; + public static double MIN_TEMP = 50.0; + + /** + * 鍚庡彴鍙戦�佽姹傚悗锛岀粓绔殑鍝嶅簲 + * + * @param reMessage + * @param device + */ + public void analysis8815(ReMessage reMessage, GatewayDevice device) { + BaseRequest request = new BaseRequest(); + request.setSerId(device.getId()); + request.setCompanyId(device.getOrgId()); + request.setIp(reMessage.getIp()); + request.setPort(reMessage.getPort()); + request.setSerName(device.getDeviceName()); + + zldzGatewayGrainService.reply8815(request); + } + + /** + * 鎺ユ敹鍒扮伯鎯呯殑鍥炲 + * + * @param reMessage + * @param device + */ + public void reply8817(ReMessage reMessage, GatewayDevice device) { + String bodyMsg = reMessage.getBody().getContent(); + + log.info("鍒嗘満------>>>骞冲彴锛氭敹鍒扮殑绮儏淇℃伅-{}-{}", + device.getDeviceName(), reMessage.getStrMsg()); + + // Step1鏍规嵁闇�瑕佺畝鍖栭渶瑕佺殑淇℃伅 + String ua = bodyMsg.substring(0 * 2, 1 * 2); + log.debug("鑾峰彇鍒扮殑鐢靛帇淇℃伅锛岀郴缁熶笉淇濈暀={}", ua); + + // 鎬诲寘鏁� + // int sumPacket = BytesUtil.hexToInt(bodyMsg.substring(1 * 2, 1 * 2 + 1 + // * 2)); + // 褰撳墠鍖� + String hexCurPacket = bodyMsg.substring(2 * 2, 2 * 2 + 1 * 2); + // int curPacket = BytesUtil.hexToInt(hexCurPacket); + + BaseRequest request = new BaseRequest(); + request.setSerId(device.getDeviceSn()); + request.setCompanyId(device.getOrgId()); + request.setIp(reMessage.getIp()); + request.setPort(reMessage.getPort()); + request.setSerName(device.getDeviceName()); + + zldzGatewayGrainService.reply8817(request, hexCurPacket); + } + + + public void analysis8817(ReMessage msg, GatewayDevice device) { + // 鎺ュ埌淇℃伅鍥炲--涓绘満 + reply8817(msg, device); + + + BaseReqData reqData = ProtocolUtils.getSyncReq(device.getDepotIdSys()); + + if (null == reqData) { + log.warn("---------娌℃湁鑾峰彇鍒板綋鍓嶄粨搴撶殑璇锋眰锛屼笉鎵ц瑙f瀽-----{}", device.getDeviceName()); + return; + } + + analysisStep(msg, device, reqData); + } + + + /** + * 寮�濮嬭В鏋� + * + * @param device + * @param msg + * @throws Exception + */ + private void analysisStep(ReMessage msg, GatewayDevice device, BaseReqData reqData) { + // 绮儏鐨勬壒娆″彿閲嶆柊鏍规嵁棰戠巼璋冩暣 + msg.setBatchId(ContextUtil.getDefaultBatchId()); + + String bodyMsg = msg.getBody().getContent(); + + log.debug("{}=鏀跺埌鐨勭伯鎯呬俊鎭�={}", device.getDeviceName(), + msg.getStrMsg()); + + // Step1鏍规嵁闇�瑕佺畝鍖栭渶瑕佺殑淇℃伅 + String ua = bodyMsg.substring(0 * 2, 1 * 2); + log.debug("鑾峰彇鍒扮殑鐢靛帇淇℃伅锛岀郴缁熶笉淇濈暀={}", ua); + + // 鎬诲寘鏁� + int sumPacket = BytesUtil.hexToInt(bodyMsg.substring(1 * 2, + 1 * 2 + 1 * 2)); + // 褰撳墠鍖� + String hexCurPacket = bodyMsg.substring(2 * 2, 2 * 2 + 1 * 2); + int curPacket = BytesUtil.hexToInt(hexCurPacket); + + log.debug("鍒嗘満={},鍖呮�绘暟={},褰撳墠鍖�={}", device.getDeviceName(), sumPacket, curPacket); + + // 绮娓╁害淇℃伅 + String grainStr = bodyMsg.substring(3 * 2); + + String contextKey = this.buildContextKey(msg, device.getDepotIdSys()); + String curKey = this.buildCurKey(msg, curPacket); + + Map<String, String> curMap = contextMap.get(contextKey); + if (null == curMap) { + curMap = new HashMap<>(); + contextMap.put(contextKey, curMap); + } + contextMap.get(contextKey).put(curKey, grainStr); + + // 鏁版嵁鍑嗗濂斤紝寮�濮嬫墽琛岃В鏋� + if (curPacket == sumPacket) { + analysisStep0(msg, device, reqData, contextKey, sumPacket); + log.info("鍒嗘満------>>>骞冲彴锛氬懡浠ょ被鍨�=8817--绮儏鍏ㄩ儴鏀跺埌锛屽紑濮嬭В鏋�-{}", + device.getDeviceName()); + } + } + + private void analysisStep0(ReMessage msg, GatewayDevice device, BaseReqData reqData, String contextKey, int sumPacket) { + + Map<String, String> curMap = contextMap.get(contextKey); + + if (StringUtils.isEmpty(device.getCableCir())) { + analysisStep1(msg, device, reqData, curMap, sumPacket); + } else { + analysisStep2(msg, device, reqData, curMap, sumPacket); + } + } + + /** + * 鍦嗙瓛浠撶殑绮儏瑙f瀽 + * + * @param packetMap + * @param sumPackets + */ + private void analysisStep2(ReMessage msg, + GatewayDevice device, BaseReqData reqData, + Map<String, String> packetMap, int sumPackets) { + + + String[] cableRuleAtt = device.getCableRule().split("-"); + String[] cableCirAtt = device.getCableCir().split("-"); + + if (cableRuleAtt.length != cableCirAtt.length) { + log.error("鍒嗘満------>>>骞冲彴锛氬綋鍓嶄粨搴擄細{}锛屽竷绾胯鍒欎笉姝g‘锛屾棤娉曡В鏋愮伯鎯呬俊鎭�︹��", device.getDeviceName()); + return; + } + + // 鑾峰彇鏈�澶х殑灞傞厤缃�--榛樿姣忎竴鍦堥兘涓�鏍� + int layMax = Integer.valueOf(cableCirAtt[0]); + for (int i = 0; i < cableCirAtt.length; i++) { + if (Integer.valueOf(cableCirAtt[i]) >= layMax) layMax = Integer.valueOf(cableCirAtt[i]); + } + + // 鑾峰彇瀹屾暣鐨勭伯鎯呭寘淇℃伅 + String strPoints = ""; + for (int i = 1; i <= sumPackets; i++) { + strPoints += packetMap.get(buildCurKey(msg, i)); + } + + log.info("------绛掍粨瀹屾暣鐨勭伯鎯呮姤鏂�={}------", strPoints); + + int sumNum = 0, cableZ = 1;// sumNum 鍏卞灏戞牴鐢电紗锛沜ableZ 灞傜殑鏈�澶у�硷紝閿ュ舰浠撹ˉ榻愭渶澶у眰 + for (int i = 0; i < cableCirAtt.length; i++) { + if (Integer.valueOf(cableCirAtt[i]) > cableZ) { + cableZ = Integer.valueOf(cableCirAtt[i]); + } + sumNum += Integer.valueOf(cableRuleAtt[i]); + } + + // 鏍规嵁灞傝鍒楄幏鍙栨寚瀹氶暱搴� + int start = 0; + int len = 4 * cableZ * sumNum; + + strPoints = strPoints.substring(start, start + len); + log.info("鍒嗘満------>>>骞冲彴锛氳繑鍥炵伯鎯呭畬鏁翠俊鎭紝鍒嗘満={}", device.getDeviceName()); + + // 灏嗙伯鎯呰В鏋愭垚鏁扮粍 + List<Double> temps = new ArrayList<>(); + double tempValue; + String temp; + int curLay = 1;//鎵�鍦ㄥ眰浠�1寮�濮� + int curRoot = 1;//鎵�鍦ㄦ牴 + int curCir = 1;//鎵�鍦ㄥ湀 + int cirLay = 1;//褰撳墠鍦堢殑灞� + for (int i = 0; i < strPoints.length() / 4; i++) { + temp = strPoints.substring(i * 4, i * 4 + 4); + if (temp == null) { + temp = "0000"; + } + tempValue = BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) / 10.0; + + //闈炴甯稿�� + if (tempValue > ERROR_CHECK_TAG2) { + tempValue = ProtocolUtils.ERROR_TEMP; + } + + // 鏁呴殰鍊煎鐞� + if (tempValue >= FAULT_CHECK_TAG) { + tempValue = ProtocolUtils.FAULT_TEMP; + } + + // 澶囩敤鍊� + if (tempValue == ERROR_CHECK_TAG) { + tempValue = ProtocolUtils.ERROR_TEMP; + + + //楠岃瘉鏄笉鏄敟褰粨琛ュ伩鍊� + curLay = (i % layMax) + 1; + curRoot = (i / layMax) + 1; + curCir = getCurCir(curRoot, cableRuleAtt); + + cirLay = Integer.valueOf(cableCirAtt[curCir - 1]); + + //姣斿閰嶇疆浜�5灞備絾鏄綋鍓嶆槸6灞傦紝璇存槑褰撳墠鐐逛负琛ュ伩鐐� + if (curLay > cirLay) { + tempValue = ProtocolUtils.ADD_TEMP; + } else { + temps.add(tempValue); + } + + } else { + temps.add(tempValue); + } + } + + // 灏嗛泦鍚堣В鏋愭垚鍧愭爣鏁版嵁 + addPoint2(temps, msg, device, reqData); + } + + /** + * 鑾峰彇褰撳墠璺熸墍鍦ㄥ湀 + * + * @param curRoot + * @param cableRuleAtt + * @return + */ + private int getCurCir(int curRoot, String[] cableRuleAtt) { + + int sum = 0; + for (int i = 0; i < cableRuleAtt.length; i++) { + sum += Integer.valueOf(cableRuleAtt[i]); + if (curRoot <= sum) return i + 1; + } + + return 1; + } + + /** + * 骞虫柟浠撶殑瑙f瀽锛岃В鏋愰渶瑕佽�冭檻褰撳墠鏄惁鍚敤鐨勪竴鍒嗘満澶氫粨 + * + * @param depotConf + * @param msg + * @param ser + * @param exeRequest + * @param packetMap + * @param sumPackets + */ + private void analysisStep1(ReMessage msg, + GatewayDevice device, BaseReqData reqData, + Map<String, String> packetMap, int sumPackets) { + // 鑾峰彇瀹屾暣鐨勭伯鎯呭寘淇℃伅 + String strPoints = ""; + for (int i = 1; i <= sumPackets; i++) { + strPoints += packetMap.get(buildCurKey(msg, i)); + } + + String[] attCable = device.getCableRule().split("-"); + int cableZ = Integer.valueOf(attCable[0]); + int cableY = Integer.valueOf(attCable[1]); + int cableX = Integer.valueOf(attCable[2]); + + // 鏍规嵁灞傝鍒楄幏鍙栨寚瀹氶暱搴� + int start = 4 * (device.getCableStart() - 1) * cableZ * cableY; + int len = 4 * cableZ * cableY * cableX; + + log.info("鍒嗘満------>>>骞冲彴锛氳繑鍥炵伯鎯呭畬鏁翠俊鎭紝鍒嗘満={}", device.getDeviceName()); + + strPoints = strPoints.substring(start, start + len); + + // 灏嗙伯鎯呰В鏋愭垚鏁扮粍 + List<Double> temps = new ArrayList<>(); + double tempValue; + String temp; + for (int i = 0; i < strPoints.length() / 4; i++) { + temp = strPoints.substring(i * 4, i * 4 + 4); + if (temp == null) { + temp = "0000"; + } + tempValue = BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) / 10.0; + // 璇存槑瑙f瀽鐨勬暟鎹湁闂 + if (tempValue == ERROR_CHECK_TAG || tempValue == ERROR_CHECK_TAG2) { + tempValue = ProtocolUtils.ERROR_TEMP; + } + // 鏁呴殰鍊煎鐞� + if (tempValue >= FAULT_CHECK_TAG) { + tempValue = ProtocolUtils.FAULT_TEMP; + } + temps.add(tempValue); + } + + + // 灏嗛泦鍚堣В鏋愭垚鍧愭爣鏁版嵁 + addPoint1(temps, msg, device, reqData); + } + + private String buildCurKey(ReMessage msg, int curPacket) { + return msg.getSourceId() + "_" + curPacket; + } + + private String buildContextKey(ReMessage msg, String depotId) { + return msg.getCompanyId() + "_" + msg.getSourceId() + "_" + depotId; + } + + /** + * 骞虫埧浠擄紝瑙f瀽绗簩姝ワ紝瑙f瀽鍒板潗鏍囨暟鎹� + * + * @param temps + * @throws Exception + */ + private void addPoint1(List<Double> temps, ReMessage msg, + GatewayDevice device, BaseReqData reqData) { + //TODO + } + + /** + * 鍦嗙瓛浠撹В鏋愭楠� + * + * @param temps + * @param msg + */ + private void addPoint2(List<Double> temps, ReMessage msg, + GatewayDevice device, BaseReqData reqData) { + + //TODO + } + + +} \ No newline at end of file diff --git a/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisService.java b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisService.java new file mode 100644 index 0000000..12878e3 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisService.java @@ -0,0 +1,169 @@ +package com.fzzy.protocol.zldz.analysis; + +import com.fzzy.api.Constant; +import com.fzzy.api.data.ApiCommonDevice; +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.gateway.GatewayUtils; +import com.fzzy.gateway.entity.GatewayDevice; +import com.fzzy.protocol.zldz.cmd.ReMessageBuilder; +import com.fzzy.protocol.zldz.data.ReMessage; +import com.fzzy.protocol.zldz.server.ZldzServerUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import com.fzzy.protocol.zldz.util.*; + +import javax.annotation.Resource; + + +/** + * 鍗忚瑙f瀽鍏ュ彛 + * + * @author jiazx + */ +@Slf4j +@Component(AnalysisService.BEAN_ID) +public class AnalysisService { + + public static final String BEAN_ID = "zldz.analysisService"; + + + @Resource + private AnalysisGrain analysisGrain; + + @Resource + private AnalysisTH analysisTH; + + @Resource + private AnalysisConf analysisConf; + + + public void analysis(String ip, Integer port, String strMsg) { + + ApiCommonDevice commonDevice = Constant.getCommonDeviceCacheByIp(ip); + + ReMessage reMessage = ReMessageBuilder.getInstance().buildMessage(null, commonDevice, strMsg); + + if (null == reMessage) { + log.error("鍒嗘満----->>>骞冲彴锛氳繑鍥炰俊鎭病鏈夎幏鍙栫粍缁囩紪鐮佷俊鎭紝鏃犳硶缁х画瑙f瀽----{}", commonDevice.getIp()); + return; + } + + //鑾峰彇璁惧淇℃伅 + GatewayDevice device = GatewayUtils.getCacheByDeviceSn(commonDevice.getSn()); + if (null == device) { + log.error("鍒嗘満----->>>骞冲彴锛氫负鑾峰彇鍒拌澶囬厤缃俊鎭紝鏃犳硶缁х画瑙f瀽----{}", commonDevice.getIp()); + return; + } + + String checkMsg = this.checkMsg(reMessage, device); + + if (null != checkMsg) { + log.error("鍒嗘満----->>>骞冲彴锛氬懡浠ら獙璇佸紓甯革細寮傚父淇℃伅-{}锛屾姤鏂囦俊鎭�-{}", checkMsg, reMessage.getStrMsg()); + return; + } + + switch (reMessage.getMessageType()) { + case ZldzServerUtil.MSG_TYPE_8828:// 娓╂箍搴﹁В鏋� + analysisTH.analysis8828(reMessage, device); + break; + + case ZldzServerUtil.MSG_TYPE_8815:// 缁堢搴旂瓟绮儏璇锋眰 + analysisGrain.analysis8815(reMessage, device); + break; + + case ZldzServerUtil.MSG_TYPE_1117:// 绮儏鏁版嵁淇℃伅 + analysisGrain.analysis8817(reMessage, device); + break; + case ZldzServerUtil.MSG_TYPE_1129:// 缁堢鍙戦�侀�氶亾鐢电紗鏁版嵁缁欏悗鍙� + analysisConf.analysis1129(reMessage, device); + break; + case ZldzServerUtil.MSG_TYPE_8816:// 瑙f瀽閰嶇疆 + analysisConf.analysis8816(reMessage, device); + break; + case ZldzServerUtil.MSG_TYPE_8822:// 鐢电紗鍒濆鍖栨垚鍔� + analysisConf.analysis8822(reMessage, device); + break; + case ZldzServerUtil.MSG_TYPE_8823:// 鐢电紗姹囨�诲簲绛� + analysisConf.analysis8823(reMessage, device); + break; + + case ZldzServerUtil.MSG_TYPE_8825:// 璇诲彇閫氶亾鐢电紗閰嶇疆 + analysisConf.analysis8825(reMessage, device); + break; + + case ZldzServerUtil.MSG_TYPE_8826:// 淇敼鐢电紗鐨勮繑鍥� + analysisConf.analysis8826(reMessage, device); + break; + default: + break; + } + } + + /** + * 鏍¢獙锛屾甯歌繑鍥濶ULL锛屽紓甯歌繑鍥炲紓甯稿師鍥狅紝姝e父寮傚父鍘熷洜杩斿洖涓嶈涪鎺夐噸鏂拌繛鎺� + * + * @param message + * @param device + * @return + * @throws Exception + */ + private String checkMsg(ReMessage message, GatewayDevice device) { + String result; + if (message.getResult() == 1) { + result = "鍒嗘満=" + device.getDeviceName() + " 鍗忚瑙f瀽澶辫触锛屽師鍥狅細鎺ユ敹鐨勫簲绛旀牎楠屽紓甯�"; + return result; + } + if (message.getResult() == 2) { + result = "鍒嗘満=" + device.getDeviceName() + " 鍗忚瑙f瀽澶辫触锛屽師鍥狅細鍙戦�佺殑鏁版嵁鏍¢獙寮傚父"; + return result; + } + if (message.getResult() == 3) { + result = "鍒嗘満=" + device.getDeviceName() + " 鍗忚瑙f瀽澶辫触锛屽師鍥狅細鎺掗槦涓�"; + return result; + } + if (message.getResult() == 4) { + result = "鍒嗘満=" + device.getDeviceName() + " 鍗忚瑙f瀽澶辫触锛屽師鍥狅細鍙戦�佽秴鏃堕敊璇�"; + return result; + } + if (message.getResult() == 9) { + result = "鍒嗘満=" + device.getDeviceName() + " 鍗忚瑙f瀽澶辫触锛屽師鍥狅細Io妯″潡涓嶅湪绾�"; + return result; + } + if (message.getResult() == 10) { + result = "鍒嗘満=" + device.getDeviceName() + " 鍗忚瑙f瀽澶辫触锛屽師鍥狅細鍙戦�佷腑"; + return result; + } + if (message.getResult() == 11) { + result = "鍒嗘満=" + device.getDeviceName() + " 鍗忚瑙f瀽澶辫触锛屽師鍥狅細鏃犵嚎妯″潡鏁呴殰"; + return result; + } + if (message.getResult() == 12) { + result = "鍒嗘満=" + device.getDeviceName() + + " 鍗忚瑙f瀽澶辫触锛屽師鍥狅細涓昏鏄弽棣堝潗鏍囩偣浜ゆ崲鍛戒护鎴栬�呮槸鐢电紗淇敼鍛戒护鐨勯敊璇紝鎴栬�呯伯鎯呴噰闆嗙殑鍙傛暟涓嶅"; + return result; + } + if (message.getResult() == 13) { + result = "鍒嗘満=" + device.getDeviceName() + " 鍗忚瑙f瀽澶辫触锛屽師鍥狅細缃戝叧鏈敞鍐�"; + return result; + } + + // 鑾峰彇鏍¢獙鐨勬姤鏂囦俊鎭� + int end = 4 + 18 * 2 + message.getBody().getLength() * 2; + String checkStr = message.getStrMsg().substring(4, end); + + byte[] byteCrc16 = BytesUtil.hexStrToBytes(checkStr); + short crc16Hex2 = CRC8.calculateCrc16(byteCrc16, (short) 0, + (short) (byteCrc16.length)); + byte[] crc16Hex2Bytes = BytesUtil.shortToByte(crc16Hex2); + int crc16 = BytesUtil.bytesToInt(crc16Hex2Bytes); + String hexCrc16 = BytesUtil.intToHexStr(crc16); + + if (message.getHexCrc16().equals(hexCrc16)) { + return null; + } else { + result = "鍒嗘満=" + device.getDeviceName() + " 鍗忚瑙f瀽澶辫触锛屽師鍥狅細CRC16鏍¢獙寮傚父"; + return result; + } + } + +} diff --git a/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisTH.java b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisTH.java new file mode 100644 index 0000000..beb890b --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisTH.java @@ -0,0 +1,87 @@ +package com.fzzy.protocol.zldz.analysis; + + +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.gateway.entity.GatewayDevice; +import com.fzzy.protocol.ProtocolUtils; +import com.fzzy.protocol.data.THDto; +import com.fzzy.protocol.zldz.data.ReMessage; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +/** + * 瑙f瀽娓╂箍搴︿俊鎭� + * + * @author Andy + */ +@Slf4j +@Component(AnalysisTH.BEAN_ID) +public class AnalysisTH { + + public static final String BEAN_ID = "zldz.analysisTH"; + + + public static int ERROR_TH_TAG = -50; + + /** + * 瑙f瀽浠撳簱娓╂箍搴︿俊鎭紝灏嗕俊鎭斁鍦ㄧ紦瀛樹腑锛屼互渚跨伯鎯呬娇鐢� + * + * @param msg + * @param ser + */ + public void analysis8828(ReMessage msg, GatewayDevice ser) { + + log.debug("-----------analysis8828------------{}.{}", ser.getIp(), ser.getPort()); + + String content = msg.getBody().getContent(); + + //娓╂箍搴﹀湴鍧�,鎴彇4浣� + String temp = content.substring(0, 0 + 2 * 2); + //楂樹綆浣嶈浆鎹� + temp = BytesUtil.tran_LH(temp); + int thAddr = BytesUtil.hexToInt(temp); + + int t, h; + + THDto th = new THDto(); + th.setTempIn(ProtocolUtils.ERROR_TEMP); + th.setHumidityIn(ProtocolUtils.ERROR_TEMP); + + //娓╁害 + int start = 2, len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + t = BytesUtil.hexToInt(temp); + if (t > 127) {//璇存槑鏄礋鏁� + t = BytesUtil.hexToInt("FF" + temp); + } + if (t == ERROR_TH_TAG) { + log.error("{}娓╂箍搴﹁В鏋愬紓甯革紝鍘熷洜锛氭病鏈夋娴嬪埌浼犳劅鍣�", ser.getDeviceName()); + th.setRemark(ser.getDeviceName() + "娓╂箍搴﹁В鏋愬紓甯革紝鍘熷洜锛氭病鏈夋娴嬪埌浼犳劅鍣�"); + } else { + th.setTempIn(Double.valueOf(t)); + } + + //婀垮害 + start = 3; + len = 1; + temp = content.substring(start * 2, start * 2 + len * 2); + h = BytesUtil.hexToInt(temp); + if (h > 127) {//璇存槑鏄礋鏁� + h = BytesUtil.hexToInt("FF" + temp); + } + if (h == ERROR_TH_TAG) { + log.error("{}娓╂箍搴﹁В鏋愬紓甯革紝鍘熷洜锛氭病鏈夋娴嬪埌浼犳劅鍣�", ser.getDeviceName()); + th.setRemark(ser.getDeviceName() + "娓╂箍搴﹁В鏋愬紓甯革紝鍘熷洜锛氭病鏈夋娴嬪埌浼犳劅鍣�"); + } else { + th.setHumidityIn(Double.valueOf(h)); + } + + th.setSerId(ser.getId()); + th.setThAddr(thAddr + ""); + + //瀛樻斁缂撳瓨 + ProtocolUtils.addTh2Map(ser.getDeviceId(), th); + + log.info("浠撴俯浠撴箍瑙f瀽瀹屾垚={}", th); + } +} diff --git a/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder.java b/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder.java new file mode 100644 index 0000000..e6b0fb9 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder.java @@ -0,0 +1,144 @@ +package com.fzzy.protocol.zldz.cmd; + + +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.io.request.BaseRequest; +import com.fzzy.protocol.zldz.data.Message; +import com.fzzy.protocol.zldz.data.MessageBody; +import com.fzzy.protocol.zldz.server.ZldzServerUtil; +import com.fzzy.protocol.zldz.util.*; + +/** + * 鍛戒护鐢熸垚鍣� + * + * @author Andy + */ +public abstract class CommandBuilder { + + /* + * 鍛戒护鍒涘缓 + * + * @param order + */ + public Message buildMessage(BaseRequest request) { + Message message = new Message(); + + message.setBegin(ZldzServerUtil.MSG_START); + + // 璋冩暣Message澶撮儴淇℃伅 + buildHeader(message, request); + + //鍖呭ご鏍¢獙 + buildCrc8(message); + + // 鍒涘缓娑堟伅浣� + buildBody(message, request); + + // 娣诲姞鏍¢獙 + buildCrc16(message); + + message.setEnd(ZldzServerUtil.MSG_END); + + // 鐢熸垚鏁翠綋鍛戒护淇℃伅鍜孊Yte淇℃伅 + build(message); + + return message; + } + + + public void buildHeader(Message message, BaseRequest request) { + String hexCompanyId = BytesUtil.getTargetId(request.getCompanyId(),true); + hexCompanyId = BytesUtil.tran_LH(hexCompanyId); + message.setSourceId(hexCompanyId); + message.setSendId(hexCompanyId); + message.setCompanyId(hexCompanyId); + + message.setSourceId(BytesUtil.getTargetId(request.getSerId(), true)); + + message.setMessageType(ZldzServerUtil.MSG_TYPE_1116); + } + + public void buildBody(Message message, BaseRequest request) { + MessageBody body = new MessageBody(); + body.setContent(""); + body.setLength(0); + message.setBody(body); + } + + public void buildCrc8(Message message) { + StringBuffer hexSbCrc8 = new StringBuffer(); + hexSbCrc8.append(message.getSourceId()); + hexSbCrc8.append(message.getTargetId()); + hexSbCrc8.append(message.getSendId()); + hexSbCrc8.append(message.getGateway1()); + hexSbCrc8.append(message.getGateway2()); + hexSbCrc8.append(message.getMessageType()); + hexSbCrc8.append(message.getResult()); + hexSbCrc8.append(message.getCompanyId()); + + byte[] bytes = BytesUtil.hexStrToBytes(hexSbCrc8.toString()); + byte crc8 = CRC8.calcCrc8(bytes, (short) 0, (short) (bytes.length), + (byte) 0); + + message.setHexCrc8(BytesUtil.byteToHex(crc8)); + } + + + public void buildCrc16(Message message) { + StringBuffer hexSbCrc16 = new StringBuffer(); + hexSbCrc16.append(message.getSourceId()); + hexSbCrc16.append(message.getTargetId()); + hexSbCrc16.append(message.getSendId()); + hexSbCrc16.append(message.getGateway1()); + hexSbCrc16.append(message.getGateway2()); + hexSbCrc16.append(message.getMessageType()); + hexSbCrc16.append(message.getResult()); + hexSbCrc16.append(message.getCompanyId()); + + hexSbCrc16.append(message.getHexCrc8()); + + hexSbCrc16.append(BytesUtil.tran_LH(BytesUtil + .getHex2LenStr(message.getBody().getLength()))); + + hexSbCrc16.append(message.getBody().getContent()); + + byte[] byteCrc16 = BytesUtil.hexStrToBytes(hexSbCrc16.toString()); + short crc16Hex2 = CRC8.calculateCrc16(byteCrc16, (short) 0, (short) (byteCrc16.length)); + byte[] crc16Hex2Bytes = BytesUtil.shortToByte(crc16Hex2); + int crc16 = BytesUtil.bytesToInt(crc16Hex2Bytes); + + + message.setHexCrc16(BytesUtil.intToHexStr(crc16)); + } + + /** + * 鏍规嵁鐢熸垚鐨勫懡浠わ紝杩涜璋冩暣 + * + * @param message + */ + public void build(Message message) { + StringBuffer hexSb = new StringBuffer(); + + hexSb.append(message.getBegin()); + hexSb.append(message.getSourceId()); + hexSb.append(message.getTargetId()); + hexSb.append(message.getSendId()); + hexSb.append(message.getGateway1()); + hexSb.append(message.getGateway2()); + hexSb.append(message.getMessageType()); + hexSb.append(message.getResult()); + hexSb.append(message.getCompanyId()); + hexSb.append(message.getHexCrc8()); + + hexSb.append(BytesUtil.tran_LH(BytesUtil + .getHex2LenStr(message.getBody().getLength()))); + + hexSb.append(message.getBody().getContent()); + hexSb.append(message.getHexCrc16()); + hexSb.append(message.getEnd()); + + message.setStrMsg(hexSb.toString()); + message.setByteMsg(BytesUtil.hexStrToBytes(message.getStrMsg())); + } + +} diff --git a/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder1115.java b/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder1115.java new file mode 100644 index 0000000..7f58fb5 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder1115.java @@ -0,0 +1,85 @@ +package com.fzzy.protocol.zldz.cmd; + +import com.fzzy.api.data.ApiCommonDevice; +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.gateway.data.BaseReqData; +import com.fzzy.gateway.entity.GatewayDevice; +import com.fzzy.protocol.zldz.data.Message; +import com.fzzy.protocol.zldz.data.MessageBody; +import com.fzzy.protocol.zldz.server.ZldzServerUtil; + + +/** + * 绮儏淇℃伅鍛戒护 + * + * @author Andy + */ +public class CommandBuilder1115 extends CommandBuilder { + + + private final static CommandBuilder1115 instance = new CommandBuilder1115(); + + private CommandBuilder1115() { + + } + + public static CommandBuilder1115 getInstance() { + return instance; + } + + public Message buildMessage(BaseReqData reqData, ApiCommonDevice commonDevice) { + + //request.setCompanyId("1"); + + Message message = new Message(); + + message.setBegin(ZldzServerUtil.MSG_START); + + // 璋冩暣Message澶撮儴淇℃伅 + buildHeader(message, reqData, commonDevice); + + //鍖呭ご鏍¢獙 + buildCrc8(message); + + // 鍒涘缓娑堟伅浣� + buildBody(message, reqData, commonDevice); + + // 娣诲姞鏍¢獙 + buildCrc16(message); + + message.setEnd(ZldzServerUtil.MSG_END); + + // 鐢熸垚鏁翠綋鍛戒护淇℃伅鍜孊Yte淇℃伅 + build(message); + + return message; + } + + public void buildHeader(Message message, BaseReqData reqData, ApiCommonDevice commonDevice) { + + GatewayDevice device = reqData.getDevice(); + String hexCompanyId = BytesUtil.getTargetId(commonDevice.getOrgId(), true); + hexCompanyId = BytesUtil.tran_LH(hexCompanyId); + + message.setSourceId(hexCompanyId); + message.setSendId(hexCompanyId); + message.setCompanyId(hexCompanyId); + + message.setTargetId(BytesUtil.tran_LH(BytesUtil.getTargetId(device.getDeviceSn(), true))); + message.setMessageType(BytesUtil.tran_LH(ZldzServerUtil.MSG_TYPE_1115)); + } + + + public void buildBody(Message message, BaseReqData reqData, ApiCommonDevice commonDevice) { + + GatewayDevice device = reqData.getDevice(); + + MessageBody body = new MessageBody(); + body.setLength(2); + int start = device.getCableStart(); + int end = device.getCableEnd(); + body.setContent(BytesUtil.toHexString(start) + BytesUtil.toHexString(end)); + message.setBody(body); + } + +} diff --git a/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder8815.java b/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder8815.java new file mode 100644 index 0000000..59be89e --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder8815.java @@ -0,0 +1,41 @@ +package com.fzzy.protocol.zldz.cmd; + + +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.io.request.BaseRequest; +import com.fzzy.protocol.zldz.data.Message; +import com.fzzy.protocol.zldz.server.ZldzServerUtil; + +/** + * 绮儏淇℃伅鍛戒护 + * + * @author Andy + */ +public class CommandBuilder8815 extends CommandBuilder { + + + private final static CommandBuilder8815 instance = new CommandBuilder8815(); + + private CommandBuilder8815() { + + } + + public static CommandBuilder8815 getInstance() { + return instance; + } + + + public void buildHeader(Message message, BaseRequest request) { + String hexCompanyId = BytesUtil.getTargetId(request.getCompanyId(), true); + hexCompanyId = BytesUtil.tran_LH(hexCompanyId); + + message.setSourceId(hexCompanyId); + message.setSendId(hexCompanyId); + message.setCompanyId(hexCompanyId); + + message.setTargetId(BytesUtil.tran_LH(BytesUtil.getTargetId(request.getSerId(), true))); + + message.setMessageType(BytesUtil.tran_LH(ZldzServerUtil.MSG_TYPE_8815)); + } + +} diff --git a/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder8817.java b/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder8817.java new file mode 100644 index 0000000..76b0e23 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder8817.java @@ -0,0 +1,54 @@ +package com.fzzy.protocol.zldz.cmd; + + +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.io.request.BaseRequest; +import com.fzzy.protocol.zldz.data.Message; +import com.fzzy.protocol.zldz.data.MessageBody; +import com.fzzy.protocol.zldz.server.ZldzServerUtil; + +/** + * 绮儏淇℃伅鍛戒护 + * + * @author Andy + */ +public class CommandBuilder8817 extends CommandBuilder { + + + private final static CommandBuilder8817 instance = new CommandBuilder8817(); + + private CommandBuilder8817() { + + } + + public static CommandBuilder8817 getInstance() { + return instance; + } + + + public void buildHeader(Message message, BaseRequest request) { + + //request.setCompanyId("1"); + + String hexCompanyId = BytesUtil.getTargetId(request.getCompanyId(),true); + hexCompanyId = BytesUtil.tran_LH(hexCompanyId); + + message.setSourceId(hexCompanyId); + message.setSendId(hexCompanyId); + message.setCompanyId(hexCompanyId); + + message.setTargetId(BytesUtil.tran_LH(BytesUtil.getTargetId(request.getSerId(), true))); + + message.setMessageType(BytesUtil.tran_LH(ZldzServerUtil.MSG_TYPE_8817)); + } + + + public void buildBody(Message message, BaseRequest request) { + MessageBody body = new MessageBody(); + body.setContent(request.getBizType()); + body.setLength(1); + message.setBody(body); + } + + +} diff --git a/src/main/java/com/fzzy/protocol/zldz/cmd/ReMessageBuilder.java b/src/main/java/com/fzzy/protocol/zldz/cmd/ReMessageBuilder.java new file mode 100644 index 0000000..e8c3e2c --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/cmd/ReMessageBuilder.java @@ -0,0 +1,131 @@ +package com.fzzy.protocol.zldz.cmd; + +import com.fzzy.api.data.ApiCommonDevice; +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.api.utils.ContextUtil; +import com.fzzy.protocol.zldz.data.ReMessage; +import com.fzzy.protocol.zldz.data.ReMessageBody; + +import java.util.Date; + +/** + * 鏍规嵁鎺ユ敹鍒扮殑淇℃伅杩涜灏佽 + * + * @author Andy + */ +public class ReMessageBuilder { + + private final static ReMessageBuilder instance = new ReMessageBuilder(); + + private ReMessageBuilder() { + } + + public static ReMessageBuilder getInstance() { + return instance; + } + + /** + * 浣跨敤涓彂鐜癈RC16鍙兘浠E缁撳熬锛孨ETTY璇互涓烘槸缁撳熬瀵艰嚧鎴彇灏戜簡涓�涓狤E锛屽綋鍓嶆柟娉曞繀椤讳粠澶村悜鍚庢埅鍙栵紝淇濊瘉CRC16鐨勪綅缃噯纭� + * + * @param bytes + * @param device + * @param strMsg + * @return + */ + public ReMessage buildMessage(byte[] bytes, ApiCommonDevice device, String strMsg) { + + ReMessage message = new ReMessage(); + message.setIp(device.getIp()); + message.setPort(device.getPort()); + message.setStrMsg(strMsg); + message.setByteMsg(bytes); + + // 婧怚D锛屽垎鏈哄敮涓�ID + int start = 2, len = 2; + String temp = strMsg.substring(start * 2, start * 2 + len * 2); + message.setSourceId(BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) + ""); + + //鍒嗘満鎵�灞炵粍缁囩紪鐮� + String companyId = device.getOrgId(); + + // 鐩爣ID + start = 4; + len = 2; + temp = strMsg.substring(start * 2, start * 2 + len * 2); + message.setTargetId(BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) + ""); + + // 鍙戦�両D + start = 6; + len = 2; + temp = strMsg.substring(start * 2, start * 2 + len * 2); + message.setSendId(BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) + ""); + + // 璺敱淇℃伅锛屼笉瑙f瀽 + + // 娑堟伅绫诲瀷 + start = 12; + len = 2; + temp = strMsg.substring(start * 2, start * 2 + len * 2); + message.setMessageType(BytesUtil.tran_LH(temp)); + + // 澶勭悊缁撴灉 + start = 14; + len = 1; + temp = strMsg.substring(start * 2, start * 2 + len * 2); + message.setResult(BytesUtil.hexToInt(temp)); + + // 缁勭粐缂栫爜 + start = 15; + len = 2; + temp = strMsg.substring(start * 2, start * 2 + len * 2); + if (null == companyId) { + message.setCompanyId(BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) + ""); + } else { + message.setCompanyId(companyId); + } + + //鍖呭ご鏍¢獙 + start = 17; + len = 1; + temp = strMsg.substring(start * 2, start * 2 + len * 2); + message.setHexCrc8(temp); + + // 鍛戒护浣�-闀垮害 + message.setBody(buildBody()); + start = 18; + len = 2; + temp = strMsg.substring(start * 2, start * 2 + len * 2); + int bodyLen = BytesUtil.hexToInt(BytesUtil.tran_LH(temp)); + message.getBody().setLength(bodyLen); + + //鍛戒护浣�-鍐呭 + start = 20; + len = bodyLen; + temp = strMsg.substring(start * 2, start * 2 + len * 2); + message.getBody().setContent(temp); + + // crc16楠岃瘉 + start = 20 + bodyLen; + len = 2; + temp = strMsg.substring(start * 2, start * 2 + len * 2); + message.setHexCrc16(temp); + + message.setReceiveDate(new Date()); + + message.setBatchId(ContextUtil.getDefaultBatchId()); + return message; + } + + /** + * 鍒涘缓BODY瀵硅薄 + * + * @param + * @return + */ + public ReMessageBody buildBody() { + ReMessageBody body = new ReMessageBody(); + body.setLength(0); + body.setContent(""); + return body; + } +} diff --git a/src/main/java/com/fzzy/protocol/zldz/data/Message.java b/src/main/java/com/fzzy/protocol/zldz/data/Message.java new file mode 100644 index 0000000..cda2e78 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/data/Message.java @@ -0,0 +1,52 @@ +package com.fzzy.protocol.zldz.data; + +import lombok.Data; + +/** + * 瀛楃涓插�煎潎閲囩敤鍗佸叚杩涘埗锛屾柟渚挎牎楠屽拰寮�鍙� + */ +@Data +public class Message { + + private String begin; + + private String sourceId;// 婧� ID + + private String targetId="FFFF";// 鐩爣ID + + private String sendId;//鍙戦�佽�匢D + + private String gateway1 = "0100";//璺敱1 + + private String gateway2 = "0000";//璺敱2 + + private String messageType;// 娑堟伅绫诲瀷 + + private String result ="00";// 澶勭悊缁撴灉 + + private String companyId;// 缁勭粐缂栫爜 + + private String hexCrc8;//crc8鍖呭ご鏍¢獙 + + private MessageBody body; + + private String hexCrc16;//crc16鏍¢獙 + + private String end; + + private byte[] byteMsg;// 褰撳墠鍛戒护鐨勫瓧鑺備俊鎭� + + private String strMsg;// 褰撳墠鍛戒护杞崲涓哄瓧绗︿覆鐨勪俊鎭� + + @Override + public String toString() { + return "Message [sourceId=" + sourceId + + ", targetId=" + targetId + + ", messageType=" + messageType + + ", result=" + result + ", companyId=" + companyId + + ", hexCrc8=" + hexCrc8 + ", body=" + body + ", hexCrc16=" + + hexCrc16 + ", strMsg=" + strMsg + "]"; + } + + +} \ No newline at end of file diff --git a/src/main/java/com/fzzy/protocol/zldz/data/MessageBody.java b/src/main/java/com/fzzy/protocol/zldz/data/MessageBody.java new file mode 100644 index 0000000..273f41f --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/data/MessageBody.java @@ -0,0 +1,13 @@ +package com.fzzy.protocol.zldz.data; + +import lombok.Data; + +@Data +public class MessageBody { + + //榛樿10杩涘埗闀垮害 + private int length = 0; + + //娑堟伅浣撳唴瀹� + private String content = ""; +} diff --git a/src/main/java/com/fzzy/protocol/zldz/data/Metadata.java b/src/main/java/com/fzzy/protocol/zldz/data/Metadata.java new file mode 100644 index 0000000..fa08b44 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/data/Metadata.java @@ -0,0 +1,18 @@ +package com.fzzy.protocol.zldz.data; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; + +@Retention(RetentionPolicy.RUNTIME) +@Target(FIELD) +public @interface Metadata { + + String label() default ""; + + int bytes() default 0; + + int startIndex() default 0; +} \ No newline at end of file diff --git a/src/main/java/com/fzzy/protocol/zldz/data/ReMessage.java b/src/main/java/com/fzzy/protocol/zldz/data/ReMessage.java new file mode 100644 index 0000000..d180d4e --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/data/ReMessage.java @@ -0,0 +1,74 @@ +package com.fzzy.protocol.zldz.data; + + +import lombok.Data; + +import java.util.Date; + +/** + * 娑堟伅澶村璞� + * + * @author Andy + * + */ +@Data +public class ReMessage { + + private String ip; + + private int port; + + private String begin; + + private String sourceId;// 婧� ID + + private String targetId;// 鐩爣ID + + private String sendId;//鍙戦�佽�匢D + + private String gateway1;//璺敱1 + + private String gateway2;//璺敱2 + + private String messageType;// 娑堟伅绫诲瀷 + + private int result;// 澶勭悊缁撴灉 + + private String companyId;// 缁勭粐缂栫爜 + + private String hexCrc8;//crc8鍖呭ご鏍¢獙 + + private ReMessageBody body; + + private String hexCrc16;//crc16鏍¢獙 + + private String end; + + private byte[] byteMsg;// 褰撳墠鍛戒护鐨勫瓧鑺備俊鎭� + + private String strMsg;// 褰撳墠鍛戒护杞崲涓哄瓧绗︿覆鐨勪俊鎭� + + private String batchId; + + private Date receiveDate; + + // 寮傚父淇℃伅 + private String error; + + @Override + public String toString() { + return "ReMessage{" + + "ip='" + ip + '\'' + + ", port=" + port + + ", sourceId='" + sourceId + '\'' + + ", targetId='" + targetId + '\'' + + ", sendId='" + sendId + '\'' + + ", messageType='" + messageType + '\'' + + ", result=" + result + + ", companyId='" + companyId + '\'' + + ", hexCrc8='" + hexCrc8 + '\'' + + ", hexCrc16='" + hexCrc16 + '\'' + + ", strMsg='" + strMsg + '\'' + + '}'; + } +} diff --git a/src/main/java/com/fzzy/protocol/zldz/data/ReMessageBody.java b/src/main/java/com/fzzy/protocol/zldz/data/ReMessageBody.java new file mode 100644 index 0000000..402c829 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/data/ReMessageBody.java @@ -0,0 +1,18 @@ +package com.fzzy.protocol.zldz.data; + +import lombok.Data; + +/** + * 娑堟伅-娑堟伅浣� + * + * @author Andy + * + */ +@Data +public class ReMessageBody { + + private int length; + + private String content; + +} diff --git a/src/main/java/com/fzzy/protocol/zldz/package-info.java b/src/main/java/com/fzzy/protocol/zldz/package-info.java new file mode 100644 index 0000000..5125e1a --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/package-info.java @@ -0,0 +1,4 @@ +/** + * 姝f潵鐢靛瓙-绮儏鍗忚瑙f瀽 + */ +package com.fzzy.protocol.zldz; \ No newline at end of file diff --git a/src/main/java/com/fzzy/protocol/zldz/server/HeartbeatProviderImpl.java b/src/main/java/com/fzzy/protocol/zldz/server/HeartbeatProviderImpl.java new file mode 100644 index 0000000..4785ebf --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/server/HeartbeatProviderImpl.java @@ -0,0 +1,25 @@ +package com.fzzy.protocol.zldz.server; + +import com.ld.io.api.HeartbeatProvider; +import com.ld.io.api.IoSession; +import lombok.extern.slf4j.Slf4j; + + +/** + * 蹇冭烦缁存寔锛岄獙璇佸綋鍓嶈繛鎺ヤ腑鐨処P鍦ㄦ湇鍔″櫒涓婇厤缃殑鏈夋病鏈夛紝濡傛灉鏈夊垯琛ㄧず姝e父杩炴帴锛屽鏋滄病鏈夊垯韪㈤櫎杩炴帴銆� + * <p> + * 濡傛灉鏈夋甯稿績璺筹紝鍒欏彂閫佹甯稿績璺冲懡浠わ紝濡傛灉娌℃湁鍒欏彂閫佽澶囩姸鎬佹煡璇紝 + */ +@Slf4j +public class HeartbeatProviderImpl implements HeartbeatProvider { + + + @Override + public byte[] provide(IoSession session) { + + log.debug("========绯荤粺寮�濮嬫墽琛屽績璺�=========={}---{}", session.getCompanyId(), session.getBusinessKey()); + + return null; + } + +} diff --git a/src/main/java/com/fzzy/protocol/zldz/server/MsgConsumer.java b/src/main/java/com/fzzy/protocol/zldz/server/MsgConsumer.java new file mode 100644 index 0000000..9452d36 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/server/MsgConsumer.java @@ -0,0 +1,44 @@ +package com.fzzy.protocol.zldz.server; + +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.api.utils.SpringUtil; +import com.fzzy.protocol.bhzn.cmd.ReMessageBuilder; +import com.fzzy.protocol.bhzn.data.IoMessage; +import com.fzzy.protocol.bhzn.server.BhznGrainV2ServerUtils; +import com.fzzy.protocol.zldz.analysis.AnalysisService; +import com.ld.io.api.IoMsgConsumer; +import com.ld.io.api.IoSession; +import lombok.extern.slf4j.Slf4j; + +/** + * 鎺ユ敹缁堢杩斿洖鍥炴潵鐨勪俊鎭�� + * + * @author Andy + */ +@Slf4j +public class MsgConsumer implements IoMsgConsumer { + + + private AnalysisService analysis; + + @Override + public void consume(IoSession session, byte[] bytes) { + + try { + String hexStr = BytesUtil.bytesToString(bytes); + + log.debug("銆�" + session.getAddress() + "銆戜富鏈�------->>骞冲彴锛氭姤鏂囷細" + hexStr); + // hexStr = hexStr.substring(ZldzServerUtil.MSG_START.length()); + + if (null == analysis) { + analysis = (AnalysisService) SpringUtil.getBean(AnalysisService.BEAN_ID); + } + + analysis.analysis(session.getAddress(),session.getPort(), hexStr); + + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + +} diff --git a/src/main/java/com/fzzy/protocol/zldz/server/SessionListener.java b/src/main/java/com/fzzy/protocol/zldz/server/SessionListener.java new file mode 100644 index 0000000..ccf6678 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/server/SessionListener.java @@ -0,0 +1,59 @@ +package com.fzzy.protocol.zldz.server; + +import com.fzzy.api.Constant; +import com.fzzy.api.data.ApiCommonDevice; +import com.fzzy.api.utils.SpringUtil; +import com.fzzy.gateway.service.GatewayDeviceService; +import com.fzzy.protocol.bhzn.server.BhznGrainV2ServerUtils; +import com.ld.io.api.IoSession; +import com.ld.io.api.IoSessionListener; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class SessionListener implements IoSessionListener { + + + private GatewayDeviceService gatewayDeviceService; + + /** + * 璁惧鍒涘缓鍦ㄧ嚎锛岄渶瑕佹敞鎰忓綋鍓嶄娇鐢ㄤ富鏈烘ā寮忥紝涓绘満涓婄嚎榛樿鎵�鏈夌浉鍚孲N閰嶇疆鐨勫垎鏈哄叏閮ㄤ笂绾� + * + * @param session + */ + @Override + public void onCreate(IoSession session) { + + log.info("++++鏂板缓杩炴帴++++-IP={}锛孭ORT={}", session.getAddress(), session.getPort()); + + // 娣诲姞鑷畾涔変笟鍔D + session.setBusinessKey(BhznGrainV2ServerUtils.getServerKey(session.getAddress(), session.getPort())); + + //鎵ц鍒嗘満涓婄嚎 + ApiCommonDevice commonDevice = Constant.updateCacheOnline(session.getAddress(), session.getPort()); + if (null == commonDevice.getSn()) { + log.error("------璁惧涓婄嚎锛岀郴缁熶腑鏈彂鐜板綋鍓嶉厤缃�-------"); + } + } + + /** + * 娉ㄦ剰锛屽綋鍓嶉噰鐢ㄤ富鏈烘ā寮忥紝涓绘満绂荤嚎鎵�鏈夌浉鍚孲N閰嶇疆鐨勫垎鏈哄叏閮ㄧ绾� + * + * @param session + */ + @Override + public void onDestroy(IoSession session) { + log.info("----杩炴帴鏂紑-----IP={}锛孭ORT={}", session.getAddress(), session.getPort()); + + //璁剧疆鍒嗘満鎺夌嚎 + ApiCommonDevice commonDevice = Constant.updateCacheOffline(session.getAddress(), session.getPort()); + + if (null == commonDevice) return; + + if (null == gatewayDeviceService) { + gatewayDeviceService = SpringUtil.getBean(GatewayDeviceService.class); + + gatewayDeviceService.OfflineByCommonDevice(commonDevice); + } + + } +} diff --git a/src/main/java/com/fzzy/protocol/zldz/server/ZldzServerEngine.java b/src/main/java/com/fzzy/protocol/zldz/server/ZldzServerEngine.java new file mode 100644 index 0000000..78e0c88 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/server/ZldzServerEngine.java @@ -0,0 +1,85 @@ +package com.fzzy.protocol.zldz.server; + +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.protocol.fzzy.server.ServerUtils; +import com.ld.io.api.*; +import com.ld.io.netty.NettyServer; +import lombok.extern.slf4j.Slf4j; + +import java.util.List; + +/** + * 閰嶇疆涓绘湇鍔′俊鎭� + * + */ +@Slf4j +public class ZldzServerEngine { + + public static final int PORT = 19302; + + public static NettyServer ioServer = null; + // 閰嶇疆娑堟伅鎺ユ敹绫� + private static MsgConsumer messageConsume = new MsgConsumer(); + // 鐩戝惉浼氳瘽鐨勫垱寤轰笌閿�姣� + private static SessionListener ioSessionListener = new SessionListener(); + + // 蹇冭烦鎻愪緵 锛岃鏈嶅姟涓嶉渶瑕佸績璺崇淮鎸� + private static HeartbeatProvider heartbeatProvider = new HeartbeatProviderImpl(); + + + public static void start() { + start(PORT); + } + + public static void start(int port) { + // 閰嶇疆Server鐨勯厤缃� + IoServerOption ioServerOption = new IoServerOption(port); + + ioServerOption.setSplitDecoderType(SplitByteDecoderType.DELIMITER_SYMBOL); + ioServerOption.setDelimiter(BytesUtil.hexStrToBytes(ZldzServerUtil.MSG_END)); + + // 閰嶇疆蹇冭烦鎵ц鏃堕棿 + ioServerOption.setReaderIdleTime(60 * 60); + + ioServer = new NettyServer(ioServerOption, messageConsume, ioSessionListener, heartbeatProvider); + + ioServer.startup(); + + log.info("* ========================"); + log.info("* "); + log.info("* FZZY-ZLDZ绾伯鎯呭崗璁紝绔彛鍙�={}", port); + log.info("* "); + log.info("* ========================"); + + + System.out.println("* ========================"); + System.out.println("* "); + System.out.println("* FZZY-ZLDZ绾伯鎯呭崗璁紝绔彛鍙�=" + port); + System.out.println("* "); + System.out.println("* ========================"); + } + + /** + * 鍙戦�佷俊鎭� + * + * @param ip + * @param port + * @param msg + * @return + */ + public static InvokeResult push(String ip, int port, byte[] msg) { + IoSessionQuery sessionFactory = ioServer.getSessionQuery(); + List<IoSession> sessions = sessionFactory.getAllSession(); + IoSession session = null; + for (IoSession ioSession : sessions) { + if (ServerUtils.getServerKey(ip, port).equals(ioSession.getBusinessKey())) { + session = ioSession; + break; + } + } + if (null == session) { + return InvokeResult.CHANNEL_CLOSED; + } + return session.invoke(msg); + } +} diff --git a/src/main/java/com/fzzy/protocol/zldz/server/ZldzServerUtil.java b/src/main/java/com/fzzy/protocol/zldz/server/ZldzServerUtil.java new file mode 100644 index 0000000..a1bccd7 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/server/ZldzServerUtil.java @@ -0,0 +1,58 @@ +package com.fzzy.protocol.zldz.server; + +public class ZldzServerUtil { + + + public static final String MSG_START = "AAAA"; + public static final String MSG_END = "EEEE"; + + //榛樿缁勭粐缂栧彿 + public static final String DEFAULT_COMPANY = "0000"; + + + public static final String MSG_TYPE_1115 = "1115";// 鍚庡彴鑾峰彇绮儏鏁版嵁璇锋眰 + + public static final String MSG_TYPE_8815 = "8815";// 缁堢搴旂瓟绮儏鏁版嵁璇锋眰 + + public static final String MSG_TYPE_1116 = "1116";// 閰嶇疆璇诲彇鍒嗘満鍙傛暟 + + public static final String MSG_TYPE_8816 = "8816";// 閰嶇疆璇诲彇鍒嗘満鍙傛暟搴旂瓟 + + public static final String MSG_TYPE_1117 = "1117";// 缁堢鍙戦�佺伯鎯呮暟鎹粰鍚庡彴 + + public static final String MSG_TYPE_8817 = "8817";// 鍚庡彴鏀跺埌绮儏鏁版嵁搴旂瓟 + + public static final String MSG_TYPE_1122 = "1122";// 鍒濆鍖栫數缂嗙數缂� + + public static final String MSG_TYPE_1123 = "1123";// 鐢电紗姹囨�� + + public static final String MSG_TYPE_1125 = "1125";// 璇诲彇閫氶亾鐢电紗 + + public static final String MSG_TYPE_8825 = "8825";//璇诲彇閫氶亾鐢电紗搴旂瓟 + + public static final String MSG_TYPE_1126 = "1126";// 淇敼閫氶亾鐢电紗 + + public static final String MSG_TYPE_8826 = "8826";// 淇敼閫氶亾鐢电紗搴旂瓟 + + + public static final String MSG_TYPE_1128 = "1128";// 璇诲彇鏈夌嚎娓╂箍搴� + + public static final String MSG_TYPE_8822 = "8822";// 搴旂瓟鍒濆鍖栫數缂嗙數缂� + + public static final String MSG_TYPE_8823 = "8823";// 搴旂瓟鐢电紗姹囨�荤粨鏋� + + public static final String MSG_TYPE_8828 = "8828";// 璇诲彇鏈夌嚎娓╂箍搴﹀簲绛� + + public static final String MSG_TYPE_1129 = "1129";// 缁堢鍙戦�侀�氶亾鐢电紗鏁版嵁缁欏悗鍙� + + public static final String MSG_TYPE_8829 = "8829";// 鍚庡彴鏀跺埌鐢电紗鏁版嵁搴旂瓟 + + public static final String MSG_TYPE_112B = "112B";// 閲嶅惎鍒嗘満 + + public static final String MSG_TYPE_882B = "882B";// 閲嶅惎鍒嗘満搴旂瓟 + + + public static String getServerKey(String ip, int port) { + return ip + ":" + port; + } +} diff --git a/src/main/java/com/fzzy/protocol/zldz/service/ZldzGatewayGrainService.java b/src/main/java/com/fzzy/protocol/zldz/service/ZldzGatewayGrainService.java new file mode 100644 index 0000000..eb65882 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/service/ZldzGatewayGrainService.java @@ -0,0 +1,147 @@ +package com.fzzy.protocol.zldz.service; + +import com.fzzy.api.Constant; +import com.fzzy.api.data.ApiCommonDevice; +import com.fzzy.api.data.GatewayDeviceProtocol; +import com.fzzy.api.utils.BytesUtil; +import com.fzzy.gateway.api.GatewaySyncGranService; +import com.fzzy.gateway.data.BaseReqData; +import com.fzzy.gateway.data.BaseResp; +import com.fzzy.gateway.entity.GatewayDevice; +import com.fzzy.io.request.BaseRequest; +import com.fzzy.protocol.ProtocolUtils; +import com.fzzy.protocol.bhzn.cmd.CommandBuild; +import com.fzzy.protocol.bhzn.server.BhznGrainV2ServerEngine; +import com.fzzy.protocol.zldz.cmd.CommandBuilder1115; +import com.fzzy.protocol.zldz.cmd.CommandBuilder8815; +import com.fzzy.protocol.zldz.cmd.CommandBuilder8817; +import com.fzzy.protocol.zldz.data.Message; +import com.fzzy.protocol.zldz.server.ZldzServerEngine; +import com.fzzy.protocol.zldz.server.ZldzServerUtil; +import com.ld.io.api.InvokeResult; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + + +/** + * 姝f潵鐢靛瓙绮儏鍗忚瀹炵幇 + */ +@Slf4j +@Component +public class ZldzGatewayGrainService implements GatewaySyncGranService { + + @Override + public String getGrainProtocol() { + return GatewayDeviceProtocol.GRAIN_FZZY_ZLDZ_WEB.getCode(); + } + + @Override + public BaseResp syncGrain(BaseReqData reqData) { + + BaseResp resp = new BaseResp(); + + GatewayDevice device = reqData.getDevice(); + + //鑾峰彇杩炴帴涓殑璁惧淇℃伅 + ApiCommonDevice apiCommonDevice = Constant.getCommonDeviceCache(device.getDeviceSn()); + + if (null == apiCommonDevice) { + resp.setCode(500); + resp.setMsg("绯荤粺鏈幏鍙栧埌涓嬭杩炴帴璁惧淇℃伅锛屾棤娉曟墽琛�"); + log.error("----------------绯荤粺鏈幏鍙栧埌涓嬭杩炴帴璁惧淇℃伅,鏃犳硶鎵ц---------"); + return resp; + } + + if (Constant.YN_N.equals(apiCommonDevice.getStatus())) { + resp.setCode(500); + resp.setMsg("涓嬭璁惧涓嶅湪绾�---鏃犳硶鎵ц----" + device.getDeviceName()); + log.error("涓嬭璁惧涓嶅湪绾�---鏃犳硶鎵ц----" + device.getDeviceName()); + return resp; + } + + device.setIp(apiCommonDevice.getIp()); + device.setPort(apiCommonDevice.getPort()); + + reqData.setDevice(device); + + try { + //Step 璇锋眰淇℃伅鏀惧叆鍐呭瓨 + ProtocolUtils.addSyncReq2Map(device.getDepotIdSys(), reqData); + + // 鐢熸垚绮儏淇℃伅 + Message message = CommandBuilder1115.getInstance().buildMessage(reqData, apiCommonDevice); + + // 鍙戦�佸懡浠� + InvokeResult result = ZldzServerEngine.push(apiCommonDevice.getIp(), apiCommonDevice.getPort(), message.getByteMsg()); + + log.error("骞冲彴------>>>>鎺у埗鏌滐細鍙戦�佺伯鎯呮娴嬪懡浠�-{}", message); + + // 灏佽杩斿洖淇℃伅 + if (!InvokeResult.SUCCESS.getCode().equals(result.getCode())) { + log.error("骞冲彴------>>>>鎺у埗鏌滐細鍙戦�佺伯鎯呮娴嬪懡浠�-澶辫触{}", result.getMessage()); + resp.setCode(500); + resp.setMsg("骞冲彴------>>>>鎺у埗鏌滐細鍙戦�佺伯鎯呮娴嬪懡浠�-澶辫触锛�" + result.getMessage()); + } + + } catch (Exception e) { + log.error("绮儏妫�娴嬪紓甯革細{}", e); + resp.setCode(500); + resp.setMsg("骞冲彴------>>>>鎺у埗鏌滐細鍙戦�佺伯鎯呮娴嬪懡浠わ細" + e.getMessage()); + return resp; + } + return resp; + } + + /** + * 鏈嶅姟鍣ㄥ洖澶嶇粓绔殑鏀跺埌璇锋眰 + * + * @param request + */ + public BaseResp reply8815(BaseRequest request) { + BaseResp resp = new BaseResp(); + // Step1 鐢熸垚绮儏淇℃伅 + Message message = CommandBuilder8815.getInstance().buildMessage( + request); + + log.debug("骞冲彴------>>>鍒嗘満锛�" +request.getSerName() + "-绮儏鏈嶅姟绔洖澶嶅懡浠�=" + message.toString()); + + // 鍙戦�佸懡浠� + InvokeResult result = ZldzServerEngine.push(request.getIp(), request.getPort(), message.getByteMsg()); + // 灏佽杩斿洖淇℃伅 + if (!InvokeResult.SUCCESS.getCode().equals(result.getCode())) { + log.error("骞冲彴------>>>>鎺у埗鏌滐細鍙戦�佺伯鎯呮娴嬪懡浠�-澶辫触{}", result.getMessage()); + resp.setCode(500); + resp.setMsg("骞冲彴------>>>>鎺у埗鏌滐細鍙戦�佺伯鎯呮娴嬪懡浠�-澶辫触锛�" + result.getMessage()); + } + + return resp; + } + + /** + * 鍥炲鍛戒护 + * + * @param request + * @param hexCurPacket 褰撳墠鍖呯殑hex + */ + public BaseResp reply8817(BaseRequest request, String hexCurPacket) { + BaseResp resp = new BaseResp(); + // Step1 鐢熸垚绮儏淇℃伅锛屾妸淇℃伅鏀惧湪request + request.setBizType(hexCurPacket); + + Message message = CommandBuilder8817.getInstance().buildMessage(request); + + log.debug("骞冲彴------>>>鍒嗘満锛�" +request.getSerName() + "-绮儏鏈嶅姟绔洖澶嶆敹鍒扮伯鎯呭寘鍛戒护=" + message.toString()); + + // 鍙戦�佸懡浠� + InvokeResult result = ZldzServerEngine.push(request.getIp(), request.getPort(), + message.getByteMsg()); + // 灏佽杩斿洖淇℃伅 + if (!InvokeResult.SUCCESS.getCode().equals(result.getCode())) { + log.error("骞冲彴------>>>>鎺у埗鏌滐細鍙戦�佺伯鎯呮娴嬪懡浠�-澶辫触{}", result.getMessage()); + resp.setCode(500); + resp.setMsg("骞冲彴------>>>>鎺у埗鏌滐細鍙戦�佺伯鎯呮娴嬪懡浠�-澶辫触锛�" + result.getMessage()); + } + + return resp; + } +} diff --git a/src/main/java/com/fzzy/protocol/zldz/util/CRC16.java b/src/main/java/com/fzzy/protocol/zldz/util/CRC16.java new file mode 100644 index 0000000..1443919 --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/util/CRC16.java @@ -0,0 +1,129 @@ +package com.fzzy.protocol.zldz.util; + +/*** + * + * @author Andy + * + */ +public class CRC16 { + + static byte[] crc16_tab_h = { (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, + (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, + (byte) 0x81, (byte) 0x40 }; + + static byte[] crc16_tab_l = { (byte) 0x00, (byte) 0xC0, (byte) 0xC1, (byte) 0x01, (byte) 0xC3, (byte) 0x03, + (byte) 0x02, (byte) 0xC2, (byte) 0xC6, (byte) 0x06, (byte) 0x07, (byte) 0xC7, (byte) 0x05, (byte) 0xC5, + (byte) 0xC4, (byte) 0x04, (byte) 0xCC, (byte) 0x0C, (byte) 0x0D, (byte) 0xCD, (byte) 0x0F, (byte) 0xCF, + (byte) 0xCE, (byte) 0x0E, (byte) 0x0A, (byte) 0xCA, (byte) 0xCB, (byte) 0x0B, (byte) 0xC9, (byte) 0x09, + (byte) 0x08, (byte) 0xC8, (byte) 0xD8, (byte) 0x18, (byte) 0x19, (byte) 0xD9, (byte) 0x1B, (byte) 0xDB, + (byte) 0xDA, (byte) 0x1A, (byte) 0x1E, (byte) 0xDE, (byte) 0xDF, (byte) 0x1F, (byte) 0xDD, (byte) 0x1D, + (byte) 0x1C, (byte) 0xDC, (byte) 0x14, (byte) 0xD4, (byte) 0xD5, (byte) 0x15, (byte) 0xD7, (byte) 0x17, + (byte) 0x16, (byte) 0xD6, (byte) 0xD2, (byte) 0x12, (byte) 0x13, (byte) 0xD3, (byte) 0x11, (byte) 0xD1, + (byte) 0xD0, (byte) 0x10, (byte) 0xF0, (byte) 0x30, (byte) 0x31, (byte) 0xF1, (byte) 0x33, (byte) 0xF3, + (byte) 0xF2, (byte) 0x32, (byte) 0x36, (byte) 0xF6, (byte) 0xF7, (byte) 0x37, (byte) 0xF5, (byte) 0x35, + (byte) 0x34, (byte) 0xF4, (byte) 0x3C, (byte) 0xFC, (byte) 0xFD, (byte) 0x3D, (byte) 0xFF, (byte) 0x3F, + (byte) 0x3E, (byte) 0xFE, (byte) 0xFA, (byte) 0x3A, (byte) 0x3B, (byte) 0xFB, (byte) 0x39, (byte) 0xF9, + (byte) 0xF8, (byte) 0x38, (byte) 0x28, (byte) 0xE8, (byte) 0xE9, (byte) 0x29, (byte) 0xEB, (byte) 0x2B, + (byte) 0x2A, (byte) 0xEA, (byte) 0xEE, (byte) 0x2E, (byte) 0x2F, (byte) 0xEF, (byte) 0x2D, (byte) 0xED, + (byte) 0xEC, (byte) 0x2C, (byte) 0xE4, (byte) 0x24, (byte) 0x25, (byte) 0xE5, (byte) 0x27, (byte) 0xE7, + (byte) 0xE6, (byte) 0x26, (byte) 0x22, (byte) 0xE2, (byte) 0xE3, (byte) 0x23, (byte) 0xE1, (byte) 0x21, + (byte) 0x20, (byte) 0xE0, (byte) 0xA0, (byte) 0x60, (byte) 0x61, (byte) 0xA1, (byte) 0x63, (byte) 0xA3, + (byte) 0xA2, (byte) 0x62, (byte) 0x66, (byte) 0xA6, (byte) 0xA7, (byte) 0x67, (byte) 0xA5, (byte) 0x65, + (byte) 0x64, (byte) 0xA4, (byte) 0x6C, (byte) 0xAC, (byte) 0xAD, (byte) 0x6D, (byte) 0xAF, (byte) 0x6F, + (byte) 0x6E, (byte) 0xAE, (byte) 0xAA, (byte) 0x6A, (byte) 0x6B, (byte) 0xAB, (byte) 0x69, (byte) 0xA9, + (byte) 0xA8, (byte) 0x68, (byte) 0x78, (byte) 0xB8, (byte) 0xB9, (byte) 0x79, (byte) 0xBB, (byte) 0x7B, + (byte) 0x7A, (byte) 0xBA, (byte) 0xBE, (byte) 0x7E, (byte) 0x7F, (byte) 0xBF, (byte) 0x7D, (byte) 0xBD, + (byte) 0xBC, (byte) 0x7C, (byte) 0xB4, (byte) 0x74, (byte) 0x75, (byte) 0xB5, (byte) 0x77, (byte) 0xB7, + (byte) 0xB6, (byte) 0x76, (byte) 0x72, (byte) 0xB2, (byte) 0xB3, (byte) 0x73, (byte) 0xB1, (byte) 0x71, + (byte) 0x70, (byte) 0xB0, (byte) 0x50, (byte) 0x90, (byte) 0x91, (byte) 0x51, (byte) 0x93, (byte) 0x53, + (byte) 0x52, (byte) 0x92, (byte) 0x96, (byte) 0x56, (byte) 0x57, (byte) 0x97, (byte) 0x55, (byte) 0x95, + (byte) 0x94, (byte) 0x54, (byte) 0x9C, (byte) 0x5C, (byte) 0x5D, (byte) 0x9D, (byte) 0x5F, (byte) 0x9F, + (byte) 0x9E, (byte) 0x5E, (byte) 0x5A, (byte) 0x9A, (byte) 0x9B, (byte) 0x5B, (byte) 0x99, (byte) 0x59, + (byte) 0x58, (byte) 0x98, (byte) 0x88, (byte) 0x48, (byte) 0x49, (byte) 0x89, (byte) 0x4B, (byte) 0x8B, + (byte) 0x8A, (byte) 0x4A, (byte) 0x4E, (byte) 0x8E, (byte) 0x8F, (byte) 0x4F, (byte) 0x8D, (byte) 0x4D, + (byte) 0x4C, (byte) 0x8C, (byte) 0x44, (byte) 0x84, (byte) 0x85, (byte) 0x45, (byte) 0x87, (byte) 0x47, + (byte) 0x46, (byte) 0x86, (byte) 0x82, (byte) 0x42, (byte) 0x43, (byte) 0x83, (byte) 0x41, (byte) 0x81, + (byte) 0x80, (byte) 0x40 }; + + /** + * 璁$畻CRC16鏍¢獙 + * + * @param data + * 闇�瑕佽绠楃殑鏁扮粍 + * @return CRC16鏍¢獙鍊� + */ + public static int calcCrc16(byte[] data) { + return calcCrc16(data, 0, data.length); + } + + /** + * 璁$畻CRC16鏍¢獙 + * + * @param data + * 闇�瑕佽绠楃殑鏁扮粍 + * @param offset + * 璧峰浣嶇疆 + * @param len + * 闀垮害 + * @return CRC16鏍¢獙鍊� + */ + public static int calcCrc16(byte[] data, int offset, int len) { + return calcCrc16(data, offset, len, 0xffff); + } + + /** + * 璁$畻CRC16鏍¢獙 + * + * @param data + * 闇�瑕佽绠楃殑鏁扮粍 + * @param offset + * 璧峰浣嶇疆 + * @param len + * 闀垮害 + * @param preval + * 涔嬪墠鐨勬牎楠屽�� + * @return CRC16鏍¢獙鍊� + */ + public static int calcCrc16(byte[] data, int offset, int len, int preval) { + int ucCRCHi = (preval & 0xff00) >> 8; + int ucCRCLo = preval & 0x00ff; + int iIndex; + for (int i = 0; i < len; ++i) { + iIndex = (ucCRCLo ^ data[offset + i]) & 0x00ff; + ucCRCLo = ucCRCHi ^ crc16_tab_h[iIndex]; + ucCRCHi = crc16_tab_l[iIndex]; + } + return ((ucCRCHi & 0x00ff) << 8) | (ucCRCLo & 0x00ff) & 0xffff; + } + +} diff --git a/src/main/java/com/fzzy/protocol/zldz/util/CRC8.java b/src/main/java/com/fzzy/protocol/zldz/util/CRC8.java new file mode 100644 index 0000000..91cd47f --- /dev/null +++ b/src/main/java/com/fzzy/protocol/zldz/util/CRC8.java @@ -0,0 +1,182 @@ +package com.fzzy.protocol.zldz.util; + +/** + *璁$畻CRC8鏍¢獙鍊� + */ +public class CRC8 { + + static short[] gCrc16Table = { (short) 0x0000, (short) 0x1021, (short) 0x2042, (short) 0x3063, (short) 0x4084, + (short) 0x50a5, (short) 0x60c6, (short) 0x70e7, (short) 0xd94c, (short) 0xc96d, (short) 0xf90e, + (short) 0xe92f, (short) 0x99c8, (short) 0x89e9, (short) 0xb98a, (short) 0xa9ab, (short) 0x5844, + (short) 0x4865, (short) 0x7806, (short) 0x6827, (short) 0x18c0, (short) 0x08e1, (short) 0x3882, + (short) 0x28a3, (short) 0xcb7d, (short) 0xdb5c, (short) 0xeb3f, (short) 0xfb1e, (short) 0x8bf9, + (short) 0x9bd8, (short) 0xabbb, (short) 0xbb9a, (short) 0x4a75, (short) 0x5a54, (short) 0x6a37, + (short) 0x7a16, (short) 0x0af1, (short) 0x1ad0, (short) 0x2ab3, (short) 0x3a92, (short) 0xfd2e, + (short) 0xed0f, (short) 0xdd6c, (short) 0xcd4d, (short) 0xbdaa, (short) 0xad8b, (short) 0x9de8, + (short) 0x8dc9, (short) 0xc9cc, (short) 0xd9ed, (short) 0xe98e, (short) 0xf9af, (short) 0x8948, + (short) 0x9969, (short) 0xa90a, (short) 0xb92b, (short) 0x5af5, (short) 0x4ad4, (short) 0x7ab7, + (short) 0x6a96, (short) 0x1a71, (short) 0x0a50, (short) 0x3a33, (short) 0x2a12, (short) 0xdbfd, + (short) 0xcbdc, (short) 0xfbbf, (short) 0xeb9e, (short) 0x9b79, (short) 0x8b58, (short) 0xbb3b, + (short) 0xab1a, (short) 0x6ca6, (short) 0x7c87, (short) 0x4ce4, (short) 0x5cc5, (short) 0x2c22, + (short) 0x3c03, (short) 0x0c60, (short) 0x1c41, (short) 0x8108, (short) 0x9129, (short) 0xa14a, + (short) 0xb16b, (short) 0xc18c, (short) 0xd1ad, (short) 0xe1ce, (short) 0xf1ef, (short) 0x1231, + (short) 0x0210, (short) 0x3273, (short) 0x2252, (short) 0x52b5, (short) 0x4294, (short) 0x72f7, + (short) 0x62d6, (short) 0x9339, (short) 0x8318, (short) 0xb37b, (short) 0xa35a, (short) 0xd3bd, + (short) 0xc39c, (short) 0xf3ff, (short) 0xe3de, (short) 0x2462, (short) 0x3443, (short) 0x0420, + (short) 0x1401, (short) 0x64e6, (short) 0x74c7, (short) 0x44a4, (short) 0x5485, (short) 0xa56a, + (short) 0xb54b, (short) 0x8528, (short) 0x9509, (short) 0xe5ee, (short) 0xf5cf, (short) 0xc5ac, + (short) 0xd58d, (short) 0x3653, (short) 0x2672, (short) 0x1611, (short) 0x0630, (short) 0x76d7, + (short) 0x66f6, (short) 0x5695, (short) 0x46b4, (short) 0xb75b, (short) 0xa77a, (short) 0x9719, + (short) 0x8738, (short) 0xf7df, (short) 0xe7fe, (short) 0xd79d, (short) 0xc7bc, (short) 0x48c4, + (short) 0x58e5, (short) 0x6886, (short) 0x78a7, (short) 0x0840, (short) 0x1861, (short) 0x2802, + (short) 0x3823, (short) 0x83b9, (short) 0x9398, (short) 0xa3fb, (short) 0xb3da, (short) 0xc33d, + (short) 0xd31c, (short) 0xe37f, (short) 0xf35e, (short) 0x02b1, (short) 0x1290, (short) 0x22f3, + (short) 0x32d2, (short) 0x4235, (short) 0x5214, (short) 0x6277, (short) 0x7256, (short) 0xb5ea, + (short) 0xa5cb, (short) 0x95a8, (short) 0x8589, (short) 0xf56e, (short) 0xe54f, (short) 0xd52c, + (short) 0xc50d, (short) 0x34e2, (short) 0x24c3, (short) 0x14a0, (short) 0x0481, (short) 0x7466, + (short) 0x6447, (short) 0x5424, (short) 0x4405, (short) 0xa7db, (short) 0xb7fa, (short) 0x8799, + (short) 0x97b8, (short) 0xe75f, (short) 0xf77e, (short) 0xc71d, (short) 0xd73c, (short) 0x26d3, + (short) 0x36f2, (short) 0x0691, (short) 0x16b0, (short) 0x6657, (short) 0x7676, (short) 0x4615, + (short) 0x5634, (short) 0xedae, (short) 0xfd8f, (short) 0xcdec, (short) 0xddcd, (short) 0xad2a, + (short) 0xbd0b, (short) 0x8d68, (short) 0x9d49, (short) 0x7e97, (short) 0x6eb6, (short) 0x5ed5, + (short) 0x4ef4, (short) 0x3e13, (short) 0x2e32, (short) 0x1e51, (short) 0x0e70, (short) 0xff9f, + (short) 0xefbe, (short) 0xdfdd, (short) 0xcffc, (short) 0xbf1b, (short) 0xaf3a, (short) 0x9f59, + (short) 0x8f78, (short) 0x9188, (short) 0x81a9, (short) 0xb1ca, (short) 0xa1eb, (short) 0xd10c, + (short) 0xc12d, (short) 0xf14e, (short) 0xe16f, (short) 0x1080, (short) 0x00a1, (short) 0x30c2, + (short) 0x20e3, (short) 0x5004, (short) 0x4025, (short) 0x7046, (short) 0x6067, (short) 0x7c26, + (short) 0x6c07, (short) 0x5c64, (short) 0x4c45, (short) 0x3ca2, (short) 0x2c83, (short) 0x1ce0, + (short) 0x0cc1, (short) 0xef1f, (short) 0xff3e, (short) 0xcf5d, (short) 0xdf7c, (short) 0xaf9b, + (short) 0xbfba, (short) 0x8fd9, (short) 0x9ff8, (short) 0x6e17, (short) 0x7e36, (short) 0x4e55, + (short) 0x5e74, (short) 0x2e93, (short) 0x3eb2, (short) 0x0ed1, (short) 0x1ef0 }; + + static short[] crc8_tab = { (short) 0x00, (short) 0x07, (short) 0x0E, (short) 0x09, (short) 0x1C, (short) 0x1B, + (short) 0x12, (short) 0x15, (short) 0x38, (short) 0x3F, (short) 0x36, (short) 0x31, (short) 0x24, + (short) 0x23, (short) 0x2A, (short) 0x2D, (short) 0x70, (short) 0x77, (short) 0x7E, (short) 0x79, + (short) 0x6C, (short) 0x6B, (short) 0x62, (short) 0x65, (short) 0x48, (short) 0x4F, (short) 0x46, + (short) 0x41, (short) 0x54, (short) 0x53, (short) 0x5A, (short) 0x5D, (short) 0xE0, (short) 0xE7, + (short) 0xEE, (short) 0xE9, (short) 0xFC, (short) 0xFB, (short) 0xF2, (short) 0xF5, (short) 0xD8, + (short) 0xDF, (short) 0xD6, (short) 0xD1, (short) 0xC4, (short) 0xC3, (short) 0xCA, (short) 0xCD, + (short) 0x90, (short) 0x97, (short) 0x9E, (short) 0x99, (short) 0x8C, (short) 0x8B, (short) 0x82, + (short) 0x85, (short) 0xA8, (short) 0xAF, (short) 0xA6, (short) 0xA1, (short) 0xB4, (short) 0xB3, + (short) 0xBA, (short) 0xBD, (short) 0xC7, (short) 0xC0, (short) 0xC9, (short) 0xCE, (short) 0xDB, + (short) 0xDC, (short) 0xD5, (short) 0xD2, (short) 0xFF, (short) 0xF8, (short) 0xF1, (short) 0xF6, + (short) 0xE3, (short) 0xE4, (short) 0xED, (short) 0xEA, (short) 0xB7, (short) 0xB0, (short) 0xB9, + (short) 0xBE, (short) 0xAB, (short) 0xAC, (short) 0xA5, (short) 0xA2, (short) 0x8F, (short) 0x88, + (short) 0x81, (short) 0x86, (short) 0x93, (short) 0x94, (short) 0x9D, (short) 0x9A, (short) 0x27, + (short) 0x20, (short) 0x29, (short) 0x2E, (short) 0x3B, (short) 0x3C, (short) 0x35, (short) 0x32, + (short) 0x1F, (short) 0x18, (short) 0x11, (short) 0x16, (short) 0x03, (short) 0x04, (short) 0x0D, + (short) 0x0A, (short) 0x57, (short) 0x50, (short) 0x59, (short) 0x5E, (short) 0x4B, (short) 0x4C, + (short) 0x45, (short) 0x42, (short) 0x6F, (short) 0x68, (short) 0x61, (short) 0x66, (short) 0x73, + (short) 0x74, (short) 0x7D, (short) 0x7A, (short) 0x89, (short) 0x8E, (short) 0x87, (short) 0x80, + (short) 0x95, (short) 0x92, (short) 0x9B, (short) 0x9C, (short) 0xB1, (short) 0xB6, (short) 0xBF, + (short) 0xB8, (short) 0xAD, (short) 0xAA, (short) 0xA3, (short) 0xA4, (short) 0xF9, (short) 0xFE, + (short) 0xF7, (short) 0xF0, (short) 0xE5, (short) 0xE2, (short) 0xEB, (short) 0xEC, (short) 0xC1, + (short) 0xC6, (short) 0xCF, (short) 0xC8, (short) 0xDD, (short) 0xDA, (short) 0xD3, (short) 0xD4, + (short) 0x69, (short) 0x6E, (short) 0x67, (short) 0x60, (short) 0x75, (short) 0x72, (short) 0x7B, + (short) 0x7C, (short) 0x51, (short) 0x56, (short) 0x5F, (short) 0x58, (short) 0x4D, (short) 0x4A, + (short) 0x43, (short) 0x44, (short) 0x19, (short) 0x1E, (short) 0x17, (short) 0x10, (short) 0x05, + (short) 0x02, (short) 0x0B, (short) 0x0C, (short) 0x21, (short) 0x26, (short) 0x2F, (short) 0x28, + (short) 0x3D, (short) 0x3A, (short) 0x33, (short) 0x34, (short) 0x4E, (short) 0x49, (short) 0x40, + (short) 0x47, (short) 0x52, (short) 0x55, (short) 0x5C, (short) 0x5B, (short) 0x76, (short) 0x71, + (short) 0x78, (short) 0x7F, (short) 0x6A, (short) 0x6D, (short) 0x64, (short) 0x63, (short) 0x3E, + (short) 0x39, (short) 0x30, (short) 0x37, (short) 0x22, (short) 0x25, (short) 0x2C, (short) 0x2B, + (short) 0x06, (short) 0x01, (short) 0x08, (short) 0x0F, (short) 0x1A, (short) 0x1D, (short) 0x14, + (short) 0x13, (short) 0xAE, (short) 0xA9, (short) 0xA0, (short) 0xA7, (short) 0xB2, (short) 0xB5, + (short) 0xBC, (short) 0xBB, (short) 0x96, (short) 0x91, (short) 0x98, (short) 0x9F, (short) 0x8A, + (short) 0x8D, (short) 0x84, (short) 0x83, (short) 0xDE, (short) 0xD9, (short) 0xD0, (short) 0xD7, + (short) 0xC2, (short) 0xC5, (short) 0xCC, (short) 0xCB, (short) 0xE6, (short) 0xE1, (short) 0xE8, + (short) 0xEF, (short) 0xFA, (short) 0xFD, (short) 0xF4, (short) 0xF3 }; + + static byte[] crc8_tab2 = { (byte) 0x00, (byte) 0x07, (byte) 0x0E, (byte) 0x09, (byte) 0x1C, (byte) 0x1B, + (byte) 0x12, (byte) 0x15, (byte) 0x38, (byte) 0x3F, (byte) 0x36, (byte) 0x31, (byte) 0x24, (byte) 0x23, + (byte) 0x2A, (byte) 0x2D, (byte) 0x70, (byte) 0x77, (byte) 0x7E, (byte) 0x79, (byte) 0x6C, (byte) 0x6B, + (byte) 0x62, (byte) 0x65, (byte) 0x48, (byte) 0x4F, (byte) 0x46, (byte) 0x41, (byte) 0x54, (byte) 0x53, + (byte) 0x5A, (byte) 0x5D, (byte) 0xE0, (byte) 0xE7, (byte) 0xEE, (byte) 0xE9, (byte) 0xFC, (byte) 0xFB, + (byte) 0xF2, (byte) 0xF5, (byte) 0xD8, (byte) 0xDF, (byte) 0xD6, (byte) 0xD1, (byte) 0xC4, (byte) 0xC3, + (byte) 0xCA, (byte) 0xCD, (byte) 0x90, (byte) 0x97, (byte) 0x9E, (byte) 0x99, (byte) 0x8C, (byte) 0x8B, + (byte) 0x82, (byte) 0x85, (byte) 0xA8, (byte) 0xAF, (byte) 0xA6, (byte) 0xA1, (byte) 0xB4, (byte) 0xB3, + (byte) 0xBA, (byte) 0xBD, (byte) 0xC7, (byte) 0xC0, (byte) 0xC9, (byte) 0xCE, (byte) 0xDB, (byte) 0xDC, + (byte) 0xD5, (byte) 0xD2, (byte) 0xFF, (byte) 0xF8, (byte) 0xF1, (byte) 0xF6, (byte) 0xE3, (byte) 0xE4, + (byte) 0xED, (byte) 0xEA, (byte) 0xB7, (byte) 0xB0, (byte) 0xB9, (byte) 0xBE, (byte) 0xAB, (byte) 0xAC, + (byte) 0xA5, (byte) 0xA2, (byte) 0x8F, (byte) 0x88, (byte) 0x81, (byte) 0x86, (byte) 0x93, (byte) 0x94, + (byte) 0x9D, (byte) 0x9A, (byte) 0x27, (byte) 0x20, (byte) 0x29, (byte) 0x2E, (byte) 0x3B, (byte) 0x3C, + (byte) 0x35, (byte) 0x32, (byte) 0x1F, (byte) 0x18, (byte) 0x11, (byte) 0x16, (byte) 0x03, (byte) 0x04, + (byte) 0x0D, (byte) 0x0A, (byte) 0x57, (byte) 0x50, (byte) 0x59, (byte) 0x5E, (byte) 0x4B, (byte) 0x4C, + (byte) 0x45, (byte) 0x42, (byte) 0x6F, (byte) 0x68, (byte) 0x61, (byte) 0x66, (byte) 0x73, (byte) 0x74, + (byte) 0x7D, (byte) 0x7A, (byte) 0x89, (byte) 0x8E, (byte) 0x87, (byte) 0x80, (byte) 0x95, (byte) 0x92, + (byte) 0x9B, (byte) 0x9C, (byte) 0xB1, (byte) 0xB6, (byte) 0xBF, (byte) 0xB8, (byte) 0xAD, (byte) 0xAA, + (byte) 0xA3, (byte) 0xA4, (byte) 0xF9, (byte) 0xFE, (byte) 0xF7, (byte) 0xF0, (byte) 0xE5, (byte) 0xE2, + (byte) 0xEB, (byte) 0xEC, (byte) 0xC1, (byte) 0xC6, (byte) 0xCF, (byte) 0xC8, (byte) 0xDD, (byte) 0xDA, + (byte) 0xD3, (byte) 0xD4, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0x60, (byte) 0x75, (byte) 0x72, + (byte) 0x7B, (byte) 0x7C, (byte) 0x51, (byte) 0x56, (byte) 0x5F, (byte) 0x58, (byte) 0x4D, (byte) 0x4A, + (byte) 0x43, (byte) 0x44, (byte) 0x19, (byte) 0x1E, (byte) 0x17, (byte) 0x10, (byte) 0x05, (byte) 0x02, + (byte) 0x0B, (byte) 0x0C, (byte) 0x21, (byte) 0x26, (byte) 0x2F, (byte) 0x28, (byte) 0x3D, (byte) 0x3A, + (byte) 0x33, (byte) 0x34, (byte) 0x4E, (byte) 0x49, (byte) 0x40, (byte) 0x47, (byte) 0x52, (byte) 0x55, + (byte) 0x5C, (byte) 0x5B, (byte) 0x76, (byte) 0x71, (byte) 0x78, (byte) 0x7F, (byte) 0x6A, (byte) 0x6D, + (byte) 0x64, (byte) 0x63, (byte) 0x3E, (byte) 0x39, (byte) 0x30, (byte) 0x37, (byte) 0x22, (byte) 0x25, + (byte) 0x2C, (byte) 0x2B, (byte) 0x06, (byte) 0x01, (byte) 0x08, (byte) 0x0F, (byte) 0x1A, (byte) 0x1D, + (byte) 0x14, (byte) 0x13, (byte) 0xAE, (byte) 0xA9, (byte) 0xA0, (byte) 0xA7, (byte) 0xB2, (byte) 0xB5, + (byte) 0xBC, (byte) 0xBB, (byte) 0x96, (byte) 0x91, (byte) 0x98, (byte) 0x9F, (byte) 0x8A, (byte) 0x8D, + (byte) 0x84, (byte) 0x83, (byte) 0xDE, (byte) 0xD9, (byte) 0xD0, (byte) 0xD7, (byte) 0xC2, (byte) 0xC5, + (byte) 0xCC, (byte) 0xCB, (byte) 0xE6, (byte) 0xE1, (byte) 0xE8, (byte) 0xEF, (byte) 0xFA, (byte) 0xFD, + (byte) 0xF4, (byte) 0xF3 }; + + /** + * 璁$畻鏁扮粍鐨凜RC8鏍¢獙鍊� + * + * @param data + * 闇�瑕佽绠楃殑鏁扮粍 + * @return CRC8鏍¢獙鍊� + */ + public static byte calcCrc8(byte[] data) { + return calcCrc8(data, (short) 2, (short) (data.length - 2), (byte) 0); + } + + /** + * 璁$畻CRC8鏍¢獙鍊� + * + * @param data + * 鏁版嵁 + * @param offset + * 璧峰浣嶇疆 + * @param len + * 闀垮害 + * @return 鏍¢獙鍊� + */ + public static byte calcCrc8(byte[] data, short offset, short len) { + return calcCrc8(data, offset, len, (byte) 0); + } + + public static byte calcCrc8(byte[] ucPtr, short offset, short ucLen, byte preval) { + short ucIndex; // CRC8鏍¢獙琛ㄦ牸绱㈠紩 + short ucCRC8 = 0; // CRC8瀛楄妭鍒濆鍖� + short i = 0; + // 杩涜CRC8浣嶆牎楠� + while ((ucLen--) > 0) { + short indexI = (short) (i + offset); + short data = ucPtr[indexI]; + if (data < 0) { + int sd = Byte.toUnsignedInt(ucPtr[indexI]); + data = (short) sd; + } + ucIndex = (short) (ucCRC8 ^ data); + ucCRC8 = crc8_tab[(short) ucIndex]; + i++; + } + // 杩斿洖CRC8鏍¢獙鏁版嵁 + return (byte) (~ucCRC8); + } + + public static short crc16Byte(short crc, byte data) { + return (short) ((crc << 8) ^ gCrc16Table[((crc >> 8) ^ data) & 0xff]); + } + + public static short calculateCrc16(byte[] buf, short offset, short length) { + short crc = 0; + short i = 0; + while ((length--) > 0) { + crc = crc16Byte(crc, buf[offset + (i++)]); + } + return crc; + } +} -- Gitblit v1.9.3