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