001/** 002 * 003 * Copyright © 2016-2020 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.iot.data.element; 018 019import java.util.Collections; 020import java.util.Date; 021import java.util.List; 022 023import javax.xml.namespace.QName; 024 025import org.jivesoftware.smack.packet.ExtensionElement; 026import org.jivesoftware.smack.packet.Message; 027import org.jivesoftware.smack.util.XmlStringBuilder; 028 029import org.jivesoftware.smackx.iot.element.NodeInfo; 030 031public class IoTFieldsExtension implements ExtensionElement { 032 033 public static final String ELEMENT = "fields"; 034 public static final String NAMESPACE = Constants.IOT_SENSORDATA_NAMESPACE; 035 public static final QName QNAME = new QName(NAMESPACE, ELEMENT); 036 037 private final int seqNr; 038 private final boolean done; 039 private final List<NodeElement> nodes; 040 041 public IoTFieldsExtension(int seqNr, boolean done, NodeElement node) { 042 this(seqNr, done, Collections.singletonList(node)); 043 } 044 045 public IoTFieldsExtension(int seqNr, boolean done, List<NodeElement> nodes) { 046 this.seqNr = seqNr; 047 this.done = done; 048 this.nodes = Collections.unmodifiableList(nodes); 049 } 050 051 public int getSequenceNr() { 052 return seqNr; 053 } 054 055 public boolean isDone() { 056 return done; 057 } 058 059 public List<NodeElement> getNodes() { 060 return nodes; 061 } 062 063 @Override 064 public String getElementName() { 065 return ELEMENT; 066 } 067 068 @Override 069 public String getNamespace() { 070 return NAMESPACE; 071 } 072 073 @Override 074 public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 075 XmlStringBuilder xml = new XmlStringBuilder(this); 076 xml.attribute("seqnr", Integer.toString(seqNr)); 077 xml.attribute("done", done); 078 xml.rightAngleBracket(); 079 080 xml.append(nodes); 081 082 xml.closeElement(this); 083 return xml; 084 } 085 086 @SuppressWarnings("JavaUtilDate") 087 public static IoTFieldsExtension buildFor(int seqNr, boolean done, NodeInfo nodeInfo, 088 List<? extends IoTDataField> data) { 089 TimestampElement timestampElement = new TimestampElement(new Date(), data); 090 NodeElement nodeElement = new NodeElement(nodeInfo, timestampElement); 091 return new IoTFieldsExtension(seqNr, done, nodeElement); 092 } 093 094 public static IoTFieldsExtension from(Message message) { 095 return message.getExtension(IoTFieldsExtension.class); 096 } 097}