30
30
import org .transitclock .db .structs .AvlReport .AssignmentType ;
31
31
import io .swagger .client .ApiClient ;
32
32
import io .swagger .client .api .DefaultApi ;
33
+ import io .swagger .client .model .Device ;
33
34
import io .swagger .client .model .Position ;
34
35
import io .swagger .client .model .User ;
35
36
@@ -47,6 +48,7 @@ public class TraccarAVLModule extends PollUrlAvlModule {
47
48
48
49
private User user = null ;
49
50
private DefaultApi api = null ;
51
+
50
52
51
53
private static StringConfigValue traccarEmail = new StringConfigValue ("transitclock.avl.traccar.email" , "admin" ,
52
54
"This is the username for the traccar server api." );
@@ -71,6 +73,7 @@ public TraccarAVLModule(String agencyId) throws Throwable {
71
73
client .setPassword (traccarPassword .getValue ());
72
74
api .setApiClient (client );
73
75
user = api .sessionPost (traccarEmail .getValue (), traccarPassword .getValue ());
76
+ user .getId ();
74
77
if (user != null )
75
78
logger .debug ("Traccar login succeeded." );
76
79
}
@@ -79,22 +82,44 @@ public TraccarAVLModule(String agencyId) throws Throwable {
79
82
protected void getAndProcessData () throws Exception {
80
83
81
84
Collection <AvlReport > avlReportsReadIn = new ArrayList <AvlReport >();
85
+
86
+ List <Device > devices = api .devicesGet (true , user .getId (), null , null );
87
+
82
88
if (api != null && user != null ) {
83
89
List <Position > results = api .positionsGet (null , null , null , null );
84
- for (Position result : results ) {
85
- logger .debug (result .toString ());
86
-
87
- AvlReport avlReport = new AvlReport (result .getDeviceId ().toString (),
90
+ for (Position result : results ) {
91
+ Device device =findDeviceById (devices , result .getDeviceId ());
92
+
93
+ AvlReport avlReport = null ;
94
+ // If have device details use name.
95
+ if (device !=null && device .getName ()!=null && !device .getName ().isEmpty ())
96
+ {
97
+ avlReport = new AvlReport (device .getName (),
98
+ result .getDeviceTime ().toDate ().getTime (), result .getLatitude ().doubleValue (),
99
+ result .getLongitude ().doubleValue (), traccarSource .toString ());
100
+ }
101
+ else
102
+ {
103
+ avlReport = new AvlReport (result .getDeviceId ().toString (),
88
104
result .getDeviceTime ().toDate ().getTime (), result .getLatitude ().doubleValue (),
89
105
result .getLongitude ().doubleValue (), traccarSource .toString ());
90
-
91
- avlReportsReadIn . add (avlReport );
92
- }
93
-
106
+ }
107
+ if (avlReport != null )
108
+ avlReportsReadIn . add ( avlReport );
109
+ }
94
110
forwardAvlReports (avlReportsReadIn );
95
111
}
96
112
}
97
-
113
+ private Device findDeviceById (List <Device > devices , Integer id )
114
+ {
115
+ for (Device device :devices )
116
+ {
117
+ if (device .getId ()==id )
118
+ return device ;
119
+ }
120
+ return null ;
121
+ }
122
+
98
123
@ Override
99
124
protected Collection <AvlReport > processData (InputStream in ) throws Exception {
100
125
// TODO Auto-generated method stub
0 commit comments