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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package art.servers.controller;
 
import art.library.interop.InteropParameter;
import art.library.interop.InteropParameters;
import art.library.interop.InteropResponse;
import art.library.interop.serialization.Serialization;
import art.library.interop.serialization.SerializationException;
import art.library.model.devices.Device;
import art.library.model.devices.application.ApplicationInformation;
import art.servers.Shared;
import art.servers.configuration.ConfigurationTransactions;
import art.servers.configuration.ConfigurationTransactionsService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class ControllerTransactions extends Controller 
{
    private ConfigurationTransactions configuration = null;
    private ApplicationInformation[] lapplication = new ApplicationInformation[0];
    private ConfigurationTransactionsService currentTransactionsService = null;
 
    
    public ControllerTransactions(ConfigurationTransactions configuration)
    {
        this.configuration = configuration;
        this.setName("Controller transactions");
        start();
    }
    
    
    
    public ApplicationInformation[] getApplications()
    {
        return lapplication;
    }
    
    
    
    public ApplicationInformation[] getApplications(String service)
    {
        List<ApplicationInformation> result = new ArrayList<ApplicationInformation>();
        
        for (ApplicationInformation applicationInformation : lapplication)
        {
            if ((applicationInformation.serverServiceName != null)  && (applicationInformation.serverServiceName.equals(service)))
            {
                result.add(applicationInformation);
            }
        }
        
        return result.toArray(new ApplicationInformation[result.size()]);
    }
    
    
    
    
    
    public void run()
    {
        Shared.traceInformation(configuration.name, "Starting");
        
        long lastTimestamp = System.currentTimeMillis();
        
        while ((isInterrupted() == false) && (exit == false))
        {
            if (Shared.isServerEnabled() == true)
            {
                try
                {
                    // reload
                    
                    reload();
                    
                    // Wait 
 
                    long timesleep = (configuration.polling * 1000) - (System.currentTimeMillis() - lastTimestamp);
 
                    if ((timesleep > 0) && (timesleep < (configuration.polling * 1000)))
                    {
                        sleep(timesleep);
                    }
 
                    // Update devices
 
                    long currentTimestamp = System.currentTimeMillis();
                    lastTimestamp = currentTimestamp;
                }
                catch (Exception e)
                {
                }
            }
        }
        
        Shared.traceInformation(configuration.name, "Finishing");
    }
 
 
    
    
    
    
    
    private void reload()
    {
        for (ConfigurationTransactionsService configurationTransactionsService : configuration.lservicesTransactions)
        {
            try
            {
                InteropParameters parameters = new InteropParameters();
                parameters.addParameter(new InteropParameter("operation", "getApplicationsInformation"));
                parameters.addParameter(new InteropParameter("language", Shared.configuration.general.language));
                InteropResponse response = (InteropResponse)Serialization.invoke("get", parameters, configurationTransactionsService.address, configurationTransactionsService.port, configurationTransactionsService.timeout);
                ApplicationInformation[] lapplication = Arrays.copyOf(response.getValue(), response.getValue().length, ApplicationInformation[].class);
                
                if (lapplication.length > 0) 
                {
                    currentTransactionsService = configurationTransactionsService;
                    this.lapplication = lapplication;
                    return;
                }
            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }
        }
    }
    
    
    
    
    
    public InteropResponse set (InteropParameters parameters) throws  SerializationException
    {
        try
        {
            return (InteropResponse)Serialization.invoke("set", parameters, currentTransactionsService.address, currentTransactionsService.port, currentTransactionsService.timeout);
        }
        catch (Exception exception)
        {
        }
        
        throw new SerializationException("Operation error (transactions server)");
    }
    
    
    
    
    
    public InteropResponse get (InteropParameters parameters) throws  SerializationException
    {
        try
        {
            return (InteropResponse)Serialization.invoke("get", parameters, currentTransactionsService.address, currentTransactionsService.port, currentTransactionsService.timeout);
        }
        catch (Exception exception)
        {
        }
        
        throw new SerializationException("Operation error (transactions server)");
    }
    
    
    
    
    
    
    
    
    public Device getDevice(String service, String identifier) throws SerializationException
    {
        try
        {
            for (ApplicationInformation application : getApplications(service))
            {
                try
                {
                    InteropParameters parameters = new InteropParameters();
                    parameters.addParameter(new InteropParameter("service", application.serverServiceName));
                    parameters.addParameter(new InteropParameter("operation", "getDevice"));
                    parameters.addParameter(new InteropParameter("identifier", identifier));
                    parameters.addParameter(new InteropParameter("language", Shared.configuration.general.language));
                    InteropResponse response = (InteropResponse)Serialization.invoke("get", parameters, application.serverAddress, application.serverPort, 30000);
                    return (Device)response.getValue()[0];
                }
                catch (Exception exception)
                {
                }
            }
        }
        catch (Exception e)
        {
        }
        
        throw new SerializationException("Device not found");
    }
    
    
    
    public List<Device> getDevices(String service) throws SerializationException
    {
        List<Device> result = new ArrayList<Device>();
        ApplicationInformation[] applications = getApplications(service);
 
        for (ApplicationInformation application : applications)
        {
            if (service.equalsIgnoreCase(application.serverServiceName))
            {
                try
                {
                    InteropParameters parameters = new InteropParameters();
                    parameters.addParameter(new InteropParameter("service", application.serverServiceName));
                    parameters.addParameter(new InteropParameter("operation", "getDevices"));
                    parameters.addParameter(new InteropParameter("language", Shared.configuration.general.language));
                    InteropResponse response = (InteropResponse)Serialization.invoke("get", parameters, application.serverAddress, application.serverPort, 30000);
                    Device[] devices = Arrays.copyOf(response.getValue(), response.getValue().length, Device[].class);
                    result.addAll(Arrays.asList(devices));
                }
                catch (Exception exception)
                {
                }
            }
        }
        
        return result;
    }
    
    
    public List<Device> getDevices() throws SerializationException
    {
        List<Device> result = new ArrayList<Device>();
        
        for (ApplicationInformation application : getApplications())
        {
            try
            {
                InteropParameters parameters = new InteropParameters();
                parameters.addParameter(new InteropParameter("service", application.serverServiceName));
                parameters.addParameter(new InteropParameter("operation", "getDevices"));
                parameters.addParameter(new InteropParameter("language", Shared.configuration.general.language));
                InteropResponse response = (InteropResponse)Serialization.invoke("get", parameters, application.serverAddress, application.serverPort, 30000);
                Device[] devices = Arrays.copyOf(response.getValue(), response.getValue().length, Device[].class);
                result.addAll(Arrays.asList(devices));
            }
            catch (Exception exception)
            {
            }
        }
        
        return result;
    }
    
    
    
    
    
    public ApplicationInformation getApplication(String service) throws SerializationException
    {
        for (ApplicationInformation application : getApplications(service))
        {
            if (service.equalsIgnoreCase(application.serverServiceName))
            {
                return(application);
            }
        }
        
        throw new SerializationException("Application not found");
    }
 
}