use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class PiligrimProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
HttpRequest request = (HttpRequest) msg;
String uri = request.getUri();
if (uri.startsWith("/config")) {
sendResponse(channel, "CONFIG: OK");
} else if (uri.startsWith("/addlog")) {
sendResponse(channel, "ADDLOG: OK");
} else if (uri.startsWith("/inform")) {
sendResponse(channel, "INFORM: OK");
} else if (uri.startsWith("/bingps")) {
sendResponse(channel, "BINGPS: OK");
QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, decoder.getParameters().get("imei").get(0));
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
ChannelBuffer buf = request.getContent();
while (buf.readableBytes() > 2) {
// header
buf.readUnsignedByte();
int type = buf.readUnsignedByte();
// length
buf.readUnsignedByte();
if (type == MSG_GPS || type == MSG_GPS_SENSORS) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
DateBuilder dateBuilder = new DateBuilder().setDay(buf.readUnsignedByte()).setMonth(buf.getByte(buf.readerIndex()) & 0x0f).setYear(2010 + (buf.readUnsignedByte() >> 4)).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
position.setTime(dateBuilder.getDate());
double latitude = buf.readUnsignedByte();
latitude += buf.readUnsignedByte() / 60.0;
latitude += buf.readUnsignedByte() / 6000.0;
latitude += buf.readUnsignedByte() / 600000.0;
double longitude = buf.readUnsignedByte();
longitude += buf.readUnsignedByte() / 60.0;
longitude += buf.readUnsignedByte() / 6000.0;
longitude += buf.readUnsignedByte() / 600000.0;
int flags = buf.readUnsignedByte();
if (BitUtil.check(flags, 0)) {
latitude = -latitude;
}
if (BitUtil.check(flags, 1)) {
longitude = -longitude;
}
position.setLatitude(latitude);
position.setLongitude(longitude);
int satellites = buf.readUnsignedByte();
position.set(Position.KEY_SATELLITES, satellites);
position.setValid(satellites >= 3);
position.setSpeed(buf.readUnsignedByte());
double course = buf.readUnsignedByte() << 1;
course += (flags >> 2) & 1;
course += buf.readUnsignedByte() / 100.0;
position.setCourse(course);
if (type == MSG_GPS_SENSORS) {
double power = buf.readUnsignedByte();
power += buf.readUnsignedByte() << 8;
position.set(Position.KEY_POWER, power * 0.01);
double battery = buf.readUnsignedByte();
battery += buf.readUnsignedByte() << 8;
position.set(Position.KEY_BATTERY, battery * 0.01);
buf.skipBytes(6);
}
positions.add(position);
} else if (type == MSG_EVENTS) {
buf.skipBytes(13);
}
}
return positions;
}
return null;
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class OigoProtocolDecoder method decodeArMessage.
private Position decodeArMessage(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
// header
buf.skipBytes(1);
// length
buf.readUnsignedShort();
int type = buf.readUnsignedByte();
int tag = buf.readUnsignedByte();
DeviceSession deviceSession;
switch(BitUtil.to(tag, 3)) {
case 0:
String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
deviceSession = getDeviceSession(channel, remoteAddress, imei);
break;
case 1:
buf.skipBytes(1);
String meid = buf.readBytes(14).toString(StandardCharsets.US_ASCII);
deviceSession = getDeviceSession(channel, remoteAddress, meid);
break;
default:
deviceSession = getDeviceSession(channel, remoteAddress);
break;
}
if (deviceSession == null || type != MSG_AR_LOCATION) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_EVENT, buf.readUnsignedByte());
int mask = buf.readInt();
if (BitUtil.check(mask, 0)) {
position.set(Position.KEY_INDEX, buf.readUnsignedShort());
}
if (BitUtil.check(mask, 1)) {
int date = buf.readUnsignedByte();
DateBuilder dateBuilder = new DateBuilder().setDate(BitUtil.between(date, 4, 8) + 2010, BitUtil.to(date, 4), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
position.setTime(dateBuilder.getDate());
}
if (BitUtil.check(mask, 2)) {
// device time
buf.skipBytes(5);
}
if (BitUtil.check(mask, 3)) {
position.setLatitude(buf.readUnsignedInt() * 0.000001 - 90);
position.setLongitude(buf.readUnsignedInt() * 0.000001 - 180.0);
}
if (BitUtil.check(mask, 4)) {
int status = buf.readUnsignedByte();
position.setValid(BitUtil.between(status, 4, 8) != 0);
position.set(Position.KEY_SATELLITES, BitUtil.to(status, 4));
position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
}
if (BitUtil.check(mask, 5)) {
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
}
if (BitUtil.check(mask, 6)) {
position.setCourse(buf.readUnsignedShort());
}
if (BitUtil.check(mask, 7)) {
position.setAltitude(buf.readShort());
}
if (BitUtil.check(mask, 8)) {
position.set(Position.KEY_RSSI, buf.readUnsignedByte());
}
if (BitUtil.check(mask, 9)) {
position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
}
if (BitUtil.check(mask, 10)) {
position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
}
if (BitUtil.check(mask, 11)) {
// gpio
buf.skipBytes(2);
}
if (BitUtil.check(mask, 12)) {
position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
}
if (BitUtil.check(mask, 13)) {
// software version
buf.skipBytes(6);
}
if (BitUtil.check(mask, 14)) {
// hardware version
buf.skipBytes(5);
}
if (BitUtil.check(mask, 15)) {
// device config
buf.readUnsignedShort();
}
return position;
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class OkoProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
Parser parser = new Parser(PATTERN, (String) msg);
if (!parser.matches()) {
return null;
}
DeviceSession deviceSession;
if (parser.hasNext()) {
deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
} else {
deviceSession = getDeviceSession(channel, remoteAddress);
}
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
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(), parser.nextInt(), parser.nextInt());
position.setTime(dateBuilder.getDate());
position.set(Position.KEY_SATELLITES, parser.nextInt());
position.set(Position.PREFIX_ADC + 1, parser.nextDouble());
position.set(Position.KEY_EVENT, parser.next());
position.set(Position.KEY_POWER, parser.nextDouble());
position.set(Position.KEY_INPUT, parser.nextHexInt());
return position;
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class OpenGtsProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
HttpRequest request = (HttpRequest) msg;
QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
Map<String, List<String>> params = decoder.getParameters();
Position position = new Position();
position.setProtocol(getProtocolName());
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
String value = entry.getValue().get(0);
switch(entry.getKey()) {
case "id":
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
if (deviceSession == null) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
break;
case "gprmc":
Parser parser = new Parser(PATTERN, value);
if (!parser.matches()) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
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());
break;
case "alt":
position.setAltitude(Double.parseDouble(value));
break;
case "batt":
position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value));
break;
default:
break;
}
}
if (position.getDeviceId() != 0) {
sendResponse(channel, HttpResponseStatus.OK);
return position;
} else {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class RitiProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// header
buf.skipBytes(2);
Position position = new Position(getProtocolName());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedShort()));
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
position.set("mode", buf.readUnsignedByte());
position.set(Position.KEY_COMMAND, buf.readUnsignedByte());
position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
// status
buf.skipBytes(5);
// idleCount
buf.readUnsignedShort();
// idleTime in seconds
buf.readUnsignedShort();
position.set(Position.KEY_DISTANCE, buf.readUnsignedInt());
position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());
// Parse GPRMC
int end = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*');
String gprmc = buf.toString(buf.readerIndex(), end - buf.readerIndex(), StandardCharsets.US_ASCII);
Parser parser = new Parser(PATTERN, gprmc);
if (!parser.matches()) {
return null;
}
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());
return position;
}
Aggregations