ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/framework/message/Directive.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.framework.message; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import com.baidu.duer.dcs.util.ObjectMapperUtil; |
| TMBOY | 45:2aa9f933c8d2 | 19 | |
| TMBOY | 45:2aa9f933c8d2 | 20 | import org.codehaus.jackson.JsonNode; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import org.codehaus.jackson.JsonParseException; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import org.codehaus.jackson.JsonParser; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import org.codehaus.jackson.annotate.JsonIgnore; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import org.codehaus.jackson.map.DeserializationContext; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import org.codehaus.jackson.map.JsonDeserializer; |
| TMBOY | 45:2aa9f933c8d2 | 26 | import org.codehaus.jackson.map.JsonMappingException; |
| TMBOY | 45:2aa9f933c8d2 | 27 | import org.codehaus.jackson.map.ObjectReader; |
| TMBOY | 45:2aa9f933c8d2 | 28 | import org.codehaus.jackson.map.annotate.JsonDeserialize; |
| TMBOY | 45:2aa9f933c8d2 | 29 | import org.codehaus.jackson.node.ObjectNode; |
| TMBOY | 45:2aa9f933c8d2 | 30 | |
| TMBOY | 45:2aa9f933c8d2 | 31 | import java.io.IOException; |
| TMBOY | 45:2aa9f933c8d2 | 32 | import java.util.Iterator; |
| TMBOY | 45:2aa9f933c8d2 | 33 | import java.util.Map; |
| TMBOY | 45:2aa9f933c8d2 | 34 | |
| TMBOY | 45:2aa9f933c8d2 | 35 | /** |
| TMBOY | 45:2aa9f933c8d2 | 36 | * 指令类:服务端下发给设备端去执行 |
| TMBOY | 45:2aa9f933c8d2 | 37 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 38 | * Created by wuruisheng on 2017/5/31. |
| TMBOY | 45:2aa9f933c8d2 | 39 | */ |
| TMBOY | 45:2aa9f933c8d2 | 40 | @JsonDeserialize(using = Directive.DirectiveDeserializer.class) |
| TMBOY | 45:2aa9f933c8d2 | 41 | public class Directive { |
| TMBOY | 45:2aa9f933c8d2 | 42 | public Header header; |
| TMBOY | 45:2aa9f933c8d2 | 43 | public Payload payload; |
| TMBOY | 45:2aa9f933c8d2 | 44 | |
| TMBOY | 45:2aa9f933c8d2 | 45 | @JsonIgnore |
| TMBOY | 45:2aa9f933c8d2 | 46 | public String rawMessage; |
| TMBOY | 45:2aa9f933c8d2 | 47 | |
| TMBOY | 45:2aa9f933c8d2 | 48 | |
| TMBOY | 45:2aa9f933c8d2 | 49 | public Directive(Header header, JsonNode payload, String rawMessage) throws IOException { |
| TMBOY | 45:2aa9f933c8d2 | 50 | this.header = header; |
| TMBOY | 45:2aa9f933c8d2 | 51 | String namespace = header.getNamespace(); |
| TMBOY | 45:2aa9f933c8d2 | 52 | String name = header.getName(); |
| TMBOY | 45:2aa9f933c8d2 | 53 | Class<?> payloadType = PayloadConfig.getInstance().findPayloadClass(namespace, name); |
| TMBOY | 45:2aa9f933c8d2 | 54 | if (null != payloadType) { |
| TMBOY | 45:2aa9f933c8d2 | 55 | this.payload = ObjectMapperUtil.instance().getObjectReader().withType(payloadType).readValue(payload); |
| TMBOY | 45:2aa9f933c8d2 | 56 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 57 | this.payload = new Payload(); |
| TMBOY | 45:2aa9f933c8d2 | 58 | } |
| TMBOY | 45:2aa9f933c8d2 | 59 | |
| TMBOY | 45:2aa9f933c8d2 | 60 | this.rawMessage = rawMessage; |
| TMBOY | 45:2aa9f933c8d2 | 61 | } |
| TMBOY | 45:2aa9f933c8d2 | 62 | |
| TMBOY | 45:2aa9f933c8d2 | 63 | /** |
| TMBOY | 45:2aa9f933c8d2 | 64 | * jackson反序列化directive对象 |
| TMBOY | 45:2aa9f933c8d2 | 65 | */ |
| TMBOY | 45:2aa9f933c8d2 | 66 | public static class DirectiveDeserializer extends JsonDeserializer<Directive> { |
| TMBOY | 45:2aa9f933c8d2 | 67 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 68 | public Directive deserialize(JsonParser jp, DeserializationContext ctx) |
| TMBOY | 45:2aa9f933c8d2 | 69 | throws IOException { |
| TMBOY | 45:2aa9f933c8d2 | 70 | ObjectReader reader = ObjectMapperUtil.instance().getObjectReader(); |
| TMBOY | 45:2aa9f933c8d2 | 71 | ObjectNode obj = (ObjectNode) reader.readTree(jp); |
| TMBOY | 45:2aa9f933c8d2 | 72 | Iterator<Map.Entry<String, JsonNode>> elementsIterator = obj.getFields(); |
| TMBOY | 45:2aa9f933c8d2 | 73 | |
| TMBOY | 45:2aa9f933c8d2 | 74 | String rawMessage = obj.toString(); |
| TMBOY | 45:2aa9f933c8d2 | 75 | DialogRequestIdHeader header = null; |
| TMBOY | 45:2aa9f933c8d2 | 76 | JsonNode payloadNode = null; |
| TMBOY | 45:2aa9f933c8d2 | 77 | ObjectReader headerReader = |
| TMBOY | 45:2aa9f933c8d2 | 78 | ObjectMapperUtil.instance().getObjectReader(DialogRequestIdHeader.class); |
| TMBOY | 45:2aa9f933c8d2 | 79 | while (elementsIterator.hasNext()) { |
| TMBOY | 45:2aa9f933c8d2 | 80 | Map.Entry<String, JsonNode> element = elementsIterator.next(); |
| TMBOY | 45:2aa9f933c8d2 | 81 | if (element.getKey().equals("header")) { |
| TMBOY | 45:2aa9f933c8d2 | 82 | header = headerReader.readValue(element.getValue()); |
| TMBOY | 45:2aa9f933c8d2 | 83 | } |
| TMBOY | 45:2aa9f933c8d2 | 84 | if (element.getKey().equals("payload")) { |
| TMBOY | 45:2aa9f933c8d2 | 85 | payloadNode = element.getValue(); |
| TMBOY | 45:2aa9f933c8d2 | 86 | } |
| TMBOY | 45:2aa9f933c8d2 | 87 | } |
| TMBOY | 45:2aa9f933c8d2 | 88 | if (header == null) { |
| TMBOY | 45:2aa9f933c8d2 | 89 | throw ctx.mappingException("Missing header"); |
| TMBOY | 45:2aa9f933c8d2 | 90 | } |
| TMBOY | 45:2aa9f933c8d2 | 91 | if (payloadNode == null) { |
| TMBOY | 45:2aa9f933c8d2 | 92 | throw ctx.mappingException("Missing payload"); |
| TMBOY | 45:2aa9f933c8d2 | 93 | } |
| TMBOY | 45:2aa9f933c8d2 | 94 | |
| TMBOY | 45:2aa9f933c8d2 | 95 | return createDirective(header, payloadNode, rawMessage); |
| TMBOY | 45:2aa9f933c8d2 | 96 | } |
| TMBOY | 45:2aa9f933c8d2 | 97 | |
| TMBOY | 45:2aa9f933c8d2 | 98 | private Directive createDirective(Header header, JsonNode payload, String rawMessage) |
| TMBOY | 45:2aa9f933c8d2 | 99 | throws JsonParseException, JsonMappingException, IOException { |
| TMBOY | 45:2aa9f933c8d2 | 100 | return new Directive(header, payload, rawMessage); |
| TMBOY | 45:2aa9f933c8d2 | 101 | } |
| TMBOY | 45:2aa9f933c8d2 | 102 | } |
| TMBOY | 45:2aa9f933c8d2 | 103 | |
| TMBOY | 45:2aa9f933c8d2 | 104 | @JsonIgnore |
| TMBOY | 45:2aa9f933c8d2 | 105 | public String getName() { |
| TMBOY | 45:2aa9f933c8d2 | 106 | return header.getName(); |
| TMBOY | 45:2aa9f933c8d2 | 107 | } |
| TMBOY | 45:2aa9f933c8d2 | 108 | |
| TMBOY | 45:2aa9f933c8d2 | 109 | public Payload getPayload() { |
| TMBOY | 45:2aa9f933c8d2 | 110 | return payload; |
| TMBOY | 45:2aa9f933c8d2 | 111 | } |
| TMBOY | 45:2aa9f933c8d2 | 112 | } |
