ruebox / openhab2-addons

Add-ons for openHAB 2.x
Eclipse Public License 1.0
16 stars 6 forks source link

Information sharing #13

Closed lassem closed 5 years ago

lassem commented 6 years ago

I wanted to share my rudimentary work from last year. Maybe there is something useful in here. For one I see that we're connecting using two different methods. You are using BOSH like the browser implementation, while I use a traditional tcp connection.

Feel free to close this issue whenever you want.

package no.lamatech;

import org.springframework.stereotype.Service;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.XmppException;
import rocks.xmpp.core.session.ConnectionConfiguration;
import rocks.xmpp.core.session.TcpConnectionConfiguration;
import rocks.xmpp.core.session.XmppClient;
import rocks.xmpp.core.session.XmppSessionConfiguration;
import rocks.xmpp.core.stanza.model.IQ;
import rocks.xmpp.core.stanza.model.Presence;
import rocks.xmpp.core.stream.model.StreamElement;
import rocks.xmpp.extensions.caps.EntityCapabilitiesManager;
import rocks.xmpp.extensions.rpc.model.Rpc;
import rocks.xmpp.extensions.rpc.model.Value;
import rocks.xmpp.im.roster.RosterManager;
import rocks.xmpp.im.subscription.PresenceManager;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Locale;
import java.util.Random;
import java.util.logging.Logger;

@Service
public class XmppService {
    private static final Logger log = Logger.getLogger(XmppService.class.getSimpleName());
    private static final String BUSCH_JAEGER_DE = "busch-jaeger.de";
    private static final Jid MRHA_JID = Jid.of("mrha@" + BUSCH_JAEGER_DE);
    private static final Jid INSTALLER_JID = Jid.of("installer@" + BUSCH_JAEGER_DE);
    private static final Jid MRHA_RPC_JID = Jid.of("mrha@" + BUSCH_JAEGER_DE + "/rpc");
    private static final String DIGEST_MD5 = "DIGEST-MD5";
    private static final String GET_CONFIG_ID = new BigInteger(Long.SIZE, new Random()).toString();
    private static final Locale LOCALE = Locale.forLanguageTag("nb-NO");
    private static final String RESOURCE_ID = String.format("%x", new Random(System.currentTimeMillis()).nextInt(Integer.MAX_VALUE));

