package Controller;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import DataListeners.AbstractDataListener;
import DataListeners.DataListenerFactory;
import Models.Device;
import Utils.FilerReader;
import Utils.xmlReader;
/**
* Core Implementation for managing the devices. Steps 1. initialize the
* registered listeners 2. initialize the registered devices 3. initialize the
* listener for receiving new device configs from DCF then monitor the overall
* life cycle of a device added on the hub
*
* @author sudeep
*
*/
public class DeviceManagement {
/**
* List of registered devices
*/
private ArrayList<Device> devices = new ArrayList<Device>();
private static DeviceManagement singleton = null;
/**
* List of registered data listeners
*/
private HashMap<String, AbstractDataListener> dataListeners = new HashMap<String, AbstractDataListener>();
public DeviceManagement() {
super();
if (singleton == null) {
devices = new ArrayList<Device>();
initListeners();
initDevices();
singleton = this;
}
}
public ArrayList<Device> getDevices() {
return devices;
}
public void setDevices(ArrayList<Device> devices) {
this.devices = devices;
}
public HashMap<String, AbstractDataListener> getDataListeners() {
return dataListeners;
}
public void setDataListeners(HashMap<String, AbstractDataListener> dataListeners) {
this.dataListeners = dataListeners;
}
public static DeviceManagement getInstance() {
if (singleton == null)
singleton = new DeviceManagement();
return singleton;
}
public synchronized boolean MonitorForNewDevices() throws Exception {
FileChangedWatcher fcw = new FileChangedWatcher("Resources/devices.txt");
fcw.monitor();
return true;
}
/**
* initialize the registered data listeners. Available data listeners are
* added in the resources/configs.xml
*/
private void initListeners() {
xmlReader xr = new xmlReader();
ArrayList<String> listners = xr.loadconfigs();
for (String listener : listners) {
DataListenerFactory dlf = new DataListenerFactory();
AbstractDataListener adl;
try {
adl = dlf.getCommunicationListner(listener);
if (adl != null) {
this.dataListeners.put(listener, adl);
adl.start();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// startListnerForDevices();
}
/**
* initialize all the registered devices. Available devices are in
* resources/devices.txt This is important to deal with the case when the
* machine goes offline and online
*/
private void initDevices() {
FilerReader fr = new FilerReader();
try {
this.devices = fr.readDevicesList(new File("Resources/devices.txt"));
for (Device device : this.devices) {
addDeviceToDataListener(device);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// startListnerForDevices();
}
private void addDeviceToDataListener(Device device) {
System.out.println("Type" + device.getCommunicationListenerType());
// if
// (this.dataListeners.containsKey(device.getCommunicationListenerType()))
this.dataListeners.get(device.getCommunicationListenerType()).setAssociatedDevice(device);
}
// TODO <b>REDO</b> this function to use the action parameter ; as of now
// all
// existing registration are removed and initialized again
public void updateDevices(ArrayList<Device> devices) {
System.out.println("UpdateDevice Called>>>");
this.devices.clear();
this.devices.addAll(devices);
for (AbstractDataListener adl : dataListeners.values()) {
adl.getAssociatedDevices().clear();
}
for (Device device : this.devices) {
addDeviceToDataListener(device);
}
}
public Device getDeviceById(int id) {
Device d = new Device();
for (Device device : devices) {
if (device.getId() == id) {
d = device;
break;
}
}
return d;
}
}