IoTControlManager.java

  1. /**
  2.  *
  3.  * Copyright 2016 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.iot.control;

  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.Map;
  21. import java.util.WeakHashMap;
  22. import java.util.concurrent.ConcurrentHashMap;

  23. import org.jivesoftware.smack.SmackException.NoResponseException;
  24. import org.jivesoftware.smack.SmackException.NotConnectedException;
  25. import org.jivesoftware.smack.XMPPConnection;
  26. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  27. import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
  28. import org.jivesoftware.smack.packet.IQ;

  29. import org.jivesoftware.smackx.iot.IoTManager;
  30. import org.jivesoftware.smackx.iot.Thing;
  31. import org.jivesoftware.smackx.iot.control.element.IoTSetRequest;
  32. import org.jivesoftware.smackx.iot.control.element.IoTSetResponse;
  33. import org.jivesoftware.smackx.iot.control.element.SetData;
  34. import org.jivesoftware.smackx.iot.element.NodeInfo;

  35. import org.jxmpp.jid.FullJid;

  36. /**
  37.  * A manger for XEP-0325: Internet of Things - Control.
  38.  *
  39.  * @author Florian Schmaus {@literal <flo@geekplace.eu>}
  40.  * @see <a href="http://xmpp.org/extensions/xep-0325.html">XEP-0323: Internet of Things - Control</a>
  41.  */
  42. public final class IoTControlManager extends IoTManager {

  43.     private static final Map<XMPPConnection, IoTControlManager> INSTANCES = new WeakHashMap<>();

  44.     /**
  45.      * Get the manger instance responsible for the given connection.
  46.      *
  47.      * @param connection the XMPP connection.
  48.      * @return a manager instance.
  49.      */
  50.     public static synchronized IoTControlManager getInstanceFor(XMPPConnection connection) {
  51.         IoTControlManager manager = INSTANCES.get(connection);
  52.         if (manager == null) {
  53.             manager = new IoTControlManager(connection);
  54.             INSTANCES.put(connection, manager);
  55.         }
  56.         return manager;
  57.     }

  58.     private final Map<NodeInfo, Thing> things = new ConcurrentHashMap<>();

  59.     private IoTControlManager(XMPPConnection connection) {
  60.         super(connection);

  61.         connection.registerIQRequestHandler(new IoTIqRequestHandler(IoTSetRequest.ELEMENT, IoTSetRequest.NAMESPACE, IQ.Type.set, Mode.async) {
  62.             @Override
  63.             public IQ handleIoTIqRequest(IQ iqRequest) {
  64.                 // TODO Lookup thing and provide data.
  65.                 IoTSetRequest iotSetRequest = (IoTSetRequest) iqRequest;

  66.                 // TODO Add support for multiple things(/NodeInfos).
  67.                 final Thing thing = things.get(NodeInfo.EMPTY);
  68.                 if (thing == null) {
  69.                     // TODO return error if not at least one thing registered.
  70.                     return null;
  71.                 }

  72.                 ThingControlRequest controlRequest = thing.getControlRequestHandler();
  73.                 if (controlRequest == null) {
  74.                     // TODO return error if no request handler for things.
  75.                     return null;
  76.                 }

  77.                 try {
  78.                     controlRequest.processRequest(iotSetRequest.getFrom(), iotSetRequest.getSetData());
  79.                 } catch (XMPPErrorException e) {
  80.                     return IQ.createErrorResponse(iotSetRequest, e.getStanzaError());
  81.                 }

  82.                 return new IoTSetResponse(iotSetRequest);
  83.             }
  84.         });
  85.     }

  86.     /**
  87.      * Control a thing by sending a collection of {@link SetData} instructions.
  88.      *
  89.      * @param jid TODO javadoc me please
  90.      * @param data TODO javadoc me please
  91.      * @return a IoTSetResponse
  92.      * @throws NoResponseException if there was no response from the remote entity.
  93.      * @throws XMPPErrorException if there was an XMPP error returned.
  94.      * @throws NotConnectedException if the XMPP connection is not connected.
  95.      * @throws InterruptedException if the calling thread was interrupted.
  96.      * @see #setUsingIq(FullJid, Collection)
  97.      */
  98.     public IoTSetResponse setUsingIq(FullJid jid, SetData data) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  99.         return setUsingIq(jid, Collections.singleton(data));
  100.     }

  101.     /**
  102.      * Control a thing by sending a collection of {@link SetData} instructions.
  103.      *
  104.      * @param jid the thing to control.
  105.      * @param data a collection of {@link SetData} instructions.
  106.      * @return the {@link IoTSetResponse} if successful.
  107.      * @throws NoResponseException if there was no response from the remote entity.
  108.      * @throws XMPPErrorException if there was an XMPP error returned.
  109.      * @throws NotConnectedException if the XMPP connection is not connected.
  110.      * @throws InterruptedException if the calling thread was interrupted.
  111.      */
  112.     public IoTSetResponse setUsingIq(FullJid jid, Collection<? extends SetData> data) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  113.         IoTSetRequest request = new IoTSetRequest(data);
  114.         request.setTo(jid);
  115.         IoTSetResponse response = connection().sendIqRequestAndWaitForResponse(request);
  116.         return response;
  117.     }

  118.     /**
  119.      * Install a thing in the manager. Activates control functionality (if provided by the thing).
  120.      *
  121.      * @param thing the thing to install.
  122.      */
  123.     public void installThing(Thing thing) {
  124.         things.put(thing.getNodeInfo(), thing);
  125.     }

  126.     public Thing uninstallThing(Thing thing) {
  127.         return uninstallThing(thing.getNodeInfo());
  128.     }

  129.     public Thing uninstallThing(NodeInfo nodeInfo) {
  130.         return things.remove(nodeInfo);
  131.     }
  132. }