use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class Tlt2hProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = (String) msg;
sentence = sentence.trim();
String header = sentence.substring(0, sentence.indexOf('\r'));
Parser parser = new Parser(PATTERN_HEADER, header);
if (!parser.matches()) {
return null;
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
String status = parser.next();
String[] messages = sentence.substring(sentence.indexOf('\n') + 1).split("\r\n");
List<Position> positions = new LinkedList<>();
for (String message : messages) {
parser = new Parser(PATTERN_POSITION, message);
if (parser.matches()) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
// base station info
parser.next();
DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
position.setValid(parser.next().equals("A"));
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
position.setSpeed(parser.nextDouble(0));
position.setCourse(parser.nextDouble(0));
dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
position.setTime(dateBuilder.getDate());
decodeStatus(position, status);
positions.add(position);
}
}
return positions;
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class UproProtocolDecoder method decodeLocation.
private void decodeLocation(Position position, String data) {
Parser parser = new Parser(PATTERN_LOCATION, data);
if (parser.matches()) {
DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
position.setValid(true);
position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_MIN_MIN));
position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_MIN_MIN));
int flags = parser.nextInt(0);
position.setValid(BitUtil.check(flags, 0));
if (!BitUtil.check(flags, 1)) {
position.setLatitude(-position.getLatitude());
}
if (!BitUtil.check(flags, 2)) {
position.setLongitude(-position.getLongitude());
}
position.setSpeed(parser.nextInt(0) * 2);
position.setCourse(parser.nextInt(0) * 10);
dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
position.setTime(dateBuilder.getDate());
}
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class V680ProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = (String) msg;
sentence = sentence.trim();
if (sentence.length() == 16) {
getDeviceSession(channel, remoteAddress, sentence.substring(1, sentence.length()));
} else {
Parser parser = new Parser(PATTERN, sentence);
if (!parser.matches()) {
return null;
}
Position position = new Position(getProtocolName());
DeviceSession deviceSession;
if (parser.hasNext()) {
deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
} else {
deviceSession = getDeviceSession(channel, remoteAddress);
}
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
position.set("user", parser.next());
position.setValid(parser.nextInt(0) > 0);
position.set("password", parser.next());
position.set(Position.KEY_EVENT, parser.next());
position.set("packet", parser.next());
position.set("lbsData", parser.next());
double lon = parser.nextDouble(0);
boolean west = parser.next().equals("W");
double lat = parser.nextDouble(0);
boolean south = parser.next().equals("S");
if (lat > 90 || lon > 180) {
int lonDegrees = (int) (lon * 0.01);
lon = (lon - lonDegrees * 100) / 60.0;
lon += lonDegrees;
int latDegrees = (int) (lat * 0.01);
lat = (lat - latDegrees * 100) / 60.0;
lat += latDegrees;
}
position.setLongitude(west ? -lon : lon);
position.setLatitude(south ? -lat : lat);
position.setSpeed(parser.nextDouble(0));
position.setCourse(parser.nextDouble(0));
int day = parser.nextInt(0);
int month = parser.nextInt(0);
if (day == 0 && month == 0) {
// invalid date
return null;
}
DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextInt(0), month, day).setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
position.setTime(dateBuilder.getDate());
return position;
}
return null;
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class TrvProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = (String) msg;
String id = sentence.startsWith("TRV") ? sentence.substring(0, 3) : sentence.substring(0, 2);
String type = sentence.substring(id.length(), id.length() + 4);
if (channel != null) {
String responseHeader = id + (char) (type.charAt(0) + 1) + type.substring(1);
if (type.equals("AP00") && id.equals("IW")) {
String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
channel.write(responseHeader + "," + time + ",0#");
} else if (type.equals("AP14")) {
channel.write(responseHeader + ",0.000,0.000#");
} else {
channel.write(responseHeader + "#");
}
}
if (type.equals("AP00")) {
getDeviceSession(channel, remoteAddress, sentence.substring(id.length() + type.length()));
return null;
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
if (type.equals("CP01")) {
Parser parser = new Parser(PATTERN_HEATRBEAT, sentence);
if (!parser.matches()) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, null);
decodeCommon(position, parser);
if (parser.hasNext(3)) {
position.set(Position.KEY_BLOCKED, decodeOptionalValue(parser, 2));
position.set(Position.KEY_CHARGE, decodeOptionalValue(parser, 1));
position.set(Position.KEY_MOTION, decodeOptionalValue(parser, 1));
}
return position;
} else if (type.equals("AP01") || type.equals("AP10")) {
Parser parser = new Parser(PATTERN, sentence);
if (!parser.matches()) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextInt(), parser.nextInt(), parser.nextInt());
position.setValid(parser.next().equals("A"));
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
dateBuilder.setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
position.setTime(dateBuilder.getDate());
position.setCourse(parser.nextDouble());
decodeCommon(position, parser);
position.setNetwork(new Network(CellTower.from(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt())));
return position;
}
return null;
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class GatorProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// header
buf.skipBytes(2);
int type = buf.readUnsignedByte();
// length
buf.readUnsignedShort();
String id = decodeId(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
sendResponse(channel, buf.getByte(buf.writerIndex() - 2));
if (type == MSG_POSITION_DATA || type == MSG_ROLLCALL_RESPONSE || type == MSG_ALARM_DATA || type == MSG_BLIND_AREA) {
Position position = new Position(getProtocolName());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, "1" + id, id);
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
DateBuilder dateBuilder = new DateBuilder().setYear(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)).setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2));
position.setTime(dateBuilder.getDate());
position.setLatitude(BcdUtil.readCoordinate(buf));
position.setLongitude(BcdUtil.readCoordinate(buf));
position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4)));
position.setCourse(BcdUtil.readInteger(buf, 4));
int flags = buf.readUnsignedByte();
position.setValid((flags & 0x80) != 0);
position.set(Position.KEY_SATELLITES, flags & 0x0f);
position.set(Position.KEY_STATUS, buf.readUnsignedByte());
position.set("key", buf.readUnsignedByte());
position.set("oil", buf.readUnsignedShort() / 10.0);
position.set(Position.KEY_POWER, buf.readUnsignedByte() + buf.readUnsignedByte() * 0.01);
position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
return position;
}
return null;
}
Aggregations