BowlerKernel
DeviceManager.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.common;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 
6 import javax.management.RuntimeErrorException;
7 
8 import com.neuronrobotics.sdk.bootloader.NRBootLoader;
9 import com.neuronrobotics.sdk.bowlercam.device.BowlerCamDevice;
10 import com.neuronrobotics.sdk.dyio.DyIO;
11 import com.neuronrobotics.sdk.genericdevice.GenericDevice;
12 import com.neuronrobotics.sdk.pid.GenericPIDDevice;
13 import com.neuronrobotics.sdk.ui.ConnectionDialog;
14 import com.neuronrobotics.sdk.util.ThreadUtil;
15 
16 // TODO: Auto-generated Javadoc
20 public class DeviceManager {
21 
23  private static final ArrayList<BowlerAbstractDevice> devices = new ArrayList<BowlerAbstractDevice>();
24 
26  private static final ArrayList<IDeviceAddedListener> deviceAddedListener = new ArrayList<IDeviceAddedListener>();
27 
36  public static void addConnection(final Object newDevice, String name) {
37 
38  if (BowlerAbstractDevice.class.isInstance(newDevice)) {
39  addConnectionBAD((BowlerAbstractDevice) newDevice, name);
40  } else if (DMDevice.wrappable(newDevice)) {
41  try {
42  addConnectionBAD(new DMDevice(newDevice), name);
43  } catch (Exception e) {
44  e.printStackTrace();
45  }
46  } else {
47  throw new RuntimeException("This object can not behave as a device");
48  }
49  }
50 
59  private static void addConnectionBAD(final BowlerAbstractDevice newDevice, String name) {
60  if (DeviceManager.getSpecificDevice(name) == newDevice) {
61  System.out.println("Device " + name + " is already in the manager");
62  return;
63  }
64  if ( DMDevice.class.isInstance(newDevice)) {
65  DMDevice incoming = (DMDevice) newDevice;
66  for(String s:listConnectedDevice() ){
67  Object sDev = DeviceManager.getSpecificDevice(s);
68  if(DMDevice.class.isInstance(sDev)) {
69  DMDevice inside = (DMDevice) sDev;
70  if (inside.getWrapped() == incoming.getWrapped()) {
71  System.out.println("Wrapped Device " + name + " is already in the manager");
72  return;
73  }
74  }
75  }
76 
77  }
78 
79  if (!newDevice.isAvailable())
80  newDevice.connect();
81  if (!newDevice.isAvailable()) {
82  throw new BowlerRuntimeException(
83  "Device " + name + " of type " + newDevice.getClass().getSimpleName() + " is not availible");
84  }
85  if (devices.contains(newDevice)) {
86  Log.warning("Device is already added " + newDevice.getScriptingName());
87  }
88  int numOfThisDeviceType = 0;
89 
90  for (int i = 0; i < devices.size(); i++) {
91  if (newDevice.getClass().isInstance(devices.get(i))
92  && devices.get(i).getScriptingName().contentEquals(name))
93  numOfThisDeviceType++;
94  }
95  if (numOfThisDeviceType > 0)
96  name = name + numOfThisDeviceType;
97  newDevice.setScriptingName(name);
98  devices.add(newDevice);
100  @Override
101  public void onDisconnect(BowlerAbstractDevice source) {
102  if (source == newDevice && source != null)
103  DeviceManager.remove(newDevice);
104  }
105 
106  @Override
107  public void onConnect(BowlerAbstractDevice source) {
108  }
109  });
110 
111  for (int i=0;i< deviceAddedListener.size();i++) {
113  l.onNewDeviceAdded(newDevice);
114  }
115  }
116 
123  public static void addConnection(BowlerAbstractConnection connection) {
124  if (connection == null) {
125  return;
126  }
127 
128  GenericDevice gen = new GenericDevice(connection);
129  try {
130  if (!gen.connect()) {
131  throw new InvalidConnectionException("Connection is invalid");
132  }
133  if (!gen.ping(true)) {
134  throw new InvalidConnectionException("Communication failed");
135  }
136  } catch (Exception e) {
137  // connection.disconnect();
138  ThreadUtil.wait(1000);
140  if (!gen.connect()) {
141  throw new InvalidConnectionException("Connection is invalid");
142  }
143  if (!gen.ping()) {
144  connection = null;
145  throw new InvalidConnectionException("Communication failed");
146  }
147  throw new RuntimeException(e);
148  }
149  if (gen.hasNamespace("neuronrobotics.dyio.*")) {
150  DyIO dyio = new DyIO(gen.getConnection());
151  dyio.connect();
152  String name = "dyio";
153 
154  addConnection(dyio, name);
155 
156  }else if (gen.hasNamespace("bcs.pid.*")) {
157  GenericPIDDevice delt = new GenericPIDDevice();
158  delt.setConnection(gen.getConnection());
159  delt.connect();
160  String name = "pid";
161 
162  addConnection(delt, name);
163  } else if (gen.hasNamespace("bcs.bootloader.*") || gen.hasNamespace("neuronrobotics.bootloader.*")) {
164  NRBootLoader delt = new NRBootLoader(gen.getConnection());
165  String name = "bootloader";
166 
167  addConnection(delt, name);
168  } else if (gen.hasNamespace("neuronrobotics.bowlercam.*")) {
169  BowlerCamDevice delt = new BowlerCamDevice();
170  delt.setConnection(gen.getConnection());
171  delt.connect();
172  String name = "bowlercam";
173  addConnection(delt, name);
174  } else {
175  addConnection(gen, "device");
176  }
177 
178  }
179 
183  public static void addConnection() {
184  new Thread() {
185  public void run() {
186  setName("Connection Dialog displayer thread");
187  try {
190  } catch (BowlerRuntimeException ex) {
191  // try one more time is it fails to connect
194  }
195  }
196  }.start();
197  }
198 
205  public static void remove(BowlerAbstractDevice newDevice) {
206  if (devices.contains(newDevice) && newDevice != null) {
207  devices.remove(newDevice);
209  l.onDeviceRemoved(newDevice);
210  }
211  }
212  }
213 
221  if (!deviceAddedListener.contains(l))
222  deviceAddedListener.add(l);
223  }
224 
232  if (deviceAddedListener.contains(l))
233  deviceAddedListener.remove(l);
234  }
235 
243  public static Object getSpecificDevice(String name, IDeviceProvider provider) {
244  if(name.contains("*")) {
245  name = name.split("\\*")[0];
246  }
247  for (int i = 0; i < devices.size(); i++) {
248  if (devices.get(i).getScriptingName().contains(name)) {
249  if(DMDevice.class.isInstance(devices.get(i))) {
250  return ((DMDevice)devices.get(i)).getWrapped();
251  }
252  return devices.get(i);
253  }
254  }
255  // device doesn't exist already so we use the call back to build a new one on
256  // the fly
257  Object newDev = provider.call();
258  addConnection(newDev, name);
259  Object dev= getSpecificDevice(name);
260 
261  if(DMDevice.class.isInstance(dev)) {
262  return ((DMDevice)dev).getWrapped();
263  }
264  return dev;
265  }
266 
274  public static Object getSpecificDevice(String name) {
275  if(name.contains("*")) {
276  name = name.split("\\*")[0];
277  }
278  for (int i = 0; i < devices.size(); i++) {
279  String devname = devices.get(i).getScriptingName();
280  if (devname.contains(name)) {
281  BowlerAbstractDevice dev = devices.get(i);
282  if(DMDevice.class.isInstance(dev)) {
283  return ((DMDevice)dev).getWrapped();
284  }
285  return dev;
286  }
287  }
288  return null;
289  }
290 
300  public static Object getSpecificDevice(Class<?> class1, String name) {
301  if(name.contains("*")) {
302  name = name.split("\\*")[0];
303  }
304  if (class1 == null)
305  return getSpecificDevice(name);
306  List<String> devs = listConnectedDevice(class1);
307  if (devs.size() == 0)
308  return null;
309  else
310  for (String d : devs) {
311  // if the string is null it just returns the first of its kind
312  if (name == null || d.contentEquals(name)) {
313  for (int i = 0; i < devices.size(); i++) {
314  if (devices.get(i).getScriptingName().contains(d))
315  return devices.get(i);
316  }
317  }
318 
319  }
320  return null;
321  }
322 
328  public static List<String> listConnectedDevice() {
329  List<String> choices = new ArrayList<String>();
330  for (int i = 0; i < devices.size(); i++) {
331  choices.add(devices.get(i).getScriptingName());
332  }
333  return choices;
334 
335  }
336 
344  public static List<String> listConnectedDevice(Class<?> class1) {
345  List<String> choices = new ArrayList<String>();
346  for (int i = 0; i < devices.size(); i++) {
347  if (class1 == null)
348  choices.add(devices.get(i).getScriptingName());
349  else if (class1.isInstance(devices.get(i))) {
350  choices.add(devices.get(i).getScriptingName());
351  }
352  }
353  return choices;
354 
355  }
356 }
void setConnection(BowlerAbstractConnection connection)
void addConnectionEventListener(final IDeviceConnectionEventListener l)
static void setUseBowlerV4(boolean useBowlerV4)
static boolean wrappable(Object o)
Definition: DMDevice.java:107
static void addConnection(final Object newDevice, String name)
static final ArrayList< BowlerAbstractDevice > devices
static Object getSpecificDevice(String name)
static void addConnection(BowlerAbstractConnection connection)
static void removeDeviceAddedListener(IDeviceAddedListener l)
static Object getSpecificDevice(String name, IDeviceProvider provider)
static void addConnectionBAD(final BowlerAbstractDevice newDevice, String name)
static final ArrayList< IDeviceAddedListener > deviceAddedListener
static void remove(BowlerAbstractDevice newDevice)
static void addDeviceAddedListener(IDeviceAddedListener l)
static Object getSpecificDevice(Class<?> class1, String name)
static List< String > listConnectedDevice(Class<?> class1)
static void warning(String message)
Definition: Log.java:101
void setConnection(BowlerAbstractConnection connection)
static BowlerAbstractConnection promptConnection()
void onNewDeviceAdded(BowlerAbstractDevice bad)