/*
 *  Copyright (C) 2016  Datamatica (dev@datamatica.pl)
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published
 *  by the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 * 
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package pl.datamatica.traccar.api.services;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import pl.datamatica.traccar.api.CommandHandler;

public class CommandService {

    public Map<String, Object> sendCommand(Object activeDevice, Object backendCommand) {
        final Map<String, Object> result = new HashMap<>();
        final Object awaiter = new Object();
        
        try {
            Method sendCommand = activeDevice.getClass().getDeclaredMethod("sendCommand",
                    backendCommand.getClass(), Object.class);
            sendCommand.invoke(activeDevice, backendCommand, new CommandHandler(result, awaiter));
            synchronized (awaiter) {
                awaiter.wait();
            }
        } catch (Exception e) {
            return null;
        }
        
        return result;
    }
    
    public Map<String, Object> sendCustomCommand(Object activeDevice, String msg) {
        final Map<String, Object> result = new HashMap<>();
        final Object awaiter = new Object();
        
        try {
            Method sendCommand = activeDevice.getClass().getDeclaredMethod("write", Object.class,
                                Object.class);
            sendCommand.invoke(activeDevice, msg, new CommandHandler(result, awaiter));
            synchronized(awaiter) {
                awaiter.wait();
            }
        } catch(Exception e) {
            return null;
        }
        return result;
    }

}

@@ -53,19 +53,17 @@
 a.pl)%0A *
-  
 %0A
+
  *  This
@@ -315,25 +315,24 @@
  version.%0A *
- 
 %0A *  This pr
@@ -579,17 +579,16 @@
 ails.%0A *
- 
 %0A *  You
@@ -779,16 +779,68 @@
 vices;%0A%0A
+import java.lang.reflect.InvocationTargetException;%0A
 import j
@@ -1183,33 +1183,25 @@
 w Object();%0A
-        
 %0A
+
         try 
@@ -1553,16 +1553,167 @@
  catch (
+IllegalAccessException %7C IllegalArgumentException %7C InterruptedException%0A                %7C NoSuchMethodException %7C SecurityException %7C InvocationTarget
 Exceptio
@@ -1754,25 +1754,17 @@
       %7D%0A
-        
 %0A
+
         
@@ -1784,21 +1784,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     publ
@@ -1978,17 +1978,9 @@
 ();%0A
-        
 %0A
+
     
@@ -2087,28 +2087,16 @@
 .class,%0A
-            
         
@@ -2114,32 +2114,32 @@
  Object.class);%0A
+
             send
@@ -2234,16 +2234,17 @@
 hronized
+ 
 (awaiter
@@ -2308,17 +2308,169 @@
  %7D catch
-(
+ (IllegalAccessException %7C IllegalArgumentException %7C InterruptedException%0A                %7C NoSuchMethodException %7C SecurityException %7C InvocationTarget
 Exceptio

Use specific exceptions instead of generic one in CommandService