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.control;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Map;
022import java.util.WeakHashMap;
023import java.util.concurrent.ConcurrentHashMap;
024
025import org.jivesoftware.smack.SmackException.NoResponseException;
026import org.jivesoftware.smack.SmackException.NotConnectedException;
027import org.jivesoftware.smack.XMPPConnection;
028import org.jivesoftware.smack.XMPPException.XMPPErrorException;
029import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
030import org.jivesoftware.smack.packet.IQ;
031import org.jivesoftware.smackx.iot.IoTManager;
032import org.jivesoftware.smackx.iot.Thing;
033import org.jivesoftware.smackx.iot.control.element.IoTSetRequest;
034import org.jivesoftware.smackx.iot.control.element.IoTSetResponse;
035import org.jivesoftware.smackx.iot.control.element.SetData;
036import org.jivesoftware.smackx.iot.element.NodeInfo;
037import org.jxmpp.jid.FullJid;
038
039/**
040 * A manger for XEP-0325: Internet of Things - Control.
041 * 
042 * @author Florian Schmaus {@literal <flo@geekplace.eu>}
043 * @see <a href="http://xmpp.org/extensions/xep-0325.html">XEP-0323: Internet of Things - Control</a>
044 */
045public final class IoTControlManager extends IoTManager {
046
047    private static final Map<XMPPConnection, IoTControlManager> INSTANCES = new WeakHashMap<>();
048
049    /**
050     * Get the manger instance responsible for the given connection.
051     *
052     * @param connection the XMPP connection.
053     * @return a manager instance.
054     */
055    public static synchronized IoTControlManager getInstanceFor(XMPPConnection connection) {
056        IoTControlManager manager = INSTANCES.get(connection);
057        if (manager == null) {
058            manager = new IoTControlManager(connection);
059            INSTANCES.put(connection, manager);
060        }
061        return manager;
062    }
063
064    private final Map<NodeInfo, Thing> things = new ConcurrentHashMap<>();
065
066    private IoTControlManager(XMPPConnection connection) {
067        super(connection);
068
069        connection.registerIQRequestHandler(new IoTIqRequestHandler(IoTSetRequest.ELEMENT, IoTSetRequest.NAMESPACE, IQ.Type.set, Mode.async) {
070            @Override
071            public IQ handleIoTIqRequest(IQ iqRequest) {
072                // TODO Lookup thing and provide data.
073                IoTSetRequest iotSetRequest = (IoTSetRequest) iqRequest;
074
075                // TODO Add support for multiple things(/NodeInfos).
076                final Thing thing = things.get(NodeInfo.EMPTY);
077                if (thing == null) {
078                    // TODO return error if not at least one thing registered.
079                    return null;
080                }
081
082                ThingControlRequest controlRequest = thing.getControlRequestHandler();
083                if (controlRequest == null) {
084                    // TODO return error if no request handler for things.
085                    return null;
086                }
087
088                try {
089                    controlRequest.processRequest(iotSetRequest.getFrom(), iotSetRequest.getSetData());
090                } catch (XMPPErrorException e) {
091                    return IQ.createErrorResponse(iotSetRequest, e.getXMPPError());
092                }
093
094                return new IoTSetResponse(iotSetRequest);
095            }
096        });
097    }
098
099    /**
100     * Control a thing by sending a collection of {@link SetData} instructions.
101     *
102     * @param jid
103     * @param data
104     * @return a IoTSetResponse
105     * @throws NoResponseException
106     * @throws XMPPErrorException
107     * @throws NotConnectedException
108     * @throws InterruptedException
109     * @see #setUsingIq(FullJid, Collection)
110     */
111    public IoTSetResponse setUsingIq(FullJid jid, SetData data) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
112        return setUsingIq(jid, Collections.singleton(data));
113    }
114
115    /**
116     * Control a thing by sending a collection of {@link SetData} instructions.
117     *
118     * @param jid the thing to control.
119     * @param data a collection of {@link SetData} instructions.
120     * @return the {@link IoTSetResponse} if successful.
121     * @throws NoResponseException
122     * @throws XMPPErrorException
123     * @throws NotConnectedException
124     * @throws InterruptedException
125     */
126    public IoTSetResponse setUsingIq(FullJid jid, Collection<? extends SetData> data) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
127        IoTSetRequest request = new IoTSetRequest(data);
128        request.setTo(jid);
129        IoTSetResponse response = connection().createStanzaCollectorAndSend(request).nextResultOrThrow();
130        return response;
131    }
132
133    /**
134     * Install a thing in the manager. Activates control functionality (if provided by the thing).
135     *
136     * @param thing the thing to install.
137     */
138    public void installThing(Thing thing) {
139        things.put(thing.getNodeInfo(), thing);
140    }
141
142    public Thing uninstallThing(Thing thing) {
143        return uninstallThing(thing.getNodeInfo());
144    }
145
146    public Thing uninstallThing(NodeInfo nodeInfo) {
147        return things.remove(nodeInfo);
148    }
149}