aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol/HuaShengProtocolDecoder.java
blob: 6743db47d0e320d0fcf016893f5e82f5585066a4 (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
/*
 * Copyright 2016 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 org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.helper.*;
import org.traccar.model.Position;

import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;

public class HuaShengProtocolDecoder extends BaseProtocolDecoder {

    public HuaShengProtocolDecoder(HuaShengProtocol protocol) {
        super(protocol);
    }

    public static final int MSG_POSITION = 0xAA00;
    public static final int MSG_POSITION_RSP = 0xFF01;
    public static final int MSG_LOGIN = 0xAA02;
    public static final int MSG_LOGIN_RSP = 0xFF03;

    private static void sendResponse(Channel channel, int type, ChannelBuffer content) {
        if (channel != null) {
            ChannelBuffer response = ChannelBuffers.dynamicBuffer();
            response.writeByte(0xC0);
            response.writeShort(0x0100);
            response.writeShort(12 + content.readableBytes());
            response.writeShort(type);
            response.writeShort(0);
            response.writeInt(1);
            response.writeBytes(content);
            response.writeByte(0xC0);
            channel.write(response);
        }
    }

    @Override
    protected Object decode(
            Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

        ChannelBuffer buf = (ChannelBuffer) msg;

        buf.skipBytes(1); // start marker
        buf.readUnsignedByte(); // flag
        buf.readUnsignedByte(); // reserved
        buf.readUnsignedShort(); // length

        int type = buf.readUnsignedShort();

        buf.readUnsignedShort(); // checksum
        int index = buf.readInt();

        if (type == MSG_LOGIN) {

            while (buf.readableBytes() > 4) {
                int subtype = buf.readUnsignedShort();
                int length = buf.readUnsignedShort() - 4;
                if (subtype == 0x0003) {
                    String imei = buf.readBytes(length).toString(StandardCharsets.US_ASCII);
                    if (identify(imei, channel, remoteAddress) && channel != null) {
                        ChannelBuffer content = ChannelBuffers.dynamicBuffer();
                        content.writeByte(0); // success
                        sendResponse(channel, MSG_LOGIN_RSP, content);
                    }
                } else {
                    buf.skipBytes(length);
                }
            }

        } else if (type == MSG_POSITION) {

            Position position = new Position();
            position.setProtocol(getProtocolName());
            position.setDeviceId(getDeviceId());

            position.set(Position.KEY_STATUS, buf.readUnsignedShort());
            position.set(Position.KEY_EVENT, buf.readUnsignedShort());

            String time = buf.readBytes(12).toString(StandardCharsets.US_ASCII);

            DateBuilder dateBuilder = new DateBuilder()
                    .setYear(Integer.valueOf(time.substring(0, 2)))
                    .setMonth(Integer.valueOf(time.substring(2, 4)))
                    .setDay(Integer.valueOf(time.substring(4, 6)))
                    .setHour(Integer.valueOf(time.substring(6, 8)))
                    .setMinute(Integer.valueOf(time.substring(8, 10)))
                    .setSecond(Integer.valueOf(time.substring(10, 12)));
            position.setTime(dateBuilder.getDate());

            position.setLongitude(buf.readInt() * 0.00001);
            position.setLatitude(buf.readInt() * 0.00001);

            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
            position.setCourse(buf.readUnsignedShort());
            position.setAltitude(buf.readUnsignedShort());

            position.set(Position.KEY_ODOMETER, buf.readUnsignedShort());

            while (buf.readableBytes() > 4) {
                buf.readUnsignedShort(); // subtype
                int length = buf.readUnsignedShort() - 4;
                buf.skipBytes(length);
            }

            ChannelBuffer content = ChannelBuffers.dynamicBuffer();
            content.writeInt(index);
            sendResponse(channel, MSG_POSITION_RSP, content);

            return position;

        }

        return null;
    }

}