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;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022import java.util.WeakHashMap;
023import java.util.concurrent.ConcurrentHashMap;
024import java.util.concurrent.atomic.AtomicInteger;
025import java.util.logging.Level;
026import java.util.logging.Logger;
027
028import org.jivesoftware.smack.ConnectionCreationListener;
029import org.jivesoftware.smack.SmackException.NoResponseException;
030import org.jivesoftware.smack.SmackException.NotConnectedException;
031import org.jivesoftware.smack.StanzaCollector;
032import org.jivesoftware.smack.XMPPConnection;
033import org.jivesoftware.smack.XMPPConnectionRegistry;
034import org.jivesoftware.smack.XMPPException.XMPPErrorException;
035import org.jivesoftware.smack.filter.StanzaFilter;
036import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
037import org.jivesoftware.smack.packet.IQ;
038import org.jivesoftware.smack.packet.Message;
039
040import org.jivesoftware.smackx.iot.IoTManager;
041import org.jivesoftware.smackx.iot.Thing;
042import org.jivesoftware.smackx.iot.data.element.IoTDataField;
043import org.jivesoftware.smackx.iot.data.element.IoTDataReadOutAccepted;
044import org.jivesoftware.smackx.iot.data.element.IoTDataRequest;
045import org.jivesoftware.smackx.iot.data.element.IoTFieldsExtension;
046import org.jivesoftware.smackx.iot.data.filter.IoTFieldsExtensionFilter;
047import org.jivesoftware.smackx.iot.element.NodeInfo;
048
049import org.jxmpp.jid.EntityFullJid;
050
051/**
052 * A manager for XEP-0323: Internet of Things - Sensor Data.
053 *
054 * @author Florian Schmaus {@literal <flo@geekplace.eu>}
055 * @see <a href="http://xmpp.org/extensions/xep-0323.html">XEP-0323: Internet of Things - Sensor Data</a>
056 */
057public final class IoTDataManager extends IoTManager {
058
059    private static final Logger LOGGER = Logger.getLogger(IoTDataManager.class.getName());
060
061    private static final Map<XMPPConnection, IoTDataManager> INSTANCES = new WeakHashMap<>();
062
063    // Ensure a IoTDataManager exists for every connection.
064    static {
065        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
066            @Override
067            public void connectionCreated(XMPPConnection connection) {
068                if (!isAutoEnableActive()) return;
069                getInstanceFor(connection);
070            }
071        });
072    }
073
074    /**
075     * Get the manger instance responsible for the given connection.
076     *
077     * @param connection the XMPP connection.
078     * @return a manager instance.
079     */
080    public static synchronized IoTDataManager getInstanceFor(XMPPConnection connection) {
081        IoTDataManager manager = INSTANCES.get(connection);
082        if (manager == null) {
083            manager = new IoTDataManager(connection);
084            INSTANCES.put(connection, manager);
085        }
086        return manager;
087    }
088
089    private final AtomicInteger nextSeqNr = new AtomicInteger();
090
091    private final Map<NodeInfo, Thing> things = new ConcurrentHashMap<>();
092
093    private IoTDataManager(XMPPConnection connection) {
094        super(connection);
095        connection.registerIQRequestHandler(new IoTIqRequestHandler(IoTDataRequest.ELEMENT,
096                        IoTDataRequest.NAMESPACE, IQ.Type.get, Mode.async) {
097            @Override
098            public IQ handleIoTIqRequest(IQ iqRequest) {
099                final IoTDataRequest dataRequest = (IoTDataRequest) iqRequest;
100
101                if (!dataRequest.isMomentary()) {
102                    // TODO return error IQ that non momentary requests are not implemented yet.
103                    return null;
104                }
105
106                // TODO Add support for multiple things(/NodeInfos).
107                final Thing thing = things.get(NodeInfo.EMPTY);
108                if (thing == null) {
109                    // TODO return error if not at least one thing registered.
110                    return null;
111                }
112
113                ThingMomentaryReadOutRequest readOutRequest = thing.getMomentaryReadOutRequestHandler();
114                if (readOutRequest == null) {
115                    // TODO Thing does not provide momentary read-out
116                    return null;
117                }
118
119                // Callback hell begins here. :) XEP-0323 decouples the read-out results from the IQ result. I'm not
120                // sure if I would have made the same design decision but the reasons where likely being able to get a
121                // fast read-out acknowledgement back to the requester even with sensors that take "a long time" to
122                // read-out their values. I had designed that as special case and made the "results in IQ response" the
123                // normal case.
124                readOutRequest.momentaryReadOutRequest(new ThingMomentaryReadOutResult() {
125                    @Override
126                    public void momentaryReadOut(List<? extends IoTDataField> results) {
127                        IoTFieldsExtension iotFieldsExtension = IoTFieldsExtension.buildFor(dataRequest.getSequenceNr(), true, thing.getNodeInfo(), results);
128                        Message message = new Message(dataRequest.getFrom());
129                        message.addExtension(iotFieldsExtension);
130                        try {
131                            connection().sendStanza(message);
132                        }
133                        catch (NotConnectedException | InterruptedException e) {
134                            LOGGER.log(Level.SEVERE, "Could not send read-out response " + message, e);
135                        }
136                    }
137                });
138
139                return new IoTDataReadOutAccepted(dataRequest);
140            }
141        });
142    }
143
144    /**
145     * Install a thing in the manager. Activates data read out functionality (if provided by the
146     * thing).
147     *
148     * @param thing the thing to install.
149     */
150    public void installThing(Thing thing) {
151        things.put(thing.getNodeInfo(), thing);
152    }
153
154    public Thing uninstallThing(Thing thing) {
155        return uninstallThing(thing.getNodeInfo());
156    }
157
158    public Thing uninstallThing(NodeInfo nodeInfo) {
159        return things.remove(nodeInfo);
160    }
161
162    /**
163     * Try to read out a things momentary values.
164     *
165     * @param jid the full JID of the thing to read data from.
166     * @return a list with the read out data.
167     * @throws NoResponseException
168     * @throws XMPPErrorException
169     * @throws NotConnectedException
170     * @throws InterruptedException
171     */
172    public List<IoTFieldsExtension> requestMomentaryValuesReadOut(EntityFullJid jid)
173                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
174        final XMPPConnection connection = connection();
175        final int seqNr = nextSeqNr.incrementAndGet();
176        IoTDataRequest iotDataRequest = new IoTDataRequest(seqNr, true);
177        iotDataRequest.setTo(jid);
178
179        StanzaFilter doneFilter = new IoTFieldsExtensionFilter(seqNr, true);
180        StanzaFilter dataFilter = new IoTFieldsExtensionFilter(seqNr, false);
181
182        // Setup the IoTFieldsExtension message collectors before sending the IQ to avoid a data race.
183        StanzaCollector doneCollector = connection.createStanzaCollector(doneFilter);
184
185        StanzaCollector.Configuration dataCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(
186                        dataFilter).setCollectorToReset(doneCollector);
187        StanzaCollector dataCollector = connection.createStanzaCollector(dataCollectorConfiguration);
188
189        try {
190            connection.createStanzaCollectorAndSend(iotDataRequest).nextResultOrThrow();
191            // Wait until a message with an IoTFieldsExtension and the done flag comes in.
192            doneCollector.nextResult();
193        }
194        finally {
195            // Ensure that the two collectors are canceled in any case.
196            dataCollector.cancel();
197            doneCollector.cancel();
198        }
199
200        int collectedCount = dataCollector.getCollectedCount();
201        List<IoTFieldsExtension> res = new ArrayList<>(collectedCount);
202        for (int i = 0; i < collectedCount; i++) {
203            Message message = dataCollector.pollResult();
204            IoTFieldsExtension iotFieldsExtension = IoTFieldsExtension.from(message);
205            res.add(iotFieldsExtension);
206        }
207
208        return res;
209    }
210}