View difference between Paste ID: 7bnYDqGz and JwM9rUjN
SHOW: | | - or go back to the newest paste.
1
import java.util.*;
2
import java.io.*;
3
 
4
import java.util.logging.Level;
5
import java.util.logging.Logger;
6
import org.jivesoftware.smack.Chat;
7
import org.jivesoftware.smack.ChatManager;
8
import org.jivesoftware.smack.ChatManagerListener;
9
import org.jivesoftware.smack.ConnectionConfiguration;
10
import org.jivesoftware.smack.ConnectionListener;
11
import org.jivesoftware.smack.MessageListener;
12
import org.jivesoftware.smack.PacketListener;
13
import org.jivesoftware.smack.Roster;
14
import org.jivesoftware.smack.RosterEntry;
15
import org.jivesoftware.smack.SASLAuthentication;
16
import org.jivesoftware.smack.XMPPConnection;
17
import org.jivesoftware.smack.XMPPException;
18
import org.jivesoftware.smack.filter.MessageTypeFilter;
19
import org.jivesoftware.smack.filter.PacketFilter;
20
import org.jivesoftware.smack.filter.PacketTypeFilter;
21
import org.jivesoftware.smack.packet.Message;
22
import org.jivesoftware.smack.packet.Packet;
23
import org.jivesoftware.smack.packet.PacketExtension;
24
import org.jivesoftware.smack.packet.Presence;
25
import org.jivesoftware.smack.provider.ProviderManager;
26
import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
27
import org.jivesoftware.smack.packet.IQ;
28
import org.jivesoftware.smack.util.StringUtils;
29
import org.jivesoftware.smackx.Form;
30
import org.jivesoftware.smackx.FormField;
31
import org.jivesoftware.smackx.commands.AdHocCommand;
32
import org.jivesoftware.smackx.commands.AdHocCommandManager;
33
import org.jivesoftware.smackx.commands.RemoteCommand;
34
import org.jivesoftware.smackx.pubsub.AccessModel;
35
import org.jivesoftware.smackx.pubsub.ConfigureForm;
36
import org.jivesoftware.smackx.pubsub.FormType;
37
import org.jivesoftware.smackx.pubsub.LeafNode;
38
import org.jivesoftware.smackx.pubsub.Node;
39
import org.jivesoftware.smackx.pubsub.PubSubManager;
40
import org.jivesoftware.smackx.pubsub.PublishModel;
41
import org.jivesoftware.smackx.pubsub.SubscribeOptionFields;
42
43
44
/*
45
 * REV0005
46
 * WARNING: This is a sample of non-working code.
47
 * Questions and Problems:
48
 *     1) I am uncertain as to exactly how and where the client (this App) JID should be established.
49
 *     2) I am uncertain as to exactly when to use server JID or client JID, and if server JID is correct.
50
 *     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.
51-
    public String whoknows_Hostname = "[email protected]/NvrCore";
51+
52
 *     
53
 *     This is a class intended to talk to one camera, multiple instances would be used for more than one camera.
54
 *       It has been hacked into a reverse-engineer console application with many prints and exception blocks.
55
 */
