package org.traccar.protocol;

import java.nio.ByteOrder;
import java.nio.charset.Charset;
import org.jboss.netty.buffer.ChannelBuffers;
import org.traccar.helper.TestDataManager;
import org.junit.Test;
import org.traccar.helper.ChannelBufferTools;
import static org.traccar.helper.DecoderVerifier.verify;

public class MeitrackProtocolDecoderTest {

    @Test
    public void testDecode() throws Exception {

        MeitrackProtocolDecoder decoder = new MeitrackProtocolDecoder(null);
        decoder.setDataManager(new TestDataManager());
        
        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$X138,862170010187175,AAA,35,-29.960365,-51.655455,130507201625,A,8,9,0,107,0.9,7,169322,126582,724|6|0547|132B,0000,0009|000A||0278|0000,*BE", Charset.defaultCharset())));
        
        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$X138,862170010187175,AAA,35,-29.960365,-51.655455,130507201625,A,8,9,0,107,0.9,-7,169322,126582,724|6|0547|132B,0000,0009|000A||0278|0000,*BE", Charset.defaultCharset())));

        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$]138,012896000475498,AAA,35,-6.138255,106.910545,121205074600,A,5,18,0,0,0,49,3800,24826,510|10|0081|4F4F,0000,0011|0012|0010|0963|0000,,*94", Charset.defaultCharset())));

        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$d138,012896000475498,AAA,35,-6.138255,106.910545,121205074819,A,7,18,0,0,0,49,3800,24965,510|10|0081|4F4F,0000,000D|0010|0012|0963|0000,,*BF", Charset.defaultCharset())));

        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$j138,012896000475498,AAA,35,-6.138306,106.910655,121205103708,A,3,11,0,0,1,36,4182,35025,510|10|0081|4F4F,0000,000A|000C|000A|0915|0000,,*BF", Charset.defaultCharset())));

        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$m139,012896005334567,AAA,35,-33.866423,151.190060,121208020649,A,7,27,0,32,4,13,6150,49517,505|2|0B67|5A6C,0000,0000|0000|0000|0977|0000,,*F1", Charset.defaultCharset())));

        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$A141,012896005334567,AAA,35,-33.866543,151.190148,121209081758,A,6,27,0,16,1,48,65551,152784,505|2|0B5F|D9D3,0000,0000|0000|0000|0A39|0000,,*5B", Charset.defaultCharset())));
        
        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$_128,861074020109479,AAA,34,22.512618,114.057065,090215000318,V,0,31,0,0,0,0,0,733,302|720|3EE4|BBB5,0000,0006|0006||028C|0000,*E3", Charset.defaultCharset())));
        
        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$K146,013227004985762,AAA,35,28.618005,-81.246783,131101213828,A,9,22,0,209,1.1,23,80974,1187923,310|260|2A13|634E,0000,0000|0000|0000|09DA|0B34,,*51", Charset.defaultCharset())));
        
        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$E150,013777001165479,AAA,35,10.296601,123.872115,140501161505,A,4,22,1,170,1.4,77,39097,393563,515|3|A0CC|ED96,0000,0008|0003|0000|09D5|0000,,,1,0009*1E", Charset.defaultCharset())));

        verify(decoder.decode(null, null, ChannelBuffers.copiedBuffer(
                "$$B140,013777001293701,AAA,35,-7.266760,112.743550,140521095314,A,3,22,0,275,2.7,45,1984,8059,510|1|3504|EBFE,0000,0000|0000|0000|0914|0002,,*F9\r\n", Charset.defaultCharset())));

        int[] buf1 = {0x24,0x24,0x47,0x39,0x37,0x30,0x2c,0x33,0x36,0x39,0x38,0x30,0x30,0x30,0x31,0x33,0x34,0x36,0x30,0x36,0x37,0x34,0x2c,0x43,0x43,0x43,0x2c,0x02,0x01,0x34,0x00,0x5b,0x00,0x00,0x00,0x01,0x0c,0xe3,0x04,0x03,0x5d,0xb9,0xe0,0x00,0xec,0x6f,0x59,0x1a,0x00,0x00,0x13,0x00,0x00,0x00,0x00,0x0c,0x00,0x18,0x01,0xed,0xb7,0x02,0x00,0xc9,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x01,0x00,0x03,0x00,0xa1,0x01,0xc2,0x04,0x00,0x00,0x00,0x00,0x01,0x0c,0xe3,0x04,0x03,0x5d,0xb9,0xe0,0x00,0xee,0x6f,0x59,0x1a,0x00,0x00,0x13,0x00,0x00,0x00,0x00,0x0c,0x00,0x18,0x01,0xed,0xb7,0x02,0x00,0xca,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x01,0x00,0x03,0x00,0xa1,0x01,0xc2,0x04,0x00,0x00,0x00,0x00,0x01,0x0c,0xe3,0x04,0x03,0x5d,0xb9,0xe0,0x00,0xef,0x6f,0x59,0x1a,0x00,0x00,0x13,0x00,0x00,0x00,0x00,0x0c,0x00,0x18,0x01,0xed,0xb7,0x02,0x00,0xcc,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x01,0x00,0x03,0x00,0xa1,0x01,0xc2,0x04,0x00,0x00,0x00,0x00,0x02,0x0c,0xe3,0x04,0x03,0x5d,0xb9,0xe0,0x00,0xf7,0x6f,0x59,0x1a,0x00,0x00,0x16,0x00,0x00,0x00,0x00,0x0c,0x00,0x18,0x01,0xed,0xb7,0x02,0x00,0xd3,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa1,0x01,0xbf,0x04,0x00,0x00,0x00,0x00,0x0a,0x0c,0xe3,0x04,0x03,0x5d,0xb9,0xe0,0x00,0xf7,0x6f,0x59,0x1a,0x00,0x00,0x16,0x00,0x00,0x00,0x00,0x0c,0x00,0x18,0x01,0xed,0xb7,0x02,0x00,0xd4,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x00,0x00,0x03,0x00,0xa1,0x01,0xbf,0x04,0x00,0x00,0x00,0x00,0x02,0x0c,0xe3,0x04,0x03,0x5d,0xb9,0xe0,0x00,0xfb,0x6f,0x59,0x1a,0x00,0x00,0x16,0x00,0x00,0x00,0x00,0x0c,0x00,0x18,0x01,0xed,0xb7,0x02,0x00,0xd8,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa1,0x01,0x76,0x04,0x00,0x00,0x00,0x00,0x18,0x0c,0xe3,0x04,0x03,0x5d,0xb9,0xe0,0x00,0xfc,0x6f,0x59,0x1a,0x00,0x00,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x8c,0x00,0xed,0xb7,0x02,0x00,0xd9,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa1,0x01,0x76,0x04,0x00,0x00,0x00,0x00,0x19,0xb1,0xe2,0x04,0x03,0x23,0xb9,0xe0,0x00,0x0b,0x70,0x59,0x1a,0x01,0x05,0x15,0x06,0x00,0xbb,0x00,0x12,0x00,0x29,0x01,0xed,0xb7,0x02,0x00,0xe7,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x70,0x05,0x00,0x00,0x00,0x00,0x20,0x23,0xe3,0x04,0x03,0x1f,0xb9,0xe0,0x00,0x10,0x70,0x59,0x1a,0x01,0x06,0x15,0x07,0x00,0x27,0x01,0x0d,0x00,0x16,0x01,0xfc,0xb7,0x02,0x00,0xec,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x80,0x05,0x00,0x00,0x00,0x00,0x20,0x1f,0xe3,0x04,0x03,0x02,0xb9,0xe0,0x00,0x11,0x70,0x59,0x1a,0x01,0x06,0x15,0x09,0x00,0x19,0x01,0x0d,0x00,0x15,0x01,0xfe,0xb7,0x02,0x00,0xed,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x80,0x05,0x00,0x00,0x00,0x00,0x20,0x18,0xe3,0x04,0x03,0xdc,0xb8,0xe0,0x00,0x12,0x70,0x59,0x1a,0x01,0x06,0x15,0x0b,0x00,0x11,0x01,0x0d,0x00,0x15,0x01,0x00,0xb8,0x02,0x00,0xee,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x80,0x05,0x00,0x00,0x00,0x00,0x20,0x36,0xe3,0x04,0x03,0x45,0xb8,0xe0,0x00,0x15,0x70,0x59,0x1a,0x01,0x07,0x15,0x0b,0x00,0x2d,0x01,0x0b,0x00,0x13,0x01,0x0a,0xb8,0x02,0x00,0xf1,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x80,0x05,0x00,0x00,0x00,0x00,0x20,0x53,0xe3,0x04,0x03,0x26,0xb8,0xe0,0x00,0x16,0x70,0x59,0x1a,0x01,0x07,0x15,0x0d,0x00,0x41,0x01,0x0b,0x00,0x13,0x01,0x0e,0xb8,0x02,0x00,0xf2,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x80,0x05,0x00,0x00,0x00,0x00,0x20,0x70,0xe3,0x04,0x03,0x10,0xb8,0xe0,0x00,0x17,0x70,0x59,0x1a,0x01,0x07,0x15,0x0e,0x00,0x4f,0x01,0x0b,0x00,0x13,0x01,0x11,0xb8,0x02,0x00,0xf3,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x80,0x05,0x00,0x00,0x00,0x00,0x20,0x95,0xe3,0x04,0x03,0x06,0xb8,0xe0,0x00,0x18,0x70,0x59,0x1a,0x01,0x07,0x15,0x0d,0x00,0x5a,0x01,0x0b,0x00,0x14,0x01,0x15,0xb8,0x02,0x00,0xf4,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x80,0x05,0x00,0x00,0x00,0x00,0x20,0xb3,0xe3,0x04,0x03,0x05,0xb8,0xe0,0x00,0x19,0x70,0x59,0x1a,0x01,0x07,0x15,0x0b,0x00,0x60,0x01,0x0b,0x00,0x14,0x01,0x18,0xb8,0x02,0x00,0xf5,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x83,0x05,0x00,0x00,0x00,0x00,0x20,0xcf,0xe3,0x04,0x03,0x08,0xb8,0xe0,0x00,0x1a,0x70,0x59,0x1a,0x01,0x07,0x15,0x0b,0x00,0x66,0x01,0x0b,0x00,0x14,0x01,0x1b,0xb8,0x02,0x00,0xf6,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x83,0x05,0x00,0x00,0x00,0x00,0x20,0xee,0xe3,0x04,0x03,0x0c,0xb8,0xe0,0x00,0x1b,0x70,0x59,0x1a,0x01,0x06,0x17,0x0b,0x00,0x04,0x00,0x0d,0x00,0x14,0x01,0x1e,0xb8,0x02,0x00,0xf7,0x6d,0x01,0x00,0xe6,0x00,0x01,0x00,0x48,0x38,0x57,0x65,0x02,0x00,0x03,0x00,0xa2,0x01,0x83,0x05,0x00,0x00,0x00,0x00,0x2a,0x62,0x35,0x0d,0x0a};
        verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertArray(buf1))));
        
        //int[] buf2 = {0x24,0x24,0x47,0x39,0x37,0x30,0x2C,0x33,0x36,0x39,0x38,0x30,0x30,0x30,0x31,0x33,0x32,0x32,0x30,0x30,0x32,0x30,0x2C,0x43,0x43,0x43,0x2C,0x02,0x01,0x34,0x00,0x54,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0xF5,0x74,0xA4,0x19,0x00,0x06,0x14,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x81,0x08,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x79,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x04,0x00,0x00,0x80,0x04,0x00,0x00,0x80,0x04,0x00,0x00,0x80,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xEE,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0xCD,0x7A,0xA4,0x19,0x00,0x06,0x14,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x26,0x0D,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x78,0x01,0xA4,0x03,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x25,0x7D,0xA4,0x19,0x00,0x06,0x17,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x7E,0x0F,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x77,0x01,0xA4,0x03,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x7E,0x7F,0xA4,0x19,0x00,0x00,0x16,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0xD6,0x11,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x77,0x01,0xA6,0x03,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0xD6,0x81,0xA4,0x19,0x00,0x00,0x17,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x2E,0x14,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x76,0x01,0xA6,0x03,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x2E,0x84,0xA4,0x19,0x00,0x00,0x15,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x86,0x16,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x75,0x01,0xA6,0x03,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x7A,0x83,0xA5,0x19,0x00,0x06,0x0E,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x65,0x19,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0xEA,0xB7,0x00,0x00,0x00,0x00,0x73,0x01,0xA4,0x03,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0xD2,0x85,0xA5,0x19,0x00,0x06,0x0E,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0xBD,0x1B,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x71,0x01,0xA2,0x03,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x2B,0x88,0xA5,0x19,0x00,0x06,0x0F,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x15,0x1E,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x70,0x01,0xA2,0x03,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x83,0x8A,0xA5,0x19,0x00,0x06,0x13,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x6D,0x20,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x70,0x01,0xA6,0x03,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0xDB,0x8C,0xA5,0x19,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0xC5,0x22,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x6E,0x01,0xA6,0x03,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x34,0x8F,0xA5,0x19,0x00,0x00,0x16,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x1D,0x25,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x64,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0xD9,0xD9,0xA5,0x19,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x78,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x54,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x2D,0xDC,0xA5,0x19,0x00,0x06,0x16,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0xCC,0x2A,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x02,0x00,0x00,0x00,0x7B,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x0A,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x5B,0xDD,0xA5,0x19,0x00,0x06,0x13,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0xF9,0x2B,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x00,0x00,0x00,0x00,0x7C,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x5C,0xDD,0xA5,0x19,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0xFB,0x2B,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x02,0x00,0x00,0x00,0x7C,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x0E,0x88,0x57,0x01,0x22,0x5F,0xCC,0x06,0x85,0xDE,0xA5,0x19,0x00,0x00,0x13,0x00,0x00,0x00,0x00,0x12,0x00,0x28,0x00,0x12,0x39,0x0C,0x00,0x24,0x2D,0x00,0x00,0xCC,0x01,0x00,0x00,0x62,0x30,0x54,0x46,0x02,0x00,0x00,0x00,0x7D,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x2A,0x35,0x62,0x0D,0x0A};
        //verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertArray(buf2))));
        
    }

}