001/** 002 * 003 * Copyright © 2016 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 org.jivesoftware.smack.packet.ExtensionElement; 024import org.jivesoftware.smack.packet.Message; 025import org.jivesoftware.smack.util.XmlStringBuilder; 026import org.jivesoftware.smackx.iot.element.NodeInfo; 027 028public class IoTFieldsExtension implements ExtensionElement { 029 030 public static final String ELEMENT = "fields"; 031 public static final String NAMESPACE = Constants.IOT_SENSORDATA_NAMESPACE; 032 033 private final int seqNr; 034 private final boolean done; 035 private final List<NodeElement> nodes; 036 037 public IoTFieldsExtension(int seqNr, boolean done, NodeElement node) { 038 this(seqNr, done, Collections.singletonList(node)); 039 } 040 041 public IoTFieldsExtension(int seqNr, boolean done, List<NodeElement> nodes) { 042 this.seqNr = seqNr; 043 this.done = done; 044 this.nodes = Collections.unmodifiableList(nodes); 045 } 046 047 public int getSequenceNr() { 048 return seqNr; 049 } 050 051 public boolean isDone() { 052 return done; 053 } 054 055 public List<NodeElement> getNodes() { 056 return nodes; 057 } 058 059 @Override 060 public String getElementName() { 061 return ELEMENT; 062 } 063 064 @Override 065 public String getNamespace() { 066 return NAMESPACE; 067 } 068 069 @Override 070 public XmlStringBuilder toXML() { 071 XmlStringBuilder xml = new XmlStringBuilder(this); 072 xml.attribute("seqnr", Integer.toString(seqNr)); 073 xml.attribute("done", done); 074 xml.rightAngleBracket(); 075 076 xml.append(nodes); 077 078 xml.closeElement(this); 079 return xml; 080 } 081 082 public static IoTFieldsExtension buildFor(int seqNr, boolean done, NodeInfo nodeInfo, 083 List<? extends IoTDataField> data) { 084 TimestampElement timestampElement = new TimestampElement(new Date(), data); 085 NodeElement nodeElement = new NodeElement(nodeInfo, timestampElement); 086 return new IoTFieldsExtension(seqNr, done, nodeElement); 087 } 088 089 public static IoTFieldsExtension from(Message message) { 090 return message.getExtension(ELEMENT, NAMESPACE); 091 } 092}