56
public class Main implements MessageListener {
57
 
58
    public XMPPConnection connection;
59
    public String targetHostname = "jabber.org";
60
    public String targetUsername = "admin";
61
    public String targetCameraJID1 = "[email protected]/NvrCore";
62
    public String myClientJID = "";
63
    public String myClientTag = "";
64-
         conconfig.setSendPresence(false);
64+
65
    
66
    
67
    @Override
68
    public void processMessage(Chat inChat, Message inMessage) {
69
        // TODO Auto-generated method stub
70
        System.out.println("PLACE0400 incoming chat message " + inChat.toString() + " ::: " + inMessage.toString());
71
    }
72
     
73
    public void loginCamera(String userName, String password) throws XMPPException {
74
        // LearningQ: third ConnectionConfiguration parameter is proxy name? required?
75
        ConnectionConfiguration conconfig = new ConnectionConfiguration(targetHostname, 5222, targetHostname);
76
        // set authentication just to have the topic open for debate. Not sure if this line is required with defaults in smack.
77
        conconfig.setSASLAuthenticationEnabled(true);
78
        // Enable debugging so we can gather all the connection information we can.
79
        conconfig.setDebuggerEnabled(true);
80
        // Set presence to semi-automatic so we can pull trigger as we desire.
81
        conconfig.setSendPresence(false);
82
        
83
        // from iAlert gloox code
84
        //   clientJidString = username + "@" + addr.toString() + "/Commander/" + uuid.mid(1,36);
85
        // resulting in
86
        //   from='[email protected]/Commander/c1657dec-4d31-43ea-b41b-9c10e7ca9966'
87
        //
88-
                // For the Logitech cameras, the user@ seems to work if included, exception of left off.
88+
        // myClientJID = targetUsername + "@" + targetHostname;
89-
                connection.login(targetUsername + "@" + targetHostname, password, "XMPPCamera_smack");
89+
        // Hard-coded app UUID, ending digits should differ.
90
        myClientTag = "Commander/" + "feedcabb-4d31-43ea-b41b-9c10ffff0000";
91
        myClientJID = targetUsername + "@" + targetHostname;
92
               
93
        connection = new XMPPConnection(conconfig);
94
        establishListener();
95
 
96
        try
97
        {
98
            // Do the actual connection step.
99
            connection.connect();
100-
            System.out.println("PLACE0010 isAuthenticated: " + connection.isAuthenticated());
100+
101
        catch (Exception e1)
102
        {
103
            System.out.println("PLACE0000 Exception on connect");
104
            e1.printStackTrace();
105
        }
106
        
107
        if (connection.isConnected())
108
        {
109
            // Let's do a short sleep just to let some XMPP flow before we invoke more activity.
110
            try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
111
            System.out.println("PLACE0001 after post-connect sleep. Camera login next.");
112
113
            try
114
            {
115
                // Note that smack seems to have some differences in username as to the need for the user@ part.
116
                // For the Logitech cameras, the user@ seems to work if included, exception if left off.
117
                // connection.login(myClientJID + myClientTag, password, "XMPPCamera_smack");
118
                connection.login(myClientJID, password, myClientTag);
119
            } catch (XMPPException e1) {
120
                System.out.println("PLACE0002 Exception on login");
121
                e1.printStackTrace();
122
            }
123
            catch (Exception e2)
124
            {
125
                System.out.println("PLACE0003 Exception on login");
126
                e2.printStackTrace();
127
            }
128
        
129
            System.out.println("PLACE0010 isAuthenticated: " + connection.isAuthenticated() + " myClientJID: " + myClientJID);
130
        
131
            if (connection.isAuthenticated())
132
            {
133
                // Let's do a short sleep just to let some XMPP flow before we invoke more activity.
134
                try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
135
                // Setup the incoming message listener
136
                //establishListener();
137
138-
            // Should we be targeting the pubsub to name? 2nd parameter be whoknows_Hostname ?
138+
139
                try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
140
                System.out.println("PLACE0011 after authenticate sleep, going to set presence to available");
141
                setMyPresenceAvailable();
142
            };
143
        }
144
    }
145
    
146
    public ChatManager chatmanager1;
147
    
148
    public void establishListener()
149
    {
150
        chatmanager1 = connection.getChatManager();
151
          
152
        chatmanager1.addChatListener(new ChatManagerListener()
153
        {
154
            public void chatCreated(final Chat chat, final boolean createdLocally)
155
            {
156
                System.out.println("PLACE700 chatCreated called");
157
                chat.addMessageListener(new MessageListener()
158
                {
159
                    public void processMessage(Chat chat, Message message)
160
                    {
161
                        System.out.println("PLACE0410 processMessage");
162
                        System.out.println("PLACE0411 Received message: " + (message != null ? message.getBody() : "NULL"));
163
                    }
164
                });
165
            }
166
        });
167
        
168
                   if (connection != null) {
169
                    // Add a packet listener to get messages sent to us
170
                       System.out.println("PLACE0499 creating packetListener");
171
                    PacketFilter filter = new MessageTypeFilter(Message.Type.normal);
172
                    connection.addPacketListener(new PacketListener() {
173
                        public void processPacket(Packet packet) {
174
                            Message message = (Message) packet;
175
                            if (message.getBody() != null) {
176
                                String fromName = StringUtils.parseBareAddress(message.getFrom());
177
                                System.out.println("PLACE0490 Got text [" + message.getBody() + "] from [" + fromName + "]");
178
                            }
179
                            else
180
                            {
181
                                System.out.println("PLACE0491 processPacket got null");
182
                            }
183
                        }
184
                    }, filter);
185
                }
186
187
    }
188
    
189-
                testPubSub1();
189+
190-
                System.out.println("PLACE0202 after testPubSub1...");
190+
191
        Presence myPresence = new Presence(Presence.Type.available);
192
        myPresence.setStatus("Available");
193
        connection.sendPacket(myPresence);
194
    }
195-
        try { Thread.sleep(8500); } catch (InterruptedException e) { e.printStackTrace(); }
195+
196-
        System.out.println("PLACE0290 after post-testPubSub1 attemp sleep.");
196+
197
        connection.disconnect();
198
    }
199
 
200
    
201
    private PubSubManager psManager1;
202
    private LeafNode node1;
203
    private ConnectionListener connectionListener;
204
    private static Main myInstance;
205
206
    /*
207
     * Try to subscribe to settings, as the captured XMPP is clearly using PubSub ;)
208
     * This needs work, entirely not working code.
209
     * Written by someone lost in trying to use smack classes to reverse-engineer captured XMPP conversations.
210
     */
211
    public void testPubSub1old1()
212
    {
213
        try {
214
            // LearningQ: This is one of the problem areas.
215
            // Should we be targeting the pubsub to name? 2nd parameter be targetCameraJID1 ?
216
            psManager1 = new PubSubManager(connection, targetCameraJID);
217
        } catch (Exception e1) {
218
            System.out.println("PLACE0300 Exception in testPubSub1");
219
            e1.printStackTrace();
220
        }
221
        
222
        try {
223
            // LearningQ: should we be using getNode and catching the exception as normal behavior - then createNode?
224
            node1 = (LeafNode) psManager1.createNode("urn:logitech-com:logitech-alert:remote-event:device:alert:settings");
225
        } catch (Exception e1) {
226
            System.out.println("PLACE0310 Exception in testPubSub1");
227
            e1.printStackTrace();
228
        }
229
        
230
        try {
231
            ConfigureForm form1 = new ConfigureForm(FormType.submit);
232
            // Logitech Camera, let's try to turn OFF the email alerts
233
            FormField field1 = new FormField("ALERT EMAILALERTSENABLED");
234
            field1.setType(FormField.TYPE_HIDDEN);
235
            field1.addValue("0");
236
            form1.addField(field1);
237
            
238
            node1.sendConfigurationForm(form1);
239
        } catch (Exception e1) {
240
            System.out.println("PLACE0320 Exception in testPubSub1");
241
            e1.printStackTrace();
242
        }
243
    }
244
    
245
    public void testPubSub1()
246
    {        
247
        try {
248
            // LearningQ: This is one of the problem areas.
249
            // Should we be targeting the pubsub to name? 2nd parameter be targetCameraJID1 ?
250
            psManager1 = new PubSubManager(connection, "[email protected]/NvrCore");
251
        } catch (Exception e1) {
252
            System.out.println("PLACE0800 Exception in testPubSub1");
253
            e1.printStackTrace();
254
        }
255
256
        try {
257
            // LearningQ: should we be using getNode and catching the exception as normal behavior - then createNode?
258
            node1 = (LeafNode) psManager1.createNode("urn:logitech-com:logitech-alert:remote-event:device:alert:settings");
259
        } catch (Exception e1) {
260
            System.out.println("PLACE0805 Exception in testPubSub1");
261
            e1.printStackTrace();
262
        }
263
264
        Form form1 = new Form(Form.TYPE_SUBMIT);
265
        try {
266
            // LearningQ: should we be using getNode and catching the exception as normal behavior - then createNode?
267
            //node1 = (LeafNode) psManager1.createNode("urn:logitech-com:logitech-alert:remote-event:device:alert:settings");
268
            // Form form1 = new Form(Form.TYPE_SUBMIT);
269
            // form1.setTitle("Get NVR Basic Request");
270
            FormField field1 = new FormField("ALERT EMAILALERTSENABLED");
271
            field1.setType(FormField.TYPE_HIDDEN);
272
            field1.addValue("0");
273
            form1.addField(field1);
274
        } catch (Exception e1) {
275
            System.out.println("PLACE0810 Exception in testPubSub1");
276
            e1.printStackTrace();
277
        }
278
        
279
        try {
280
            node1.sendConfigurationForm(form1);
281
        } catch (Exception e1) {
282
            System.out.println("PLACE0820 Exception in testPubSub1");
283
            e1.printStackTrace();
284
        }
285
        System.out.println("Pause here");
286
    }
287
 
288
    public void testPubSub2()
289
    {        
290
        try {
291
            // LearningQ: This is one of the problem areas.
292
            // Should we be targeting the pubsub to name? 2nd parameter be targetCameraJID1 ?
293
            psManager1 = new PubSubManager(connection, "[email protected]/NvrCore");
294
        } catch (Exception e1) {
295
            System.out.println("PLACE0800 Exception in testPubSub1");
296
            e1.printStackTrace();
297
        }
298
299
        try {
300
            // LearningQ: should we be using getNode and catching the exception as normal behavior - then createNode?
301
            node1 = (LeafNode) psManager1.createNode("urn:logitech-com:logitech-alert:remote-event:device:media:recording:ended");
302
        } catch (Exception e1) {
303
            System.out.println("PLACE0805 Exception in testPubSub1");
304
            e1.printStackTrace();
305
        }
306
307
        try {
308
            //
309
         } catch (Exception e1) {
310
            System.out.println("PLACE0810 Exception in testPubSub1");
311
            e1.printStackTrace();
312
        }
313
        
314
        try {
315
            //node1.sendConfigurationForm(form1);
316
        } catch (Exception e1) {
317
            System.out.println("PLACE0820 Exception in testPubSub1");
318
            e1.printStackTrace();
319
        }
320
        System.out.println("Pause here");
321
    }
322
    
323
    public void commandNvrBasicGet()
324
    {        
325
        // gloox XMPP is generating this valid AdHoc Command
326
        //   "<iq to='[email protected]/NvrCore' id='uid:51ddd8e4:1191831f' type='set' from='[email protected]/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>" 
327
        // we are generating this commmand:
328
        //    <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>
329
        // 2nd attempt:
330
        //    <iq id="cBR82-4" to="[email protected]/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>
331
332
333
        Form form1 = new Form(Form.TYPE_SUBMIT);
334
        try {
335
            // LearningQ: should we be using getNode and catching the exception as normal behavior - then createNode?
336
            //node1 = (LeafNode) psManager1.createNode("urn:logitech-com:logitech-alert:remote-event:device:alert:settings");
337
            // Form form1 = new Form(Form.TYPE_SUBMIT);
338
            form1.setTitle("Get NVR Basic Request");
339
            FormField field1 = new FormField("FORM_TYPE");
340
            field1.setType(FormField.TYPE_HIDDEN);
341
            field1.addValue("urn:logitech-com:logitech-alert:nvr:basic:get");
342
            form1.addField(field1);
343
        } catch (Exception e1) {
344
            System.out.println("PLACE0510 Exception in commandNvrBasicGet");
345
            e1.printStackTrace();
346
        }
347
        
348
        try {
349
            AdHocCommandManager ahcmanager = AdHocCommandManager.getAddHocCommandsManager(connection);
350
            // org.jivesoftware.smackx.commands.AdHocCommand ahcommand = new AdHocCommand();
351
            RemoteCommand ahcommand = ahcmanager.getRemoteCommand("[email protected]/NvrCore", "urn:logitech-com:logitech-alert:nvr:basic:get");
352
            // ahcommand.setName(myClientJID);
353
            
354
            ahcommand.execute(form1);
355
            // node1.sendConfigurationForm(form1);
356
        } catch (Exception e1) {
357
            System.out.println("PLACE0520 Exception in commandNvrBasicGet");
358
            e1.printStackTrace();
359
        }
360
        System.out.println("Pause here");
361
    }
362
 
363
    public void commandNvrSetNotification1()
364
    {        
365
        Form form1 = new Form(Form.TYPE_SUBMIT);
366
        try {
367
            // LearningQ: should we be using getNode and catching the exception as normal behavior - then createNode?
368
            //node1 = (LeafNode) psManager1.createNode("urn:logitech-com:logitech-alert:remote-event:device:alert:settings");
369
            // Form form1 = new Form(Form.TYPE_SUBMIT);
370
            // form1.setTitle("Get NVR Basic Request");
371
            FormField field1 = new FormField("ALERT EMAILALERTSENABLED");
372
            field1.setType(FormField.TYPE_HIDDEN);
373
            field1.addValue("0");
374
            form1.addField(field1);
375
        } catch (Exception e1) {
376
            System.out.println("PLACE0910 Exception in commandNvrBasicGet");
377
            e1.printStackTrace();
378
        }
379
        
380
        try {
381
            AdHocCommandManager ahcmanager = AdHocCommandManager.getAddHocCommandsManager(connection);
382
            // org.jivesoftware.smackx.commands.AdHocCommand ahcommand = new AdHocCommand();
383
            RemoteCommand ahcommand = ahcmanager.getRemoteCommand("[email protected]/NvrCore", "urn:logitech-com:logitech-alert:remote-event:device:alert:settings");
384
            // ahcommand.setName(myClientJID);
385
            
386
            ahcommand.execute(form1);
387
            // node1.sendConfigurationForm(form1);
388
        } catch (Exception e1) {
389
            System.out.println("PLACE0920 Exception in commandNvrBasicGet");
390
            e1.printStackTrace();
391
        }
392
        System.out.println("Pause here");
393
    }
