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

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

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

    if (!identify(parser.next(), channel, remoteAddress)) {
      return null;
    }
    position.setDeviceId(getDeviceId());

    position.set("command", parser.next());

    position.setValid(parser.next().equals("A"));

    DateBuilder dateBuilder =
        new DateBuilder()
            .setDate(parser.nextInt(16), parser.nextInt(16), parser.nextInt(16))
            .setTime(parser.nextInt(16), parser.nextInt(16), parser.nextInt(16));
    position.setTime(dateBuilder.getDate());

    if (BitUtil.check(parser.nextInt(16), 7)) {
      position.setLatitude(-parser.nextInt(16) / 600000.0);
    } else {
      position.setLatitude(parser.nextInt(16) / 600000.0);
    }

    if (BitUtil.check(parser.nextInt(16), 7)) {
      position.setLongitude(-parser.nextInt(16) / 600000.0);
    } else {
      position.setLongitude(parser.nextInt(16) / 600000.0);
    }

    position.setSpeed(parser.nextInt(16) / 100.0);
    position.setCourse(parser.nextInt(16) / 100.0);

    position.set(Event.KEY_STATUS, parser.next());
    position.set("signal", parser.next());
    position.set(Event.KEY_POWER, parser.nextDouble());
    position.set("oil", parser.nextInt(16));
    position.set(Event.KEY_ODOMETER, parser.nextInt(16));

    position.setAltitude(parser.nextDouble());

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

    String sentence = (String) msg;

    if (sentence.startsWith("!1")) {

      identify(sentence.substring(3, sentence.length()), channel, remoteAddress);

    } else if ((sentence.startsWith("!D") || sentence.startsWith("!A")) && hasDeviceId()) {

      Parser parser = new Parser(PATTERN, sentence);
      if (!parser.matches()) {
        return null;
      }

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

      DateBuilder dateBuilder =
          new DateBuilder()
              .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt())
              .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
      position.setTime(dateBuilder.getDate());

      position.setLatitude(parser.nextDouble());
      position.setLongitude(parser.nextDouble());
      position.setSpeed(parser.nextDouble());

      position.setCourse(parser.nextDouble());
      if (position.getCourse() > 360) {
        position.setCourse(0);
      }

      if (parser.hasNext(5)) {

        int flags = parser.nextInt(16);
        position.set(Event.KEY_FLAGS, flags);
        position.setValid(BitUtil.check(flags, 0));

        position.setAltitude(parser.nextDouble());

        position.set(Event.KEY_BATTERY, parser.next());
        position.set(Event.KEY_SATELLITES, parser.next());
      }

      return position;
    }

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

    ChannelBuffer buf = (ChannelBuffer) msg;
    int type = buf.readUnsignedShort();
    buf.readUnsignedShort(); // length

    if (type == MSG_IDENT || type == MSG_IDENT_FULL) {

      buf.readUnsignedInt(); // id
      int length = buf.readUnsignedShort();
      buf.skipBytes(length);
      length = buf.readUnsignedShort();
      buf.skipBytes(length);
      length = buf.readUnsignedShort();
      String imei = buf.readBytes(length).toString(Charset.defaultCharset());
      identify(imei, channel, remoteAddress);

    } else if (hasDeviceId() && (type == MSG_POINT || type == MSG_ALARM || type == MSG_LOGMSG)) {

      List<Position> positions = new LinkedList<>();

      int recordCount = 1;
      if (type == MSG_LOGMSG) {
        recordCount = buf.readUnsignedShort();
      }

      for (int j = 0; j < recordCount; j++) {
        Position position = new Position();
        position.setProtocol(getProtocolName());
        position.setDeviceId(getDeviceId());

        if (type == MSG_LOGMSG) {
          position.set(Event.KEY_ARCHIVE, true);
          int subtype = buf.readUnsignedShort();
          if (subtype == MSG_ALARM) {
            position.set(Event.KEY_ALARM, true);
          }
          if (buf.readUnsignedShort() > buf.readableBytes()) {
            lastIndex += 1;
            break; // workaround for device bug
          }
          lastIndex = buf.readUnsignedInt();
          position.set(Event.KEY_INDEX, lastIndex);
        } else {
          newIndex = buf.readUnsignedInt();
        }

        position.setTime(new Date(buf.readUnsignedInt() * 1000));
        position.setLatitude(buf.readInt() * 180.0 / 0x7FFFFFFF);
        position.setLongitude(buf.readInt() * 180.0 / 0x7FFFFFFF);
        position.setSpeed(buf.readUnsignedInt() * 0.01);
        position.setCourse(buf.readUnsignedShort() * 0.01);
        position.setAltitude(buf.readUnsignedShort() * 0.01);

        int satellites = buf.readUnsignedByte();
        position.setValid(satellites >= 3);
        position.set(Event.KEY_SATELLITES, satellites);

        position.set(Event.KEY_GSM, buf.readUnsignedByte());
        position.set(Event.KEY_ODOMETER, buf.readUnsignedInt());

        long extraFlags = buf.readLong();

        if (BitUtil.check(extraFlags, 0)) {
          int count = buf.readUnsignedShort();
          for (int i = 1; i <= count; i++) {
            position.set(Event.PREFIX_ADC + i, buf.readUnsignedShort());
          }
        }

        if (BitUtil.check(extraFlags, 1)) {
          int size = buf.readUnsignedShort();
          position.set("can", buf.toString(buf.readerIndex(), size, Charset.defaultCharset()));
          buf.skipBytes(size);
        }

        if (BitUtil.check(extraFlags, 2)) {
          position.set("passenger", ChannelBuffers.hexDump(buf.readBytes(buf.readUnsignedShort())));
        }

        if (type == MSG_ALARM) {
          position.set(Event.KEY_ALARM, true);
          byte[] response = {(byte) 0xC9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
          channel.write(ChannelBuffers.wrappedBuffer(response));
        }

        buf.readUnsignedInt(); // crc

        positions.add(position);
      }

      requestArchive(channel);

      return positions;
    }

    return null;
  }
  private ParseResult parsePosition(ChannelBuffer buf) {
    Position position = new Position();
    position.setProtocol(getProtocolName());

    position.setDeviceId(getDeviceId());

    int format;
    if (buf.getUnsignedByte(buf.readerIndex()) == 0) {
      format = buf.readUnsignedShort();
    } else {
      format = buf.readUnsignedByte();
    }
    position.set("format", format);

    long index = buf.readUnsignedInt();
    position.set(Event.KEY_INDEX, index);

    position.set(Event.KEY_EVENT, buf.readUnsignedShort());

    buf.skipBytes(6); // event time

    position.set(Event.KEY_ALARM, buf.readUnsignedByte());
    position.set(Event.KEY_STATUS, buf.readUnsignedByte());
    position.set(Event.KEY_GSM, buf.readUnsignedByte());

    if (isFormat(format, F10, F20, F30)) {
      position.set(Event.KEY_OUTPUT, buf.readUnsignedShort());
    } else if (isFormat(format, F40, F50, F51, F52)) {
      position.set(Event.KEY_OUTPUT, buf.readUnsignedByte());
    }

    if (isFormat(format, F10, F20, F30, F40)) {
      position.set(Event.KEY_INPUT, buf.readUnsignedShort());
    } else if (isFormat(format, F50, F51, F52)) {
      position.set(Event.KEY_INPUT, buf.readUnsignedByte());
    }

    position.set(Event.KEY_POWER, buf.readUnsignedShort() * 0.001);
    position.set(Event.KEY_BATTERY, buf.readUnsignedShort());

    if (isFormat(format, F10, F20, F30)) {
      position.set(Event.PREFIX_TEMP + 1, buf.readShort());
    }

    if (isFormat(format, F10, F20, F50, F52)) {
      position.set(Event.PREFIX_ADC + 1, buf.readUnsignedShort());
      position.set(Event.PREFIX_ADC + 2, buf.readUnsignedShort());
    }

    // Impulse counters
    if (isFormat(format, F20, F50, F51, F52)) {
      buf.readUnsignedInt();
      buf.readUnsignedInt();
    }

    if (isFormat(format, F20, F50, F51, F52)) {
      int locationStatus = buf.readUnsignedByte();
      position.setValid(BitUtil.check(locationStatus, 1));

      DateBuilder dateBuilder =
          new DateBuilder()
              .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
              .setDateReverse(
                  buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
      position.setTime(dateBuilder.getDate());

      position.setLatitude(buf.readFloat() / Math.PI * 180);
      position.setLongitude(buf.readFloat() / Math.PI * 180);
      position.setSpeed(buf.readFloat());
      position.setCourse(buf.readUnsignedShort());

      position.set(Event.KEY_ODOMETER, buf.readFloat());

      position.set("segment", buf.readFloat()); // last segment

      // Segment times
      buf.readUnsignedShort();
      buf.readUnsignedShort();
    }

    // Other
    if (isFormat(format, F51, F52)) {
      buf.readUnsignedShort();
      buf.readByte();
      buf.readUnsignedShort();
      buf.readUnsignedShort();
      buf.readByte();
      buf.readUnsignedShort();
      buf.readUnsignedShort();
      buf.readByte();
      buf.readUnsignedShort();
    }

    // Four temperature sensors
    if (isFormat(format, F40, F52)) {
      buf.readByte();
      buf.readByte();
      buf.readByte();
      buf.readByte();
    }

    return new ParseResult(index, position);
  }
  @Override
  protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg)
      throws Exception {

    ChannelBuffer buf = (ChannelBuffer) msg;

    buf.skipBytes(2); // header
    ChannelBuffer id = buf.readBytes(12);
    buf.readUnsignedByte(); // separator
    int type = buf.readUnsignedByte();
    buf.readUnsignedShort(); // length

    if (type == MSG_LOGIN) {

      int command = buf.readUnsignedByte(); // 0x00 - heartbeat

      if (command == 0x01) {
        String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII);
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
        if (deviceSession != null && channel != null) {
          ChannelBuffer response = ChannelBuffers.dynamicBuffer();
          response.writeByte(0x48);
          response.writeByte(0x52); // header
          response.writeBytes(id);
          response.writeByte(0x2c); // separator
          response.writeByte(type);
          response.writeShort(0x0002); // length
          response.writeShort(0x8000);
          response.writeShort(0x0000); // checksum
          channel.write(response);
        }
      }

    } else if (type == MSG_GPS) {

      DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
      if (deviceSession == null) {
        return null;
      }

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

      position.setTime(new Date(buf.readUnsignedInt() * 1000));

      int flags = buf.readUnsignedByte();

      position.setValid(true);
      position.setLatitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 0)));
      position.setLongitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 1)));

      position.setSpeed(buf.readUnsignedByte());
      position.setCourse(buf.readUnsignedByte());

      position.set(Position.KEY_LAC, buf.readUnsignedShort());
      position.set(Position.KEY_CID, buf.readUnsignedShort());

      return position;
    }

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

    ChannelBuffer buf = (ChannelBuffer) msg;

    buf.readUnsignedByte(); // header
    int length = (buf.readUnsignedShort() & 0x7fff) + 3;

    List<Position> positions = new LinkedList<>();
    Set<Integer> tags = new HashSet<>();
    boolean hasLocation = false;
    Position position = new Position();
    position.setProtocol(getProtocolName());

    while (buf.readerIndex() < length) {

      // Check if new message started
      int tag = buf.readUnsignedByte();
      if (tags.contains(tag)) {
        if (hasLocation && position.getFixTime() != null) {
          positions.add(position);
        }
        tags.clear();
        hasLocation = false;
        position = new Position();
      }
      tags.add(tag);

      switch (tag) {
        case TAG_IMEI:
          getDeviceSession(
              channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII));
          break;

        case TAG_DATE:
          position.setTime(new Date(buf.readUnsignedInt() * 1000));
          break;

        case TAG_COORDINATES:
          hasLocation = true;
          position.setValid((buf.readUnsignedByte() & 0xf0) == 0x00);
          position.setLatitude(buf.readInt() / 1000000.0);
          position.setLongitude(buf.readInt() / 1000000.0);
          break;

        case TAG_SPEED_COURSE:
          position.setSpeed(buf.readUnsignedShort() * 0.0539957);
          position.setCourse(buf.readUnsignedShort() * 0.1);
          break;

        case TAG_ALTITUDE:
          position.setAltitude(buf.readShort());
          break;

        case TAG_STATUS:
          int status = buf.readUnsignedShort();
          position.set(Position.KEY_IGNITION, BitUtil.check(status, 9));
          position.set(Position.KEY_ALARM, BitUtil.check(status, 15));
          position.set(Position.KEY_POWER, BitUtil.check(status, 2));
          break;

        case TAG_DIGITAL_INPUTS:
          int input = buf.readUnsignedShort();
          for (int i = 0; i < 16; i++) {
            position.set(Position.PREFIX_IO + (i + 1), BitUtil.check(input, i));
          }
          break;

        case TAG_DIGITAL_OUTPUTS:
          int output = buf.readUnsignedShort();
          for (int i = 0; i < 16; i++) {
            position.set(Position.PREFIX_IO + (i + 17), BitUtil.check(output, i));
          }
          break;

        case TAG_INPUT_VOLTAGE1:
          position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort() / 1000.0);
          break;

        case TAG_INPUT_VOLTAGE2:
          position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort() / 1000.0);
          break;

        case TAG_INPUT_VOLTAGE3:
          position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort() / 1000.0);
          break;

        case TAG_INPUT_VOLTAGE4:
          position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShort() / 1000.0);
          break;

        case TAG_XT1:
        case TAG_XT2:
        case TAG_XT3:
          buf.skipBytes(16);
          break;

        default:
          break;
      }
    }

    if (hasLocation && position.getFixTime() != null) {
      positions.add(position);
    }

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
      return null;
    }

    sendReply(channel, buf.readUnsignedShort());

    for (Position p : positions) {
      p.setDeviceId(deviceSession.getDeviceId());
    }

    if (positions.isEmpty()) {
      return null;
    }

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

    ChannelBuffer buf = (ChannelBuffer) msg;

    buf.readUnsignedShort(); // device id
    buf.readUnsignedByte(); // length

    int type = buf.readUnsignedByte();

    DeviceSession deviceSession;
    if (type == MSG_IMEI) {
      deviceSession =
          getDeviceSession(
              channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII));
    } else {
      deviceSession = getDeviceSession(channel, remoteAddress);
    }

    if (deviceSession == null) {
      return null;
    }

    if (BitUtil.to(type, 2) == 0) {

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

      buf.readUnsignedByte(); // firmware version
      buf.readUnsignedShort(); // index

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

      position.setValid(true);
      position.setLatitude(buf.readFloat());
      position.setLongitude(buf.readFloat());
      position.setCourse(buf.readUnsignedShort() * 0.1);
      position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() * 0.1));

      buf.readUnsignedByte(); // acceleration

      position.setAltitude(buf.readUnsignedShort());

      position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
      position.set(Position.KEY_SATELLITES, buf.readUnsignedByte() & 0x0f);

      position.setTime(new Date(buf.readUnsignedInt() * 1000));

      position.set(Position.KEY_POWER, buf.readUnsignedShort());
      position.set(Position.KEY_BATTERY, buf.readUnsignedShort());

      if (BitUtil.check(type, 2)) {
        buf.skipBytes(4);
      }

      if (BitUtil.check(type, 3)) {
        buf.skipBytes(12);
      }

      if (BitUtil.check(type, 4)) {
        buf.skipBytes(8);
      }

      if (BitUtil.check(type, 5)) {
        buf.skipBytes(9);
      }

      if (BitUtil.check(type, 6)) {
        buf.skipBytes(buf.getUnsignedByte(buf.readerIndex()));
      }

      if (BitUtil.check(type, 7)) {
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
      }

      return position;
    }

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

    ChannelBuffer buf = (ChannelBuffer) msg;

    buf.readUnsignedByte(); // protocol
    buf.readUnsignedShort(); // length
    int index = buf.readUnsignedByte() >> 3;

    if (channel != null) {
      ChannelBuffer response = ChannelBuffers.copiedBuffer("^" + index, Charset.defaultCharset());
      channel.write(response, remoteAddress);
    }

    String id = String.valueOf(buf.readUnsignedInt());
    if (!identify(id, channel, remoteAddress)) {
      return null;
    }

    List<Position> positions = new LinkedList<>();

    while (buf.readableBytes() > 2) {

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

      int end = buf.readerIndex() + buf.readUnsignedByte();

      position.setTime(new Date(buf.readUnsignedInt() * 1000));

      int flags = buf.readUnsignedByte();
      position.set(Event.KEY_SATELLITES, BitUtil.range(flags, 2));
      position.setValid(BitUtil.range(flags, 0, 2) > 0);

      // Latitude
      double lat = buf.readUnsignedMedium();
      lat = lat * -180 / 16777216 + 90;
      position.setLatitude(lat);

      // Longitude
      double lon = buf.readUnsignedMedium();
      lon = lon * 360 / 16777216 - 180;
      position.setLongitude(lon);

      // Status
      flags = buf.readUnsignedByte();
      position.set(Event.KEY_IGNITION, BitUtil.check(flags, 0));
      position.set(Event.KEY_GSM, BitUtil.range(flags, 2, 3));
      position.setCourse((BitUtil.range(flags, 5) * 45 + 180) % 360);

      // Speed
      int speed = buf.readUnsignedByte();
      if (speed < 250) {
        position.setSpeed(UnitsConverter.knotsFromKph(speed));
      }

      while (buf.readerIndex() < end) {

        int type = buf.readUnsignedByte();
        int length = buf.readUnsignedByte();
        if (length == 255) {
          length += buf.readUnsignedByte();
        }

        switch (type) {
          case 2:
            position.set(Event.KEY_ODOMETER, buf.readUnsignedMedium());
            break;
          case 5:
            position.set(Event.KEY_INPUT, buf.readUnsignedByte());
            break;
          case 6:
            {
              int n = buf.readUnsignedByte() >> 4;
              if (n < 2) {
                position.set(Event.PREFIX_ADC + n, readSwappedFloat(buf));
              } else {
                position.set("di" + (n - 2), readSwappedFloat(buf));
              }
            }
            break;
          case 7:
            {
              int alarm = buf.readUnsignedByte();
              buf.readUnsignedByte();
              if (BitUtil.check(alarm, 5)) {
                position.set(Event.KEY_ALARM, BitUtil.range(alarm, 0, 4));
              }
            }
            break;
          case 8:
            position.set("antihijack", buf.readUnsignedByte());
            break;
          case 9:
            position.set("authorized", ChannelBufferTools.readHexString(buf, 16));
            break;
          case 10:
            position.set("unauthorized", ChannelBufferTools.readHexString(buf, 16));
            break;
          case 24:
            {
              Set<Integer> temps = new LinkedHashSet<>();
              int temp = buf.readUnsignedByte();
              for (int i = 3; i >= 0; i--) {
                int n = (temp >> (2 * i)) & 0x03;
                if (!temps.contains(n)) {
                  temps.add(n);
                }
              }
              for (int n : temps) {
                position.set(Event.PREFIX_TEMP + n, buf.readUnsignedByte());
              }
            }
            break;
          case 28:
            position.set("weight", buf.readUnsignedShort());
            buf.readUnsignedByte();
            break;
          case 90:
            position.set(Event.KEY_POWER, readSwappedFloat(buf));
            break;
          case 101:
            position.set(Event.KEY_OBD_SPEED, buf.readUnsignedByte());
            break;
          case 102:
            position.set(Event.KEY_RPM, buf.readUnsignedByte() * 50);
            break;
          case 107:
            {
              int fuel = buf.readUnsignedShort();
              switch (fuel >> 14) {
                case 1:
                  position.set(Event.KEY_FUEL, (fuel & 0x3fff) * 0.4 + "%");
                  break;
                case 2:
                  position.set(Event.KEY_FUEL, (fuel & 0x3fff) * 0.5 + " l");
                  break;
                case 3:
                  position.set(Event.KEY_FUEL, (fuel & 0x3fff) * -0.5 + " l");
                  break;
              }
            }
            break;
          case 108:
            position.set(Event.KEY_OBD_ODOMETER, buf.readUnsignedInt() * 5);
            break;
          case 150:
            position.set("door", buf.readUnsignedByte());
            break;
          default:
            buf.skipBytes(length);
            break;
        }
      }

      positions.add(position);
    }

    return positions;
  }