Alejandro Acuña
2024-07-30 65a64a81d30f00f1fffd5da6866850e1308e1135
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
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)
        {
        }
    }
    
    
        
}