Alejandro Acuña
2024-08-12 831c7bd85763b5eb5ef46664c65f0181824f9e13
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
package art.servers.etdserver.controller;
 
import art.library.interop.InteropParameters;
import art.library.interop.InteropResponse;
import art.library.interop.serialization.SerializationException;
import art.library.model.devices.Device;
import art.library.model.devices.etd.EtdRealtime;
import art.library.model.devices.etd.status.EtdStatusAggregation;
import art.library.model.devices.etd.status.EtdStatusVehicle;
import art.servers.ServerException;
import art.servers.etdserver.Shared;
import art.servers.etdserver.historical.HistoricalPeriods;
import java.util.List;
 
 
public class ListenerImplementation extends art.servers.controller.ListenerImplementation
{
    /**
     *  identifier = Etd identifier to get traffic data
     *  timestamp1 = Minimum timestamp of data to return
     *  timestamp2 = Maximum timestamp of data to return
     */
 
    public InteropResponse getRealtimeTrafficData(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            String identifier = (String)parameters.getParameterValue("identifier");
            long from = Long.parseLong((String)parameters.getParameterValue("timestamp1"));
            long to = Long.parseLong((String)parameters.getParameterValue("timestamp2"));
            return(new InteropResponse(getControllerEtd(identifier).getRealtimeTrafficData(from, to)));
        }
        catch (ServerException exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
 
 
 
    /**
     *  identifier = Etd identifier to get vehicles detected
     *  timestamp = Minimum timestamp of data to return
     */
    // http://gddkia-lublin-dk.pl:7016/get?operation=getVehicles&identifier=etd-łabuniereforma-1&timestamp=2018-09-18T08:00:00.000Z
    // http://172.16.11.205:7016/get?operation=getVehicles&identifier=etd-łabuniereforma-1&timestamp=2018-09-18T08:00:00.000Z
    // http://79.162.230.30:7016/get?operation=getVehicles&identifier=etd-łabuniereforma-1&timestamp=2018-11-21T09:00:00.000Z
    
    public InteropResponse getVehicles(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            String identifier = (parameters.hasParameter("etd") == true) ? (String)parameters.getParameterValue("etd") : null;
            if (identifier == null) identifier = (parameters.hasParameter("identifier") == true) ? (String)parameters.getParameterValue("identifier") : null;
            if (identifier == null) identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
            long timestamp = (parameters.hasParameter("timestamp") == true) ? getTimestamp((String)parameters.getParameterValue("timestamp")) : System.currentTimeMillis();
            ControllerEtd controller = getControllerEtd(identifier);
            if (controller.getEtd().getDeviceInformation().protocol.equalsIgnoreCase("DIAMOND-RAW") == true)
            {
                List<EtdStatusVehicle> lvehicle = controller.getVehicles(timestamp);
                InteropResponse response = new InteropResponse(lvehicle.toArray(new EtdStatusVehicle[0]));
                return(response);
            }
 
            return(new InteropResponse(new EtdRealtime()));
        }
        catch (ServerException exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
    
    
    // http://79.162.230.30:7016/get?operation=getAggregationData&identifier=etdtcp-rzeszów-2&from=2019-01-01T00:00:00.000Z&to=2019-01-02T00:00:00.000Z&lane=1&aggregationPeriod=60
    // http://79.162.230.30.pl:7016/get?operation=getAggregationData&identifier=etdtcp-rzeszów-2&from=2019-01-01T00:00:00.000Z&to=2019-01-02T00:00:00.000Z&lane=1&aggregationPeriod=3600
    
    
    public InteropResponse getAggregationData(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            String identifier = (parameters.hasParameter("etd") == true) ? (String)parameters.getParameterValue("etd") : null;
            if (identifier == null) identifier = (parameters.hasParameter("identifier") == true) ? (String)parameters.getParameterValue("identifier") : null;
            if (identifier == null) identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
 
            long timestamp1 = getTimestamp((String)parameters.getParameterValue("from"));
            long timestamp2 = getTimestamp((String)parameters.getParameterValue("to"));
            int lane = (parameters.hasParameter("lane") == true) ? Integer.parseInt((String)parameters.getParameterValue("lane")) : -1;
            int section = (parameters.hasParameter("section") == true) ? Integer.parseInt((String)parameters.getParameterValue("section")) : -1;
            int aggregationPeriod = Integer.parseInt((String)parameters.getParameterValue("aggregationPeriod"));
 
            System.out.println("1.GetAggregationData: " + identifier + " - " + Device.getDate(timestamp1) + " to " + Device.getDate(timestamp2) + " - Lane: " + lane + " - Section: " + section + " - Period: " + aggregationPeriod);
            HistoricalPeriods historical = new HistoricalPeriods(identifier, lane, section, timestamp1, timestamp2, aggregationPeriod, language);
            EtdStatusAggregation aggregation = historical.generate();
            System.out.println("2.GetAggregationData: " + identifier + " - " + Device.getDate(timestamp1) + " to " + Device.getDate(timestamp2) + " - Lane: " + lane + " - Section: " + section + " - Period: " + aggregationPeriod + " - " + aggregation.ldata.size());
            return(new InteropResponse(aggregation));
        }
        catch (ServerException exception)
        {
            exception.printStackTrace();
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()));
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
 
 
 
 
    private ControllerEtd getControllerEtd(String identifier) throws ServerException
    {
        return((ControllerEtd)Shared.getDeviceController(identifier));
    }
 
}