|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package cn.edu.tsinghua.iginx.mqtt; |
| 20 | + |
| 21 | +import cn.edu.tsinghua.iginx.conf.Config; |
| 22 | +import cn.edu.tsinghua.iginx.conf.ConfigDescriptor; |
| 23 | +import cn.edu.tsinghua.iginx.thrift.DataType; |
| 24 | +import com.google.gson.Gson; |
| 25 | +import com.google.gson.GsonBuilder; |
| 26 | +import com.google.gson.JsonArray; |
| 27 | +import com.google.gson.JsonElement; |
| 28 | +import com.google.gson.JsonObject; |
| 29 | +import io.netty.buffer.ByteBuf; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +import java.nio.charset.StandardCharsets; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +public class ShangFeiPayloadFormatter implements IPayloadFormatter { |
| 39 | + |
| 40 | + private static final Gson gson = new GsonBuilder().create(); |
| 41 | + |
| 42 | + private static final Logger logger = LoggerFactory.getLogger(ShangFeiPayloadFormatter.class); |
| 43 | + |
| 44 | + private final Config config = ConfigDescriptor.getInstance().getConfig(); |
| 45 | + |
| 46 | + @Override |
| 47 | + public List<Message> format(ByteBuf payload) { |
| 48 | + if (payload == null) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + String txt = payload.toString(StandardCharsets.UTF_8); |
| 52 | + JsonArray jsonArray = gson.fromJson(txt, JsonArray.class); |
| 53 | + List<Message> messages = new ArrayList<>(); |
| 54 | + |
| 55 | + for (int i = 0; i < jsonArray.size(); i++) { |
| 56 | + JsonObject jsonObject = jsonArray.get(i).getAsJsonObject(); |
| 57 | + JsonObject metadata = jsonObject.get("metadata").getAsJsonObject(); |
| 58 | + JsonElement value = jsonObject.get("value"); |
| 59 | + |
| 60 | + String displayName = metadata.get("displayName").getAsString(); |
| 61 | + String path = metadata.get("path").getAsString().replace('/', '.').substring(1) + jsonObject.get("name").getAsString() + "@" + displayName; |
| 62 | + if (config.isEnableEdgeCloudCollaboration() && config.isEdge() && !config.getEdgeName().equals("")) { |
| 63 | + path = config.getEdgeName() + "." + path; |
| 64 | + } |
| 65 | + long timestamp = jsonObject.get("timestamp").getAsLong(); |
| 66 | + |
| 67 | + Message message = new Message(); |
| 68 | + message.setPath(path); |
| 69 | + message.setTimestamp(timestamp); |
| 70 | + |
| 71 | + String dataType = metadata.get("dataType").getAsString(); |
| 72 | + switch (dataType) { |
| 73 | + case "Boolean": |
| 74 | + message.setValue(value.getAsBoolean()); |
| 75 | + message.setDataType(DataType.BOOLEAN); |
| 76 | + break; |
| 77 | + case "Char": |
| 78 | + message.setValue(value.getAsString().getBytes(StandardCharsets.UTF_8)); |
| 79 | + message.setDataType(DataType.BINARY); |
| 80 | + break; |
| 81 | + case "Byte": |
| 82 | + message.setValue(value.getAsInt()); |
| 83 | + message.setDataType(DataType.INTEGER); |
| 84 | + break; |
| 85 | + default: |
| 86 | + logger.warn("unknown datatype of mqtt: " + dataType); |
| 87 | + } |
| 88 | + messages.add(message); |
| 89 | + } |
| 90 | + return messages; |
| 91 | + } |
| 92 | +} |
0 commit comments