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.ChatManager;
- import org.jivesoftware.smack.ChatManagerListener;
- 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.MessageTypeFilter;
- import org.jivesoftware.smack.filter.PacketFilter;
- 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.smack.util.StringUtils;
- import org.jivesoftware.smackx.Form;
- import org.jivesoftware.smackx.FormField;
- import org.jivesoftware.smackx.commands.AdHocCommand;
- import org.jivesoftware.smackx.commands.AdHocCommandManager;
- import org.jivesoftware.smackx.commands.RemoteCommand;
- 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;
- /*
- * REV0005
- * 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 targetCameraJID1 = "server@127.0.0.1/NvrCore";
- public String myClientJID = "";
- public String myClientTag = "";
- public String targetCameraJID = "";
- @Override
- public void processMessage(Chat inChat, Message inMessage) {
- // TODO Auto-generated method stub
- System.out.println("PLACE0400 incoming chat message " + inChat.toString() + " ::: " + inMessage.toString());
- }
- 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);
- // from iAlert gloox code
- // clientJidString = username + "@" + addr.toString() + "/Commander/" + uuid.mid(1,36);
- // resulting in
- // from='admin@192.168.4.116/Commander/c1657dec-4d31-43ea-b41b-9c10e7ca9966'
- //
- // myClientJID = targetUsername + "@" + targetHostname;
- // Hard-coded app UUID, ending digits should differ.
- myClientTag = "Commander/" + "feedcabb-4d31-43ea-b41b-9c10ffff0000";
- myClientJID = targetUsername + "@" + targetHostname;
- connection = new XMPPConnection(conconfig);
- establishListener();
- 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 if left off.
- // connection.login(myClientJID + myClientTag, password, "XMPPCamera_smack");
- connection.login(myClientJID, password, myClientTag);
- } 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() + " myClientJID: " + myClientJID);
- 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(); }
- // Setup the incoming message listener
- //establishListener();
- // 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 ChatManager chatmanager1;
- public void establishListener()
- {
- chatmanager1 = connection.getChatManager();
- chatmanager1.addChatListener(new ChatManagerListener()
- {
- public void chatCreated(final Chat chat, final boolean createdLocally)
- {
- System.out.println("PLACE700 chatCreated called");
- chat.addMessageListener(new MessageListener()
- {
- public void processMessage(Chat chat, Message message)
- {
- System.out.println("PLACE0410 processMessage");
- System.out.println("PLACE0411 Received message: " + (message != null ? message.getBody() : "NULL"));
- }
- });
- }
- });
- if (connection != null) {
- // Add a packet listener to get messages sent to us
- System.out.println("PLACE0499 creating packetListener");
- PacketFilter filter = new MessageTypeFilter(Message.Type.normal);
- connection.addPacketListener(new PacketListener() {
- public void processPacket(Packet packet) {
- Message message = (Message) packet;
- if (message.getBody() != null) {
- String fromName = StringUtils.parseBareAddress(message.getFrom());
- System.out.println("PLACE0490 Got text [" + message.getBody() + "] from [" + fromName + "]");
- }
- else
- {
- System.out.println("PLACE0491 processPacket got null");
- }
- }
- }, filter);
- }
- }
- 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 testPubSub1old1()
- {
- try {
- // LearningQ: This is one of the problem areas.
- // Should we be targeting the pubsub to name? 2nd parameter be targetCameraJID1 ?
- 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 testPubSub1()
- {
- try {
- // LearningQ: This is one of the problem areas.
- // Should we be targeting the pubsub to name? 2nd parameter be targetCameraJID1 ?
- psManager1 = new PubSubManager(connection, "server@127.0.0.1/NvrCore");
- } catch (Exception e1) {
- System.out.println("PLACE0800 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("PLACE0805 Exception in testPubSub1");
- e1.printStackTrace();
- }
- Form form1 = new Form(Form.TYPE_SUBMIT);
- 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");
- // Form form1 = new Form(Form.TYPE_SUBMIT);
- // form1.setTitle("Get NVR Basic Request");
- FormField field1 = new FormField("ALERT EMAILALERTSENABLED");
- field1.setType(FormField.TYPE_HIDDEN);
- field1.addValue("0");
- form1.addField(field1);
- } catch (Exception e1) {
- System.out.println("PLACE0810 Exception in testPubSub1");
- e1.printStackTrace();
- }
- try {
- node1.sendConfigurationForm(form1);
- } catch (Exception e1) {
- System.out.println("PLACE0820 Exception in testPubSub1");
- e1.printStackTrace();
- }
- System.out.println("Pause here");
- }
- public void testPubSub2()
- {
- try {
- // LearningQ: This is one of the problem areas.
- // Should we be targeting the pubsub to name? 2nd parameter be targetCameraJID1 ?
- psManager1 = new PubSubManager(connection, "server@127.0.0.1/NvrCore");
- } catch (Exception e1) {
- System.out.println("PLACE0800 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:media:recording:ended");
- } catch (Exception e1) {
- System.out.println("PLACE0805 Exception in testPubSub1");
- e1.printStackTrace();
- }
- try {
- //
- } catch (Exception e1) {
- System.out.println("PLACE0810 Exception in testPubSub1");
- e1.printStackTrace();
- }
- try {
- //node1.sendConfigurationForm(form1);
- } catch (Exception e1) {
- System.out.println("PLACE0820 Exception in testPubSub1");
- e1.printStackTrace();
- }
- System.out.println("Pause here");
- }
- public void commandNvrBasicGet()
- {
- // gloox XMPP is generating this valid AdHoc Command
- // "<iq to='server@127.0.0.1/NvrCore' id='uid:51ddd8e4:1191831f' type='set' from='admin@192.168.4.116/Commander/c1657dec-4d31-43ea-b41b-9c10e7ca9966' xmlns='jabber:client'><command xmlns='http://jabber.org/protocol/commands' node='urn:logitech-com:logitech-alert:nvr:basic:get' action='execute'><x xmlns='jabber:x:data' type='submit'><title>Get NVR Basic Request</title><field type='hidden' var='FORM_TYPE'><value>urn:logitech-com:logitech-alert:nvr:basic:get</value></field></x></command></iq>"
- // we are generating this commmand:
- // <iq id="35o9V-4" to="urn:logitech-com:logitech-alert:nvr:basic:get" type="set"><command xmlns="http://jabber.org/protocol/commands" node="" action="execute"><x xmlns="jabber:x:data" type="submit"><field var="FORM_TYPE" type="hidden"><value>urn:logitech-com:logitech-alert:nvr:basic:get</value></field></x></command></iq>
- // 2nd attempt:
- // <iq id="cBR82-4" to="server@127.0.0.1/NvrCore" type="set"><command xmlns="http://jabber.org/protocol/commands" node="urn:logitech-com:logitech-alert:nvr:basic:get" action="execute"><x xmlns="jabber:x:data" type="submit"><field var="FORM_TYPE" type="hidden"><value>urn:logitech-com:logitech-alert:nvr:basic:get</value></field></x></command></iq>
- Form form1 = new Form(Form.TYPE_SUBMIT);
- 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");
- // Form form1 = new Form(Form.TYPE_SUBMIT);
- form1.setTitle("Get NVR Basic Request");
- FormField field1 = new FormField("FORM_TYPE");
- field1.setType(FormField.TYPE_HIDDEN);
- field1.addValue("urn:logitech-com:logitech-alert:nvr:basic:get");
- form1.addField(field1);
- } catch (Exception e1) {
- System.out.println("PLACE0510 Exception in commandNvrBasicGet");
- e1.printStackTrace();
- }
- try {
- AdHocCommandManager ahcmanager = AdHocCommandManager.getAddHocCommandsManager(connection);
- // org.jivesoftware.smackx.commands.AdHocCommand ahcommand = new AdHocCommand();
- RemoteCommand ahcommand = ahcmanager.getRemoteCommand("server@127.0.0.1/NvrCore", "urn:logitech-com:logitech-alert:nvr:basic:get");
- // ahcommand.setName(myClientJID);
- ahcommand.execute(form1);
- // node1.sendConfigurationForm(form1);
- } catch (Exception e1) {
- System.out.println("PLACE0520 Exception in commandNvrBasicGet");
- e1.printStackTrace();
- }
- System.out.println("Pause here");
- }
- public void commandNvrSetNotification1()
- {
- Form form1 = new Form(Form.TYPE_SUBMIT);
- 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");
- // Form form1 = new Form(Form.TYPE_SUBMIT);
- // form1.setTitle("Get NVR Basic Request");
- FormField field1 = new FormField("ALERT EMAILALERTSENABLED");
- field1.setType(FormField.TYPE_HIDDEN);
- field1.addValue("0");
- form1.addField(field1);
- } catch (Exception e1) {
- System.out.println("PLACE0910 Exception in commandNvrBasicGet");
- e1.printStackTrace();
- }
- try {
- AdHocCommandManager ahcmanager = AdHocCommandManager.getAddHocCommandsManager(connection);
- // org.jivesoftware.smackx.commands.AdHocCommand ahcommand = new AdHocCommand();
- RemoteCommand ahcommand = ahcmanager.getRemoteCommand("server@127.0.0.1/NvrCore", "urn:logitech-com:logitech-alert:remote-event:device:alert:settings");
- // ahcommand.setName(myClientJID);
- ahcommand.execute(form1);
- // node1.sendConfigurationForm(form1);
- } catch (Exception e1) {
- System.out.println("PLACE0920 Exception in commandNvrBasicGet");
- e1.printStackTrace();
- }
- System.out.println("Pause here");
- }
- /*
- * example output from gloox iAlert
- * "<iq to='server@127.0.0.1/NvrCore' id='uid:51ddd8e4:400aacc0' type='get' from='admin@192.168.4.116/Commander/c1657dec-4d31-43ea-b41b-9c10e7ca9966' xmlns='jabber:client'>
- * <Query xmlns='urn:logitech-com:logitech-alert:device:media:recording:search'><set xmlns='http://jabber.org/protocol/rsm'><index>0</index><max>1000000</max></set><Fields><Field name='Id'/><Field name='StartTime'/><Field name='Duration'/></Fields><Criteria><Field name='DeviceId' type='list-multi'><Value>00-12-AB-1C-2A-EC</Value></Field><Field name='StartTime' type='dateTime'><Sort allowed='1' order='1' direction='ASC'/><MinInclusive/><MaxInclusive/></Field></Criteria></Query>
- * </iq>"
- * try1:
- * <iq id="T2nCR-5" to="server@127.0.0.1/NvrCore" from="admin@192.168.4.116/Commander/feedcabb-4d31-43ea-b41b-9c10ffff0000" type="set"><Query xmlns='urn:logitech-com:logitech-alert:device:media:recording:search'><set xmlns='http://jabber.org/protocol/rsm'><index>0</index><max>1000000</max></set><Fields><Field name='Id'/><Field name='StartTime'/><Field name='Duration'/></Fields><Criteria><Field name='DeviceId' type='list-multi'><Value>00-12-AB-1C-2A-EC</Value></Field><Field name='StartTime' type='dateTime'><Sort allowed='1' order='1' direction='ASC'/><MinInclusive/><MaxInclusive/></Field></Criteria></Query></iq>
- */
- public void getRecordingList()
- {
- IQ customiq = new IQ()
- {
- public String getChildElementXML()
- {
- String node1 = "urn:logitech-com:logitech-alert:device:media:recording:search";
- return "<Query xmlns='" + node1 + "'>"
- + "<set xmlns='http://jabber.org/protocol/rsm'><index>0</index><max>1000000</max></set>"
- + "<Fields>"
- + "<Field name='Id'/><Field name='StartTime'/><Field name='Duration'/></Fields><Criteria><Field name='DeviceId' type='list-multi'><Value>00-12-AB-1C-2A-EC</Value></Field>"
- + "<Field name='StartTime' type='dateTime'><Sort allowed='1' order='1' direction='ASC'/><MinInclusive/><MaxInclusive/></Field>"
- + "</Criteria>"
- + "</Query>";
- }
- };
- customiq.setType(IQ.Type.GET);
- customiq.setTo(targetCameraJID1);
- customiq.setFrom(connection.getUser());
- customiq.setDefaultXmlns("urn:logitech-com:logitech-alert:device:media:recording:search");
- connection.sendPacket(customiq);
- }
- 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...");
- commandNvrBasicGet();
- // 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("PLACE0202 after post-commandNvrBasicGet sleep.");
- //commandNvrSetNotification1();
- //testPubSub1();
- //testPubSub2();
- getRecordingList();
- System.out.println("PLACE0203 after testPubSub1...");
- }
- }
- // Let's do a longer sleep to let some XMPP flow before we quit.
- try { Thread.sleep(60500); } catch (InterruptedException e) { e.printStackTrace(); }
- System.out.println("PLACE0290 after post-testPubSub1 attempt 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement