/*
 * Copyright 2013 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.LinkedList;
import java.util.List;
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.ServerManager;
import org.traccar.helper.Log;
import org.traccar.model.ExtendedInfoFormatter;
import org.traccar.model.Position;

public class Tlt2hProtocolDecoder extends BaseProtocolDecoder {

    public Tlt2hProtocolDecoder(ServerManager serverManager) {
        super(serverManager);
    }

    static private Pattern pattern = Pattern.compile(
            "#([0-9a-f]+)?" +              // Cell info
            "\\$GPRMC," +
            "(\\d{2})(\\d{2})(\\d{2})\\.(\\d+)," + // Time (HHMMSS.SSS)
            "([AV])," +                    // Validity
            "(\\d+)(\\d{2}\\.\\d+)," +     // Latitude (DDMM.MMMM)
            "([NS])," +
            "(\\d+)(\\d{2}\\.\\d+)," +     // Longitude (DDDMM.MMMM)
            "([EW])," +
            "(\\d+\\.\\d{2})?," +          // Speed
            "(\\d+\\.\\d{2})?," +          // Course
            "(\\d{2})(\\d{2})(\\d{2})" +   // Date (DDMMYY)
            ".+");                         // Other (Checksumm)

    @Override
    protected Object decode(
            ChannelHandlerContext ctx, Channel channel, Object msg)
            throws Exception {

        String sentence = (String) msg;

        // Get device identifier
        String imei = sentence.substring(1, sentence.indexOf('#', 1));
        long deviceId;
        try {
            deviceId = getDataManager().getDeviceByImei(imei).getId();
        } catch(Exception error) {
            Log.warning("Unknown device - " + imei);
            return null;
        }
        
        String[] messages = sentence.substring(sentence.indexOf('\n') + 1).split("\r\n");
        List<Position> positions = new LinkedList<Position>();
        
        for (String message : messages) {
            Matcher parser = pattern.matcher(message);
            if (parser.matches()) {
                Position position = new Position();
                ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("tlt2h");
                position.setDeviceId(deviceId);

                Integer index = 1;
                
                // Cell
                extendedInfo.set("cell", parser.group(index++));

                // Time
                Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                time.clear();
                time.set(Calendar.HOUR, Integer.valueOf(parser.group(index++)));
                time.set(Calendar.MINUTE, Integer.valueOf(parser.group(index++)));
                time.set(Calendar.SECOND, Integer.valueOf(parser.group(index++)));
                index += 1; // Skip milliseconds

                // Validity
                position.setValid(parser.group(index++).compareTo("A") == 0 ? true : false);

                // Latitude
                Double latitude = Double.valueOf(parser.group(index++));
                latitude += Double.valueOf(parser.group(index++)) / 60;
                if (parser.group(index++).compareTo("S") == 0) latitude = -latitude;
                position.setLatitude(latitude);

                // Longitude
                Double longitude = Double.valueOf(parser.group(index++));
                longitude += Double.valueOf(parser.group(index++)) / 60;
                if (parser.group(index++).compareTo("W") == 0) longitude = -longitude;
                position.setLongitude(longitude);

                // Speed
                String speed = parser.group(index++);
                if (speed != null) {
                    position.setSpeed(Double.valueOf(speed));
                } else {
                    position.setSpeed(0.0);
                }

                // Course
                String course = parser.group(index++);
                if (course != null) {
                    position.setCourse(Double.valueOf(course));
                } else {
                    position.setCourse(0.0);
                }

                // Date
                time.set(Calendar.DAY_OF_MONTH, Integer.valueOf(parser.group(index++)));
                time.set(Calendar.MONTH, Integer.valueOf(parser.group(index++)) - 1);
                time.set(Calendar.YEAR, 2000 + Integer.valueOf(parser.group(index++)));
                position.setTime(time.getTime());

                // Altitude
                position.setAltitude(0.0);
                
                position.setExtendedInfo(extendedInfo.toString());
                positions.add(position);
            }
        }

        return positions;
    }

}

@@ -1301,16 +1301,298 @@
  pattern
+Header = Pattern.compile(%0A            %22#(%5C%5Cd+)#%22 +                   // IMEI%0A            %22%5B%5E#%5D+#%22 +%0A            %22%5C%5Cd+#%22 +%0A            %22(%5B%5E#%5D+)#%22 +                   // Status%0A            %22%5C%5Cd+%22);                       // Number of records%0A%0A    static private Pattern patternPosition
  = Patte
@@ -2425,27 +2425,19 @@
  // 
-Get device identifi
+Decode head
 er%0A 
@@ -2450,20 +2450,22 @@
  String 
-imei
+header
  = sente
@@ -2478,17 +2478,17 @@
 bstring(
-1
+0
 , senten
@@ -2503,14 +2503,209 @@
 Of('
-#', 1)
+%5Cr'));%0A        Matcher parser = patternHeader.matcher(header);%0A        if (!parser.matches()) %7B%0A            return null;%0A        %7D%0A%0A        // Get device identifier%0A        String imei = parser.group(1
 );%0A 
@@ -2935,32 +2935,104 @@
      %7D%0A        %0A
+        // Get status%0A        String status = parser.group(2);%0A        %0A
         String%5B%5D
@@ -3227,32 +3227,24 @@
             
-Matcher 
 parser = pat
@@ -3247,16 +3247,24 @@
  pattern
+Position
 .matcher
@@ -5775,32 +5775,127 @@
                %0A
+                // Status%0A                extendedInfo.set(%22status%22, status);%0A                %0A
                 

Add status to tlt2h (fix #364)