Parent: [469c9d] (diff)

Child: [f9de09] (diff)

Download this file

DeviceManagement.java    152 lines (131 with data), 4.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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;
}
}