Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.jivesoftware.smack.Chat;
- import org.jivesoftware.smack.ConnectionConfiguration;
- import org.jivesoftware.smack.ConnectionListener;
- import org.jivesoftware.smack.MessageListener;
- import org.jivesoftware.smack.PacketListener;
- import org.jivesoftware.smack.Roster;
- import org.jivesoftware.smack.RosterEntry;
- import org.jivesoftware.smack.SASLAuthentication;
- import org.jivesoftware.smack.XMPPConnection;
- import org.jivesoftware.smack.XMPPException;
- import org.jivesoftware.smack.filter.PacketTypeFilter;
- import org.jivesoftware.smack.packet.Message;
- import org.jivesoftware.smack.packet.Packet;
- import org.jivesoftware.smack.packet.PacketExtension;
- import org.jivesoftware.smack.packet.Presence;
- import org.jivesoftware.smack.provider.ProviderManager;
- import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
- import org.jivesoftware.smack.packet.IQ;
- import org.jivesoftware.smackx.FormField;
- import org.jivesoftware.smackx.pubsub.AccessModel;
- import org.jivesoftware.smackx.pubsub.ConfigureForm;
- import org.jivesoftware.smackx.pubsub.FormType;
- import org.jivesoftware.smackx.pubsub.LeafNode;
- import org.jivesoftware.smackx.pubsub.Node;
- import org.jivesoftware.smackx.pubsub.PubSubManager;
- import org.jivesoftware.smackx.pubsub.PublishModel;
- import org.jivesoftware.smackx.pubsub.SubscribeOptionFields;
- /*
- * WARNING: This is a sample of non-working code.
- * Questions and Problems:
- * 1) I am uncertain as to exactly how and where the client (this App) JID should be established.
- * 2) I am uncertain as to exactly when to use server JID or client JID, and if server JID is correct.
- * 3) I am uncertain as to the class structure of PubSub and how to generate the desired captured XML/XMPP content desired to talk to the Logitech Alert cameras.
- * 4) related to #3, createNode is likely being used entirely with incorrect understanding in this code sample - does the "urn:" go here at all?
- *
- * This is a class intended to talk to one camera, multiple instances would be used for more than one camera.
- * It has been hacked into a reverse-engineer console application with many prints and exception blocks.
- */
- public class Main implements MessageListener {
- public XMPPConnection connection;
- public String targetHostname = "jabber.org";
- public String targetUsername = "admin";
- public String whoknows_Hostname = "server@127.0.0.1/NvrCore";
- public String myClientJID = "";
- public String targetCameraJID = "";
- public void loginCamera(String userName, String password) throws XMPPException {
- // LearningQ: third ConnectionConfiguration parameter is proxy name? required?
- ConnectionConfiguration conconfig = new ConnectionConfiguration(targetHostname, 5222, targetHostname);
- // set authentication just to have the topic open for debate. Not sure if this line is required with defaults in smack.
- conconfig.setSASLAuthenticationEnabled(true);
- // Enable debugging so we can gather all the connection information we can.
- conconfig.setDebuggerEnabled(true);
- // Set presence to semi-automatic so we can pull trigger as we desire.
- conconfig.setSendPresence(false);
- connection = new XMPPConnection(conconfig);
- try
- {
- // Do the actual connection step.
- connection.connect();
- }
- catch (Exception e1)
- {
- System.out.println("PLACE0000 Exception on connect");
- e1.printStackTrace();
- }
- if (connection.isConnected())
- {
- // Let's do a short sleep just to let some XMPP flow before we invoke more activity.
- try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
- System.out.println("PLACE0001 after post-connect sleep. Camera login next.");
- try
- {
- // Note that smack seems to have some differences in username as to the need for the user@ part.
- // For the Logitech cameras, the user@ seems to work if included, exception of left off.
- connection.login(targetUsername + "@" + targetHostname, password, "XMPPCamera_smack");
- } catch (XMPPException e1) {
- System.out.println("PLACE0002 Exception on login");
- e1.printStackTrace();
- }
- catch (Exception e2)
- {
- System.out.println("PLACE0003 Exception on login");
- e2.printStackTrace();
- }
- System.out.println("PLACE0010 isAuthenticated: " + connection.isAuthenticated());
- if (connection.isAuthenticated())
- {
- // Let's do a short sleep just to let some XMPP flow before we invoke more activity.
- try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
- System.out.println("PLACE0011 after authenticate sleep, going to set presence to available");
- setMyPresenceAvailable();
- };
- }
- }
- public void setMyPresenceAvailable()
- {
- Presence myPresence = new Presence(Presence.Type.available);
- myPresence.setStatus("Available");
- connection.sendPacket(myPresence);
- }
- public void disconnect() {
- connection.disconnect();
- }
- private PubSubManager psManager1;
- private LeafNode node1;
- private ConnectionListener connectionListener;
- private static Main myInstance;
- /*
- * Try to subscribe to settings, as the captured XMPP is clearly using PubSub ;)
- * This needs work, entirely not working code.
- * Written by someone lost in trying to use smack classes to reverse-engineer captured XMPP conversations.
- */
- public void testPubSub1()
- {
- try {
- // LearningQ: This is one of the problem areas.
- // Should we be targeting the pubsub to name? 2nd parameter be whoknows_Hostname ?
- psManager1 = new PubSubManager(connection, targetCameraJID);
- } catch (Exception e1) {
- System.out.println("PLACE0300 Exception in testPubSub1");
- e1.printStackTrace();
- }
- try {
- // LearningQ: should we be using getNode and catching the exception as normal behavior - then createNode?
- node1 = (LeafNode) psManager1.createNode("urn:logitech-com:logitech-alert:remote-event:device:alert:settings");
- } catch (Exception e1) {
- System.out.println("PLACE0310 Exception in testPubSub1");
- e1.printStackTrace();
- }
- try {
- ConfigureForm form1 = new ConfigureForm(FormType.submit);
- // Logitech Camera, let's try to turn OFF the email alerts
- FormField field1 = new FormField("ALERT EMAILALERTSENABLED");
- field1.setType(FormField.TYPE_HIDDEN);
- field1.addValue("0");
- form1.addField(field1);
- node1.sendConfigurationForm(form1);
- } catch (Exception e1) {
- System.out.println("PLACE0320 Exception in testPubSub1");
- e1.printStackTrace();
- }
- }
- public void test1(String inTargetHostname) throws XMPPException, IOException {
- targetHostname = inTargetHostname;
- targetUsername = "admin";
- // turn on the enhanced debugger
- XMPPConnection.DEBUG_ENABLED = true;
- loginCamera(targetUsername, "Logitech");
- // Let's do a short sleep just to let some XMPP flow before we invoke more activity.
- try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
- System.out.println("PLACE0200 after post-loginCamera sleep.");
- if (connection.isConnected())
- {
- if (connection.isAuthenticated())
- {
- // This is probably entirely wrong and shows how lost in using smack that I am.
- targetCameraJID = targetUsername + "@" + targetHostname;
- System.out.println("PLACE0201 Calling testPubSub1...");
- testPubSub1();
- System.out.println("PLACE0202 after testPubSub1...");
- }
- }
- // Let's do a longer sleep to let some XMPP flow before we quit.
- try { Thread.sleep(8500); } catch (InterruptedException e) { e.printStackTrace(); }
- System.out.println("PLACE0290 after post-testPubSub1 attemp sleep.");
- System.out.println("PLACE9990 app going to end connection and exit...");
- disconnect();
- }
- public static void main(String args[]) throws XMPPException, IOException {
- // Put your test target Logitech Alert camera IP Address here
- String inTargetHostname = "192.168.4.116";
- myInstance = new Main();
- myInstance.test1(inTargetHostname);
- System.exit(0);
- }
- @Override
- public void processMessage(Chat inChat, Message inMessage) {
- // TODO Auto-generated method stub
- System.out.println("PLACE0400 incoming chat message " + inChat.toString() + " ::: " + inMessage.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement