diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java new file mode 100644 index 0000000..ab1e0a6 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java @@ -0,0 +1,82 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class CRCUtil { + + /** + * 将十六进制的字符串 求出 CRC 8 的 CRC_H CRC_L + */ + public static String getCRC8(String msg) { + //十六进制的字符串转换成字节数组 + byte[] bytes = hexStrToBinaryStr(msg); + //计算CRC8 + byte crc8 = CRC8(bytes, 0, bytes.length); + String crc8ToStr = "" + Integer.toHexString(0xff & crc8); + return crc8ToStr; + } + + /** + * 将十六进制的字符串转换成字节数组 + */ + public static byte[] hexStrToBinaryStr(String hexString) { + hexString = hexString.replaceAll(" ", ""); + if ((hexString.length() % 2) != 0) { + throw new IllegalArgumentException("长度不是偶数"); + } + int len = hexString.length(); + int index = 0; + byte[] bytes = new byte[len / 2]; + while (index < len) { + String sub = hexString.substring(index, index + 2); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); + index += 2; + } + return bytes; + } + + /** + * CRC8 校验 多项式 x8+x2+x+1 + * + * @param b + * @param off + * @param len + * @return + */ + public static byte CRC8(final byte[] b, final int off, final int len) { + byte result = 0x00; + for (int i = 0; i < len; i++) { + result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF]; + } + return (byte) (result & 0xFFL); + } + + private final static byte[] CHECKSUM_TABLE = {(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}; + +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java new file mode 100644 index 0000000..ab1e0a6 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java @@ -0,0 +1,82 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class CRCUtil { + + /** + * 将十六进制的字符串 求出 CRC 8 的 CRC_H CRC_L + */ + public static String getCRC8(String msg) { + //十六进制的字符串转换成字节数组 + byte[] bytes = hexStrToBinaryStr(msg); + //计算CRC8 + byte crc8 = CRC8(bytes, 0, bytes.length); + String crc8ToStr = "" + Integer.toHexString(0xff & crc8); + return crc8ToStr; + } + + /** + * 将十六进制的字符串转换成字节数组 + */ + public static byte[] hexStrToBinaryStr(String hexString) { + hexString = hexString.replaceAll(" ", ""); + if ((hexString.length() % 2) != 0) { + throw new IllegalArgumentException("长度不是偶数"); + } + int len = hexString.length(); + int index = 0; + byte[] bytes = new byte[len / 2]; + while (index < len) { + String sub = hexString.substring(index, index + 2); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); + index += 2; + } + return bytes; + } + + /** + * CRC8 校验 多项式 x8+x2+x+1 + * + * @param b + * @param off + * @param len + * @return + */ + public static byte CRC8(final byte[] b, final int off, final int len) { + byte result = 0x00; + for (int i = 0; i < len; i++) { + result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF]; + } + return (byte) (result & 0xFFL); + } + + private final static byte[] CHECKSUM_TABLE = {(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}; + +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java new file mode 100644 index 0000000..3478ab3 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java @@ -0,0 +1,36 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class MyUtils { + public static int[] byteToInt(byte[] Mybytes, int len) { + int[] RetInt = new int[len / 4]; + int i32Count = len / 4; + int i32Index = 0; + + for (int i32Increase = 0; i32Increase < i32Count; i32Increase++) { + RetInt[i32Increase] = transform(Mybytes[3 + i32Index]) | transform(Mybytes[2 + i32Index]) << 8 | transform(Mybytes[1 + i32Index]) << 16 | transform(Mybytes[0 + i32Index]) << 24; + i32Index += 4; + } + + return RetInt; + } + + public static byte[] IntToBytes(int[] MyInts, int len) { + byte[] RetBytes = new byte[len * 4]; + + for (int i32Index = 0; i32Index < len; i32Index++) { + RetBytes[0 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 24) & 0xFF); + RetBytes[1 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 16) & 0xFF); + RetBytes[2 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 8) & 0xFF); + RetBytes[3 + i32Index * 4] = (byte) (MyInts[i32Index] & 0xFF); + } + return RetBytes; + } + + private static int transform(byte temp) { + int tempInt = (int) temp; + if (tempInt < 0) { + tempInt += 256; + } + return tempInt; + } +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java new file mode 100644 index 0000000..ab1e0a6 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java @@ -0,0 +1,82 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class CRCUtil { + + /** + * 将十六进制的字符串 求出 CRC 8 的 CRC_H CRC_L + */ + public static String getCRC8(String msg) { + //十六进制的字符串转换成字节数组 + byte[] bytes = hexStrToBinaryStr(msg); + //计算CRC8 + byte crc8 = CRC8(bytes, 0, bytes.length); + String crc8ToStr = "" + Integer.toHexString(0xff & crc8); + return crc8ToStr; + } + + /** + * 将十六进制的字符串转换成字节数组 + */ + public static byte[] hexStrToBinaryStr(String hexString) { + hexString = hexString.replaceAll(" ", ""); + if ((hexString.length() % 2) != 0) { + throw new IllegalArgumentException("长度不是偶数"); + } + int len = hexString.length(); + int index = 0; + byte[] bytes = new byte[len / 2]; + while (index < len) { + String sub = hexString.substring(index, index + 2); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); + index += 2; + } + return bytes; + } + + /** + * CRC8 校验 多项式 x8+x2+x+1 + * + * @param b + * @param off + * @param len + * @return + */ + public static byte CRC8(final byte[] b, final int off, final int len) { + byte result = 0x00; + for (int i = 0; i < len; i++) { + result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF]; + } + return (byte) (result & 0xFFL); + } + + private final static byte[] CHECKSUM_TABLE = {(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}; + +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java new file mode 100644 index 0000000..3478ab3 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java @@ -0,0 +1,36 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class MyUtils { + public static int[] byteToInt(byte[] Mybytes, int len) { + int[] RetInt = new int[len / 4]; + int i32Count = len / 4; + int i32Index = 0; + + for (int i32Increase = 0; i32Increase < i32Count; i32Increase++) { + RetInt[i32Increase] = transform(Mybytes[3 + i32Index]) | transform(Mybytes[2 + i32Index]) << 8 | transform(Mybytes[1 + i32Index]) << 16 | transform(Mybytes[0 + i32Index]) << 24; + i32Index += 4; + } + + return RetInt; + } + + public static byte[] IntToBytes(int[] MyInts, int len) { + byte[] RetBytes = new byte[len * 4]; + + for (int i32Index = 0; i32Index < len; i32Index++) { + RetBytes[0 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 24) & 0xFF); + RetBytes[1 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 16) & 0xFF); + RetBytes[2 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 8) & 0xFF); + RetBytes[3 + i32Index * 4] = (byte) (MyInts[i32Index] & 0xFF); + } + return RetBytes; + } + + private static int transform(byte temp) { + int tempInt = (int) temp; + if (tempInt < 0) { + tempInt += 256; + } + return tempInt; + } +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java index a3ee58b..f509a64 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java @@ -1,5 +1,6 @@ package com.casic.missiles.codec.decoder.safe.impl; +import io.netty.buffer.ByteBufUtil; import org.springframework.stereotype.Service; /** @@ -14,106 +15,109 @@ * @version v1.0: Tea.java, v 0.1 2021/3/12 13:48 $ */ @Service("tea") +///tea算法类 + public class Tea { - //加密 - public byte[] encrypt(byte[] content, int offset, int[] key, int times) {//times为加密轮数 - int[] tempInt = byteToInt(content, offset); - int y = tempInt[0], z = tempInt[1], sum = 0, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; - for (i = 0; i < times; i++) { - sum += delta; - y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + public static byte[] MyTea_Encrypt(byte[] MyTeaContent, int ByteLen, byte[] MyTeakey) { + int[] Value = MyUtils.byteToInt(MyTeaContent, ByteLen); + int sum = 0; + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + + + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + for (int i = 0; i < Value.length; i++) { + int v0 = Value[0], v1 = Value[1]; + for (; i32Index < i32times; i32Index++) { + sum += i32delta; + v0 += ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + v1 += ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + } + Value[0] = v0; + Value[1] = v1; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + int len = ByteLen / 4; + return MyUtils.IntToBytes(Value, len); } - //解密 - public byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times) { - int[] tempInt = byteToInt(encryptContent, offset); - int y = tempInt[0], z = tempInt[1], sum = 0xC6EF3720, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; + /** + * 参数为8字节的明文输入和16字节的密钥,输出8字节密文 + * + * @param MyTeaSecContent + * @param MyTeakey + * @return + */ + public static byte[] MyTea_Decrypt(byte[] MyTeaSecContent, int ByteLen, byte[] MyTeakey) { + System.out.println("解密函数:"); + int[] Value = MyUtils.byteToInt(MyTeaSecContent, ByteLen); + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + int sum = i32delta * i32times; - for (i = 0; i < times; i++) { - z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); - y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - sum -= delta; + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + int v0 = Value[0], v1 = Value[1]; + + for (; i32Index < i32times; i32Index++) { + v1 -= ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + v0 -= ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + sum -= i32delta; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + Value[0] = v0; + Value[1] = v1; + + + int len = ByteLen / 4; + + return MyUtils.IntToBytes(Value, len); } - //byte[]型数据转成int[]型数据 - private int[] byteToInt(byte[] content, int offset) { - - int[] result = new int[content.length >> 2]; //除以2的n次方 == 右移n位 即 content.length / 4 == content.length >> 2 - for (int i = 0, j = offset; j < content.length; i++, j += 4) { - result[i] = transform(content[j + 3]) | transform(content[j + 2]) << 8 | - transform(content[j + 1]) << 16 | (int) content[j] << 24; - } - return result; - - } - - //int[]型数据转成byte[]型数据 - private byte[] intToByte(int[] content, int offset) { - byte[] result = new byte[content.length << 2]; //乘以2的n次方 == 左移n位 即 content.length * 4 == content.length << 2 - for (int i = 0, j = offset; j < result.length; i++, j += 4) { - result[j + 3] = (byte) (content[i] & 0xff); - result[j + 2] = (byte) ((content[i] >> 8) & 0xff); - result[j + 1] = (byte) ((content[i] >> 16) & 0xff); - result[j] = (byte) ((content[i] >> 24) & 0xff); - } - return result; - } - - //若某字节被解释成负的则需将其转成无符号正数 - private static int transform(byte temp) { - int tempInt = (int) temp; - if (tempInt < 0) { - tempInt += 256; - } - return tempInt; - } - - // 测试代码如下: public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; - int[] KEY = new int[]{//加密解密所用的KEY - 0x789f5645, 0xf68bd5a4, - 0x81963ffa, 0x458fac58 - }; - Tea tea = new Tea(); + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37, + 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - byte[] info = new byte[]{ + byte[] demo2 = {0x10, 0x00, 0x00, 0x51, 0x00, 0x06, 0x12, 0x0B, 0x09, 0x0A, 0x33, 0x0E, 0x00, 0x00, 0x00, 0x00}; - 1, 2, 3, 4, 5, 6, 7, 8 - }; - System.out.print("原数据:"); - for (byte i : info) { - System.out.print(i + " "); + byte[] MyKeys = {48, 49, 50, 51, + 52, 53, 54, 55, + 56, 57, 97, 98, + 99, 100, 101, 102}; + + byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, + 0x0D, 0x0E, 0x0F, 0x10}; + + System.out.println("原始数据为:" + ByteBufUtil.hexDump(demo2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + byte[] retEnBytes = MyTea_Encrypt(demo2, demo2.length, MyKeys); + System.out.println("加密后为:" + ByteBufUtil.hexDump(retEnBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retEnBytes[i32Index]) + " "); } System.out.println(); - byte[] secretInfo = tea.encrypt(info, 0, KEY, 32); - System.out.print("加密后的数据:"); - for (byte i : secretInfo) { - System.out.print(i + " "); + byte[] retDeBytes = MyTea_Decrypt(retEnBytes, retEnBytes.length, MyKeys); + System.out.println("解密后为:" + ByteBufUtil.hexDump(retDeBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retDeBytes[i32Index]) + " "); } - System.out.println(); - - byte[] decryptInfo = tea.decrypt(secretInfo, 0, KEY, 32); - System.out.print("解密后的数据:"); - for (byte i : decryptInfo) { - System.out.print(i + " "); - } - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java new file mode 100644 index 0000000..ab1e0a6 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java @@ -0,0 +1,82 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class CRCUtil { + + /** + * 将十六进制的字符串 求出 CRC 8 的 CRC_H CRC_L + */ + public static String getCRC8(String msg) { + //十六进制的字符串转换成字节数组 + byte[] bytes = hexStrToBinaryStr(msg); + //计算CRC8 + byte crc8 = CRC8(bytes, 0, bytes.length); + String crc8ToStr = "" + Integer.toHexString(0xff & crc8); + return crc8ToStr; + } + + /** + * 将十六进制的字符串转换成字节数组 + */ + public static byte[] hexStrToBinaryStr(String hexString) { + hexString = hexString.replaceAll(" ", ""); + if ((hexString.length() % 2) != 0) { + throw new IllegalArgumentException("长度不是偶数"); + } + int len = hexString.length(); + int index = 0; + byte[] bytes = new byte[len / 2]; + while (index < len) { + String sub = hexString.substring(index, index + 2); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); + index += 2; + } + return bytes; + } + + /** + * CRC8 校验 多项式 x8+x2+x+1 + * + * @param b + * @param off + * @param len + * @return + */ + public static byte CRC8(final byte[] b, final int off, final int len) { + byte result = 0x00; + for (int i = 0; i < len; i++) { + result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF]; + } + return (byte) (result & 0xFFL); + } + + private final static byte[] CHECKSUM_TABLE = {(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}; + +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java new file mode 100644 index 0000000..3478ab3 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java @@ -0,0 +1,36 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class MyUtils { + public static int[] byteToInt(byte[] Mybytes, int len) { + int[] RetInt = new int[len / 4]; + int i32Count = len / 4; + int i32Index = 0; + + for (int i32Increase = 0; i32Increase < i32Count; i32Increase++) { + RetInt[i32Increase] = transform(Mybytes[3 + i32Index]) | transform(Mybytes[2 + i32Index]) << 8 | transform(Mybytes[1 + i32Index]) << 16 | transform(Mybytes[0 + i32Index]) << 24; + i32Index += 4; + } + + return RetInt; + } + + public static byte[] IntToBytes(int[] MyInts, int len) { + byte[] RetBytes = new byte[len * 4]; + + for (int i32Index = 0; i32Index < len; i32Index++) { + RetBytes[0 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 24) & 0xFF); + RetBytes[1 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 16) & 0xFF); + RetBytes[2 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 8) & 0xFF); + RetBytes[3 + i32Index * 4] = (byte) (MyInts[i32Index] & 0xFF); + } + return RetBytes; + } + + private static int transform(byte temp) { + int tempInt = (int) temp; + if (tempInt < 0) { + tempInt += 256; + } + return tempInt; + } +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java index a3ee58b..f509a64 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java @@ -1,5 +1,6 @@ package com.casic.missiles.codec.decoder.safe.impl; +import io.netty.buffer.ByteBufUtil; import org.springframework.stereotype.Service; /** @@ -14,106 +15,109 @@ * @version v1.0: Tea.java, v 0.1 2021/3/12 13:48 $ */ @Service("tea") +///tea算法类 + public class Tea { - //加密 - public byte[] encrypt(byte[] content, int offset, int[] key, int times) {//times为加密轮数 - int[] tempInt = byteToInt(content, offset); - int y = tempInt[0], z = tempInt[1], sum = 0, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; - for (i = 0; i < times; i++) { - sum += delta; - y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + public static byte[] MyTea_Encrypt(byte[] MyTeaContent, int ByteLen, byte[] MyTeakey) { + int[] Value = MyUtils.byteToInt(MyTeaContent, ByteLen); + int sum = 0; + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + + + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + for (int i = 0; i < Value.length; i++) { + int v0 = Value[0], v1 = Value[1]; + for (; i32Index < i32times; i32Index++) { + sum += i32delta; + v0 += ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + v1 += ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + } + Value[0] = v0; + Value[1] = v1; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + int len = ByteLen / 4; + return MyUtils.IntToBytes(Value, len); } - //解密 - public byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times) { - int[] tempInt = byteToInt(encryptContent, offset); - int y = tempInt[0], z = tempInt[1], sum = 0xC6EF3720, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; + /** + * 参数为8字节的明文输入和16字节的密钥,输出8字节密文 + * + * @param MyTeaSecContent + * @param MyTeakey + * @return + */ + public static byte[] MyTea_Decrypt(byte[] MyTeaSecContent, int ByteLen, byte[] MyTeakey) { + System.out.println("解密函数:"); + int[] Value = MyUtils.byteToInt(MyTeaSecContent, ByteLen); + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + int sum = i32delta * i32times; - for (i = 0; i < times; i++) { - z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); - y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - sum -= delta; + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + int v0 = Value[0], v1 = Value[1]; + + for (; i32Index < i32times; i32Index++) { + v1 -= ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + v0 -= ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + sum -= i32delta; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + Value[0] = v0; + Value[1] = v1; + + + int len = ByteLen / 4; + + return MyUtils.IntToBytes(Value, len); } - //byte[]型数据转成int[]型数据 - private int[] byteToInt(byte[] content, int offset) { - - int[] result = new int[content.length >> 2]; //除以2的n次方 == 右移n位 即 content.length / 4 == content.length >> 2 - for (int i = 0, j = offset; j < content.length; i++, j += 4) { - result[i] = transform(content[j + 3]) | transform(content[j + 2]) << 8 | - transform(content[j + 1]) << 16 | (int) content[j] << 24; - } - return result; - - } - - //int[]型数据转成byte[]型数据 - private byte[] intToByte(int[] content, int offset) { - byte[] result = new byte[content.length << 2]; //乘以2的n次方 == 左移n位 即 content.length * 4 == content.length << 2 - for (int i = 0, j = offset; j < result.length; i++, j += 4) { - result[j + 3] = (byte) (content[i] & 0xff); - result[j + 2] = (byte) ((content[i] >> 8) & 0xff); - result[j + 1] = (byte) ((content[i] >> 16) & 0xff); - result[j] = (byte) ((content[i] >> 24) & 0xff); - } - return result; - } - - //若某字节被解释成负的则需将其转成无符号正数 - private static int transform(byte temp) { - int tempInt = (int) temp; - if (tempInt < 0) { - tempInt += 256; - } - return tempInt; - } - - // 测试代码如下: public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; - int[] KEY = new int[]{//加密解密所用的KEY - 0x789f5645, 0xf68bd5a4, - 0x81963ffa, 0x458fac58 - }; - Tea tea = new Tea(); + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37, + 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - byte[] info = new byte[]{ + byte[] demo2 = {0x10, 0x00, 0x00, 0x51, 0x00, 0x06, 0x12, 0x0B, 0x09, 0x0A, 0x33, 0x0E, 0x00, 0x00, 0x00, 0x00}; - 1, 2, 3, 4, 5, 6, 7, 8 - }; - System.out.print("原数据:"); - for (byte i : info) { - System.out.print(i + " "); + byte[] MyKeys = {48, 49, 50, 51, + 52, 53, 54, 55, + 56, 57, 97, 98, + 99, 100, 101, 102}; + + byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, + 0x0D, 0x0E, 0x0F, 0x10}; + + System.out.println("原始数据为:" + ByteBufUtil.hexDump(demo2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + byte[] retEnBytes = MyTea_Encrypt(demo2, demo2.length, MyKeys); + System.out.println("加密后为:" + ByteBufUtil.hexDump(retEnBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retEnBytes[i32Index]) + " "); } System.out.println(); - byte[] secretInfo = tea.encrypt(info, 0, KEY, 32); - System.out.print("加密后的数据:"); - for (byte i : secretInfo) { - System.out.print(i + " "); + byte[] retDeBytes = MyTea_Decrypt(retEnBytes, retEnBytes.length, MyKeys); + System.out.println("解密后为:" + ByteBufUtil.hexDump(retDeBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retDeBytes[i32Index]) + " "); } - System.out.println(); - - byte[] decryptInfo = tea.decrypt(secretInfo, 0, KEY, 32); - System.out.print("解密后的数据:"); - for (byte i : decryptInfo) { - System.out.print(i + " "); - } - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java new file mode 100644 index 0000000..6e3432c --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java @@ -0,0 +1,186 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +import io.netty.buffer.ByteBufUtil; + +import java.util.ArrayList; +import java.util.List; + +public class TeaDemo { + + + static List tea_encrypt(int v1,int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y =v1, z = v2, sum = 0, i; + int delta = 0x9e3779b9; + //UInt32 a = k1, b = k2, c = k3, d = k4; + + for (i = 0; i < 32; i++) { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * tea解密 + *参数:v:要解密的数据,长度为8字节 + * k:解密用的key,长度为16字节 + **********************************************************************/ + + static List tea_decrypt(int v1, int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y = v1, z = v2, sum = 0xC6EF3720, i; + int delta = 0x9e3779b9; + //UInt32 a = k[0], b = k[1], c = k[2], d = k[3]; + + for (i = 0; i < 32; i++) { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * 加密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:密文的字节数 + **********************************************************************/ + + public static int encrypt(byte[] src, int size_src, byte[] TEA_key) { + int a = 0; + int i = 0; + int num = 0; +// Byte[] TEA_key = new Byte[]{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; + //将明文补足为8字节的倍数 + a = (int) (size_src % 8); + if (a != 0) { + for (i = 0; i < 8 - a; i++) { + src[size_src++] = 0; + } + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //加密 + num = (int) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1= (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2= (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + + List result=tea_encrypt(src1,src2, Key1, Key2, Key3, Key4); +// src1=result.get(0); +// src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1>> 16); + src[i * 8 + 1] = (byte) (src1>> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("加密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + /********************************************************************* + * 解密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:明文的字节数,如果失败,返回0 + **********************************************************************/ + + public static int decrypt(byte[] src, int size_src, byte[] TEA_key) { + int i = 0; + int num = 0; + //判断长度是否为8的倍数 + if (size_src % 8 != 0) { + //printf("\r\nchang du bu shi 8!!"); + return 0; + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //解密 + num = (byte) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_decrypt((UInt32*)(src + i * 8), (UInt32*)key); + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1 = (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2 = (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + List result=tea_decrypt(src1,src2, Key1, Key2, Key3, Key4); + src1=result.get(0); + src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1 >> 16); + src[i * 8 + 1] = (byte) (src1 >> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("解密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; + + + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37 + , 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + byte[] MyKey1 = {0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66}; +// byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, +// 0x05, 0x06, 0x07, 0x08, +// 0x09, 0x0A, 0x0B, 0x0C, +// 0x0D, 0x0E, 0x0F, 0x10}; + + +// System.out.println("原始数据为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + encrypt(info2, info2.length, MyKey1); + System.out.println("加密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retEnBytes[i32Index]) + " "); +// } +// System.out.println(); + decrypt(info2, info2.length, MyKey1); +// System.out.println("解密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retDeBytes[i32Index]) + " "); +// } + } +} diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java new file mode 100644 index 0000000..ab1e0a6 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java @@ -0,0 +1,82 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class CRCUtil { + + /** + * 将十六进制的字符串 求出 CRC 8 的 CRC_H CRC_L + */ + public static String getCRC8(String msg) { + //十六进制的字符串转换成字节数组 + byte[] bytes = hexStrToBinaryStr(msg); + //计算CRC8 + byte crc8 = CRC8(bytes, 0, bytes.length); + String crc8ToStr = "" + Integer.toHexString(0xff & crc8); + return crc8ToStr; + } + + /** + * 将十六进制的字符串转换成字节数组 + */ + public static byte[] hexStrToBinaryStr(String hexString) { + hexString = hexString.replaceAll(" ", ""); + if ((hexString.length() % 2) != 0) { + throw new IllegalArgumentException("长度不是偶数"); + } + int len = hexString.length(); + int index = 0; + byte[] bytes = new byte[len / 2]; + while (index < len) { + String sub = hexString.substring(index, index + 2); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); + index += 2; + } + return bytes; + } + + /** + * CRC8 校验 多项式 x8+x2+x+1 + * + * @param b + * @param off + * @param len + * @return + */ + public static byte CRC8(final byte[] b, final int off, final int len) { + byte result = 0x00; + for (int i = 0; i < len; i++) { + result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF]; + } + return (byte) (result & 0xFFL); + } + + private final static byte[] CHECKSUM_TABLE = {(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}; + +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java new file mode 100644 index 0000000..3478ab3 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java @@ -0,0 +1,36 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class MyUtils { + public static int[] byteToInt(byte[] Mybytes, int len) { + int[] RetInt = new int[len / 4]; + int i32Count = len / 4; + int i32Index = 0; + + for (int i32Increase = 0; i32Increase < i32Count; i32Increase++) { + RetInt[i32Increase] = transform(Mybytes[3 + i32Index]) | transform(Mybytes[2 + i32Index]) << 8 | transform(Mybytes[1 + i32Index]) << 16 | transform(Mybytes[0 + i32Index]) << 24; + i32Index += 4; + } + + return RetInt; + } + + public static byte[] IntToBytes(int[] MyInts, int len) { + byte[] RetBytes = new byte[len * 4]; + + for (int i32Index = 0; i32Index < len; i32Index++) { + RetBytes[0 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 24) & 0xFF); + RetBytes[1 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 16) & 0xFF); + RetBytes[2 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 8) & 0xFF); + RetBytes[3 + i32Index * 4] = (byte) (MyInts[i32Index] & 0xFF); + } + return RetBytes; + } + + private static int transform(byte temp) { + int tempInt = (int) temp; + if (tempInt < 0) { + tempInt += 256; + } + return tempInt; + } +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java index a3ee58b..f509a64 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java @@ -1,5 +1,6 @@ package com.casic.missiles.codec.decoder.safe.impl; +import io.netty.buffer.ByteBufUtil; import org.springframework.stereotype.Service; /** @@ -14,106 +15,109 @@ * @version v1.0: Tea.java, v 0.1 2021/3/12 13:48 $ */ @Service("tea") +///tea算法类 + public class Tea { - //加密 - public byte[] encrypt(byte[] content, int offset, int[] key, int times) {//times为加密轮数 - int[] tempInt = byteToInt(content, offset); - int y = tempInt[0], z = tempInt[1], sum = 0, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; - for (i = 0; i < times; i++) { - sum += delta; - y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + public static byte[] MyTea_Encrypt(byte[] MyTeaContent, int ByteLen, byte[] MyTeakey) { + int[] Value = MyUtils.byteToInt(MyTeaContent, ByteLen); + int sum = 0; + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + + + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + for (int i = 0; i < Value.length; i++) { + int v0 = Value[0], v1 = Value[1]; + for (; i32Index < i32times; i32Index++) { + sum += i32delta; + v0 += ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + v1 += ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + } + Value[0] = v0; + Value[1] = v1; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + int len = ByteLen / 4; + return MyUtils.IntToBytes(Value, len); } - //解密 - public byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times) { - int[] tempInt = byteToInt(encryptContent, offset); - int y = tempInt[0], z = tempInt[1], sum = 0xC6EF3720, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; + /** + * 参数为8字节的明文输入和16字节的密钥,输出8字节密文 + * + * @param MyTeaSecContent + * @param MyTeakey + * @return + */ + public static byte[] MyTea_Decrypt(byte[] MyTeaSecContent, int ByteLen, byte[] MyTeakey) { + System.out.println("解密函数:"); + int[] Value = MyUtils.byteToInt(MyTeaSecContent, ByteLen); + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + int sum = i32delta * i32times; - for (i = 0; i < times; i++) { - z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); - y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - sum -= delta; + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + int v0 = Value[0], v1 = Value[1]; + + for (; i32Index < i32times; i32Index++) { + v1 -= ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + v0 -= ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + sum -= i32delta; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + Value[0] = v0; + Value[1] = v1; + + + int len = ByteLen / 4; + + return MyUtils.IntToBytes(Value, len); } - //byte[]型数据转成int[]型数据 - private int[] byteToInt(byte[] content, int offset) { - - int[] result = new int[content.length >> 2]; //除以2的n次方 == 右移n位 即 content.length / 4 == content.length >> 2 - for (int i = 0, j = offset; j < content.length; i++, j += 4) { - result[i] = transform(content[j + 3]) | transform(content[j + 2]) << 8 | - transform(content[j + 1]) << 16 | (int) content[j] << 24; - } - return result; - - } - - //int[]型数据转成byte[]型数据 - private byte[] intToByte(int[] content, int offset) { - byte[] result = new byte[content.length << 2]; //乘以2的n次方 == 左移n位 即 content.length * 4 == content.length << 2 - for (int i = 0, j = offset; j < result.length; i++, j += 4) { - result[j + 3] = (byte) (content[i] & 0xff); - result[j + 2] = (byte) ((content[i] >> 8) & 0xff); - result[j + 1] = (byte) ((content[i] >> 16) & 0xff); - result[j] = (byte) ((content[i] >> 24) & 0xff); - } - return result; - } - - //若某字节被解释成负的则需将其转成无符号正数 - private static int transform(byte temp) { - int tempInt = (int) temp; - if (tempInt < 0) { - tempInt += 256; - } - return tempInt; - } - - // 测试代码如下: public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; - int[] KEY = new int[]{//加密解密所用的KEY - 0x789f5645, 0xf68bd5a4, - 0x81963ffa, 0x458fac58 - }; - Tea tea = new Tea(); + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37, + 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - byte[] info = new byte[]{ + byte[] demo2 = {0x10, 0x00, 0x00, 0x51, 0x00, 0x06, 0x12, 0x0B, 0x09, 0x0A, 0x33, 0x0E, 0x00, 0x00, 0x00, 0x00}; - 1, 2, 3, 4, 5, 6, 7, 8 - }; - System.out.print("原数据:"); - for (byte i : info) { - System.out.print(i + " "); + byte[] MyKeys = {48, 49, 50, 51, + 52, 53, 54, 55, + 56, 57, 97, 98, + 99, 100, 101, 102}; + + byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, + 0x0D, 0x0E, 0x0F, 0x10}; + + System.out.println("原始数据为:" + ByteBufUtil.hexDump(demo2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + byte[] retEnBytes = MyTea_Encrypt(demo2, demo2.length, MyKeys); + System.out.println("加密后为:" + ByteBufUtil.hexDump(retEnBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retEnBytes[i32Index]) + " "); } System.out.println(); - byte[] secretInfo = tea.encrypt(info, 0, KEY, 32); - System.out.print("加密后的数据:"); - for (byte i : secretInfo) { - System.out.print(i + " "); + byte[] retDeBytes = MyTea_Decrypt(retEnBytes, retEnBytes.length, MyKeys); + System.out.println("解密后为:" + ByteBufUtil.hexDump(retDeBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retDeBytes[i32Index]) + " "); } - System.out.println(); - - byte[] decryptInfo = tea.decrypt(secretInfo, 0, KEY, 32); - System.out.print("解密后的数据:"); - for (byte i : decryptInfo) { - System.out.print(i + " "); - } - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java new file mode 100644 index 0000000..6e3432c --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java @@ -0,0 +1,186 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +import io.netty.buffer.ByteBufUtil; + +import java.util.ArrayList; +import java.util.List; + +public class TeaDemo { + + + static List tea_encrypt(int v1,int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y =v1, z = v2, sum = 0, i; + int delta = 0x9e3779b9; + //UInt32 a = k1, b = k2, c = k3, d = k4; + + for (i = 0; i < 32; i++) { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * tea解密 + *参数:v:要解密的数据,长度为8字节 + * k:解密用的key,长度为16字节 + **********************************************************************/ + + static List tea_decrypt(int v1, int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y = v1, z = v2, sum = 0xC6EF3720, i; + int delta = 0x9e3779b9; + //UInt32 a = k[0], b = k[1], c = k[2], d = k[3]; + + for (i = 0; i < 32; i++) { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * 加密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:密文的字节数 + **********************************************************************/ + + public static int encrypt(byte[] src, int size_src, byte[] TEA_key) { + int a = 0; + int i = 0; + int num = 0; +// Byte[] TEA_key = new Byte[]{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; + //将明文补足为8字节的倍数 + a = (int) (size_src % 8); + if (a != 0) { + for (i = 0; i < 8 - a; i++) { + src[size_src++] = 0; + } + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //加密 + num = (int) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1= (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2= (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + + List result=tea_encrypt(src1,src2, Key1, Key2, Key3, Key4); +// src1=result.get(0); +// src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1>> 16); + src[i * 8 + 1] = (byte) (src1>> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("加密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + /********************************************************************* + * 解密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:明文的字节数,如果失败,返回0 + **********************************************************************/ + + public static int decrypt(byte[] src, int size_src, byte[] TEA_key) { + int i = 0; + int num = 0; + //判断长度是否为8的倍数 + if (size_src % 8 != 0) { + //printf("\r\nchang du bu shi 8!!"); + return 0; + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //解密 + num = (byte) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_decrypt((UInt32*)(src + i * 8), (UInt32*)key); + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1 = (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2 = (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + List result=tea_decrypt(src1,src2, Key1, Key2, Key3, Key4); + src1=result.get(0); + src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1 >> 16); + src[i * 8 + 1] = (byte) (src1 >> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("解密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; + + + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37 + , 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + byte[] MyKey1 = {0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66}; +// byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, +// 0x05, 0x06, 0x07, 0x08, +// 0x09, 0x0A, 0x0B, 0x0C, +// 0x0D, 0x0E, 0x0F, 0x10}; + + +// System.out.println("原始数据为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + encrypt(info2, info2.length, MyKey1); + System.out.println("加密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retEnBytes[i32Index]) + " "); +// } +// System.out.println(); + decrypt(info2, info2.length, MyKey1); +// System.out.println("解密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retDeBytes[i32Index]) + " "); +// } + } +} diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java index e5fbc76..958b59d 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java @@ -41,7 +41,7 @@ Integer upback = 0; if (!ObjectUtil.isEmpty(UnpackConfig)) { while (hasNextFullFrame(byteBuf, protocolConfig)) { - upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig); + upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig,null); if (upback == 1){//使用esayRule //表示可以截取 cutOffPackageBuilder(byteBuf, protocolConfig, totalLength); @@ -85,13 +85,14 @@ Map currentFrameFixedProperty = fieldParser.getFixedProperty(protocolConfig, currentbyteBuf); if (StringUtils.isEmpty(standardFreame)) { standardFreame += hexDump.substring(0, currentFrameFixedProperty.get("tailPosition")); - tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition") + 1, hexDump.length()); + tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition")); } else { - standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")); + standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")-1); } } standardFreame += tail; bufferContent.writeBytes(standardFreame.getBytes(Charset.forName("ISO-8859-1"))); + System.out.println(ByteBufUtil.hexDump(bufferContent)); codeGeneratorProvider.codeGenerator(bufferContent, protocolConfig); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java new file mode 100644 index 0000000..ab1e0a6 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java @@ -0,0 +1,82 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class CRCUtil { + + /** + * 将十六进制的字符串 求出 CRC 8 的 CRC_H CRC_L + */ + public static String getCRC8(String msg) { + //十六进制的字符串转换成字节数组 + byte[] bytes = hexStrToBinaryStr(msg); + //计算CRC8 + byte crc8 = CRC8(bytes, 0, bytes.length); + String crc8ToStr = "" + Integer.toHexString(0xff & crc8); + return crc8ToStr; + } + + /** + * 将十六进制的字符串转换成字节数组 + */ + public static byte[] hexStrToBinaryStr(String hexString) { + hexString = hexString.replaceAll(" ", ""); + if ((hexString.length() % 2) != 0) { + throw new IllegalArgumentException("长度不是偶数"); + } + int len = hexString.length(); + int index = 0; + byte[] bytes = new byte[len / 2]; + while (index < len) { + String sub = hexString.substring(index, index + 2); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); + index += 2; + } + return bytes; + } + + /** + * CRC8 校验 多项式 x8+x2+x+1 + * + * @param b + * @param off + * @param len + * @return + */ + public static byte CRC8(final byte[] b, final int off, final int len) { + byte result = 0x00; + for (int i = 0; i < len; i++) { + result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF]; + } + return (byte) (result & 0xFFL); + } + + private final static byte[] CHECKSUM_TABLE = {(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}; + +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java new file mode 100644 index 0000000..3478ab3 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java @@ -0,0 +1,36 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class MyUtils { + public static int[] byteToInt(byte[] Mybytes, int len) { + int[] RetInt = new int[len / 4]; + int i32Count = len / 4; + int i32Index = 0; + + for (int i32Increase = 0; i32Increase < i32Count; i32Increase++) { + RetInt[i32Increase] = transform(Mybytes[3 + i32Index]) | transform(Mybytes[2 + i32Index]) << 8 | transform(Mybytes[1 + i32Index]) << 16 | transform(Mybytes[0 + i32Index]) << 24; + i32Index += 4; + } + + return RetInt; + } + + public static byte[] IntToBytes(int[] MyInts, int len) { + byte[] RetBytes = new byte[len * 4]; + + for (int i32Index = 0; i32Index < len; i32Index++) { + RetBytes[0 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 24) & 0xFF); + RetBytes[1 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 16) & 0xFF); + RetBytes[2 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 8) & 0xFF); + RetBytes[3 + i32Index * 4] = (byte) (MyInts[i32Index] & 0xFF); + } + return RetBytes; + } + + private static int transform(byte temp) { + int tempInt = (int) temp; + if (tempInt < 0) { + tempInt += 256; + } + return tempInt; + } +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java index a3ee58b..f509a64 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java @@ -1,5 +1,6 @@ package com.casic.missiles.codec.decoder.safe.impl; +import io.netty.buffer.ByteBufUtil; import org.springframework.stereotype.Service; /** @@ -14,106 +15,109 @@ * @version v1.0: Tea.java, v 0.1 2021/3/12 13:48 $ */ @Service("tea") +///tea算法类 + public class Tea { - //加密 - public byte[] encrypt(byte[] content, int offset, int[] key, int times) {//times为加密轮数 - int[] tempInt = byteToInt(content, offset); - int y = tempInt[0], z = tempInt[1], sum = 0, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; - for (i = 0; i < times; i++) { - sum += delta; - y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + public static byte[] MyTea_Encrypt(byte[] MyTeaContent, int ByteLen, byte[] MyTeakey) { + int[] Value = MyUtils.byteToInt(MyTeaContent, ByteLen); + int sum = 0; + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + + + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + for (int i = 0; i < Value.length; i++) { + int v0 = Value[0], v1 = Value[1]; + for (; i32Index < i32times; i32Index++) { + sum += i32delta; + v0 += ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + v1 += ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + } + Value[0] = v0; + Value[1] = v1; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + int len = ByteLen / 4; + return MyUtils.IntToBytes(Value, len); } - //解密 - public byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times) { - int[] tempInt = byteToInt(encryptContent, offset); - int y = tempInt[0], z = tempInt[1], sum = 0xC6EF3720, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; + /** + * 参数为8字节的明文输入和16字节的密钥,输出8字节密文 + * + * @param MyTeaSecContent + * @param MyTeakey + * @return + */ + public static byte[] MyTea_Decrypt(byte[] MyTeaSecContent, int ByteLen, byte[] MyTeakey) { + System.out.println("解密函数:"); + int[] Value = MyUtils.byteToInt(MyTeaSecContent, ByteLen); + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + int sum = i32delta * i32times; - for (i = 0; i < times; i++) { - z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); - y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - sum -= delta; + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + int v0 = Value[0], v1 = Value[1]; + + for (; i32Index < i32times; i32Index++) { + v1 -= ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + v0 -= ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + sum -= i32delta; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + Value[0] = v0; + Value[1] = v1; + + + int len = ByteLen / 4; + + return MyUtils.IntToBytes(Value, len); } - //byte[]型数据转成int[]型数据 - private int[] byteToInt(byte[] content, int offset) { - - int[] result = new int[content.length >> 2]; //除以2的n次方 == 右移n位 即 content.length / 4 == content.length >> 2 - for (int i = 0, j = offset; j < content.length; i++, j += 4) { - result[i] = transform(content[j + 3]) | transform(content[j + 2]) << 8 | - transform(content[j + 1]) << 16 | (int) content[j] << 24; - } - return result; - - } - - //int[]型数据转成byte[]型数据 - private byte[] intToByte(int[] content, int offset) { - byte[] result = new byte[content.length << 2]; //乘以2的n次方 == 左移n位 即 content.length * 4 == content.length << 2 - for (int i = 0, j = offset; j < result.length; i++, j += 4) { - result[j + 3] = (byte) (content[i] & 0xff); - result[j + 2] = (byte) ((content[i] >> 8) & 0xff); - result[j + 1] = (byte) ((content[i] >> 16) & 0xff); - result[j] = (byte) ((content[i] >> 24) & 0xff); - } - return result; - } - - //若某字节被解释成负的则需将其转成无符号正数 - private static int transform(byte temp) { - int tempInt = (int) temp; - if (tempInt < 0) { - tempInt += 256; - } - return tempInt; - } - - // 测试代码如下: public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; - int[] KEY = new int[]{//加密解密所用的KEY - 0x789f5645, 0xf68bd5a4, - 0x81963ffa, 0x458fac58 - }; - Tea tea = new Tea(); + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37, + 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - byte[] info = new byte[]{ + byte[] demo2 = {0x10, 0x00, 0x00, 0x51, 0x00, 0x06, 0x12, 0x0B, 0x09, 0x0A, 0x33, 0x0E, 0x00, 0x00, 0x00, 0x00}; - 1, 2, 3, 4, 5, 6, 7, 8 - }; - System.out.print("原数据:"); - for (byte i : info) { - System.out.print(i + " "); + byte[] MyKeys = {48, 49, 50, 51, + 52, 53, 54, 55, + 56, 57, 97, 98, + 99, 100, 101, 102}; + + byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, + 0x0D, 0x0E, 0x0F, 0x10}; + + System.out.println("原始数据为:" + ByteBufUtil.hexDump(demo2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + byte[] retEnBytes = MyTea_Encrypt(demo2, demo2.length, MyKeys); + System.out.println("加密后为:" + ByteBufUtil.hexDump(retEnBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retEnBytes[i32Index]) + " "); } System.out.println(); - byte[] secretInfo = tea.encrypt(info, 0, KEY, 32); - System.out.print("加密后的数据:"); - for (byte i : secretInfo) { - System.out.print(i + " "); + byte[] retDeBytes = MyTea_Decrypt(retEnBytes, retEnBytes.length, MyKeys); + System.out.println("解密后为:" + ByteBufUtil.hexDump(retDeBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retDeBytes[i32Index]) + " "); } - System.out.println(); - - byte[] decryptInfo = tea.decrypt(secretInfo, 0, KEY, 32); - System.out.print("解密后的数据:"); - for (byte i : decryptInfo) { - System.out.print(i + " "); - } - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java new file mode 100644 index 0000000..6e3432c --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java @@ -0,0 +1,186 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +import io.netty.buffer.ByteBufUtil; + +import java.util.ArrayList; +import java.util.List; + +public class TeaDemo { + + + static List tea_encrypt(int v1,int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y =v1, z = v2, sum = 0, i; + int delta = 0x9e3779b9; + //UInt32 a = k1, b = k2, c = k3, d = k4; + + for (i = 0; i < 32; i++) { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * tea解密 + *参数:v:要解密的数据,长度为8字节 + * k:解密用的key,长度为16字节 + **********************************************************************/ + + static List tea_decrypt(int v1, int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y = v1, z = v2, sum = 0xC6EF3720, i; + int delta = 0x9e3779b9; + //UInt32 a = k[0], b = k[1], c = k[2], d = k[3]; + + for (i = 0; i < 32; i++) { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * 加密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:密文的字节数 + **********************************************************************/ + + public static int encrypt(byte[] src, int size_src, byte[] TEA_key) { + int a = 0; + int i = 0; + int num = 0; +// Byte[] TEA_key = new Byte[]{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; + //将明文补足为8字节的倍数 + a = (int) (size_src % 8); + if (a != 0) { + for (i = 0; i < 8 - a; i++) { + src[size_src++] = 0; + } + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //加密 + num = (int) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1= (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2= (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + + List result=tea_encrypt(src1,src2, Key1, Key2, Key3, Key4); +// src1=result.get(0); +// src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1>> 16); + src[i * 8 + 1] = (byte) (src1>> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("加密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + /********************************************************************* + * 解密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:明文的字节数,如果失败,返回0 + **********************************************************************/ + + public static int decrypt(byte[] src, int size_src, byte[] TEA_key) { + int i = 0; + int num = 0; + //判断长度是否为8的倍数 + if (size_src % 8 != 0) { + //printf("\r\nchang du bu shi 8!!"); + return 0; + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //解密 + num = (byte) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_decrypt((UInt32*)(src + i * 8), (UInt32*)key); + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1 = (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2 = (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + List result=tea_decrypt(src1,src2, Key1, Key2, Key3, Key4); + src1=result.get(0); + src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1 >> 16); + src[i * 8 + 1] = (byte) (src1 >> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("解密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; + + + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37 + , 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + byte[] MyKey1 = {0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66}; +// byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, +// 0x05, 0x06, 0x07, 0x08, +// 0x09, 0x0A, 0x0B, 0x0C, +// 0x0D, 0x0E, 0x0F, 0x10}; + + +// System.out.println("原始数据为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + encrypt(info2, info2.length, MyKey1); + System.out.println("加密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retEnBytes[i32Index]) + " "); +// } +// System.out.println(); + decrypt(info2, info2.length, MyKey1); +// System.out.println("解密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retDeBytes[i32Index]) + " "); +// } + } +} diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java index e5fbc76..958b59d 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java @@ -41,7 +41,7 @@ Integer upback = 0; if (!ObjectUtil.isEmpty(UnpackConfig)) { while (hasNextFullFrame(byteBuf, protocolConfig)) { - upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig); + upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig,null); if (upback == 1){//使用esayRule //表示可以截取 cutOffPackageBuilder(byteBuf, protocolConfig, totalLength); @@ -85,13 +85,14 @@ Map currentFrameFixedProperty = fieldParser.getFixedProperty(protocolConfig, currentbyteBuf); if (StringUtils.isEmpty(standardFreame)) { standardFreame += hexDump.substring(0, currentFrameFixedProperty.get("tailPosition")); - tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition") + 1, hexDump.length()); + tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition")); } else { - standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")); + standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")-1); } } standardFreame += tail; bufferContent.writeBytes(standardFreame.getBytes(Charset.forName("ISO-8859-1"))); + System.out.println(ByteBufUtil.hexDump(bufferContent)); codeGeneratorProvider.codeGenerator(bufferContent, protocolConfig); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java index 90750c2..7058286 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java @@ -16,9 +16,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import java.io.IOException; -import java.nio.charset.Charset; - /** * SensorhubEncoder 编码器 */ @@ -51,10 +48,15 @@ if (isValid(msg)) { // String hexBytes = "a3 20 00 35 41 20 18 47 00 aa 03 00 aa 08 86 01 15 c8 a8 b8 74 2e 08 13 be 51 34 b4 12 08 6d b0 ed 7e 89 d4 92 2c 5c 24 ea aa 0e da c3 89 9b b2 a2 5e c6 68 8d 41 86 fe 73 a1 75 37 2f 7c ab d7 ac c8"; //(井盖开机上报) - byte[] hexBytes = {(byte)0xa3,0x20,0x00,0x35,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x15,(byte)0xc8 - ,(byte)0xa8,(byte)0xb8,0x74,0x2e,0x08,0x13,(byte)0xbe,0x51,0x34,(byte)0xb4,0x12,0x08,0x6d,(byte)0xb0,(byte)0xed,0x7e,(byte)0x89, - (byte)0xd4,(byte)0x92,0x2c,0x5c,0x24,(byte)0xea,(byte)0xaa,0x0e,(byte)0xda,(byte)0xc3,(byte)0x89,(byte)0x9b,(byte)0xb2,(byte)0xa2,0x5e, - (byte)0xc6,0x68,(byte)0x8d,0x41,(byte)0x86,(byte)0xfe,0x73,(byte)0xa1,0x75,0x37,0x2f,0x7c,(byte)0xab,(byte)0xd7,(byte)0xac,(byte)0xc8}; +// byte[] hexBytes = {(byte)0xa3,0x20,0x00,0X3c,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x15,(byte)0xc8, +// (byte)0xa8,(byte)0xb8,0x74,0x2e,0x08,0x13,(byte)0xbe,0x51,0x34,(byte)0xb4,0x12,0x08,0x6d,(byte)0xb0,(byte)0xed,0x7e,(byte)0x89, +// (byte)0xd4,(byte)0x92,0x2c,0x5c,0x24,(byte)0xea,(byte)0xaa,0x0e,(byte)0xda,(byte)0xc3,(byte)0x89,(byte)0x9b,(byte)0xb2,(byte)0xa2,0x5e, +// (byte)0xc6,0x68,(byte)0x8d,0x41,(byte)0x86,(byte)0xfe,0x73,(byte)0xa1,0x75,0x37,0x2f,0x7c,(byte)0xab,(byte)0xd7,(byte)0xac,(byte)0xc8}; + + byte[] hexBytes = {(byte)0xa3,0x20,0x00,0X3c,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x10,(byte)0x00, + (byte)0x00,(byte)0x63,0x00,0x23,0x38,0x36,(byte)0x39,0x36,0x36,(byte)0x32,0x30,0x33,0x31,(byte)0x35,(byte)0x38,0x31,(byte)0x33, + (byte)0x37,(byte)0x31,0x38,0x39,0x38,(byte)0x36,(byte)0x30,0x36,(byte)0x31,(byte)0x37,(byte)0x30,(byte)0x37,(byte)0x30,(byte)0x30,0x32, + (byte)0x32,0x35,(byte)0x38,0x37,(byte)0x35,(byte)0x32,0x30,(byte)0x00,0x00,0x00,0x00,0x00,(byte)0x00,(byte)0x00,(byte)0xac,(byte)0xc8}; out.writeBytes(hexBytes); // 设置前导码 // byte preamble = (byte) 0xA3; @@ -165,7 +167,6 @@ log.error("oidList不能为空"); return false; } - return true; } } \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java new file mode 100644 index 0000000..ab1e0a6 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java @@ -0,0 +1,82 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class CRCUtil { + + /** + * 将十六进制的字符串 求出 CRC 8 的 CRC_H CRC_L + */ + public static String getCRC8(String msg) { + //十六进制的字符串转换成字节数组 + byte[] bytes = hexStrToBinaryStr(msg); + //计算CRC8 + byte crc8 = CRC8(bytes, 0, bytes.length); + String crc8ToStr = "" + Integer.toHexString(0xff & crc8); + return crc8ToStr; + } + + /** + * 将十六进制的字符串转换成字节数组 + */ + public static byte[] hexStrToBinaryStr(String hexString) { + hexString = hexString.replaceAll(" ", ""); + if ((hexString.length() % 2) != 0) { + throw new IllegalArgumentException("长度不是偶数"); + } + int len = hexString.length(); + int index = 0; + byte[] bytes = new byte[len / 2]; + while (index < len) { + String sub = hexString.substring(index, index + 2); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); + index += 2; + } + return bytes; + } + + /** + * CRC8 校验 多项式 x8+x2+x+1 + * + * @param b + * @param off + * @param len + * @return + */ + public static byte CRC8(final byte[] b, final int off, final int len) { + byte result = 0x00; + for (int i = 0; i < len; i++) { + result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF]; + } + return (byte) (result & 0xFFL); + } + + private final static byte[] CHECKSUM_TABLE = {(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}; + +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java new file mode 100644 index 0000000..3478ab3 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java @@ -0,0 +1,36 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class MyUtils { + public static int[] byteToInt(byte[] Mybytes, int len) { + int[] RetInt = new int[len / 4]; + int i32Count = len / 4; + int i32Index = 0; + + for (int i32Increase = 0; i32Increase < i32Count; i32Increase++) { + RetInt[i32Increase] = transform(Mybytes[3 + i32Index]) | transform(Mybytes[2 + i32Index]) << 8 | transform(Mybytes[1 + i32Index]) << 16 | transform(Mybytes[0 + i32Index]) << 24; + i32Index += 4; + } + + return RetInt; + } + + public static byte[] IntToBytes(int[] MyInts, int len) { + byte[] RetBytes = new byte[len * 4]; + + for (int i32Index = 0; i32Index < len; i32Index++) { + RetBytes[0 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 24) & 0xFF); + RetBytes[1 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 16) & 0xFF); + RetBytes[2 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 8) & 0xFF); + RetBytes[3 + i32Index * 4] = (byte) (MyInts[i32Index] & 0xFF); + } + return RetBytes; + } + + private static int transform(byte temp) { + int tempInt = (int) temp; + if (tempInt < 0) { + tempInt += 256; + } + return tempInt; + } +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java index a3ee58b..f509a64 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java @@ -1,5 +1,6 @@ package com.casic.missiles.codec.decoder.safe.impl; +import io.netty.buffer.ByteBufUtil; import org.springframework.stereotype.Service; /** @@ -14,106 +15,109 @@ * @version v1.0: Tea.java, v 0.1 2021/3/12 13:48 $ */ @Service("tea") +///tea算法类 + public class Tea { - //加密 - public byte[] encrypt(byte[] content, int offset, int[] key, int times) {//times为加密轮数 - int[] tempInt = byteToInt(content, offset); - int y = tempInt[0], z = tempInt[1], sum = 0, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; - for (i = 0; i < times; i++) { - sum += delta; - y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + public static byte[] MyTea_Encrypt(byte[] MyTeaContent, int ByteLen, byte[] MyTeakey) { + int[] Value = MyUtils.byteToInt(MyTeaContent, ByteLen); + int sum = 0; + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + + + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + for (int i = 0; i < Value.length; i++) { + int v0 = Value[0], v1 = Value[1]; + for (; i32Index < i32times; i32Index++) { + sum += i32delta; + v0 += ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + v1 += ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + } + Value[0] = v0; + Value[1] = v1; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + int len = ByteLen / 4; + return MyUtils.IntToBytes(Value, len); } - //解密 - public byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times) { - int[] tempInt = byteToInt(encryptContent, offset); - int y = tempInt[0], z = tempInt[1], sum = 0xC6EF3720, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; + /** + * 参数为8字节的明文输入和16字节的密钥,输出8字节密文 + * + * @param MyTeaSecContent + * @param MyTeakey + * @return + */ + public static byte[] MyTea_Decrypt(byte[] MyTeaSecContent, int ByteLen, byte[] MyTeakey) { + System.out.println("解密函数:"); + int[] Value = MyUtils.byteToInt(MyTeaSecContent, ByteLen); + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + int sum = i32delta * i32times; - for (i = 0; i < times; i++) { - z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); - y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - sum -= delta; + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + int v0 = Value[0], v1 = Value[1]; + + for (; i32Index < i32times; i32Index++) { + v1 -= ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + v0 -= ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + sum -= i32delta; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + Value[0] = v0; + Value[1] = v1; + + + int len = ByteLen / 4; + + return MyUtils.IntToBytes(Value, len); } - //byte[]型数据转成int[]型数据 - private int[] byteToInt(byte[] content, int offset) { - - int[] result = new int[content.length >> 2]; //除以2的n次方 == 右移n位 即 content.length / 4 == content.length >> 2 - for (int i = 0, j = offset; j < content.length; i++, j += 4) { - result[i] = transform(content[j + 3]) | transform(content[j + 2]) << 8 | - transform(content[j + 1]) << 16 | (int) content[j] << 24; - } - return result; - - } - - //int[]型数据转成byte[]型数据 - private byte[] intToByte(int[] content, int offset) { - byte[] result = new byte[content.length << 2]; //乘以2的n次方 == 左移n位 即 content.length * 4 == content.length << 2 - for (int i = 0, j = offset; j < result.length; i++, j += 4) { - result[j + 3] = (byte) (content[i] & 0xff); - result[j + 2] = (byte) ((content[i] >> 8) & 0xff); - result[j + 1] = (byte) ((content[i] >> 16) & 0xff); - result[j] = (byte) ((content[i] >> 24) & 0xff); - } - return result; - } - - //若某字节被解释成负的则需将其转成无符号正数 - private static int transform(byte temp) { - int tempInt = (int) temp; - if (tempInt < 0) { - tempInt += 256; - } - return tempInt; - } - - // 测试代码如下: public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; - int[] KEY = new int[]{//加密解密所用的KEY - 0x789f5645, 0xf68bd5a4, - 0x81963ffa, 0x458fac58 - }; - Tea tea = new Tea(); + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37, + 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - byte[] info = new byte[]{ + byte[] demo2 = {0x10, 0x00, 0x00, 0x51, 0x00, 0x06, 0x12, 0x0B, 0x09, 0x0A, 0x33, 0x0E, 0x00, 0x00, 0x00, 0x00}; - 1, 2, 3, 4, 5, 6, 7, 8 - }; - System.out.print("原数据:"); - for (byte i : info) { - System.out.print(i + " "); + byte[] MyKeys = {48, 49, 50, 51, + 52, 53, 54, 55, + 56, 57, 97, 98, + 99, 100, 101, 102}; + + byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, + 0x0D, 0x0E, 0x0F, 0x10}; + + System.out.println("原始数据为:" + ByteBufUtil.hexDump(demo2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + byte[] retEnBytes = MyTea_Encrypt(demo2, demo2.length, MyKeys); + System.out.println("加密后为:" + ByteBufUtil.hexDump(retEnBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retEnBytes[i32Index]) + " "); } System.out.println(); - byte[] secretInfo = tea.encrypt(info, 0, KEY, 32); - System.out.print("加密后的数据:"); - for (byte i : secretInfo) { - System.out.print(i + " "); + byte[] retDeBytes = MyTea_Decrypt(retEnBytes, retEnBytes.length, MyKeys); + System.out.println("解密后为:" + ByteBufUtil.hexDump(retDeBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retDeBytes[i32Index]) + " "); } - System.out.println(); - - byte[] decryptInfo = tea.decrypt(secretInfo, 0, KEY, 32); - System.out.print("解密后的数据:"); - for (byte i : decryptInfo) { - System.out.print(i + " "); - } - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java new file mode 100644 index 0000000..6e3432c --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java @@ -0,0 +1,186 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +import io.netty.buffer.ByteBufUtil; + +import java.util.ArrayList; +import java.util.List; + +public class TeaDemo { + + + static List tea_encrypt(int v1,int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y =v1, z = v2, sum = 0, i; + int delta = 0x9e3779b9; + //UInt32 a = k1, b = k2, c = k3, d = k4; + + for (i = 0; i < 32; i++) { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * tea解密 + *参数:v:要解密的数据,长度为8字节 + * k:解密用的key,长度为16字节 + **********************************************************************/ + + static List tea_decrypt(int v1, int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y = v1, z = v2, sum = 0xC6EF3720, i; + int delta = 0x9e3779b9; + //UInt32 a = k[0], b = k[1], c = k[2], d = k[3]; + + for (i = 0; i < 32; i++) { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * 加密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:密文的字节数 + **********************************************************************/ + + public static int encrypt(byte[] src, int size_src, byte[] TEA_key) { + int a = 0; + int i = 0; + int num = 0; +// Byte[] TEA_key = new Byte[]{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; + //将明文补足为8字节的倍数 + a = (int) (size_src % 8); + if (a != 0) { + for (i = 0; i < 8 - a; i++) { + src[size_src++] = 0; + } + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //加密 + num = (int) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1= (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2= (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + + List result=tea_encrypt(src1,src2, Key1, Key2, Key3, Key4); +// src1=result.get(0); +// src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1>> 16); + src[i * 8 + 1] = (byte) (src1>> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("加密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + /********************************************************************* + * 解密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:明文的字节数,如果失败,返回0 + **********************************************************************/ + + public static int decrypt(byte[] src, int size_src, byte[] TEA_key) { + int i = 0; + int num = 0; + //判断长度是否为8的倍数 + if (size_src % 8 != 0) { + //printf("\r\nchang du bu shi 8!!"); + return 0; + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //解密 + num = (byte) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_decrypt((UInt32*)(src + i * 8), (UInt32*)key); + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1 = (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2 = (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + List result=tea_decrypt(src1,src2, Key1, Key2, Key3, Key4); + src1=result.get(0); + src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1 >> 16); + src[i * 8 + 1] = (byte) (src1 >> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("解密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; + + + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37 + , 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + byte[] MyKey1 = {0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66}; +// byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, +// 0x05, 0x06, 0x07, 0x08, +// 0x09, 0x0A, 0x0B, 0x0C, +// 0x0D, 0x0E, 0x0F, 0x10}; + + +// System.out.println("原始数据为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + encrypt(info2, info2.length, MyKey1); + System.out.println("加密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retEnBytes[i32Index]) + " "); +// } +// System.out.println(); + decrypt(info2, info2.length, MyKey1); +// System.out.println("解密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retDeBytes[i32Index]) + " "); +// } + } +} diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java index e5fbc76..958b59d 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java @@ -41,7 +41,7 @@ Integer upback = 0; if (!ObjectUtil.isEmpty(UnpackConfig)) { while (hasNextFullFrame(byteBuf, protocolConfig)) { - upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig); + upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig,null); if (upback == 1){//使用esayRule //表示可以截取 cutOffPackageBuilder(byteBuf, protocolConfig, totalLength); @@ -85,13 +85,14 @@ Map currentFrameFixedProperty = fieldParser.getFixedProperty(protocolConfig, currentbyteBuf); if (StringUtils.isEmpty(standardFreame)) { standardFreame += hexDump.substring(0, currentFrameFixedProperty.get("tailPosition")); - tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition") + 1, hexDump.length()); + tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition")); } else { - standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")); + standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")-1); } } standardFreame += tail; bufferContent.writeBytes(standardFreame.getBytes(Charset.forName("ISO-8859-1"))); + System.out.println(ByteBufUtil.hexDump(bufferContent)); codeGeneratorProvider.codeGenerator(bufferContent, protocolConfig); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java index 90750c2..7058286 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java @@ -16,9 +16,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import java.io.IOException; -import java.nio.charset.Charset; - /** * SensorhubEncoder 编码器 */ @@ -51,10 +48,15 @@ if (isValid(msg)) { // String hexBytes = "a3 20 00 35 41 20 18 47 00 aa 03 00 aa 08 86 01 15 c8 a8 b8 74 2e 08 13 be 51 34 b4 12 08 6d b0 ed 7e 89 d4 92 2c 5c 24 ea aa 0e da c3 89 9b b2 a2 5e c6 68 8d 41 86 fe 73 a1 75 37 2f 7c ab d7 ac c8"; //(井盖开机上报) - byte[] hexBytes = {(byte)0xa3,0x20,0x00,0x35,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x15,(byte)0xc8 - ,(byte)0xa8,(byte)0xb8,0x74,0x2e,0x08,0x13,(byte)0xbe,0x51,0x34,(byte)0xb4,0x12,0x08,0x6d,(byte)0xb0,(byte)0xed,0x7e,(byte)0x89, - (byte)0xd4,(byte)0x92,0x2c,0x5c,0x24,(byte)0xea,(byte)0xaa,0x0e,(byte)0xda,(byte)0xc3,(byte)0x89,(byte)0x9b,(byte)0xb2,(byte)0xa2,0x5e, - (byte)0xc6,0x68,(byte)0x8d,0x41,(byte)0x86,(byte)0xfe,0x73,(byte)0xa1,0x75,0x37,0x2f,0x7c,(byte)0xab,(byte)0xd7,(byte)0xac,(byte)0xc8}; +// byte[] hexBytes = {(byte)0xa3,0x20,0x00,0X3c,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x15,(byte)0xc8, +// (byte)0xa8,(byte)0xb8,0x74,0x2e,0x08,0x13,(byte)0xbe,0x51,0x34,(byte)0xb4,0x12,0x08,0x6d,(byte)0xb0,(byte)0xed,0x7e,(byte)0x89, +// (byte)0xd4,(byte)0x92,0x2c,0x5c,0x24,(byte)0xea,(byte)0xaa,0x0e,(byte)0xda,(byte)0xc3,(byte)0x89,(byte)0x9b,(byte)0xb2,(byte)0xa2,0x5e, +// (byte)0xc6,0x68,(byte)0x8d,0x41,(byte)0x86,(byte)0xfe,0x73,(byte)0xa1,0x75,0x37,0x2f,0x7c,(byte)0xab,(byte)0xd7,(byte)0xac,(byte)0xc8}; + + byte[] hexBytes = {(byte)0xa3,0x20,0x00,0X3c,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x10,(byte)0x00, + (byte)0x00,(byte)0x63,0x00,0x23,0x38,0x36,(byte)0x39,0x36,0x36,(byte)0x32,0x30,0x33,0x31,(byte)0x35,(byte)0x38,0x31,(byte)0x33, + (byte)0x37,(byte)0x31,0x38,0x39,0x38,(byte)0x36,(byte)0x30,0x36,(byte)0x31,(byte)0x37,(byte)0x30,(byte)0x37,(byte)0x30,(byte)0x30,0x32, + (byte)0x32,0x35,(byte)0x38,0x37,(byte)0x35,(byte)0x32,0x30,(byte)0x00,0x00,0x00,0x00,0x00,(byte)0x00,(byte)0x00,(byte)0xac,(byte)0xc8}; out.writeBytes(hexBytes); // 设置前导码 // byte preamble = (byte) 0xA3; @@ -165,7 +167,6 @@ log.error("oidList不能为空"); return false; } - return true; } } \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java b/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java index 052eab4..cd10bbe 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java @@ -10,7 +10,7 @@ public class DatagramEventConfig { private Long id; - private String protocolId; + private String ruleId; private String eventName; private String safeFieldId; private String safeBean; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java new file mode 100644 index 0000000..ab1e0a6 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java @@ -0,0 +1,82 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class CRCUtil { + + /** + * 将十六进制的字符串 求出 CRC 8 的 CRC_H CRC_L + */ + public static String getCRC8(String msg) { + //十六进制的字符串转换成字节数组 + byte[] bytes = hexStrToBinaryStr(msg); + //计算CRC8 + byte crc8 = CRC8(bytes, 0, bytes.length); + String crc8ToStr = "" + Integer.toHexString(0xff & crc8); + return crc8ToStr; + } + + /** + * 将十六进制的字符串转换成字节数组 + */ + public static byte[] hexStrToBinaryStr(String hexString) { + hexString = hexString.replaceAll(" ", ""); + if ((hexString.length() % 2) != 0) { + throw new IllegalArgumentException("长度不是偶数"); + } + int len = hexString.length(); + int index = 0; + byte[] bytes = new byte[len / 2]; + while (index < len) { + String sub = hexString.substring(index, index + 2); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); + index += 2; + } + return bytes; + } + + /** + * CRC8 校验 多项式 x8+x2+x+1 + * + * @param b + * @param off + * @param len + * @return + */ + public static byte CRC8(final byte[] b, final int off, final int len) { + byte result = 0x00; + for (int i = 0; i < len; i++) { + result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF]; + } + return (byte) (result & 0xFFL); + } + + private final static byte[] CHECKSUM_TABLE = {(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}; + +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java new file mode 100644 index 0000000..3478ab3 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java @@ -0,0 +1,36 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class MyUtils { + public static int[] byteToInt(byte[] Mybytes, int len) { + int[] RetInt = new int[len / 4]; + int i32Count = len / 4; + int i32Index = 0; + + for (int i32Increase = 0; i32Increase < i32Count; i32Increase++) { + RetInt[i32Increase] = transform(Mybytes[3 + i32Index]) | transform(Mybytes[2 + i32Index]) << 8 | transform(Mybytes[1 + i32Index]) << 16 | transform(Mybytes[0 + i32Index]) << 24; + i32Index += 4; + } + + return RetInt; + } + + public static byte[] IntToBytes(int[] MyInts, int len) { + byte[] RetBytes = new byte[len * 4]; + + for (int i32Index = 0; i32Index < len; i32Index++) { + RetBytes[0 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 24) & 0xFF); + RetBytes[1 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 16) & 0xFF); + RetBytes[2 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 8) & 0xFF); + RetBytes[3 + i32Index * 4] = (byte) (MyInts[i32Index] & 0xFF); + } + return RetBytes; + } + + private static int transform(byte temp) { + int tempInt = (int) temp; + if (tempInt < 0) { + tempInt += 256; + } + return tempInt; + } +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java index a3ee58b..f509a64 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java @@ -1,5 +1,6 @@ package com.casic.missiles.codec.decoder.safe.impl; +import io.netty.buffer.ByteBufUtil; import org.springframework.stereotype.Service; /** @@ -14,106 +15,109 @@ * @version v1.0: Tea.java, v 0.1 2021/3/12 13:48 $ */ @Service("tea") +///tea算法类 + public class Tea { - //加密 - public byte[] encrypt(byte[] content, int offset, int[] key, int times) {//times为加密轮数 - int[] tempInt = byteToInt(content, offset); - int y = tempInt[0], z = tempInt[1], sum = 0, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; - for (i = 0; i < times; i++) { - sum += delta; - y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + public static byte[] MyTea_Encrypt(byte[] MyTeaContent, int ByteLen, byte[] MyTeakey) { + int[] Value = MyUtils.byteToInt(MyTeaContent, ByteLen); + int sum = 0; + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + + + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + for (int i = 0; i < Value.length; i++) { + int v0 = Value[0], v1 = Value[1]; + for (; i32Index < i32times; i32Index++) { + sum += i32delta; + v0 += ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + v1 += ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + } + Value[0] = v0; + Value[1] = v1; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + int len = ByteLen / 4; + return MyUtils.IntToBytes(Value, len); } - //解密 - public byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times) { - int[] tempInt = byteToInt(encryptContent, offset); - int y = tempInt[0], z = tempInt[1], sum = 0xC6EF3720, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; + /** + * 参数为8字节的明文输入和16字节的密钥,输出8字节密文 + * + * @param MyTeaSecContent + * @param MyTeakey + * @return + */ + public static byte[] MyTea_Decrypt(byte[] MyTeaSecContent, int ByteLen, byte[] MyTeakey) { + System.out.println("解密函数:"); + int[] Value = MyUtils.byteToInt(MyTeaSecContent, ByteLen); + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + int sum = i32delta * i32times; - for (i = 0; i < times; i++) { - z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); - y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - sum -= delta; + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + int v0 = Value[0], v1 = Value[1]; + + for (; i32Index < i32times; i32Index++) { + v1 -= ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + v0 -= ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + sum -= i32delta; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + Value[0] = v0; + Value[1] = v1; + + + int len = ByteLen / 4; + + return MyUtils.IntToBytes(Value, len); } - //byte[]型数据转成int[]型数据 - private int[] byteToInt(byte[] content, int offset) { - - int[] result = new int[content.length >> 2]; //除以2的n次方 == 右移n位 即 content.length / 4 == content.length >> 2 - for (int i = 0, j = offset; j < content.length; i++, j += 4) { - result[i] = transform(content[j + 3]) | transform(content[j + 2]) << 8 | - transform(content[j + 1]) << 16 | (int) content[j] << 24; - } - return result; - - } - - //int[]型数据转成byte[]型数据 - private byte[] intToByte(int[] content, int offset) { - byte[] result = new byte[content.length << 2]; //乘以2的n次方 == 左移n位 即 content.length * 4 == content.length << 2 - for (int i = 0, j = offset; j < result.length; i++, j += 4) { - result[j + 3] = (byte) (content[i] & 0xff); - result[j + 2] = (byte) ((content[i] >> 8) & 0xff); - result[j + 1] = (byte) ((content[i] >> 16) & 0xff); - result[j] = (byte) ((content[i] >> 24) & 0xff); - } - return result; - } - - //若某字节被解释成负的则需将其转成无符号正数 - private static int transform(byte temp) { - int tempInt = (int) temp; - if (tempInt < 0) { - tempInt += 256; - } - return tempInt; - } - - // 测试代码如下: public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; - int[] KEY = new int[]{//加密解密所用的KEY - 0x789f5645, 0xf68bd5a4, - 0x81963ffa, 0x458fac58 - }; - Tea tea = new Tea(); + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37, + 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - byte[] info = new byte[]{ + byte[] demo2 = {0x10, 0x00, 0x00, 0x51, 0x00, 0x06, 0x12, 0x0B, 0x09, 0x0A, 0x33, 0x0E, 0x00, 0x00, 0x00, 0x00}; - 1, 2, 3, 4, 5, 6, 7, 8 - }; - System.out.print("原数据:"); - for (byte i : info) { - System.out.print(i + " "); + byte[] MyKeys = {48, 49, 50, 51, + 52, 53, 54, 55, + 56, 57, 97, 98, + 99, 100, 101, 102}; + + byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, + 0x0D, 0x0E, 0x0F, 0x10}; + + System.out.println("原始数据为:" + ByteBufUtil.hexDump(demo2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + byte[] retEnBytes = MyTea_Encrypt(demo2, demo2.length, MyKeys); + System.out.println("加密后为:" + ByteBufUtil.hexDump(retEnBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retEnBytes[i32Index]) + " "); } System.out.println(); - byte[] secretInfo = tea.encrypt(info, 0, KEY, 32); - System.out.print("加密后的数据:"); - for (byte i : secretInfo) { - System.out.print(i + " "); + byte[] retDeBytes = MyTea_Decrypt(retEnBytes, retEnBytes.length, MyKeys); + System.out.println("解密后为:" + ByteBufUtil.hexDump(retDeBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retDeBytes[i32Index]) + " "); } - System.out.println(); - - byte[] decryptInfo = tea.decrypt(secretInfo, 0, KEY, 32); - System.out.print("解密后的数据:"); - for (byte i : decryptInfo) { - System.out.print(i + " "); - } - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java new file mode 100644 index 0000000..6e3432c --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java @@ -0,0 +1,186 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +import io.netty.buffer.ByteBufUtil; + +import java.util.ArrayList; +import java.util.List; + +public class TeaDemo { + + + static List tea_encrypt(int v1,int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y =v1, z = v2, sum = 0, i; + int delta = 0x9e3779b9; + //UInt32 a = k1, b = k2, c = k3, d = k4; + + for (i = 0; i < 32; i++) { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * tea解密 + *参数:v:要解密的数据,长度为8字节 + * k:解密用的key,长度为16字节 + **********************************************************************/ + + static List tea_decrypt(int v1, int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y = v1, z = v2, sum = 0xC6EF3720, i; + int delta = 0x9e3779b9; + //UInt32 a = k[0], b = k[1], c = k[2], d = k[3]; + + for (i = 0; i < 32; i++) { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * 加密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:密文的字节数 + **********************************************************************/ + + public static int encrypt(byte[] src, int size_src, byte[] TEA_key) { + int a = 0; + int i = 0; + int num = 0; +// Byte[] TEA_key = new Byte[]{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; + //将明文补足为8字节的倍数 + a = (int) (size_src % 8); + if (a != 0) { + for (i = 0; i < 8 - a; i++) { + src[size_src++] = 0; + } + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //加密 + num = (int) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1= (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2= (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + + List result=tea_encrypt(src1,src2, Key1, Key2, Key3, Key4); +// src1=result.get(0); +// src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1>> 16); + src[i * 8 + 1] = (byte) (src1>> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("加密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + /********************************************************************* + * 解密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:明文的字节数,如果失败,返回0 + **********************************************************************/ + + public static int decrypt(byte[] src, int size_src, byte[] TEA_key) { + int i = 0; + int num = 0; + //判断长度是否为8的倍数 + if (size_src % 8 != 0) { + //printf("\r\nchang du bu shi 8!!"); + return 0; + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //解密 + num = (byte) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_decrypt((UInt32*)(src + i * 8), (UInt32*)key); + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1 = (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2 = (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + List result=tea_decrypt(src1,src2, Key1, Key2, Key3, Key4); + src1=result.get(0); + src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1 >> 16); + src[i * 8 + 1] = (byte) (src1 >> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("解密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; + + + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37 + , 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + byte[] MyKey1 = {0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66}; +// byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, +// 0x05, 0x06, 0x07, 0x08, +// 0x09, 0x0A, 0x0B, 0x0C, +// 0x0D, 0x0E, 0x0F, 0x10}; + + +// System.out.println("原始数据为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + encrypt(info2, info2.length, MyKey1); + System.out.println("加密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retEnBytes[i32Index]) + " "); +// } +// System.out.println(); + decrypt(info2, info2.length, MyKey1); +// System.out.println("解密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retDeBytes[i32Index]) + " "); +// } + } +} diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java index e5fbc76..958b59d 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java @@ -41,7 +41,7 @@ Integer upback = 0; if (!ObjectUtil.isEmpty(UnpackConfig)) { while (hasNextFullFrame(byteBuf, protocolConfig)) { - upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig); + upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig,null); if (upback == 1){//使用esayRule //表示可以截取 cutOffPackageBuilder(byteBuf, protocolConfig, totalLength); @@ -85,13 +85,14 @@ Map currentFrameFixedProperty = fieldParser.getFixedProperty(protocolConfig, currentbyteBuf); if (StringUtils.isEmpty(standardFreame)) { standardFreame += hexDump.substring(0, currentFrameFixedProperty.get("tailPosition")); - tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition") + 1, hexDump.length()); + tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition")); } else { - standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")); + standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")-1); } } standardFreame += tail; bufferContent.writeBytes(standardFreame.getBytes(Charset.forName("ISO-8859-1"))); + System.out.println(ByteBufUtil.hexDump(bufferContent)); codeGeneratorProvider.codeGenerator(bufferContent, protocolConfig); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java index 90750c2..7058286 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java @@ -16,9 +16,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import java.io.IOException; -import java.nio.charset.Charset; - /** * SensorhubEncoder 编码器 */ @@ -51,10 +48,15 @@ if (isValid(msg)) { // String hexBytes = "a3 20 00 35 41 20 18 47 00 aa 03 00 aa 08 86 01 15 c8 a8 b8 74 2e 08 13 be 51 34 b4 12 08 6d b0 ed 7e 89 d4 92 2c 5c 24 ea aa 0e da c3 89 9b b2 a2 5e c6 68 8d 41 86 fe 73 a1 75 37 2f 7c ab d7 ac c8"; //(井盖开机上报) - byte[] hexBytes = {(byte)0xa3,0x20,0x00,0x35,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x15,(byte)0xc8 - ,(byte)0xa8,(byte)0xb8,0x74,0x2e,0x08,0x13,(byte)0xbe,0x51,0x34,(byte)0xb4,0x12,0x08,0x6d,(byte)0xb0,(byte)0xed,0x7e,(byte)0x89, - (byte)0xd4,(byte)0x92,0x2c,0x5c,0x24,(byte)0xea,(byte)0xaa,0x0e,(byte)0xda,(byte)0xc3,(byte)0x89,(byte)0x9b,(byte)0xb2,(byte)0xa2,0x5e, - (byte)0xc6,0x68,(byte)0x8d,0x41,(byte)0x86,(byte)0xfe,0x73,(byte)0xa1,0x75,0x37,0x2f,0x7c,(byte)0xab,(byte)0xd7,(byte)0xac,(byte)0xc8}; +// byte[] hexBytes = {(byte)0xa3,0x20,0x00,0X3c,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x15,(byte)0xc8, +// (byte)0xa8,(byte)0xb8,0x74,0x2e,0x08,0x13,(byte)0xbe,0x51,0x34,(byte)0xb4,0x12,0x08,0x6d,(byte)0xb0,(byte)0xed,0x7e,(byte)0x89, +// (byte)0xd4,(byte)0x92,0x2c,0x5c,0x24,(byte)0xea,(byte)0xaa,0x0e,(byte)0xda,(byte)0xc3,(byte)0x89,(byte)0x9b,(byte)0xb2,(byte)0xa2,0x5e, +// (byte)0xc6,0x68,(byte)0x8d,0x41,(byte)0x86,(byte)0xfe,0x73,(byte)0xa1,0x75,0x37,0x2f,0x7c,(byte)0xab,(byte)0xd7,(byte)0xac,(byte)0xc8}; + + byte[] hexBytes = {(byte)0xa3,0x20,0x00,0X3c,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x10,(byte)0x00, + (byte)0x00,(byte)0x63,0x00,0x23,0x38,0x36,(byte)0x39,0x36,0x36,(byte)0x32,0x30,0x33,0x31,(byte)0x35,(byte)0x38,0x31,(byte)0x33, + (byte)0x37,(byte)0x31,0x38,0x39,0x38,(byte)0x36,(byte)0x30,0x36,(byte)0x31,(byte)0x37,(byte)0x30,(byte)0x37,(byte)0x30,(byte)0x30,0x32, + (byte)0x32,0x35,(byte)0x38,0x37,(byte)0x35,(byte)0x32,0x30,(byte)0x00,0x00,0x00,0x00,0x00,(byte)0x00,(byte)0x00,(byte)0xac,(byte)0xc8}; out.writeBytes(hexBytes); // 设置前导码 // byte preamble = (byte) 0xA3; @@ -165,7 +167,6 @@ log.error("oidList不能为空"); return false; } - return true; } } \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java b/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java index 052eab4..cd10bbe 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java @@ -10,7 +10,7 @@ public class DatagramEventConfig { private Long id; - private String protocolId; + private String ruleId; private String eventName; private String safeFieldId; private String safeBean; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java b/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java index fe52c41..334d59d 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java @@ -5,6 +5,9 @@ import java.util.Date; +/** + * 加密字段解析和规则有关,事件调到该流程下面 + */ @Data @TableName("rule_config") public class RuleConfig { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java new file mode 100644 index 0000000..ab1e0a6 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java @@ -0,0 +1,82 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class CRCUtil { + + /** + * 将十六进制的字符串 求出 CRC 8 的 CRC_H CRC_L + */ + public static String getCRC8(String msg) { + //十六进制的字符串转换成字节数组 + byte[] bytes = hexStrToBinaryStr(msg); + //计算CRC8 + byte crc8 = CRC8(bytes, 0, bytes.length); + String crc8ToStr = "" + Integer.toHexString(0xff & crc8); + return crc8ToStr; + } + + /** + * 将十六进制的字符串转换成字节数组 + */ + public static byte[] hexStrToBinaryStr(String hexString) { + hexString = hexString.replaceAll(" ", ""); + if ((hexString.length() % 2) != 0) { + throw new IllegalArgumentException("长度不是偶数"); + } + int len = hexString.length(); + int index = 0; + byte[] bytes = new byte[len / 2]; + while (index < len) { + String sub = hexString.substring(index, index + 2); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); + index += 2; + } + return bytes; + } + + /** + * CRC8 校验 多项式 x8+x2+x+1 + * + * @param b + * @param off + * @param len + * @return + */ + public static byte CRC8(final byte[] b, final int off, final int len) { + byte result = 0x00; + for (int i = 0; i < len; i++) { + result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF]; + } + return (byte) (result & 0xFFL); + } + + private final static byte[] CHECKSUM_TABLE = {(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}; + +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java new file mode 100644 index 0000000..3478ab3 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java @@ -0,0 +1,36 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class MyUtils { + public static int[] byteToInt(byte[] Mybytes, int len) { + int[] RetInt = new int[len / 4]; + int i32Count = len / 4; + int i32Index = 0; + + for (int i32Increase = 0; i32Increase < i32Count; i32Increase++) { + RetInt[i32Increase] = transform(Mybytes[3 + i32Index]) | transform(Mybytes[2 + i32Index]) << 8 | transform(Mybytes[1 + i32Index]) << 16 | transform(Mybytes[0 + i32Index]) << 24; + i32Index += 4; + } + + return RetInt; + } + + public static byte[] IntToBytes(int[] MyInts, int len) { + byte[] RetBytes = new byte[len * 4]; + + for (int i32Index = 0; i32Index < len; i32Index++) { + RetBytes[0 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 24) & 0xFF); + RetBytes[1 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 16) & 0xFF); + RetBytes[2 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 8) & 0xFF); + RetBytes[3 + i32Index * 4] = (byte) (MyInts[i32Index] & 0xFF); + } + return RetBytes; + } + + private static int transform(byte temp) { + int tempInt = (int) temp; + if (tempInt < 0) { + tempInt += 256; + } + return tempInt; + } +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java index a3ee58b..f509a64 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java @@ -1,5 +1,6 @@ package com.casic.missiles.codec.decoder.safe.impl; +import io.netty.buffer.ByteBufUtil; import org.springframework.stereotype.Service; /** @@ -14,106 +15,109 @@ * @version v1.0: Tea.java, v 0.1 2021/3/12 13:48 $ */ @Service("tea") +///tea算法类 + public class Tea { - //加密 - public byte[] encrypt(byte[] content, int offset, int[] key, int times) {//times为加密轮数 - int[] tempInt = byteToInt(content, offset); - int y = tempInt[0], z = tempInt[1], sum = 0, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; - for (i = 0; i < times; i++) { - sum += delta; - y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + public static byte[] MyTea_Encrypt(byte[] MyTeaContent, int ByteLen, byte[] MyTeakey) { + int[] Value = MyUtils.byteToInt(MyTeaContent, ByteLen); + int sum = 0; + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + + + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + for (int i = 0; i < Value.length; i++) { + int v0 = Value[0], v1 = Value[1]; + for (; i32Index < i32times; i32Index++) { + sum += i32delta; + v0 += ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + v1 += ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + } + Value[0] = v0; + Value[1] = v1; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + int len = ByteLen / 4; + return MyUtils.IntToBytes(Value, len); } - //解密 - public byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times) { - int[] tempInt = byteToInt(encryptContent, offset); - int y = tempInt[0], z = tempInt[1], sum = 0xC6EF3720, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; + /** + * 参数为8字节的明文输入和16字节的密钥,输出8字节密文 + * + * @param MyTeaSecContent + * @param MyTeakey + * @return + */ + public static byte[] MyTea_Decrypt(byte[] MyTeaSecContent, int ByteLen, byte[] MyTeakey) { + System.out.println("解密函数:"); + int[] Value = MyUtils.byteToInt(MyTeaSecContent, ByteLen); + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + int sum = i32delta * i32times; - for (i = 0; i < times; i++) { - z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); - y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - sum -= delta; + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + int v0 = Value[0], v1 = Value[1]; + + for (; i32Index < i32times; i32Index++) { + v1 -= ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + v0 -= ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + sum -= i32delta; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + Value[0] = v0; + Value[1] = v1; + + + int len = ByteLen / 4; + + return MyUtils.IntToBytes(Value, len); } - //byte[]型数据转成int[]型数据 - private int[] byteToInt(byte[] content, int offset) { - - int[] result = new int[content.length >> 2]; //除以2的n次方 == 右移n位 即 content.length / 4 == content.length >> 2 - for (int i = 0, j = offset; j < content.length; i++, j += 4) { - result[i] = transform(content[j + 3]) | transform(content[j + 2]) << 8 | - transform(content[j + 1]) << 16 | (int) content[j] << 24; - } - return result; - - } - - //int[]型数据转成byte[]型数据 - private byte[] intToByte(int[] content, int offset) { - byte[] result = new byte[content.length << 2]; //乘以2的n次方 == 左移n位 即 content.length * 4 == content.length << 2 - for (int i = 0, j = offset; j < result.length; i++, j += 4) { - result[j + 3] = (byte) (content[i] & 0xff); - result[j + 2] = (byte) ((content[i] >> 8) & 0xff); - result[j + 1] = (byte) ((content[i] >> 16) & 0xff); - result[j] = (byte) ((content[i] >> 24) & 0xff); - } - return result; - } - - //若某字节被解释成负的则需将其转成无符号正数 - private static int transform(byte temp) { - int tempInt = (int) temp; - if (tempInt < 0) { - tempInt += 256; - } - return tempInt; - } - - // 测试代码如下: public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; - int[] KEY = new int[]{//加密解密所用的KEY - 0x789f5645, 0xf68bd5a4, - 0x81963ffa, 0x458fac58 - }; - Tea tea = new Tea(); + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37, + 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - byte[] info = new byte[]{ + byte[] demo2 = {0x10, 0x00, 0x00, 0x51, 0x00, 0x06, 0x12, 0x0B, 0x09, 0x0A, 0x33, 0x0E, 0x00, 0x00, 0x00, 0x00}; - 1, 2, 3, 4, 5, 6, 7, 8 - }; - System.out.print("原数据:"); - for (byte i : info) { - System.out.print(i + " "); + byte[] MyKeys = {48, 49, 50, 51, + 52, 53, 54, 55, + 56, 57, 97, 98, + 99, 100, 101, 102}; + + byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, + 0x0D, 0x0E, 0x0F, 0x10}; + + System.out.println("原始数据为:" + ByteBufUtil.hexDump(demo2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + byte[] retEnBytes = MyTea_Encrypt(demo2, demo2.length, MyKeys); + System.out.println("加密后为:" + ByteBufUtil.hexDump(retEnBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retEnBytes[i32Index]) + " "); } System.out.println(); - byte[] secretInfo = tea.encrypt(info, 0, KEY, 32); - System.out.print("加密后的数据:"); - for (byte i : secretInfo) { - System.out.print(i + " "); + byte[] retDeBytes = MyTea_Decrypt(retEnBytes, retEnBytes.length, MyKeys); + System.out.println("解密后为:" + ByteBufUtil.hexDump(retDeBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retDeBytes[i32Index]) + " "); } - System.out.println(); - - byte[] decryptInfo = tea.decrypt(secretInfo, 0, KEY, 32); - System.out.print("解密后的数据:"); - for (byte i : decryptInfo) { - System.out.print(i + " "); - } - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java new file mode 100644 index 0000000..6e3432c --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java @@ -0,0 +1,186 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +import io.netty.buffer.ByteBufUtil; + +import java.util.ArrayList; +import java.util.List; + +public class TeaDemo { + + + static List tea_encrypt(int v1,int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y =v1, z = v2, sum = 0, i; + int delta = 0x9e3779b9; + //UInt32 a = k1, b = k2, c = k3, d = k4; + + for (i = 0; i < 32; i++) { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * tea解密 + *参数:v:要解密的数据,长度为8字节 + * k:解密用的key,长度为16字节 + **********************************************************************/ + + static List tea_decrypt(int v1, int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y = v1, z = v2, sum = 0xC6EF3720, i; + int delta = 0x9e3779b9; + //UInt32 a = k[0], b = k[1], c = k[2], d = k[3]; + + for (i = 0; i < 32; i++) { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * 加密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:密文的字节数 + **********************************************************************/ + + public static int encrypt(byte[] src, int size_src, byte[] TEA_key) { + int a = 0; + int i = 0; + int num = 0; +// Byte[] TEA_key = new Byte[]{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; + //将明文补足为8字节的倍数 + a = (int) (size_src % 8); + if (a != 0) { + for (i = 0; i < 8 - a; i++) { + src[size_src++] = 0; + } + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //加密 + num = (int) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1= (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2= (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + + List result=tea_encrypt(src1,src2, Key1, Key2, Key3, Key4); +// src1=result.get(0); +// src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1>> 16); + src[i * 8 + 1] = (byte) (src1>> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("加密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + /********************************************************************* + * 解密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:明文的字节数,如果失败,返回0 + **********************************************************************/ + + public static int decrypt(byte[] src, int size_src, byte[] TEA_key) { + int i = 0; + int num = 0; + //判断长度是否为8的倍数 + if (size_src % 8 != 0) { + //printf("\r\nchang du bu shi 8!!"); + return 0; + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //解密 + num = (byte) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_decrypt((UInt32*)(src + i * 8), (UInt32*)key); + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1 = (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2 = (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + List result=tea_decrypt(src1,src2, Key1, Key2, Key3, Key4); + src1=result.get(0); + src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1 >> 16); + src[i * 8 + 1] = (byte) (src1 >> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("解密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; + + + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37 + , 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + byte[] MyKey1 = {0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66}; +// byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, +// 0x05, 0x06, 0x07, 0x08, +// 0x09, 0x0A, 0x0B, 0x0C, +// 0x0D, 0x0E, 0x0F, 0x10}; + + +// System.out.println("原始数据为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + encrypt(info2, info2.length, MyKey1); + System.out.println("加密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retEnBytes[i32Index]) + " "); +// } +// System.out.println(); + decrypt(info2, info2.length, MyKey1); +// System.out.println("解密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retDeBytes[i32Index]) + " "); +// } + } +} diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java index e5fbc76..958b59d 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java @@ -41,7 +41,7 @@ Integer upback = 0; if (!ObjectUtil.isEmpty(UnpackConfig)) { while (hasNextFullFrame(byteBuf, protocolConfig)) { - upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig); + upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig,null); if (upback == 1){//使用esayRule //表示可以截取 cutOffPackageBuilder(byteBuf, protocolConfig, totalLength); @@ -85,13 +85,14 @@ Map currentFrameFixedProperty = fieldParser.getFixedProperty(protocolConfig, currentbyteBuf); if (StringUtils.isEmpty(standardFreame)) { standardFreame += hexDump.substring(0, currentFrameFixedProperty.get("tailPosition")); - tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition") + 1, hexDump.length()); + tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition")); } else { - standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")); + standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")-1); } } standardFreame += tail; bufferContent.writeBytes(standardFreame.getBytes(Charset.forName("ISO-8859-1"))); + System.out.println(ByteBufUtil.hexDump(bufferContent)); codeGeneratorProvider.codeGenerator(bufferContent, protocolConfig); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java index 90750c2..7058286 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java @@ -16,9 +16,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import java.io.IOException; -import java.nio.charset.Charset; - /** * SensorhubEncoder 编码器 */ @@ -51,10 +48,15 @@ if (isValid(msg)) { // String hexBytes = "a3 20 00 35 41 20 18 47 00 aa 03 00 aa 08 86 01 15 c8 a8 b8 74 2e 08 13 be 51 34 b4 12 08 6d b0 ed 7e 89 d4 92 2c 5c 24 ea aa 0e da c3 89 9b b2 a2 5e c6 68 8d 41 86 fe 73 a1 75 37 2f 7c ab d7 ac c8"; //(井盖开机上报) - byte[] hexBytes = {(byte)0xa3,0x20,0x00,0x35,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x15,(byte)0xc8 - ,(byte)0xa8,(byte)0xb8,0x74,0x2e,0x08,0x13,(byte)0xbe,0x51,0x34,(byte)0xb4,0x12,0x08,0x6d,(byte)0xb0,(byte)0xed,0x7e,(byte)0x89, - (byte)0xd4,(byte)0x92,0x2c,0x5c,0x24,(byte)0xea,(byte)0xaa,0x0e,(byte)0xda,(byte)0xc3,(byte)0x89,(byte)0x9b,(byte)0xb2,(byte)0xa2,0x5e, - (byte)0xc6,0x68,(byte)0x8d,0x41,(byte)0x86,(byte)0xfe,0x73,(byte)0xa1,0x75,0x37,0x2f,0x7c,(byte)0xab,(byte)0xd7,(byte)0xac,(byte)0xc8}; +// byte[] hexBytes = {(byte)0xa3,0x20,0x00,0X3c,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x15,(byte)0xc8, +// (byte)0xa8,(byte)0xb8,0x74,0x2e,0x08,0x13,(byte)0xbe,0x51,0x34,(byte)0xb4,0x12,0x08,0x6d,(byte)0xb0,(byte)0xed,0x7e,(byte)0x89, +// (byte)0xd4,(byte)0x92,0x2c,0x5c,0x24,(byte)0xea,(byte)0xaa,0x0e,(byte)0xda,(byte)0xc3,(byte)0x89,(byte)0x9b,(byte)0xb2,(byte)0xa2,0x5e, +// (byte)0xc6,0x68,(byte)0x8d,0x41,(byte)0x86,(byte)0xfe,0x73,(byte)0xa1,0x75,0x37,0x2f,0x7c,(byte)0xab,(byte)0xd7,(byte)0xac,(byte)0xc8}; + + byte[] hexBytes = {(byte)0xa3,0x20,0x00,0X3c,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x10,(byte)0x00, + (byte)0x00,(byte)0x63,0x00,0x23,0x38,0x36,(byte)0x39,0x36,0x36,(byte)0x32,0x30,0x33,0x31,(byte)0x35,(byte)0x38,0x31,(byte)0x33, + (byte)0x37,(byte)0x31,0x38,0x39,0x38,(byte)0x36,(byte)0x30,0x36,(byte)0x31,(byte)0x37,(byte)0x30,(byte)0x37,(byte)0x30,(byte)0x30,0x32, + (byte)0x32,0x35,(byte)0x38,0x37,(byte)0x35,(byte)0x32,0x30,(byte)0x00,0x00,0x00,0x00,0x00,(byte)0x00,(byte)0x00,(byte)0xac,(byte)0xc8}; out.writeBytes(hexBytes); // 设置前导码 // byte preamble = (byte) 0xA3; @@ -165,7 +167,6 @@ log.error("oidList不能为空"); return false; } - return true; } } \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java b/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java index 052eab4..cd10bbe 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java @@ -10,7 +10,7 @@ public class DatagramEventConfig { private Long id; - private String protocolId; + private String ruleId; private String eventName; private String safeFieldId; private String safeBean; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java b/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java index fe52c41..334d59d 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java @@ -5,6 +5,9 @@ import java.util.Date; +/** + * 加密字段解析和规则有关,事件调到该流程下面 + */ @Data @TableName("rule_config") public class RuleConfig { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/mapper/dao/RuleConfigDao.java b/sensorhub-server/src/main/java/com/casic/missiles/mapper/dao/RuleConfigDao.java index 15275a1..9d23a90 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/mapper/dao/RuleConfigDao.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/mapper/dao/RuleConfigDao.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.List; @Component public class RuleConfigDao { @@ -14,16 +15,16 @@ private RuleConfigMapper ruleConfigMapper; public RuleConfig getNextRuleConfig(String ruleFields) { - QueryWrapper queryWrapper=new QueryWrapper() - .eq("parse_rule_json",ruleFields); + QueryWrapper queryWrapper = new QueryWrapper() + .eq("parse_rule_json", ruleFields); return ruleConfigMapper.selectOne(queryWrapper); } - public RuleConfig getRuleConfigById(String protocolId) { - QueryWrapper queryWrapper=new QueryWrapper() - .eq("protocol_id",protocolId); - return ruleConfigMapper.selectOne(queryWrapper); + public List getRuleConfigById(String protocolId) { + QueryWrapper queryWrapper = new QueryWrapper() + .eq("protocol_id", protocolId); + return ruleConfigMapper.selectList(queryWrapper); } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java index 3172bee..2cc6eee 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/CodeGeneratorProvider.java @@ -31,17 +31,19 @@ private final FieldParser fieldParser; /** - * 选定流程=> 计算业务数据位置=>执行加密/解密=> 规则解析,规则循环匹配,选定业务数据包内容 + * 统一固定字段解析=>进行规则的查询的判断 + * 计算固定字段业务值及对应管理=> 规则解析,规则循环匹配 =>选取规则对应的流程=>选定业务字段内容=>执行加密/解密=> 选定业务数据包内容 * =>业务字段解析=> 数据验证=>数据构建=> 数据发送 */ public boolean codeGenerator(ByteBuf buffer, ProtocolConfig protocolConfig) { try { String protocolId = String.valueOf(protocolConfig.getId()); - DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(protocolId); + Map fixDataMap = fieldParser.fixDataParser(protocolId,buffer); + RuleConfig ruleConfig = rulerProvider.RulerBuilder(fixDataMap,protocolId); + DatagramEventConfig datagramEventConfig = processorSelector.ProcessorProvider(String.valueOf(ruleConfig.getId())); ByteBuf bizDataContent = fieldParser.getDataContent(protocolConfig, buffer); String lightText = getSafeDatagram(datagramEventConfig, bizDataContent); - RuleConfig ruleConfig = rulerProvider.RulerBuilder(protocolId); - Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText); + Map bizDataMap = fieldParser.dataParser(ruleConfig, lightText,protocolId); Map senderDataMap = dataBuilder(datagramEventConfig, bizDataMap, protocolId); dataSender(datagramEventConfig, senderDataMap, protocolId); return true; @@ -54,13 +56,12 @@ // 是否有动态bean,没有去设置的bean,有则取之=>bean不为空,进行解密操作,否则返回原文 private String getSafeDatagram(DatagramEventConfig datagramEventConfig, ByteBuf bizDataContent) { - String safeName = StringUtils.isEmpty(datagramEventConfig.getSafeFieldId()) ? datagramEventConfig.getSafeBean() : fieldConfigDao.getFieldConfigById(datagramEventConfig.getSafeFieldId()).getFieldName(); if (!StringUtils.isEmpty(safeName)) { //需要加密 SafeStrategy safeStrategy = (SafeStrategy) ApplicationContextUtil.getBean(safeName); - String lightText=""; + String lightText = ""; // String lightText = safeStrategy.decryption(bizDataContent.toString(Charset.forName("ISO-8859-1"))); return lightText; } @@ -85,7 +86,7 @@ //获取固定字段 private Map getFixedField(String protocolId) { Map fixedDataMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); fieldConfigList.stream().forEach( fieldConfig -> { fixedDataMap.put(fieldConfig.getFieldName(), ""); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java index 735cf0b..6729cab 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/processor/impl/StandardProcessorSelector.java @@ -16,7 +16,7 @@ @Override public DatagramEventConfig ProcessorProvider(String protocolId) { QueryWrapper queryWrapper = new QueryWrapper() - .eq("protocol_id", protocolId); + .eq("rule_id", protocolId); return datagramEventMapper.selectOne(queryWrapper); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java index 4c8b940..835d11e 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/ByteMergeProvider.java @@ -34,7 +34,7 @@ for (int i = 0; i < valueList.size(); i++) { env2.put("value[" + i + "]", valueList.get(i)); } - values = String.valueOf(AviatorEvaluator.execute("value[0]+value[1]", env2)); + values = String.valueOf(AviatorEvaluator.execute(fieldRuleEngine.getExcuteOperation(), env2)); return values; } catch (ExpressionNotFoundException enf) { log.error("字节合并出现异常,解析内容为{},aviator表达式为{},异常信息为{}", JSONObject.toJSON(valueList), JSONObject.toJSON(fieldRuleEngine), enf.getMessage()); diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java index 24b9188..a5b294f 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldParser.java @@ -13,12 +13,16 @@ // Map fieldProcess(String protocolContent,); - Map dataParser(RuleConfig ruleConfig, String lightText); + Map dataParser(RuleConfig ruleConfig, String lightText,String protocolId); + + Map fixDataParser(String protocolId, ByteBuf buffer); Integer getTotalLength(ProtocolConfig protocolConfig, ByteBuf byteBuf); ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer); + ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer); + Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java index 09e03af..39f794c 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/FieldResolverManger.java @@ -8,7 +8,6 @@ import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.model.ByteResolverParam; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; @@ -30,10 +29,13 @@ private final ByteMergeProvider byteMergeProvider; //1、字段截取 => 2、简单的字节解析和复杂的字节解析 - public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig) { + public Object filedParserManger(ByteBuf byteBuf, FieldConfig fieldConfig, Integer maxFixedPosition) { Integer orignPosition = Integer.valueOf(fieldConfig.getOrignPosition()); + if (!ObjectUtil.isEmpty(maxFixedPosition)) { + orignPosition -= maxFixedPosition; + } Object fieldValue = 0; - if (fieldConfig.getOffsetLength() == 1) { + if (fieldConfig.getOffsetLength() == 1 || fieldConfig.getOffsetUnit().equals("bit")) { fieldValue = bitToResolver(byteBuf, fieldConfig); } else { ByteBuf fieldBytes = byteBuf.slice(orignPosition, fieldConfig.getOffsetLength()); @@ -76,7 +78,7 @@ } int i = 0; while (!ObjectUtil.isEmpty(ruleMapList) && i < ruleMapList.size()) { - if (ruleMapList.get(i).get("ruleType").equals("logicalShift")) { + if (ruleMapList.get(i).get("ruleType").equals("combine")) { //合并 String ruletypeId = String.valueOf(ruleMapList.get(i).get("ruleTypeId")); customizeResolveValue = byteMergeProvider.resolveByteMerge(bytStrList, ruletypeId); break; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java index d91f67a..abcb32a 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/resolver/impl/StandardFieldParser.java @@ -8,6 +8,7 @@ import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,16 +31,32 @@ //数据解析 @Override - public Map dataParser(RuleConfig ruleConfig, String lightText) { + public Map dataParser(RuleConfig ruleConfig, String lightText, String protocolId) { String[] dataFieldIds = ruleConfig.getDataFieldIds().split(","); - byte[] fieldByte = lightText.getBytes(); + ByteBuf bufferContent = ByteBufAllocator.DEFAULT.buffer(); + bufferContent.writeBytes(lightText.getBytes(Charset.forName("ISO-8859-1"))); Map dataMap = new HashMap<>(); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer maxFixedPosition = calculatePosition(fieldConfigList); for (String dataFieldId : dataFieldIds) { FieldConfig fieldConfig = fieldConfigDao.getFieldConfigById(dataFieldId); - if(!ObjectUtils.isEmpty(fieldConfig)){ - dataMap.put(fieldConfig.getFieldName(),lightText.substring(0,fieldConfig.getOffsetLength())); + if (!ObjectUtils.isEmpty(fieldConfig)) { + dataMap.put(fieldConfig.getFieldName(), String.valueOf(fieldResolverManger.filedParserManger(bufferContent, fieldConfig, maxFixedPosition))); } -// CommonParsingUtil.ByteToInt(lightText.getBytes(),fieldConfig); + } + return dataMap; + } + + //数据解析 + @Override + public Map fixDataParser(String protocolId, ByteBuf buffer) { + List dataFieldIdList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map dataMap = new HashMap<>(); + for (FieldConfig dataField : dataFieldIdList) { + if (!ObjectUtils.isEmpty(dataField.getOrignPosition())) { + Object fieldValue = (Integer) fieldResolverManger.filedParserManger(buffer, dataField, null); + dataMap.put(dataField.getFieldName(), fieldValue); + } } return dataMap; } @@ -63,7 +80,7 @@ totalFilterLength += fieldConfig.getOffsetLength(); } } - totalLength =(Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig)+ totalFilterLength; + totalLength = (Integer) fieldResolverManger.filedParserManger(byteBuf, totalFieldConfig, null) + totalFilterLength; } catch (Exception ex) { log.error("报文确认分发阶段,获取报文总长度异常,协议内容{},异常信息{}", JSONObject.toJSON(protocolConfig), ex.getMessage()); } @@ -75,22 +92,37 @@ public ByteBuf getDataContent(ProtocolConfig protocolConfig, ByteBuf buffer) { String lightText = buffer.toString(Charset.forName("ISO-8859-1")); String protocolId = String.valueOf(protocolConfig.getId()); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); // Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer totalLength = lightText.length(); Integer maxFixedPosition = calculatePosition(fieldConfigList); buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); - ByteBuf bizDataFields = buffer.slice(maxFixedPosition, (totalLength - fixedLength) + 1); + ByteBuf bizDataFields = buffer.slice(maxFixedPosition-1, (totalLength - fixedLength)); return bizDataFields; } // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 @Override + public ByteBuf getFixedData(ProtocolConfig protocolConfig, ByteBuf buffer) { + String lightText = buffer.toString(Charset.forName("ISO-8859-1")); + String protocolId = String.valueOf(protocolConfig.getId()); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Integer fixedLength = calculateLength(fieldConfigList); +// Integer totalLength = this.getTotalLength(protocolConfig, buffer); + Integer totalLength = lightText.length(); + Integer maxFixedPosition = calculatePosition(fieldConfigList); + buffer.slice(maxFixedPosition, (totalLength - fixedLength)).toString(Charset.forName("ISO-8859-1")); + ByteBuf fixDataFields = buffer.slice(0, maxFixedPosition); + return fixDataFields; + } + + // 固定字段列表=> 计算固定字段长度=> 计算业务内容起始位置=>结合总长度,计算业务内容长度=>得到业务内容 + @Override public Map getFixedProperty(ProtocolConfig protocolConfig, ByteBuf buffer) { String protocolId = String.valueOf(protocolConfig.getId()); Map fixedPropertyMap = new HashMap<>(); - List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId,1); + List fieldConfigList = fieldConfigDao.getFieldConfigs(protocolId, 1); Integer fixedLength = calculateLength(fieldConfigList); Integer totalLength = this.getTotalLength(protocolConfig, buffer); Integer maxFixedPosition = calculatePosition(fieldConfigList); @@ -128,8 +160,8 @@ Integer bitfixedLength = 0; Integer bytefixedLength = 0; for (FieldConfig fieldConfig : fieldConfigList) { - if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(String.valueOf(max))) { - if (StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { + if (!ObjectUtils.isEmpty(fieldConfig.getOrignPosition()) && fieldConfig.getOrignPosition().equals(max)) { + if (!StringUtils.isEmpty(fieldConfig.getOffsetUnit())) { if (fieldConfig.getOffsetUnit().equals("bit")) { bitfixedLength += fieldConfig.getOffsetLength(); } else if (fieldConfig.getOffsetUnit().equals("byte")) { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java index 3896ccf..12fdd55 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/rule/RulerProvider.java @@ -1,17 +1,21 @@ package com.casic.missiles.codec.decoder.rule; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.casic.missiles.domain.FieldConfig; import com.casic.missiles.domain.PreRuleConfig; import com.casic.missiles.domain.RuleConfig; import com.casic.missiles.mapper.dao.FieldConfigDao; import com.casic.missiles.mapper.dao.PreRuleConfigDao; import com.casic.missiles.mapper.dao.RuleConfigDao; +import com.mysql.cj.xdevapi.Collection; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; @Service @AllArgsConstructor @@ -21,52 +25,67 @@ private final PreRuleConfigDao preRuleConfigDao; private final RuleConfigDao ruleConfigDao; - public RuleConfig RulerBuilder(String protocolId) { + //根据前置字段的值,找到指定的规则 + //批量取出前置字段列表=>批量取出固定字段列表 =>批量规则列表=> 通过前置字段id查询字段列表的字段属性及值 + //=>循环判断=>通过规则得到业务字段 + public RuleConfig RulerBuilder(Map fixDataMap, String protocolId) { List preRuleConfigList = preRuleConfigDao.getPreRuleList(protocolId); - String ruleFields = getPreRuleFields(preRuleConfigList); - RuleConfig ruleConfig = getNextRuleConfig(ruleFields,protocolId); - while (!ObjectUtil.isEmpty(ruleConfig) && !ruleConfig.getIsBizField().equals("1")) { - ruleFields = getRuleFields(ruleConfig.getDataFieldIds().split(",")); - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); + Map fixFieldMap = getAllFieldMap(protocolId); + final String preRuleFields = getPreRuleFields(preRuleConfigList, fixDataMap, fixFieldMap); + List ruleConfigList = ruleConfigDao.getRuleConfigById(protocolId); + Optional ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig ->preRuleFields.equals( ruleConfig.getParseRuleJson())).findFirst(); + while (!ObjectUtil.isEmpty(ruleConfigOptional) && !ruleConfigOptional.get().getIsBizField().equals("1")) { + String ruleFields = getRuleFields(ruleConfigOptional.get().getDataFieldIds().split(","), fixDataMap, fixFieldMap); + ruleConfigOptional = ruleConfigList.stream().filter(ruleConfig -> ruleConfig.getParseRuleJson().equals(ruleFields)).findFirst(); } - return ruleConfig; + return ruleConfigOptional.get(); } - private String getPreRuleFields(List preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + /** + * 规则字段 + */ + private String getPreRuleFields(List preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); preRuleConfigList.stream() .forEach( preRuleConfig -> { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfig.getRuleFieldId()).getFieldName()); + String fieldName = fixFieldMap.get(Long.valueOf(preRuleConfig.getRuleFieldId())); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } ); - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } - private String getRuleFields(String[] preRuleConfigList) { - List ruleFieldList = new ArrayList<>(); + private Map getAllFieldMap(String protocolId) { + List fieldList = fieldConfigDao.getFieldConfigs(protocolId, 1); + Map fixFieldMap = fieldList.stream().collect( + Collectors.toMap(FieldConfig::getId, FieldConfig::getFieldName) + ); + return fixFieldMap; + } + + private String getRuleFields(String[] preRuleConfigList, Map fixDataMap, Map fixFieldMap) { + Map ruleFieldMap= new HashMap<>(); for (String preRuleConfigId : preRuleConfigList) { - ruleFieldList.add(fieldConfigDao.getFieldConfigById(preRuleConfigId).getFieldName()); + String fieldName = fixFieldMap.get(preRuleConfigId); + if (fixDataMap.containsKey(fieldName)) { + ruleFieldMap.put(fieldName, String.valueOf(fixDataMap.get(fieldName))); + } } - return ListToString(ruleFieldList); + return JSONObject.toJSONString(ruleFieldMap); } + + //弃置 private String ListToString(List ruleFieldList) { String ruleFields = ""; for (String ruleField : ruleFieldList) { ruleFields += ruleField + ","; } - ruleFields = StringUtils.isEmpty(ruleFields)?ruleFields:ruleFields.substring(0, ruleFields.length()-1); + ruleFields = StringUtils.isEmpty(ruleFields) ? ruleFields : ruleFields.substring(0, ruleFields.length() - 1); return ruleFields; } - private RuleConfig getNextRuleConfig(String ruleFields,String protocolId) { - RuleConfig ruleConfig=null; - if(!ObjectUtil.isEmpty(ruleFields)){ - ruleConfig = ruleConfigDao.getNextRuleConfig(ruleFields); - }else { - ruleConfig = ruleConfigDao.getRuleConfigById(protocolId); - } - return ruleConfig; - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java new file mode 100644 index 0000000..ab1e0a6 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/CRCUtil.java @@ -0,0 +1,82 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class CRCUtil { + + /** + * 将十六进制的字符串 求出 CRC 8 的 CRC_H CRC_L + */ + public static String getCRC8(String msg) { + //十六进制的字符串转换成字节数组 + byte[] bytes = hexStrToBinaryStr(msg); + //计算CRC8 + byte crc8 = CRC8(bytes, 0, bytes.length); + String crc8ToStr = "" + Integer.toHexString(0xff & crc8); + return crc8ToStr; + } + + /** + * 将十六进制的字符串转换成字节数组 + */ + public static byte[] hexStrToBinaryStr(String hexString) { + hexString = hexString.replaceAll(" ", ""); + if ((hexString.length() % 2) != 0) { + throw new IllegalArgumentException("长度不是偶数"); + } + int len = hexString.length(); + int index = 0; + byte[] bytes = new byte[len / 2]; + while (index < len) { + String sub = hexString.substring(index, index + 2); + bytes[index / 2] = (byte) Integer.parseInt(sub, 16); + index += 2; + } + return bytes; + } + + /** + * CRC8 校验 多项式 x8+x2+x+1 + * + * @param b + * @param off + * @param len + * @return + */ + public static byte CRC8(final byte[] b, final int off, final int len) { + byte result = 0x00; + for (int i = 0; i < len; i++) { + result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF]; + } + return (byte) (result & 0xFFL); + } + + private final static byte[] CHECKSUM_TABLE = {(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}; + +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java new file mode 100644 index 0000000..3478ab3 --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/MyUtils.java @@ -0,0 +1,36 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +public class MyUtils { + public static int[] byteToInt(byte[] Mybytes, int len) { + int[] RetInt = new int[len / 4]; + int i32Count = len / 4; + int i32Index = 0; + + for (int i32Increase = 0; i32Increase < i32Count; i32Increase++) { + RetInt[i32Increase] = transform(Mybytes[3 + i32Index]) | transform(Mybytes[2 + i32Index]) << 8 | transform(Mybytes[1 + i32Index]) << 16 | transform(Mybytes[0 + i32Index]) << 24; + i32Index += 4; + } + + return RetInt; + } + + public static byte[] IntToBytes(int[] MyInts, int len) { + byte[] RetBytes = new byte[len * 4]; + + for (int i32Index = 0; i32Index < len; i32Index++) { + RetBytes[0 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 24) & 0xFF); + RetBytes[1 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 16) & 0xFF); + RetBytes[2 + i32Index * 4] = (byte) ((MyInts[i32Index] >> 8) & 0xFF); + RetBytes[3 + i32Index * 4] = (byte) (MyInts[i32Index] & 0xFF); + } + return RetBytes; + } + + private static int transform(byte temp) { + int tempInt = (int) temp; + if (tempInt < 0) { + tempInt += 256; + } + return tempInt; + } +} \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java index a3ee58b..f509a64 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/Tea.java @@ -1,5 +1,6 @@ package com.casic.missiles.codec.decoder.safe.impl; +import io.netty.buffer.ByteBufUtil; import org.springframework.stereotype.Service; /** @@ -14,106 +15,109 @@ * @version v1.0: Tea.java, v 0.1 2021/3/12 13:48 $ */ @Service("tea") +///tea算法类 + public class Tea { - //加密 - public byte[] encrypt(byte[] content, int offset, int[] key, int times) {//times为加密轮数 - int[] tempInt = byteToInt(content, offset); - int y = tempInt[0], z = tempInt[1], sum = 0, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; - for (i = 0; i < times; i++) { - sum += delta; - y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + public static byte[] MyTea_Encrypt(byte[] MyTeaContent, int ByteLen, byte[] MyTeakey) { + int[] Value = MyUtils.byteToInt(MyTeaContent, ByteLen); + int sum = 0; + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + + + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + for (int i = 0; i < Value.length; i++) { + int v0 = Value[0], v1 = Value[1]; + for (; i32Index < i32times; i32Index++) { + sum += i32delta; + v0 += ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + v1 += ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + } + Value[0] = v0; + Value[1] = v1; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + int len = ByteLen / 4; + return MyUtils.IntToBytes(Value, len); } - //解密 - public byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times) { - int[] tempInt = byteToInt(encryptContent, offset); - int y = tempInt[0], z = tempInt[1], sum = 0xC6EF3720, i; - int delta = 0x9e3779b9; //这是算法标准给的值 - int a = key[0], b = key[1], c = key[2], d = key[3]; + /** + * 参数为8字节的明文输入和16字节的密钥,输出8字节密文 + * + * @param MyTeaSecContent + * @param MyTeakey + * @return + */ + public static byte[] MyTea_Decrypt(byte[] MyTeaSecContent, int ByteLen, byte[] MyTeakey) { + System.out.println("解密函数:"); + int[] Value = MyUtils.byteToInt(MyTeaSecContent, ByteLen); + int i32Index = 0; + int i32delta = 0x9e3779b9; + int i32times = 32; + int sum = i32delta * i32times; - for (i = 0; i < times; i++) { - z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); - y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); - sum -= delta; + int[] Key = MyUtils.byteToInt(MyTeakey, 16); + int v0 = Value[0], v1 = Value[1]; + + for (; i32Index < i32times; i32Index++) { + v1 -= ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]); + v0 -= ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]); + sum -= i32delta; } - tempInt[0] = y; - tempInt[1] = z; - return intToByte(tempInt, 0); + Value[0] = v0; + Value[1] = v1; + + + int len = ByteLen / 4; + + return MyUtils.IntToBytes(Value, len); } - //byte[]型数据转成int[]型数据 - private int[] byteToInt(byte[] content, int offset) { - - int[] result = new int[content.length >> 2]; //除以2的n次方 == 右移n位 即 content.length / 4 == content.length >> 2 - for (int i = 0, j = offset; j < content.length; i++, j += 4) { - result[i] = transform(content[j + 3]) | transform(content[j + 2]) << 8 | - transform(content[j + 1]) << 16 | (int) content[j] << 24; - } - return result; - - } - - //int[]型数据转成byte[]型数据 - private byte[] intToByte(int[] content, int offset) { - byte[] result = new byte[content.length << 2]; //乘以2的n次方 == 左移n位 即 content.length * 4 == content.length << 2 - for (int i = 0, j = offset; j < result.length; i++, j += 4) { - result[j + 3] = (byte) (content[i] & 0xff); - result[j + 2] = (byte) ((content[i] >> 8) & 0xff); - result[j + 1] = (byte) ((content[i] >> 16) & 0xff); - result[j] = (byte) ((content[i] >> 24) & 0xff); - } - return result; - } - - //若某字节被解释成负的则需将其转成无符号正数 - private static int transform(byte temp) { - int tempInt = (int) temp; - if (tempInt < 0) { - tempInt += 256; - } - return tempInt; - } - - // 测试代码如下: public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; - int[] KEY = new int[]{//加密解密所用的KEY - 0x789f5645, 0xf68bd5a4, - 0x81963ffa, 0x458fac58 - }; - Tea tea = new Tea(); + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37, + 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - byte[] info = new byte[]{ + byte[] demo2 = {0x10, 0x00, 0x00, 0x51, 0x00, 0x06, 0x12, 0x0B, 0x09, 0x0A, 0x33, 0x0E, 0x00, 0x00, 0x00, 0x00}; - 1, 2, 3, 4, 5, 6, 7, 8 - }; - System.out.print("原数据:"); - for (byte i : info) { - System.out.print(i + " "); + byte[] MyKeys = {48, 49, 50, 51, + 52, 53, 54, 55, + 56, 57, 97, 98, + 99, 100, 101, 102}; + + byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, + 0x0D, 0x0E, 0x0F, 0x10}; + + System.out.println("原始数据为:" + ByteBufUtil.hexDump(demo2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + byte[] retEnBytes = MyTea_Encrypt(demo2, demo2.length, MyKeys); + System.out.println("加密后为:" + ByteBufUtil.hexDump(retEnBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retEnBytes[i32Index]) + " "); } System.out.println(); - byte[] secretInfo = tea.encrypt(info, 0, KEY, 32); - System.out.print("加密后的数据:"); - for (byte i : secretInfo) { - System.out.print(i + " "); + byte[] retDeBytes = MyTea_Decrypt(retEnBytes, retEnBytes.length, MyKeys); + System.out.println("解密后为:" + ByteBufUtil.hexDump(retDeBytes)); + for (int i32Index = 0; i32Index < 8; i32Index++) { + System.out.print(Integer.toHexString(retDeBytes[i32Index]) + " "); } - System.out.println(); - - byte[] decryptInfo = tea.decrypt(secretInfo, 0, KEY, 32); - System.out.print("解密后的数据:"); - for (byte i : decryptInfo) { - System.out.print(i + " "); - } - } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java new file mode 100644 index 0000000..6e3432c --- /dev/null +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/safe/impl/TeaDemo.java @@ -0,0 +1,186 @@ +package com.casic.missiles.codec.decoder.safe.impl; + +import io.netty.buffer.ByteBufUtil; + +import java.util.ArrayList; +import java.util.List; + +public class TeaDemo { + + + static List tea_encrypt(int v1,int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y =v1, z = v2, sum = 0, i; + int delta = 0x9e3779b9; + //UInt32 a = k1, b = k2, c = k3, d = k4; + + for (i = 0; i < 32; i++) { + sum += delta; + y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * tea解密 + *参数:v:要解密的数据,长度为8字节 + * k:解密用的key,长度为16字节 + **********************************************************************/ + + static List tea_decrypt(int v1, int v2, int a, int b, int c, int d) { + List result = new ArrayList<>(); + int y = v1, z = v2, sum = 0xC6EF3720, i; + int delta = 0x9e3779b9; + //UInt32 a = k[0], b = k[1], c = k[2], d = k[3]; + + for (i = 0; i < 32; i++) { + z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); + y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); + sum -= delta; + } + result.add(y); + result.add(z); + return result; + } + + /********************************************************************* + * 加密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.加密完成后密文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:密文的字节数 + **********************************************************************/ + + public static int encrypt(byte[] src, int size_src, byte[] TEA_key) { + int a = 0; + int i = 0; + int num = 0; +// Byte[] TEA_key = new Byte[]{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; + //将明文补足为8字节的倍数 + a = (int) (size_src % 8); + if (a != 0) { + for (i = 0; i < 8 - a; i++) { + src[size_src++] = 0; + } + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //加密 + num = (int) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1= (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2= (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + + List result=tea_encrypt(src1,src2, Key1, Key2, Key3, Key4); +// src1=result.get(0); +// src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1>> 16); + src[i * 8 + 1] = (byte) (src1>> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("加密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + /********************************************************************* + * 解密算法 + *参数:src:源数据,所占空间必须为8字节的倍数.解密完成后明文也存放在这 + * size_src:源数据大小,单位字节 + * key:密钥,16字节 + *返回:明文的字节数,如果失败,返回0 + **********************************************************************/ + + public static int decrypt(byte[] src, int size_src, byte[] TEA_key) { + int i = 0; + int num = 0; + //判断长度是否为8的倍数 + if (size_src % 8 != 0) { + //printf("\r\nchang du bu shi 8!!"); + return 0; + } + + int Key1 = (int) (TEA_key[3] << 24 | TEA_key[2] << 16 | TEA_key[1] << 8 | TEA_key[0]); + int Key2 = (int) (TEA_key[7] << 24 | TEA_key[6] << 16 | TEA_key[5] << 8 | TEA_key[4]); + int Key3 = (int) (TEA_key[11] << 24 | TEA_key[10] << 16 | TEA_key[9] << 8 | TEA_key[8]); + int Key4 = (int) (TEA_key[15] << 24 | TEA_key[14] << 16 | TEA_key[13] << 8 | TEA_key[12]); + List ResultList = new ArrayList(); + //解密 + num = (byte) (size_src / 8); + for (i = 0; i < num; i++) { + //tea_decrypt((UInt32*)(src + i * 8), (UInt32*)key); + //tea_encrypt((UInt32*)(src + i * 8), key); + int src1 = (int) (src[i * 8 + 3] << 24 | src[i * 8 + 2] << 16 | src[i * 8 + 1] << 8 | src[i * 8 + 0]); + int src2 = (int) (src[i * 8 + 7] << 24 | src[i * 8 + 6] << 16 | src[i * 8 + 5] << 8 | src[i * 8 + 4]); + List result=tea_decrypt(src1,src2, Key1, Key2, Key3, Key4); + src1=result.get(0); + src2=result.get(1); + src[i * 8 + 3] = (byte) (src1 >> 24); + src[i * 8 + 2] = (byte) (src1 >> 16); + src[i * 8 + 1] = (byte) (src1 >> 8); + src[i * 8 + 0] = (byte) (src1); + src[i * 8 + 7] = (byte) (src2 >> 24); + src[i * 8 + 6] = (byte) (src2 >> 16); + src[i * 8 + 5] = (byte) (src2 >> 8); + src[i * 8 + 4] = (byte) (src2); + } + System.out.println("解密后:" + ByteBufUtil.hexDump(src)); + return size_src; + } + + public static void main(String[] args) { + byte[] info = {0x15, (byte) 0xc8, (byte) 0xa8, (byte) 0xb8, 0x74, 0x2e, 0x08, 0x13, (byte) 0xbe, 0x51, 0x34, (byte) 0xb4, 0x12, 0x08, 0x6d, (byte) 0xb0, (byte) 0xed, 0x7e, (byte) 0x89, + (byte) 0xd4, (byte) 0x92, 0x2c, 0x5c, 0x24, (byte) 0xea, (byte) 0xaa, 0x0e, (byte) 0xda, (byte) 0xc3, (byte) 0x89, (byte) 0x9b, (byte) 0xb2, (byte) 0xa2, 0x5e, + (byte) 0xc6, 0x68, (byte) 0x8d, 0x41, (byte) 0x86, (byte) 0xfe, 0x73, (byte) 0xa1, 0x75, 0x37, 0x2f, 0x7c, (byte) 0xab, (byte) 0xd7}; + + + byte[] info2 = {0x10, 0x00, 0x00, 0x63, 0x00, 0x23, 0x38, 0x36, 0x39, 0x36, 0x36, 0x32, 0x30, 0x33, 0x31, 0x35, 0x38, + 0x31, 0x33, 0x37, 0x31, 0x38, 0x39, 0x38, 0x36, 0x30, 0x36, 0x31, 0x37, 0x30, 0x37, 0x30, 0x30, 0x32, 0x32, 0x35, 0x38, 0x37 + , 0x35, 0x32, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + byte[] MyKey1 = {0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66}; +// byte[] MyKey1 = {0x01, 0x02, 0x03, 0x04, +// 0x05, 0x06, 0x07, 0x08, +// 0x09, 0x0A, 0x0B, 0x0C, +// 0x0D, 0x0E, 0x0F, 0x10}; + + +// System.out.println("原始数据为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " "); +// } + +// System.out.println("密钥为:"); +// for(int i32Index = 0; i32Index < 16; i32Index++) { +// System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " "); +// } +// System.out.println(); +// + encrypt(info2, info2.length, MyKey1); + System.out.println("加密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retEnBytes[i32Index]) + " "); +// } +// System.out.println(); + decrypt(info2, info2.length, MyKey1); +// System.out.println("解密后为:" + ByteBufUtil.hexDump(info2)); +// for(int i32Index = 0; i32Index < 8; i32Index++) { +// System.out.print("0x" + Integer.toHexString(retDeBytes[i32Index]) + " "); +// } + } +} diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java index e5fbc76..958b59d 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/decoder/selector/impl/dispenser/impl/FrameMarkDispenser.java @@ -41,7 +41,7 @@ Integer upback = 0; if (!ObjectUtil.isEmpty(UnpackConfig)) { while (hasNextFullFrame(byteBuf, protocolConfig)) { - upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig); + upback = (Integer) fieldResolverManger.filedParserManger(byteBuf, UnpackConfig,null); if (upback == 1){//使用esayRule //表示可以截取 cutOffPackageBuilder(byteBuf, protocolConfig, totalLength); @@ -85,13 +85,14 @@ Map currentFrameFixedProperty = fieldParser.getFixedProperty(protocolConfig, currentbyteBuf); if (StringUtils.isEmpty(standardFreame)) { standardFreame += hexDump.substring(0, currentFrameFixedProperty.get("tailPosition")); - tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition") + 1, hexDump.length()); + tail = hexDump.substring(currentFrameFixedProperty.get("tailPosition")); } else { - standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")); + standardFreame += hexDump.substring(currentFrameFixedProperty.get("fixedPosition"), currentFrameFixedProperty.get("tailPosition")-1); } } standardFreame += tail; bufferContent.writeBytes(standardFreame.getBytes(Charset.forName("ISO-8859-1"))); + System.out.println(ByteBufUtil.hexDump(bufferContent)); codeGeneratorProvider.codeGenerator(bufferContent, protocolConfig); } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java b/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java index 90750c2..7058286 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/codec/encoder/SensorhubEncoder.java @@ -16,9 +16,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import java.io.IOException; -import java.nio.charset.Charset; - /** * SensorhubEncoder 编码器 */ @@ -51,10 +48,15 @@ if (isValid(msg)) { // String hexBytes = "a3 20 00 35 41 20 18 47 00 aa 03 00 aa 08 86 01 15 c8 a8 b8 74 2e 08 13 be 51 34 b4 12 08 6d b0 ed 7e 89 d4 92 2c 5c 24 ea aa 0e da c3 89 9b b2 a2 5e c6 68 8d 41 86 fe 73 a1 75 37 2f 7c ab d7 ac c8"; //(井盖开机上报) - byte[] hexBytes = {(byte)0xa3,0x20,0x00,0x35,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x15,(byte)0xc8 - ,(byte)0xa8,(byte)0xb8,0x74,0x2e,0x08,0x13,(byte)0xbe,0x51,0x34,(byte)0xb4,0x12,0x08,0x6d,(byte)0xb0,(byte)0xed,0x7e,(byte)0x89, - (byte)0xd4,(byte)0x92,0x2c,0x5c,0x24,(byte)0xea,(byte)0xaa,0x0e,(byte)0xda,(byte)0xc3,(byte)0x89,(byte)0x9b,(byte)0xb2,(byte)0xa2,0x5e, - (byte)0xc6,0x68,(byte)0x8d,0x41,(byte)0x86,(byte)0xfe,0x73,(byte)0xa1,0x75,0x37,0x2f,0x7c,(byte)0xab,(byte)0xd7,(byte)0xac,(byte)0xc8}; +// byte[] hexBytes = {(byte)0xa3,0x20,0x00,0X3c,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x15,(byte)0xc8, +// (byte)0xa8,(byte)0xb8,0x74,0x2e,0x08,0x13,(byte)0xbe,0x51,0x34,(byte)0xb4,0x12,0x08,0x6d,(byte)0xb0,(byte)0xed,0x7e,(byte)0x89, +// (byte)0xd4,(byte)0x92,0x2c,0x5c,0x24,(byte)0xea,(byte)0xaa,0x0e,(byte)0xda,(byte)0xc3,(byte)0x89,(byte)0x9b,(byte)0xb2,(byte)0xa2,0x5e, +// (byte)0xc6,0x68,(byte)0x8d,0x41,(byte)0x86,(byte)0xfe,0x73,(byte)0xa1,0x75,0x37,0x2f,0x7c,(byte)0xab,(byte)0xd7,(byte)0xac,(byte)0xc8}; + + byte[] hexBytes = {(byte)0xa3,0x20,0x00,0X3c,0x41,0x20,0x18,0x47,0x00,(byte)0xaa,0x03,0x00,(byte)0xaa,0x08,(byte)0x86,0x01,0x10,(byte)0x00, + (byte)0x00,(byte)0x63,0x00,0x23,0x38,0x36,(byte)0x39,0x36,0x36,(byte)0x32,0x30,0x33,0x31,(byte)0x35,(byte)0x38,0x31,(byte)0x33, + (byte)0x37,(byte)0x31,0x38,0x39,0x38,(byte)0x36,(byte)0x30,0x36,(byte)0x31,(byte)0x37,(byte)0x30,(byte)0x37,(byte)0x30,(byte)0x30,0x32, + (byte)0x32,0x35,(byte)0x38,0x37,(byte)0x35,(byte)0x32,0x30,(byte)0x00,0x00,0x00,0x00,0x00,(byte)0x00,(byte)0x00,(byte)0xac,(byte)0xc8}; out.writeBytes(hexBytes); // 设置前导码 // byte preamble = (byte) 0xA3; @@ -165,7 +167,6 @@ log.error("oidList不能为空"); return false; } - return true; } } \ No newline at end of file diff --git a/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java b/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java index 052eab4..cd10bbe 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/domain/DatagramEventConfig.java @@ -10,7 +10,7 @@ public class DatagramEventConfig { private Long id; - private String protocolId; + private String ruleId; private String eventName; private String safeFieldId; private String safeBean; diff --git a/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java b/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java index fe52c41..334d59d 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/domain/RuleConfig.java @@ -5,6 +5,9 @@ import java.util.Date; +/** + * 加密字段解析和规则有关,事件调到该流程下面 + */ @Data @TableName("rule_config") public class RuleConfig { diff --git a/sensorhub-server/src/main/java/com/casic/missiles/mapper/dao/RuleConfigDao.java b/sensorhub-server/src/main/java/com/casic/missiles/mapper/dao/RuleConfigDao.java index 15275a1..9d23a90 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/mapper/dao/RuleConfigDao.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/mapper/dao/RuleConfigDao.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.List; @Component public class RuleConfigDao { @@ -14,16 +15,16 @@ private RuleConfigMapper ruleConfigMapper; public RuleConfig getNextRuleConfig(String ruleFields) { - QueryWrapper queryWrapper=new QueryWrapper() - .eq("parse_rule_json",ruleFields); + QueryWrapper queryWrapper = new QueryWrapper() + .eq("parse_rule_json", ruleFields); return ruleConfigMapper.selectOne(queryWrapper); } - public RuleConfig getRuleConfigById(String protocolId) { - QueryWrapper queryWrapper=new QueryWrapper() - .eq("protocol_id",protocolId); - return ruleConfigMapper.selectOne(queryWrapper); + public List getRuleConfigById(String protocolId) { + QueryWrapper queryWrapper = new QueryWrapper() + .eq("protocol_id", protocolId); + return ruleConfigMapper.selectList(queryWrapper); } } diff --git a/sensorhub-server/src/main/java/com/casic/missiles/utils/AviatorUtil.java b/sensorhub-server/src/main/java/com/casic/missiles/utils/AviatorUtil.java index b3861c0..6ef2b56 100644 --- a/sensorhub-server/src/main/java/com/casic/missiles/utils/AviatorUtil.java +++ b/sensorhub-server/src/main/java/com/casic/missiles/utils/AviatorUtil.java @@ -12,17 +12,21 @@ public class AviatorUtil { public static void main(String[] args) { - byte[] query = {0x00, 0x00, 0x00, 0x00, (byte) 0x5B, (byte) 0xA8, (byte) 0x0A, (byte) 0x40}; - String ruleJson = "[{\"ruletype\":\"logicalShift\",\"vaildRange\":\"1,2, 3,4\",},{\"ruletype\":\"mergeCombin\",\"vaildRange\":\"1,2,3,4\",},{\"ruletype\":\"conversionRule\",\"vaildRange\":\"1,2,3,4\",},{\"ruletype\":\"operation\",\"vaildRange\":\"1,2,3,4\",},{\"ruletype\":\"precision\",\"vaildRange\":\"1,2,3,4\",}]"; - List maps = JSONArray.parseArray(ruleJson, Map.class); - List bytStrList = new ArrayList<>(); - for (int i = 0; i < query.length; i++) { - bytStrList.add(query[i]); - } +// byte[] query = {0x00, 0x00, 0x00, 0x00, (byte) 0x5B, (byte) 0xA8, (byte) 0x0A, (byte) 0x40}; +// String ruleJson = "[{\"ruletype\":\"logicalShift\",\"vaildRange\":\"1,2, 3,4\",},{\"ruletype\":\"mergeCombin\",\"vaildRange\":\"1,2,3,4\",},{\"ruletype\":\"conversionRule\",\"vaildRange\":\"1,2,3,4\",},{\"ruletype\":\"operation\",\"vaildRange\":\"1,2,3,4\",},{\"ruletype\":\"precision\",\"vaildRange\":\"1,2,3,4\",}]"; +// List maps = JSONArray.parseArray(ruleJson, Map.class); +// List bytStrList = new ArrayList<>(); +// for (int i = 0; i < query.length; i++) { +// bytStrList.add(query[i]); +// } //Original String - String string = "hello world"; +// String string = "hello world"; + Map env2 = new HashMap(); + for (int i = 0; i < 2; i++) { + env2.put("value"+i ,1); + } + Object values = String.valueOf(AviatorEvaluator.execute("String.valueOf(value0) + value1", env2)); int i = 0; - }