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
package art.servers.etdserver.controller;
 
 
import art.library.interop.InteropParameter;
import art.library.interop.InteropParameters;
import art.library.interop.InteropResponse;
import art.library.model.devices.DeviceStatus;
import art.library.model.devices.etd.Etd;
import art.library.interop.serialization.Serialization;
import art.library.model.devices.Device;
import art.library.model.transactions.traces.Trace;
import art.servers.etdserver.Shared;
import art.servers.etdserver.protocols.dgt.EtdDgtProtocolAnalyser;
import static java.lang.Thread.sleep;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
 
public class ControllerEtdDgtEru extends ControllerEtd
{
    private int status = DeviceStatus.STATUS_ALARM;
    private boolean firstTime = true;
    private final EtdDgtProtocolAnalyser protocolAnalyser = new EtdDgtProtocolAnalyser();
 
 
    public ControllerEtdDgtEru(Etd etd)
    {
        super(etd);
        this.device = etd;
        this.name = Shared.getMessage("Controller etd") + " " + etd.information.name;
    }
    
    
    
    
    public void run()
    {
        String  message = Shared.getMessage("Running");
        // <editor-fold defaultstate="collapsed" desc="Trace">
        art.servers.Shared.println(Trace.getTraceInformation(art.servers.Shared.getApplicationName(), name, art.servers.Shared.getMessage("Starting"), art.servers.Shared.getMessage("Success")), true);
        // </editor-fold>      
        status = DeviceStatus.STATUS_ONLINE;
 
        while ((isInterrupted() == false) && (exit == false))
        {
            long startTimestamp = System.currentTimeMillis();
 
            try
            {
                if (art.servers.Shared.isServerEnabled() == true)
                {
                    update();
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
 
            if (startTimestamp > System.currentTimeMillis())
            {
                startTimestamp = System.currentTimeMillis();
            }
 
            long stopTimestamp = startTimestamp + (device.getDeviceInformation().polling * 1000);
 
            while ((System.currentTimeMillis() < stopTimestamp) && (exit == false))
            {
                try
                {
                    sleep(50);
                }
                catch (Exception e)
                {
                }
            }
           
            if (art.servers.Shared.model.existsDevice(device.getIdentifier()) == false)
            {
                art.servers.Shared.println(name, art.servers.Shared.getMessage("Device no longer exists"));
                exit = true;
            }
        }
        
        // <editor-fold defaultstate="collapsed" desc="Trace">
        art.servers.Shared.println(Trace.getTraceInformation(art.servers.Shared.getApplicationName(), name, art.servers.Shared.getMessage("Finishing"), art.servers.Shared.getMessage("Success")), true);
        // </editor-fold>      
    }
 
 
 
 
    /*
        Private controller functions
    */
 
    private void update() throws Exception
    {
        Etd deviceclone = (Etd)Serialization.clone(device);
 
        try
        {
 
            readAlarms(deviceclone);
 
            if (deviceclone.getAlarm("alarm_offline") > 0)
            {
                offline(deviceclone);
                return;
            }
 
            readCurrentTrafficData(deviceclone);
            online(deviceclone);
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
            offline(deviceclone);
        }
    }
 
 
    private void readAlarms(Etd deviceclone) throws Exception
    {
        try
        {
            System.out.println(Device.getDate(System.currentTimeMillis()) + " - 1. " + deviceclone.getIdentifier() + " - EYA >> ");
 
            InteropParameters parameters = new InteropParameters();
            parameters.addParameter(new InteropParameter("operation", "getEtdEya"));
            parameters.addParameter(new InteropParameter("language", "en-GB"));
            parameters.addParameter(new InteropParameter("device", deviceclone.getDeviceInformation().connectionEru.eru));
            parameters.addParameter(new InteropParameter("etd", "" + deviceclone.getDeviceInformation().connectionEru.etdNumber));
            InteropResponse response = (InteropResponse) Serialization.invoke("get", parameters, deviceclone.getDeviceInformation().connectionEru.address, deviceclone.getDeviceInformation().connectionEru.port, deviceclone.getDeviceInformation().connectionEru.timeout);
            System.out.println(Device.getDate(System.currentTimeMillis()) + " - 2. " + deviceclone.getIdentifier() + " - EYA >> ");
 
            Object[] lobject = Arrays.copyOf(response.getValue(), response.getValue().length, Object[].class);
            List<Integer> lbytes = new ArrayList<Integer>(); for (Object o : lobject){lbytes.add((Integer)o);}
 
            String s = deviceclone.getIdentifier() + " - EYA << ";
            for (Integer value : lbytes)
            {
                s += " " + String.format("%02X", value.intValue());
            }
            System.out.println(s);
 
            int numberOfEtds = lbytes.get(0).intValue();
            int etdNumber = lbytes.get(1).intValue();
            if (etdNumber == deviceclone.getDeviceInformation().connectionEru.etdNumber)
            {
                // Message received corresponds to vms
                int[] information = new int[lbytes.size() - 2];
                for (int i=2; i<lbytes.size(); i++)
                {
                    information[i-2] = lbytes.get(i);
                }
 
                protocolAnalyser.analyseResponseEyA(deviceclone, information);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw e;
        }
    }
 
 
    private void readCurrentTrafficData(Etd deviceclone) throws Exception
    {
        try
        {
            InteropParameters parameters = new InteropParameters();
            parameters.addParameter(new InteropParameter("operation", "getEtdDup"));
            parameters.addParameter(new InteropParameter("language", "en-GB"));
            parameters.addParameter(new InteropParameter("device", deviceclone.getDeviceInformation().connectionEru.eru));
            parameters.addParameter(new InteropParameter("etd", "" + deviceclone.getDeviceInformation().connectionEru.etdNumber));
            InteropResponse response = (InteropResponse) Serialization.invoke("get", parameters, deviceclone.getDeviceInformation().connectionEru.address, deviceclone.getDeviceInformation().connectionEru.port, deviceclone.getDeviceInformation().connectionEru.timeout);
 
            Object[] lobject = Arrays.copyOf(response.getValue(), response.getValue().length, Object[].class);
            List<Integer> lbytes = new ArrayList<Integer>(); for (Object o : lobject){lbytes.add((Integer)o);}
 
            String s = deviceclone.getIdentifier() + " - DUP << ";
            for (Integer value : lbytes)
            {
                s += " " + String.format("%02X", value.intValue());
            }
            System.out.println(s);
 
            int numberOfEtds = lbytes.get(0).intValue();
            int etdNumber = lbytes.get(1).intValue();
            System.out.println(deviceclone.getIdentifier() + " 1.DUP: " +  numberOfEtds + "." + etdNumber + " - " + deviceclone.getDeviceInformation().connectionEru.etdNumber);
            if (etdNumber == deviceclone.getDeviceInformation().connectionEru.etdNumber)
            {
                // Message received corresponds to vms
                int[] information = new int[lbytes.size() - 2];
                for (int i=2; i<lbytes.size(); i++)
                {
                    information[i-2] = lbytes.get(i);
                }
 
                protocolAnalyser.analyseResponseDUP(deviceclone, information);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
    }
 
 
 
 
    private void offline(Etd etdclone)throws Exception
    {
        if ((device.alarms.alarm_offline <= 0) || (firstTime == true))
        {
            firstTime = false;
            String message = art.servers.Shared.getMessage("Etd offline");
            art.servers.Shared.println(name, message);
        }
 
        if (etdclone.getAlarm("alarm_offline") <= 0)
        {
            etdclone.getDeviceAlarms().clear();
        }
        etdclone.setAlarm("alarm_offline", true);
        Shared.model.updateDevice(device, etdclone);
    }
 
 
    private void online(Etd etdclone) throws Exception
    {
        if ((device.alarms.alarm_offline > 0) || (firstTime == true))
        {
            firstTime = false;
            String message = art.servers.Shared.getMessage("Etd online");
            art.servers.Shared.println(name, message);
        }
 
        etdclone.setAlarm("alarm_offline", false);
        etdclone.setAlarm("alarm_invalid", false);
        Shared.model.updateDevice(device, etdclone);
    }
 
}