BowlerKernel
BowlerAbstractDevice.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
32 package com.neuronrobotics.sdk.common;
33 
34 import java.util.ArrayList;
35 
36 import com.neuronrobotics.sdk.commands.bcs.core.PingCommand;
37 import com.neuronrobotics.sdk.commands.neuronrobotics.dyio.InfoFirmwareRevisionCommand;
38 
39 // TODO: Auto-generated Javadoc
47 public abstract class BowlerAbstractDevice implements IBowlerDatagramListener {
48 
50  private boolean keepAlive = true;
51 
53  private boolean disconnecting = false;
54 
56  private long lastPacketTime=0;
57 
62 
64  private ArrayList<IDeviceConnectionEventListener> disconnectListeners = new ArrayList<IDeviceConnectionEventListener> ();
65 
67  private String scriptingName = "device";
68 
69 
76  public boolean isAvailable() throws InvalidConnectionException{
77  return getConnection().isConnected()&&disconnecting==false;
78  }
79 
80 
84  protected void fireDisconnectEvent() {
85  ArrayList<IDeviceConnectionEventListener> disconnectListeners2 = getDisconnectListeners();
86  for (int i = 0; i < disconnectListeners2.size(); i++) {
87  IDeviceConnectionEventListener l = disconnectListeners2.get(i);
88  l.onDisconnect(this);
89  }
90  }
91 
95  protected void fireConnectEvent() {
96  ArrayList<IDeviceConnectionEventListener> disconnectListeners2 = getDisconnectListeners();
97  for (int i = 0; i < disconnectListeners2.size(); i++) {
98  IDeviceConnectionEventListener l = disconnectListeners2.get(i);
99  l.onConnect(this);
100  }
101  }
102 
109  if(!getDisconnectListeners().contains(l)) {
110  getDisconnectListeners().add(l);
111  }
112 // System.err.println(getScriptingName()+" Adding listener "+l.getClass());
113 // l.trace.printStackTrace();
114 // new Exception().printStackTrace();
115  BowlerAbstractDevice bad = this;
116  if(connection !=null)
118 
119  @Override
120  public void onDisconnect(BowlerAbstractConnection source) {
121 
122  l.onDisconnect(bad);
123  }
124 
125  @Override
126  public void onConnect(BowlerAbstractConnection source) {
127  // TODO Auto-generated method stub
128  l.onConnect(bad);
129  }
130  });
131  }
132 
139  if(getDisconnectListeners().contains(l)) {
140  getDisconnectListeners().remove(l);
141  }
142 
143  }
144 
145 
146 
154  if(connection == null) {
155  throw new NullPointerException("Can not use a NULL connection.");
156  }
157  BowlerAbstractDevice bad = this;
158  for(int i=0;i<getDisconnectListeners().size();i++) {
159  final int index = i;
161 
162  @Override
163  public void onDisconnect(BowlerAbstractConnection source) {
164  getDisconnectListeners().get(index).onDisconnect(bad);
165  }
166 
167  @Override
168  public void onConnect(BowlerAbstractConnection source) {
169  // TODO Auto-generated method stub
170  getDisconnectListeners().get(index).onConnect(bad);
171  }
172  });
173  }
174  this.connection = connection;
176  }
177 
185  public boolean connect() throws InvalidConnectionException {
186 
187  if (connection == null) {
188  throw new InvalidConnectionException("Null Connection");
189  }
190  if(!connection.isConnected()) {
191  if(!connection.connect()) {
192  return false;
193  }else {
194  //startHeartBeat();
195  }
196  }
197 
198  return this.isAvailable();
199  }
200 
204  public void disconnect() {
205  disconnecting=true;
206  if (connection != null) {
207  if(connection.isConnected()){
208  Log.info("Disconnecting Bowler Device");
210  }
211  }
212  }
213 
220  return connection;
221  }
222 
229  if(!address.isValid()) {
230  throw new InvalidMACAddressException();
231  }
232  this.address = address;
233  }
234 
241  //System.out.println();
242  return address;
243  }
244 
245 // /**
246 // * Send a sendable to the connection.
247 // *
248 // * @param sendable the sendable
249 // * @return the syncronous response
250 // */
251 // public BowlerDatagram send(ISendable sendable) {
252 // setLastPacketTime(System.currentTimeMillis());
253 // return connection.send(sendable, getAddress());
254 // }
255 
256 
266  return send(command,1);
267  }
268 
279 
280  return connection.send(command,getAddress(), retry);
281  }
282 
296  public Object [] send(String namespace,BowlerMethod method, String rpcString, Object[] arguments, int retry) throws DeviceConnectionException{
297  return connection.send(getAddress(),namespace, method, rpcString, arguments, retry);
298  }
299 
312  public Object [] send(String namespace,BowlerMethod method, String rpcString, Object[] arguments) throws DeviceConnectionException{
313  return send(namespace, method, rpcString, arguments, 5);
314  }
315 
322  public boolean ping() {
323  return ping(false);
324  }
325 
333  public boolean ping(boolean switchParser) {
334  return connection.ping(getAddress(),switchParser);
335  }
341  public ArrayList<ByteList> getRevisions(){
342  ArrayList<ByteList> list = new ArrayList<ByteList>();
343  try {
345  Log.debug("FW info:\n"+b);
346  for(int i=0;i<(b.getData().size()/3);i++){
347  list.add(new ByteList(b.getData().getBytes((i*3),3)));
348  }
349  } catch (InvalidResponseException e) {
350  Log.error("Invalid response from Firmware rev request");
351 
352  } catch (NoConnectionAvailableException e) {
353  Log.error("No connection is available.");
354  }
355  return list;
356  }
357 
363  public ArrayList<String> getNamespaces(){
365  }
366 
373  public boolean hasNamespace(String string) {
374  return connection.hasNamespace(string,getAddress());
375  }
376 
380  public void startHeartBeat(){
382  }
383 
389  public void startHeartBeat(long msHeartBeatTime){
390  getConnection().startHeartBeat(msHeartBeatTime);
391  }
392 
396  public void stopHeartBeat(){
398  }
399 
400 
406  public void setThreadedUpstreamPackets(boolean up){
407  if(connection != null){
409  }
410  }
411 
418  public ArrayList<RpcEncapsulation> getRpcList(String namespace) {
419  return connection.getRpcList(namespace,getAddress());
420  }
421 
425  public void loadRpcList() {
426  ArrayList<String> names = getNamespaces();
427 
428  for (String s:names){
429  System.out.println(getRpcList(s));
430  }
431 
432  }
438  @Deprecated
439  public void onAllResponse(BowlerDatagram data){
440  // this is here to prevent the breaking of an interface,
441  }
442 
448  public boolean isKeepAlive() {
449  return keepAlive;
450  }
451 
457  public void setKeepAlive(boolean keepAlive) {
458  this.keepAlive = keepAlive;
459  }
460 
461 
467  public long getLastPacketTime() {
468  return lastPacketTime;
469  }
470 
471 
477  public void setLastPacketTime(long lastPacketTime) {
478  this.lastPacketTime = lastPacketTime;
479  }
480 
481 
487  public String getScriptingName() {
488  return scriptingName;
489  }
490 
491 
497  public void setScriptingName(String scriptingName) {
498  this.scriptingName = scriptingName;
499  }
500 
501 
507  public ArrayList<IDeviceConnectionEventListener> getDisconnectListeners() {
508  return disconnectListeners;
509  }
510 
511 
512 }
Object[] send(MACAddress addr, String namespace, BowlerMethod method, String rpcString, Object[] arguments, int retry)
ArrayList< RpcEncapsulation > getRpcList(String namespace, MACAddress addr)
ArrayList< RpcEncapsulation > getRpcList(String namespace)
void setConnection(BowlerAbstractConnection connection)
ArrayList< IDeviceConnectionEventListener > disconnectListeners
void addConnectionEventListener(final IDeviceConnectionEventListener l)
Object[] send(String namespace, BowlerMethod method, String rpcString, Object[] arguments)
BowlerDatagram send(BowlerAbstractCommand command)
void removeConnectionEventListener(IDeviceConnectionEventListener l)
ArrayList< IDeviceConnectionEventListener > getDisconnectListeners()
Object[] send(String namespace, BowlerMethod method, String rpcString, Object[] arguments, int retry)
BowlerDatagram send(BowlerAbstractCommand command, int retry)
static void info(String message)
Definition: Log.java:110
static void error(String message)
Definition: Log.java:92
static void debug(String message)
Definition: Log.java:128