Skip to content

Instantly share code, notes, and snippets.

@amit22786
Created October 25, 2016 04:14
Show Gist options
  • Save amit22786/1e3abd2a63c78952eb69c99b8b178bd1 to your computer and use it in GitHub Desktop.
Save amit22786/1e3abd2a63c78952eb69c99b8b178bd1 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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.helper.UnitsConverter;
import org.traccar.model.Event;
import org.traccar.model.Position;
/**
*
* @author amit
*/
public class TrackingpathProtocolDecoder extends BaseProtocolDecoder {
public NextronProtocolDecoder(TrackingpathProtocol protocol) {
super(protocol);
}
private static final Pattern PATTERN = new PatternBuilder()
.text("$loc,")
.number("(d+),")
.number("(dd)(dd)(dd)(dd)(dd)(dd),")
.expression("([AV]),")
.number("(-?d+.d+),")
.expression("([NS]),")
.number("(-?d+.d+),")
.expression("([EW]),")
.number("(d+),")
.number("(d+.d+),")
.number("(d+),")
.number("(d+.d+),")
.expression("([01]+),")
.expression("([01]+),")
.expression("([01]+),")
.expression("([01]+),")
.expression("([01]+),")
.number("(d+),")
.number("(d+),")
.number("(d+.d+),")
.expression("([01]+)")
.expression("([+-]+)")
.compile();
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
////String msg="$loc,865879026779232,050916092132,A,28.601224,N,77.326833,E,
//033,53.150,09,192.20,0,0,0,0,0,0000,0944,0.0,0+";
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());
DateBuilder dateBuilder = new DateBuilder()
.setDate(parser.nextInt(), parser.nextInt(), parser.nextInt())
.setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
position.setTime(dateBuilder.getDate());
position.setValid(parser.next().equals("A"));
String direction=null;
position.setLatitude(parser.nextDouble());
direction=parser.next();
position.setLongitude(parser.nextDouble());
direction+=parser.next();
//033,53.150,09,192.20,0,0,0,0,0,0000,0944,0.0,0+";
position.set(Event.KEY_DIRECTION, direction);
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
position.setCourse(parser.nextDouble());
position.set(Event.KEY_SATELLITES, parser.nextInt());
position.set(Event.KEY_ALTITUDE, parser.nextInt());
position.set(Event.KEY_DBI1, parser.nextInt());
position.set(Event.KEY_DBI2, parser.nextInt());
position.set(Event.KEY_DBI3, parser.nextInt());
position.set(Event.KEY_DBI4, parser.nextInt());
position.set(Event.KEY_DBO1, parser.nextInt());
position.set(Event.KEY_FUEL, parser.nextDouble());
position.set(Event.KEY_BATTERY, parser.nextInt());
position.set(Event.KEY_FIRMWARE, parser.nextDouble());
position.set(Event.KEY_TAMPER, parser.nextInt());
position.set(Event.KEY_PACKET_LOC, parser.next());
return position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment