use of org.traccar.model.Position in project traccar by tananaev.
the class GeolocationHandler method handleUpstream.
@Override
public void handleUpstream(final ChannelHandlerContext ctx, ChannelEvent evt) throws Exception {
if (!(evt instanceof MessageEvent)) {
ctx.sendUpstream(evt);
return;
}
final MessageEvent event = (MessageEvent) evt;
Object message = event.getMessage();
if (message instanceof Position) {
final Position position = (Position) message;
if ((position.getOutdated() || processInvalidPositions && !position.getValid()) && position.getNetwork() != null) {
Context.getStatisticsManager().registerGeolocationRequest();
geolocationProvider.getLocation(position.getNetwork(), new GeolocationProvider.LocationProviderCallback() {
@Override
public void onSuccess(double latitude, double longitude, double accuracy) {
position.set(Position.KEY_APPROXIMATE, true);
position.setValid(true);
position.setFixTime(position.getDeviceTime());
position.setLatitude(latitude);
position.setLongitude(longitude);
position.setAccuracy(accuracy);
position.setAltitude(0);
position.setSpeed(0);
position.setCourse(0);
position.set(Position.KEY_RSSI, 0);
Channels.fireMessageReceived(ctx, position, event.getRemoteAddress());
}
@Override
public void onFailure(Throwable e) {
Log.warning(e);
Channels.fireMessageReceived(ctx, position, event.getRemoteAddress());
}
});
} else {
Channels.fireMessageReceived(ctx, position, event.getRemoteAddress());
}
} else {
Channels.fireMessageReceived(ctx, message, event.getRemoteAddress());
}
}
use of org.traccar.model.Position in project traccar by tananaev.
the class AisProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String[] sentences = ((String) msg).split("\\r\\n");
List<Position> positions = new ArrayList<>();
Map<Integer, BitBuffer> buffers = new HashMap<>();
for (String sentence : sentences) {
if (!sentence.isEmpty()) {
Parser parser = new Parser(PATTERN, sentence);
if (parser.matches()) {
int count = parser.nextInt(0);
int index = parser.nextInt(0);
int id = parser.nextInt(0);
Position position = null;
if (count == 1) {
BitBuffer bits = new BitBuffer();
bits.writeEncoded(parser.next().getBytes(StandardCharsets.US_ASCII));
position = decodePayload(channel, remoteAddress, bits);
} else {
BitBuffer bits = buffers.get(id);
if (bits == null) {
bits = new BitBuffer();
buffers.put(id, bits);
}
bits.writeEncoded(parser.next().getBytes(StandardCharsets.US_ASCII));
if (count == index) {
position = decodePayload(channel, remoteAddress, bits);
buffers.remove(id);
}
}
if (position != null) {
positions.add(position);
}
}
}
}
return positions;
}
use of org.traccar.model.Position in project traccar by tananaev.
the class AisProtocolDecoder method decodePayload.
private Position decodePayload(Channel channel, SocketAddress remoteAddress, BitBuffer buf) {
int type = buf.readUnsigned(6);
if (type == 1 || type == 2 || type == 3 || type == 18) {
buf.readUnsigned(2);
int mmsi = buf.readUnsigned(30);
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(mmsi));
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(new Date());
if (type == 18) {
// reserved
buf.readUnsigned(8);
} else {
position.set(Position.KEY_STATUS, buf.readUnsigned(4));
position.set("turn", buf.readSigned(8));
}
position.setSpeed(buf.readUnsigned(10) * 0.1);
position.setValid(buf.readUnsigned(1) != 0);
position.setLongitude(buf.readSigned(28) * 0.0001 / 60.0);
position.setLatitude(buf.readSigned(27) * 0.0001 / 60.0);
position.setCourse(buf.readUnsigned(12) * 0.1);
position.set("heading", buf.readUnsigned(9));
return position;
}
return null;
}
use of org.traccar.model.Position in project traccar by tananaev.
the class AppelloProtocolDecoder 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;
}
String imei = parser.next();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
if (parser.hasNext(6)) {
position.setTime(parser.nextDateTime());
} else {
getLastLocation(position, null);
}
position.setLatitude(parser.nextDouble(0));
position.setLongitude(parser.nextDouble(0));
position.setSpeed(parser.nextDouble(0));
position.setCourse(parser.nextDouble(0));
position.set(Position.KEY_SATELLITES, parser.nextInt(0));
position.setAltitude(parser.nextDouble(0));
position.setValid(parser.next().equals("F"));
return position;
}
use of org.traccar.model.Position in project traccar by tananaev.
the class AquilaProtocolDecoder 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 = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_EVENT, parser.nextInt(0));
position.setLatitude(parser.nextDouble(0));
position.setLongitude(parser.nextDouble(0));
position.setTime(parser.nextDateTime());
position.setValid(parser.next().equals("A"));
if (parser.hasNext(3)) {
position.set(Position.KEY_RSSI, parser.nextInt(0));
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
position.set(Position.KEY_ODOMETER, parser.nextInt(0));
}
if (parser.hasNext(9)) {
position.set(Position.KEY_FUEL_LEVEL, parser.nextInt());
position.set(Position.PREFIX_IN + 1, parser.next());
position.set(Position.KEY_CHARGE, parser.next().equals("1"));
position.set(Position.PREFIX_IN + 2, parser.next());
position.set(Position.KEY_IGNITION, parser.nextInt(0) == 1);
int course = (parser.nextInt(0) << 3) + (parser.nextInt(0) << 2) + (parser.nextInt(0) << 1) + parser.nextInt(0);
if (course > 0 && course <= 8) {
position.setCourse((course - 1) * 45);
}
} else if (parser.hasNext(7)) {
position.setCourse(parser.nextInt(0));
position.set(Position.KEY_CHARGE, parser.next().equals("1"));
position.set(Position.KEY_IGNITION, parser.nextInt(0) == 1);
position.set(Position.KEY_POWER, parser.nextInt(0));
position.set(Position.KEY_BATTERY, parser.nextInt(0));
String obd = parser.next();
position.set("obd", obd.substring(1, obd.length() - 1));
String dtcs = parser.next();
position.set(Position.KEY_DTCS, dtcs.substring(1, dtcs.length() - 1).replace('|', ' '));
} else if (parser.hasNext(10)) {
position.setCourse(parser.nextInt(0));
position.set(Position.KEY_SATELLITES, parser.nextInt(0));
position.set(Position.KEY_HDOP, parser.nextDouble(0));
position.set(Position.PREFIX_ADC + 1, parser.nextInt(0));
position.set(Position.PREFIX_IN + 1, parser.nextInt(0));
position.set(Position.KEY_CHARGE, parser.next().equals("1"));
position.set(Position.PREFIX_IN + 2, parser.nextInt(0));
position.set(Position.KEY_IGNITION, parser.nextInt(0) == 1);
position.set(Position.KEY_POWER, parser.nextInt(0));
position.set(Position.KEY_BATTERY, parser.nextInt(0));
} else if (parser.hasNext(2)) {
position.set("sensorId", parser.nextInt());
position.set("sensorData", parser.next());
}
return position;
}
Aggregations