package art.servers.controller;
|
|
import art.library.model.devices.Device;
|
import art.servers.Shared;
|
import java.util.HashMap;
|
|
|
|
public class FactoryController extends Thread
|
{
|
private HashMap<ControllerDevice, Long> timestamps = new HashMap<ControllerDevice, Long>();
|
|
|
public FactoryController()
|
{
|
super();
|
this.setName("Factory controller");
|
}
|
|
|
public void run()
|
{
|
while (true)
|
{
|
try
|
{
|
// Add controllers
|
|
for (Device device : Shared.model.getDevices())
|
{
|
if (Shared.existsDeviceController(device) == false)
|
{
|
ControllerDevice controller = addController(device);
|
timestamps.put(controller, device.getLastTimestampInformationUpdate());
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
|
try
|
{
|
// Remove controllers
|
|
for (Controller controller : Shared.lcontroller)
|
{
|
if (controller instanceof ControllerDevice)
|
{
|
Device device = ((ControllerDevice)controller).getDevice();
|
|
if (Shared.model.existsDevice(device.getIdentifier()) == false)
|
{
|
removeController((ControllerDevice)controller);
|
}
|
else if ((Long)timestamps.get(controller) != device.getLastTimestampInformationUpdate())
|
{
|
// Information has changed, we delete controller and will be executed again
|
// May be some changes in connections changed
|
removeController((ControllerDevice)controller);
|
}
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
|
|
try
|
{
|
sleep(5000);
|
}
|
catch (Exception e)
|
{
|
}
|
|
}
|
}
|
|
|
public ControllerDevice addController(Device device)
|
{
|
return null;
|
}
|
|
|
|
public void removeController(ControllerDevice controller)
|
{
|
try
|
{
|
timestamps.remove(controller);
|
controller.close();
|
Shared.lcontroller.remove(controller);
|
|
if (Shared.window != null)
|
{
|
Device device = controller.getDevice();
|
Shared.window.getArticPanel().removeTab(Shared.getMessage(device.information.name));
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
}
|