| | |
| | | return parts.length >= 2 ? parts[parts.length - 2] : null; |
| | | } |
| | | |
| | | private String extractTopicSuffix(String mqttTopic) { |
| | | /** |
| | | * 网关格式报文拆包:报文首层 key 是设备名,value 是该设备的属性字典。 |
| | | * |
| | | * <p>例如 {"630框绞机2":{"计米":268.3499}} 拆成 {630框绞机2 -> {"计米":268.3499}}; |
| | | * 连接事件 {"device":"大拉"} 这类扁平报文没有嵌套对象,返回空 Map 由调用方另行处理。</p> |
| | | */ |
| | | public Map<String, JsonNode> splitGatewayDevices(JsonNode root) { |
| | | Map<String, JsonNode> devices = new LinkedHashMap<>(); |
| | | if (root == null || !root.isObject()) { |
| | | return devices; |
| | | } |
| | | root.properties().forEach(entry -> { |
| | | String deviceCode = StrUtil.trim(entry.getKey()); |
| | | JsonNode data = entry.getValue(); |
| | | if (StrUtil.isBlank(deviceCode) || data == null || !data.isObject()) { |
| | | return; |
| | | } |
| | | devices.put(deviceCode, data); |
| | | }); |
| | | return devices; |
| | | } |
| | | |
| | | /** |
| | | * 取主题末段作为报文类型,如 gateway/attributes -> attributes、gateway/disconnect -> disconnect |
| | | */ |
| | | public String extractTopicSuffix(String mqttTopic) { |
| | | if (StrUtil.isBlank(mqttTopic)) { |
| | | return null; |
| | | } |