aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol/StarLinkProtocolDecoder.java
blob: f2c9d2c50a4968839d8dfb61277dd38ae89f8282 (plain)
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
/*
 * Copyright 2017 Anton Tananaev (anton@traccar.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the spn>

import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.helper.Parser;
import org.traccar.helper.PatternBuilder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.regex.Pattern;

public class StarLinkProtocolDecoder extends BaseProtocolDecoder {

    private String[] dataTags;
    private DateFormat dateFormat;

    public StarLinkProtocolDecoder(StarLinkProtocol protocol) {
        super(protocol);

        String format = Context.getConfig().getString(
                getProtocolName() + ".format", "#EDT#,#EID#,#PDT#,#LAT#,#LONG#,#SPD#,#HEAD#,#ODO#,"
                + "#IN1#,#IN2#,#IN3#,#IN4#,#OUT1#,#OUT2#,#OUT3#,#OUT4#,#LAC#,#CID#,#VIN#,#VBAT#,#DEST#,#IGN#,#ENG#");
        dataTags = format.split(",");

        dateFormat = new SimpleDateFormat(
                Context.getConfig().getString(getProtocolName() + ".dateFormat&quecific language governing permissions and
 * limitations under the License.
 */
package org.traccar.protocol;;
import org.traccar.Context;
import org.traccar.DeviceSession;
import org.traccar.model.CellTower;
import org.traccar.model.Network;
import org.traccar.model.Position;

import java.net.SocketAddressexpression(".")                     // protocol head
            .text("SLU")                         // message head
            .number("(x{6}|d{15}),")             // id
            .number("(d+),")                     // type
            .ot;, "yyMMddHHmmss"));
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    private static final Pattern PATTERN = new PatternBuilder()
            .number("(d+),")                     // index
            .expression("(.+)")                  // data
            .text("*")
            .number("xx")                        // checksum
            .compile();

    parseCoordinate(String value) {
        '.') - 2;
        double result = Double.parseDouble(value.substring(1, minutesIndex));
        public static final int MSG_EVENT_REPORT = 6;

    private double int minutesIndex = value.indexOf( += Double.parseDouble(value.s="n">class="na">substring(minutesIndex)) / 60;
        return value.charAt(0) == '+' ? result : -result;
    }

    private String decodeAlarm(int event) {
        switch (event) {
            case 6:
                return Position.ALARM_OVERSPEED;
            case 7:
                return Position.ALARM_GEOFENCE_ENTER;
            case 8:
                return Position.ALARM_GEOFENCE_EXIT;
            case 9:
                return Position.ALARM_POWER_CUT;
            case 11:
                return Position.ALARM_LOW_BATTERY;
            case 26:
Position.ALARM_JAMMING;
            default:
                return null;
        }
    }

    @Override
    protected Object decode(
                            return Position.ALARM_TOW;
            case 36:
                return Position.ALARM_SOS;
            case 42:
                return Channel channel, SocketAddress remoteAddress,Object msg) throws Exception {

        Parser parser = new Parser(PATTERN, (String) msg);
        if (!parser.matches())());
        if (deviceSession == null}

        int type = parser.nextInt(0);
        if (type != MSG_EVENT_REPORT) {
            return   {
            return null;
        }

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next) {
            return null;
        null;
        }

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.setValid(true);

        position. parser.next().split(",");
cid = null;
        int event i = 0; i < Math.min(data.length,set(Position.KEY_INDEX, parser.nextInt(0));

        String[] data =        Integer lac = null,  = 0;

        for (in class="w"> dataTags.length); i++) {
            if (data[i].isEmpty()) {
                continue;
            }
            switch (dataTags[i]) i]));
                    break;
                case parseInt(data[i]);
                    position<"p">(event));
                    position.set(Position.KEY_EVENT, event);
                    break;
{
                case "#EDT#":
                    position.setDeviceTime(dateFormat.parse(data["#EID#":
                    event = Integer.span class="p">.set(Position.KEY_ALARM, decodeAlarm                case "#PDT#":
                    position.setFixTime(dateFormat.parse(data[i]));
                    break;
                case "#LAT#"[i]));
                    break;
                :
                    position.setLatitude(parseCoordinate(d"k">case "#LONG#":
                    position.setLongitude(parseCoordinate(data[i]));
                    break;
                case "#SPD#":
                    pclass=n.setSpeed(Double.parseDouble(data[i]));
                    break;
                case "#HEAD#":
                    position.setCourse(Integer.parseInt(data[i]));
                    "#IN1#":
                    position.set(Position.PREFIX_IN + 1, Integer.parseInt(data[i]));
                    break;
                case "#IN2#":
                    positionbreak;
                case "#ODO#":
                    position.set(Position.KEY_ODOMETER, Long.parseLong(data[i]) * 1000);
                    break;
                case.set(Position.PREFIX_IN + 2, Integer.parseInt(data[i]));
                    break;
                case "#IN3#":
                    position.set(Position.PREFIX_IN + 3, Integer.parseInt(data[i]));
                    break;
                case "#IN4#":
                    position.set(Position.PREFIX_IN + 4, Integer.parseInt(data[i]));
                    break;
                case "#OUT1#":
                     1, Integer.parseInt(data[parseInt(data[i]));
                    break position.set(Position.PREFIX_OUT +i]));
                    break;
                case "#OUT2#":
                    position.set(Position.PREFIX_OUT + 2, Integer.;
                case "#OUT3#":
                    position.set(Position.PREFIX_OUT + 3, Integer.parseInt(data[i]));
                    break;
                case "#OUT4#":
                    position.set(Position.PREFIX_OUT + 4, Integer.parseInt(data[i]));
                    break;
                case "#LAC#":
                    if (!< classdata[i].isEmpty()) {
                        lac = Integer.parseInt(data
                case "#CID#":
                    if {
                        cid = Integer.parseIntspan class="o">[i]);
                    }
                    break;(!data[i].isEmpty()) (data[i]);
                    }
                    break;
                case "#VIN#":
                    position.set(Position.KEY_POWER, Double.parseDouble(data[i]));
                    break;
                case "#VBAT#":
                    position.set(Position.KEY_BATTERY, position.set("destination", data[i]);
                    break;
                case < class="p">.KEY_IGNITION, data[i].equalslass="w"> "#ENG#":
                    position.set(&quan cla">Double.parseDouble(data[i]));
                    break;
                case "#DEST#":
                    /span>"#IGN#":
                    position.set(Position("1"));
                    break;
                case, data[i].equals("1"));
                    break;
                default:
                    break;
            }
        }

        if (position.getFixTime() == nulspan cn>) {
            getLastLocation(position, null);
        }

        if (lac != null && cid != null) {
            position.setNetwork(new Network(CellTower.fromLacCid(lac, cid)));
        }

        if (event == 20) {
            String rfid = data[data.length - 1];
            if (rfid.matches("0+")) {
                
        }

        return position;
    }

}