    public XmppService() throws XmppException, NoSuchAlgorithmException, InterruptedException, KeyStoreException, IOException, CertificateException, KeyManagementException {

        ConnectionConfiguration connectionConfiguration = TcpConnectionConfiguration.builder()
                .hostname("192.168.10.90")
                .secure(false)
                .port(5222)
                .build();

        XmppSessionConfiguration sessionConfiguration = XmppSessionConfiguration.builder()
                .language(LOCALE)
                .debugger(XmppLoggerDebugger.class)
                .authenticationMechanisms(DIGEST_MD5)
                .initialPresence(null)
                .build();

        XmppClient client = XmppClient.create(BUSCH_JAEGER_DE, sessionConfiguration, connectionConfiguration);
//        client.enableFeature(StreamManagement.NAMESPACE);
//        client.enableFeature(StreamFeatures.NAMESPACE);

        RosterManager rosterManager = client.getManager(RosterManager.class);
        PresenceManager presenceManager = client.getManager(PresenceManager.class);
//        ServiceDiscoveryManager serviceDiscoveryManager = client.getManager(ServiceDiscoveryManager.class);
//        StreamFeaturesManager streamFeaturesManager = client.getManager(StreamFeaturesManager.class);
        EntityCapabilitiesManager entityCapabilitiesManager = client.getManager(EntityCapabilitiesManager.class);

        rosterManager.setRetrieveRosterOnLogin(false);

        presenceManager.setEnabled(false);

//        entityCapabilitiesManager.isSupported()
        entityCapabilitiesManager.setNode("http://gonicus.de/caps");

        client.addInboundIQListener(iqEvent -> {
            if (iqEvent.getIQ().getId().equals(GET_CONFIG_ID) && iqEvent.getIQ().isResponse()) {
                Rpc response = iqEvent.getIQ().getExtension(Rpc.class);
                String s = response.getMethodResponse().getResponse().getAsString();
//                log.info(s);
            } else {
//                log.info(iqEvent.toString());
            }
        });

//        client.addInboundMessageListener(messageEvent -> log.info("MSG: " + messageEvent.getMessage().toString()));
//        client.addInboundPresenceListener(presenceEvent -> log.info("PRS: " + presenceEvent.getPresence().toString()));
        client.connect(Jid.of(BUSCH_JAEGER_DE));
        client.login("installer", "12345", RESOURCE_ID);

//        entityCapabilitiesManager.discoverCapabilities(MRHA_RPC_JID);

//        presenceManager.requestSubscription(MRHA_RPC_JID, null);

        // <presence from="installer@busch-jaeger.de" to="mrha@busch-jaeger.de/rpc" type="subscribe"/>
        // <presence><c xmlns="http://jabber.org/protocol/caps" ver="1.0" node="http://gonicus.de/caps"/></presence>

        Presence presence = new Presence(MRHA_RPC_JID, Presence.Type.SUBSCRIBE, null, null);
        presence.setFrom(INSTALLER_JID);
        client.send(presence);

        presence = new Presence();
//        presence.addExtension(new EntityCapabilities("http://genicus.de/caps", "", ""));
        client.send(presence);

        IQ iq = new IQ(MRHA_RPC_JID, IQ.Type.SET, Rpc.ofMethodCall("RemoteInterface.getAll", Value.of(LOCALE.toLanguageTag()), Value.of(4), Value.of(0), Value.of(0)), GET_CONFIG_ID);
//        IQ iq = new IQ(MRHA_RPC_JID, IQ.Type.SET, Rpc.ofMethodCall("RemoteInterface.getAll", Value.of(LOCALE.toLanguageTag()), Value.of(1), Value.of(0), Value.of(-1)), GET_CONFIG_ID);
        client.send(iq);

        IQ on = new IQ(MRHA_RPC_JID, IQ.Type.SET, Rpc.ofMethodCall("RemoteInterface.setDatapoint", Value.of("ABB700C6DF1E/ch0006/idp0000"), Value.of(true)));
        IQ off = new IQ(MRHA_RPC_JID, IQ.Type.SET, Rpc.ofMethodCall("RemoteInterface.setDatapoint", Value.of("ABB700C6DF1E/ch0006/idp0000"), Value.of(false)));

//        client.send(on);

//        Thread.sleep(1000);

//        client.send(off);

//        IQ temp = new IQ(MRHA_RPC_JID, IQ.Type.GET, Rpc.ofMethodCall("RemoteInterface.getDatapoint", Value.of("ABB700C6DF1E/ch0006/idp0000"), Value.of("0")));
//        IQ temp = new IQ(MRHA_RPC_JID, IQ.Type.SET, Rpc.ofMethodCall("RemoteInterface.getDatapointValue", Value.of("ABB700C92AB0/ch0000/odp0010")));
//
//        client.sendIQ(temp)
//                .onAcknowledge(iq1 -> {
//                    int stop = 1;
//
//                });

    }

    @XmlRootElement(name = "c")
    public static class Test implements StreamElement {
        // <presence xmlns="jabber:client"><c xmlns="http://jabber.org/protocol/caps" ver="1.0" node="http://gonicus.de/caps"/></presence>
        public static final String NAMESPACE = "http://jabber.org/protocol/caps";

        @XmlAttribute
        private final String ver = "1.0";

        @XmlAttribute
        private final String node = "http://gonicus.de/caps";

    }

}
ruebox commented 6 years ago

Thanks for sharing.

ruebox commented 6 years ago

@lassem There is still issue #16 I am not sure if it is related to my XMPP implementation. If you have time to support me, it would be great if you can check what differentiates your and mine implementation on connecting to SysAP.

Does #16 also occur with your implementation? Perhaps some proper parameterization is missing in my code.

Thanks a lot. Best