package es.unizar.cps.tecnodiscap.data;
import java.util.ArrayList;
import es.unizar.tecnodiscap.io.DriverListener;
import es.unizar.tecnodiscap.io.zigbee.ZigbeeGateway;
import es.unizar.tecnodiscap.io.zigbee.ZigbeeGatewayListener;
import es.unizar.tecnodiscap.io.zigbee.at.ZigbeeGatewayATv3;
import es.unizar.tecnodiscap.osgi4ami.device.Device;
import es.unizar.tecnodiscap.zigbee.ZigbeeDriver;
import es.unizar.tecnodiscap.io.zigbee.ZigbeeGatewayListener;
import es.unizar.tecnodiscap.zigbee.osgi4ami.device.sensor.extended.UnizarAccelerationSensor;
import es.unizar.tecnodiscap.zigbee.osgi4ami.device.simpleHMI.UnizarPendantSimpleHMI;
public class DeviceManager implements DriverListener, ZigbeeGatewayListener {
public static int Port = 5000;
private boolean enabled;
private ArrayList<DeviceManagerListener> listener = new ArrayList<DeviceManagerListener>();
private RemoteControl remoteControl;
private Brazo brazo;
private ZigbeeDriver zigbeeDriver;
public DeviceManager() {
zigbeeDriver = new ZigbeeDriver();
zigbeeDriver.registerListener(this);
for (int i=0;i<zigbeeDriver.getGateways().size();i++)
((ZigbeeGatewayATv3)zigbeeDriver.getGateways().get(i)).registerListener(this);
// this.remoteControl=new RemoteControl(zigbeeDriver);
}
public RemoteControl getRemoteControl() {
return this.remoteControl;
}
public void setRemoteCotrol(RemoteControl remoteControlAUX) {
this.remoteControl = remoteControlAUX;
}
// TODO get y set de brazo
public int disable() {
enabled = false;
return 0;
}
public int enable() {
enabled = true;
return 0;
}
public void switchOffDevices(){
zigbeeDriver.stopDriver();
zigbeeDriver.clearGateways();
zigbeeDriver.unregisterListener(this);
if (this.brazo!=null){
this.brazo=null;
}
if (this.remoteControl!=null){
this.remoteControl.switchOffRemoteControl();
}
}
public void addListener(DeviceManagerListener listener) {
this.listener.add(listener);
}
public void removeListener(DeviceManagerListener listener) {
this.listener.remove(listener);
}
public boolean isEnable() {
return enabled;
}
public ZigbeeDriver getZigbeeDriver() {
return zigbeeDriver;
}
private void fireEvent(int event) {
for (int i = 0; i < this.listener.size(); i++) {
this.listener.get(i).DeviceManagerEvent(event);
}
}
public void driverNodeEvent(int EventCode, Object arg1) {
// TODO Auto-generated method stub
Device device= (Device)arg1;
switch (EventCode) {
case DriverListener.NODE_ADDED:
System.out.println(device.getClass().toString());
if (device instanceof Brazo) {
// TODO dar de alta el brazo
} else if (device instanceof UnizarAccelerationSensor) {
if (this.remoteControl == null) {
this.remoteControl = new RemoteControl(
(UnizarAccelerationSensor) device);
} else {
this.remoteControl
.setAccelerometer((UnizarAccelerationSensor) device);
// this.remoteControl.setListener(this);
this.remoteControl.enable();
this.enable();
// this.RemoteControlEvent(RemoteControlListener.RemoteControlEnable);
fireEvent(DeviceManagerListener.RemoteControlEnable);
}
} else if (device instanceof UnizarPendantSimpleHMI) {
if (this.remoteControl == null) {
this.remoteControl = new RemoteControl(
(UnizarPendantSimpleHMI) device);
} else {
this.remoteControl
.setSimpleHMI((UnizarPendantSimpleHMI) device);
// this.remoteControl.setListener(this);
this.remoteControl.enable();
this.enable();
// this.RemoteControlEvent(RemoteControlListener.RemoteControlEnable);
fireEvent(DeviceManagerListener.RemoteControlEnable);
}
}
break;
case DriverListener.NODE_REMOVED:
if (device instanceof Brazo) {
// TODO dar de baja el brazo
} else if (device instanceof UnizarAccelerationSensor) {
this.remoteControl = null;
this.remoteControl.disable();
// this.RemoteControlEvent(RemoteControlListener.RemoteControlDisable);
fireEvent(DeviceManagerListener.RemoteControlDisable);
} else if (device instanceof UnizarPendantSimpleHMI) {
this.remoteControl = null;
this.remoteControl.disable();
// this.RemoteControlEvent(RemoteControlListener.RemoteControlDisable);
fireEvent(DeviceManagerListener.RemoteControlDisable);
}
break;
}
}
public void messageReceived(ZigbeeGateway arg0, String arg1, byte[] arg2) {
// TODO Auto-generated method stub
}
public void neigbourghAnswer(ZigbeeGateway arg0, ArrayList arg1, String arg2) {
// TODO Auto-generated method stub
}
public void networkBeat(ZigbeeGateway arg0, String arg1, String arg2, String arg3) {
// TODO Auto-generated method stub
}
public void nodeBeat(ZigbeeGateway arg0, String arg1, int arg2, String arg3, String arg4) {
// TODO Auto-generated method stub
}
public void registerDataReceived(ZigbeeGateway arg0, String arg1, String arg2, String arg3) {
// TODO Auto-generated method stub
}
}