BowlerKernel
DMDevice.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.common;
2 
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5 import java.util.ArrayList;
6 
7 public class DMDevice extends NonBowlerDevice {
8  private Object wrapped = null;
9  Method methodConnect = null;
10  Method methodDisconnect = null;
11  boolean hasGetName = false;
12  boolean hasIsAvailible=false;
13  Method methodGetName = null;
14  Method isAvaibleMeth=null;
15 
16  public DMDevice(Object o) throws NoSuchMethodException, SecurityException {
17  if(!wrappable(o))
18  throw new RuntimeException("This object is not wrappable! ");
19  setWrapped(o);
20  methodConnect = getWrapped().getClass().getMethod("connect", null);
21  methodDisconnect = getWrapped().getClass().getMethod("disconnect", null);
22  hasGetName = methodExists(getWrapped(), "getName");
23  hasIsAvailible = methodExists(getWrapped(), "isAvailable");
24  methodGetName = null;
25  }
26 
27  @Override
28  public String getScriptingName() {
29 
30  if (hasGetName) {
31  if (methodGetName == null)
32  try {
33  methodGetName = getWrapped().getClass().getMethod("getName", null);
34 
35  } catch (Exception e) {
36  return super.getScriptingName();
37  }
38  } else {
39  return super.getScriptingName();
40  }
41  if (methodGetName == null)
42  return super.getScriptingName();
43  try {
44  super.setScriptingName( (String) methodGetName.invoke(getWrapped(), null));
45  } catch (Exception e) {
46  return super.getScriptingName();
47  }
48  return super.getScriptingName();
49  }
50 
51  @Override
52  public ArrayList<String> getNamespacesImp() {
53  // TODO Auto-generated method stub
54  return new ArrayList<String>();
55  }
56 
57  @Override
58  public void disconnectDeviceImp() {
59  try {
60  methodDisconnect.invoke(getWrapped(), null);
61  } catch (Exception e) {
62  // TODO Auto-generated catch block
63  e.printStackTrace();
64  }
65  }
72  @Override
73  public boolean isAvailable() throws InvalidConnectionException{
74  if(hasIsAvailible) {
75  if(isAvaibleMeth==null) {
76  try {
77  isAvaibleMeth = getWrapped().getClass().getMethod("isAvailable", null);
78  } catch (Exception e) {
79  //true
80  }
81  }
82  try {
83  return (boolean) isAvaibleMeth.invoke(getWrapped(), null);
84  } catch (Exception e) {
85  //true
86  }
87  }
88  return true;
89  }
90 
91  @Override
92  public boolean connectDeviceImp() {
93  try {
94  Object value = methodConnect.invoke(getWrapped(), null);
95  try {
96  return (Boolean) value;
97  } catch (Exception e) {
98 
99  }
100  return true;
101  } catch (Exception e) {
102  // TODO Auto-generated catch block
103  e.printStackTrace();
104  }
105  return false;
106  }
107  public static boolean wrappable(Object o) {
108  if(o==null)
109  return false;
110  return methodExists(o, "connect") &&
111  methodExists(o, "disconnect");
112 
113  }
114  public static boolean methodExists(Object clazz, String methodName) {
115  for (Method method : clazz.getClass().getMethods()) {
116  if (method.getName().equals(methodName)) {
117  return true;
118  }
119  }
120  return false;
121  }
122 
123  public Object getWrapped() {
124  return wrapped;
125  }
126 
127  public void setWrapped(Object wrapped) {
128  this.wrapped = wrapped;
129  }
130 
131 }
static boolean wrappable(Object o)
Definition: DMDevice.java:107
ArrayList< String > getNamespacesImp()
Definition: DMDevice.java:52
static boolean methodExists(Object clazz, String methodName)
Definition: DMDevice.java:114