/*
 * Copyright 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.net.SocketAddress;
import java.util.regex.Pattern;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.Parser;
import org.traccar.helper.PatternBuilder;
import org.traccar.model.Position;

public class FreedomProtocolDecoder extends BaseProtocolDecoder {

    public FreedomProtocolDecoder(FreedomProtocol protocol) {
        super(protocol);
    }

    private static final Pattern PATTERN = new PatternBuilder()
            .text("IMEI,")
            .number("(d+),")                           // IMEI
            .number("(dddd)/(dd)/(dd), ")      // Date
            .number("(dd):(dd):(dd), ")      // Time
            .number("([NS]), Lat:(dd)(d+.d+), ") // Latitude
            .number("([EW]), Lon:(ddd)(d+.d+), ") // Longitude
            .text("Spd:").number("(d+.d+)")                   // Speed
            .any()
            .compile();

    @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)) {
            return null;
        }
        position.setDeviceId(getDeviceId());

        position.setValid(true);

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

        position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN));
        position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN));

        position.setSpeed(parser.nextDouble());

        return position;
    }

}

@@ -1248,21 +1248,15 @@
     
-      // IMEI
+// imei
 %0A   
@@ -1303,12 +1303,14 @@
     
+  
 // 
-D
+d
 ate%0A
@@ -1358,12 +1358,16 @@
     
+    
 // 
-T
+t
 ime%0A
@@ -1379,24 +1379,60 @@
        .
+expression(%22(%5BNS%5D), %22)%0A            .
 number(%22
 (%5BNS%5D), 
@@ -1423,24 +1423,16 @@
 number(%22
-(%5BNS%5D), 
 Lat:(dd)
@@ -1442,21 +1442,29 @@
 .d+), %22)
+        
  // 
-L
+l
 atitude%0A
@@ -1476,24 +1476,60 @@
        .
+expression(%22(%5BEW%5D), %22)%0A            .
 number(%22
 (%5BEW%5D), 
@@ -1520,24 +1520,16 @@
 number(%22
-(%5BEW%5D), 
 Lon:(ddd
@@ -1544,13 +1544,20 @@
 , %22)
+       
  // 
-L
+l
 ongi
@@ -1604,29 +1604,16 @@
 d+.d+)%22)
-             
       //
@@ -1613,17 +1613,17 @@
      // 
-S
+s
 peed%0A   

Format pattern builder comments