394
    
395
    /*
396
     * example output from gloox iAlert
397
     *   "<iq to='[email protected]/NvrCore' id='uid:51ddd8e4:400aacc0' type='get' from='[email protected]/Commander/c1657dec-4d31-43ea-b41b-9c10e7ca9966' xmlns='jabber:client'>
398
     *   <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>
399
     *   </iq>" 
400
     * try1:
401
     *    <iq id="T2nCR-5" to="[email protected]/NvrCore" from="[email protected]/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>
402
     */
403
    public void getRecordingList()
404
    {
405
        IQ customiq = new IQ()
406
        {
407
            public String getChildElementXML()
408
            {
409
                String node1 = "urn:logitech-com:logitech-alert:device:media:recording:search";
410
                return "<Query xmlns='" + node1 + "'>"
411
                    + "<set xmlns='http://jabber.org/protocol/rsm'><index>0</index><max>1000000</max></set>"
412
                    + "<Fields>"
413
                    + "<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>"
414
                    + "<Field name='StartTime' type='dateTime'><Sort allowed='1' order='1' direction='ASC'/><MinInclusive/><MaxInclusive/></Field>"
415
                    + "</Criteria>"
416
                    + "</Query>";
417
            }
418
        };
419
        customiq.setType(IQ.Type.GET);
420
        customiq.setTo(targetCameraJID1);
421
        customiq.setFrom(connection.getUser());
422
        customiq.setDefaultXmlns("urn:logitech-com:logitech-alert:device:media:recording:search");
423
        connection.sendPacket(customiq); 
424
    }
425
 
426
    public void test1(String inTargetHostname) throws XMPPException, IOException {
427
        targetHostname = inTargetHostname;
428
        targetUsername = "admin";
429
        
430
        // turn on the enhanced debugger
431
        XMPPConnection.DEBUG_ENABLED = true;
432
 
433
        loginCamera(targetUsername, "Logitech");
434
        
435
        // Let's do a short sleep just to let some XMPP flow before we invoke more activity.
436
        try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
437
        System.out.println("PLACE0200 after post-loginCamera sleep.");
438
        
439
        if (connection.isConnected())
440
        {
441
            if (connection.isAuthenticated())
442
            {
443
                // This is probably entirely wrong and shows how lost in using smack that I am.
444
                targetCameraJID = targetUsername + "@" + targetHostname;
445
446
                System.out.println("PLACE0201 Calling testPubSub1...");
447
                commandNvrBasicGet();
448
                
449
                // Let's do a short sleep just to let some XMPP flow before we invoke more activity.
450
                try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
451
                System.out.println("PLACE0202 after post-commandNvrBasicGet sleep.");
452
                
453
                //commandNvrSetNotification1();
454
                //testPubSub1();
455
                //testPubSub2();
456
                getRecordingList();
457
                System.out.println("PLACE0203 after testPubSub1...");
458
            }
459
        }
460
 
461
        // Let's do a longer sleep to let some XMPP flow before we quit.
462
        try { Thread.sleep(60500); } catch (InterruptedException e) { e.printStackTrace(); }
463
        System.out.println("PLACE0290 after post-testPubSub1 attempt sleep.");
464
         
465
        System.out.println("PLACE9990 app going to end connection and exit...");
466
        disconnect();       
467
    }
468
    
469
    
470
    public static void main(String args[]) throws XMPPException, IOException {
471
        // Put your test target Logitech Alert camera IP Address here
472
        String inTargetHostname = "192.168.4.116";
473
474
        myInstance = new Main();
475
        myInstance.test1(inTargetHostname);
476
        System.exit(0);
477
    }
478
}