aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol/SyrusProtocolDecoder.java
blob: 56975d09dfc3709c2e580f3c30ce5aa2707d6a83 (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
/*
 * Copyright 2013 - 2014 Anton Tananaev (anton.tananaev@gmail.com)
 *
 * 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 specific language governing permissions and
 * limitations under the License.
 */
package org.traccar.protocol;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;

import org.traccar.BaseProtocolDecoder;
import org.traccar.model.Position;

public class SyrusProtocolDecoder extends BaseProtocolDecoder {
    
    boolean sendResponse;

    public SyrusProtocolDecoder(String protocol, boolean sendResponse) {
        super(protocol);
        this.sendResponse = sendResponse;
    }

    private static final Pattern pattern = Pattern.compile(
            "R[EP]V" +                     // Type
            "(?:\\d{2}" +                  // Event index
            "(\\d{4})" +                   // Week
            "(\\d))?" +                    // Day
            "(\\d{5})" +                   // Seconds
            "([\\+\\-]\\d{2})(\\d{5})" +   // Latitude
            "([\\+\\-]\\d{3})(\\d{5})" +   // Longitude
            "(\\d{3})" +                   // Speed
            "(\\d{3})" +                   // Course
            "\\d" +                        // Fix mode
            "(\\d)" +                      // Fix age
            ".*\r?\n?");

    private Date getTime(long week, long day, long seconds) {
        Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        time.clear();
        time.set(Calendar.YEAR, 1980);
        time.set(Calendar.MONTH, 0);
        time.set(Calendar.DAY_OF_MONTH, 6);

        long millis = time.getTimeInMillis();
        millis += ((week * 7 + day) * 24 * 60 * 60 + seconds) * 1000;

        return new Date(millis);
    }
    
    private Date getTime(long seconds) {
        Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        time.set(Calendar.HOUR_OF_DAY, 0);
        time.set(Calendar.MINUTE, 0);
        time.set(Calendar.SECOND, 0);
        time.set(Calendar.MILLISECOND, 0);
        
        long millis = time.getTimeInMillis() + seconds * 1000;
        
        long diff = new Date().getTime() - millis;
        
        if (diff > 12 * 60 * 60 * 1000) {
            millis += 24 * 60 * 60 * 1000;
        } else if (diff < -12 * 60 * 60 * 1000) {
            millis -= 24 * 60 * 60 * 1000;
        }

        return new Date(millis);
    }
    
    @Override
    protected Object decode(
            ChannelHandlerContext ctx, Channel channel, Object msg)
            throws Exception {

        String sentence = (String) msg;
        
        // Find message start
        int beginIndex = sentence.indexOf('>');
        if (beginIndex != -1) {
            sentence = sentence.substring(beginIndex + 1);
        }

        // Find device ID
        beginIndex = sentence.indexOf(";ID=");
        if (beginIndex != -1) {
            beginIndex += 4;
            int endIndex = sentence.indexOf(';', beginIndex);
            if (endIndex == -1) {
                endIndex = sentence.length();
            }

            // Find device in database
            String id = sentence.substring(beginIndex, endIndex);
            if (!identify(id)) {
                return null;
            }

            // Send response
            if (sendResponse && channel != null) {
                channel.write(id);
            }
        } else {
            return null;
        }

        // Parse message
        Matcher parser = pattern.matcher(sentence);
        if (!parser.matches()) {
            return null;
        }

        // Create new position
        Position position = new Position();
        position.setProtocol(getProtocol());
        position.setDeviceId(getDeviceId());

        Integer index = 1;
        
        // Time
        String week = parser.group(index++);
        String day = parser.group(index++);
        int seconds = Integer.valueOf(parser.group(index++));
        if (week != null && day != null) {
            position.setTime(getTime(Integer.valueOf(week), Integer.valueOf(day), seconds));
        } else {
            position.setTime(getTime(seconds));
        }

        // Latitude
        String latitude = parser.group(index) + '.' + parser.group(index + 1);
        index += 2;
        position.setLatitude(Double.valueOf(latitude));

        // Latitude
        String longitude = parser.group(index) + '.' + parser.group(index + 1);
        index += 2;
        position.setLongitude(Double.valueOf(longitude));
        
        // Speed and Course
        position.setSpeed(Double.valueOf(parser.group(index++)) * 0.868976);
        position.setCourse(Double.valueOf(parser.group(index++)));
        
        // Validity
        position.setValid(Integer.valueOf(parser.group(index++)) == 2);
        return position;
    }

}