use of org.traccar.model.Position in project traccar by tananaev.
the class EelinkProtocolDecoder method decodeNew.
private Position decodeNew(DeviceSession deviceSession, ChannelBuffer buf, int index) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_INDEX, index);
position.setTime(new Date(buf.readUnsignedInt() * 1000));
int flags = buf.readUnsignedByte();
if (BitUtil.check(flags, 0)) {
position.setLatitude(buf.readInt() / 1800000.0);
position.setLongitude(buf.readInt() / 1800000.0);
position.setAltitude(buf.readShort());
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
position.setCourse(buf.readUnsignedShort());
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
} else {
getLastLocation(position, position.getDeviceTime());
}
if (BitUtil.check(flags, 1)) {
position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedInt(), buf.readUnsignedByte())));
}
if (BitUtil.check(flags, 2)) {
// bsid1
buf.skipBytes(7);
}
if (BitUtil.check(flags, 3)) {
// bsid2
buf.skipBytes(7);
}
if (BitUtil.check(flags, 4)) {
// bss0
buf.skipBytes(7);
}
if (BitUtil.check(flags, 5)) {
// bss1
buf.skipBytes(7);
}
if (BitUtil.check(flags, 6)) {
// bss2
buf.skipBytes(7);
}
if (buf.readableBytes() >= 2) {
int status = buf.readUnsignedShort();
position.setValid(BitUtil.check(status, 0));
if (BitUtil.check(status, 1)) {
position.set(Position.KEY_IGNITION, BitUtil.check(status, 2));
}
position.set(Position.KEY_STATUS, status);
}
if (buf.readableBytes() >= 2) {
position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
}
if (buf.readableBytes() >= 4) {
position.set(Position.PREFIX_ADC + 0, buf.readUnsignedShort());
position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
}
if (buf.readableBytes() >= 4) {
position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
}
if (buf.readableBytes() >= 4) {
// gsm counter
buf.readUnsignedShort();
// gps counter
buf.readUnsignedShort();
}
if (buf.readableBytes() >= 4) {
position.set(Position.KEY_STEPS, buf.readUnsignedShort());
// walking time
buf.readUnsignedShort();
}
if (buf.readableBytes() >= 12) {
position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedShort() / 256.0);
position.set("humidity", buf.readUnsignedShort() * 0.1);
position.set("illuminance", buf.readUnsignedInt() / 256.0);
position.set("co2", buf.readUnsignedInt());
}
return position;
}
use of org.traccar.model.Position in project traccar by tananaev.
the class EelinkProtocolDecoder method decodeOld.
private Position decodeOld(DeviceSession deviceSession, ChannelBuffer buf, int type, int index) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_INDEX, index);
position.setTime(new Date(buf.readUnsignedInt() * 1000));
position.setLatitude(buf.readInt() / 1800000.0);
position.setLongitude(buf.readInt() / 1800000.0);
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
position.setCourse(buf.readUnsignedShort());
position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedMedium())));
position.setValid((buf.readUnsignedByte() & 0x01) != 0);
if (type == MSG_GPS) {
if (buf.readableBytes() >= 2) {
decodeStatus(position, buf.readUnsignedShort());
}
if (buf.readableBytes() >= 2 * 4) {
position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
position.set(Position.KEY_RSSI, buf.readUnsignedShort());
position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
}
} else if (type == MSG_ALARM) {
position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte()));
} else if (type == MSG_STATE) {
int statusType = buf.readUnsignedByte();
position.set(Position.KEY_EVENT, statusType);
if (statusType == 0x01 || statusType == 0x02 || statusType == 0x03) {
// device time
buf.readUnsignedInt();
if (buf.readableBytes() >= 2) {
decodeStatus(position, buf.readUnsignedShort());
}
}
}
return position;
}
use of org.traccar.model.Position in project traccar by tananaev.
the class EelinkProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
String uniqueId = null;
DeviceSession deviceSession;
if (buf.getByte(0) == 'E' && buf.getByte(1) == 'L') {
// udp header
buf.skipBytes(2 + 2 + 2);
uniqueId = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
deviceSession = getDeviceSession(channel, remoteAddress, uniqueId);
} else {
deviceSession = getDeviceSession(channel, remoteAddress);
}
// header
buf.skipBytes(2);
int type = buf.readUnsignedByte();
// length
buf.readShort();
int index = buf.readUnsignedShort();
if (type != MSG_GPS && type != MSG_DATA) {
sendResponse(channel, remoteAddress, uniqueId, type, index);
}
if (type == MSG_LOGIN) {
if (deviceSession == null) {
getDeviceSession(channel, remoteAddress, ChannelBuffers.hexDump(buf.readBytes(8)).substring(1));
}
} else {
if (deviceSession == null) {
return null;
}
if (type == MSG_GPS || type == MSG_ALARM || type == MSG_STATE || type == MSG_SMS) {
return decodeOld(deviceSession, buf, type, index);
} else if (type >= MSG_NORMAL && type <= MSG_OBD_CODE) {
return decodeNew(deviceSession, buf, index);
} else if (type == MSG_HEARTBEAT && buf.readableBytes() >= 2) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, null);
decodeStatus(position, buf.readUnsignedShort());
return position;
} else if (type == MSG_DOWNLINK) {
return decodeResult(deviceSession, buf, index);
}
}
return null;
}
use of org.traccar.model.Position in project traccar by tananaev.
the class EgtsProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3));
List<Position> positions = new LinkedList<>();
while (buf.readableBytes() > 2) {
int length = buf.readUnsignedShort();
int index = buf.readUnsignedShort();
int recordFlags = buf.readUnsignedByte();
if (BitUtil.check(recordFlags, 0)) {
// object id
buf.readUnsignedInt();
}
if (BitUtil.check(recordFlags, 1)) {
// event id
buf.readUnsignedInt();
}
if (BitUtil.check(recordFlags, 2)) {
// time
buf.readUnsignedInt();
}
int serviceType = buf.readUnsignedByte();
// recipient service type
buf.readUnsignedByte();
int recordEnd = buf.readerIndex() + length;
Position position = new Position(getProtocolName());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession != null) {
position.setDeviceId(deviceSession.getDeviceId());
}
ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
response.writeShort(index);
// success
response.writeByte(0);
sendResponse(channel, PT_RESPONSE, index, serviceType, MSG_RECORD_RESPONSE, response);
while (buf.readerIndex() < recordEnd) {
int type = buf.readUnsignedByte();
int end = buf.readUnsignedShort() + buf.readerIndex();
if (type == MSG_TERM_IDENTITY) {
// object id
buf.readUnsignedInt();
int flags = buf.readUnsignedByte();
if (BitUtil.check(flags, 0)) {
// home dispatcher identifier
buf.readUnsignedShort();
}
if (BitUtil.check(flags, 1)) {
getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII).trim());
}
if (BitUtil.check(flags, 2)) {
getDeviceSession(channel, remoteAddress, buf.readBytes(16).toString(StandardCharsets.US_ASCII).trim());
}
if (BitUtil.check(flags, 3)) {
// language identifier
buf.skipBytes(3);
}
if (BitUtil.check(flags, 5)) {
// network identifier
buf.skipBytes(3);
}
if (BitUtil.check(flags, 6)) {
// buffer size
buf.readUnsignedShort();
}
if (BitUtil.check(flags, 7)) {
getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII).trim());
}
response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
// success
response.writeByte(0);
sendResponse(channel, PT_APPDATA, index, serviceType, MSG_RESULT_CODE, response);
} else if (type == MSG_POS_DATA) {
// since 2010-01-01
position.setTime(new Date((buf.readUnsignedInt() + 1262304000) * 1000));
position.setLatitude(buf.readUnsignedInt() * 90.0 / 0xFFFFFFFFL);
position.setLongitude(buf.readUnsignedInt() * 180.0 / 0xFFFFFFFFL);
int flags = buf.readUnsignedByte();
position.setValid(BitUtil.check(flags, 0));
if (BitUtil.check(flags, 5)) {
position.setLatitude(-position.getLatitude());
}
if (BitUtil.check(flags, 6)) {
position.setLongitude(-position.getLongitude());
}
int speed = buf.readUnsignedShort();
position.setSpeed(UnitsConverter.knotsFromKph(BitUtil.to(speed, 14) * 0.1));
position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed, 15) ? 0x100 : 0));
position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 100);
position.set(Position.KEY_INPUT, buf.readUnsignedByte());
position.set(Position.KEY_EVENT, buf.readUnsignedByte());
if (BitUtil.check(flags, 7)) {
position.setAltitude(buf.readMedium());
}
} else if (type == MSG_EXT_POS_DATA) {
int flags = buf.readUnsignedByte();
if (BitUtil.check(flags, 0)) {
position.set(Position.KEY_VDOP, buf.readUnsignedShort());
}
if (BitUtil.check(flags, 1)) {
position.set(Position.KEY_HDOP, buf.readUnsignedShort());
}
if (BitUtil.check(flags, 2)) {
position.set(Position.KEY_PDOP, buf.readUnsignedShort());
}
if (BitUtil.check(flags, 3)) {
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
}
} else if (type == MSG_AD_SENSORS_DATA) {
// inputs flags
buf.readUnsignedByte();
position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
// adc flags
buf.readUnsignedByte();
}
buf.readerIndex(end);
}
if (serviceType == SERVICE_TELEDATA && deviceSession != null) {
positions.add(position);
}
}
return positions.isEmpty() ? null : positions;
}
use of org.traccar.model.Position in project traccar by tananaev.
the class GalileoProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// header
buf.readUnsignedByte();
int length = (buf.readUnsignedShort() & 0x7fff) + 3;
List<Position> positions = new LinkedList<>();
Set<Integer> tags = new HashSet<>();
boolean hasLocation = false;
DeviceSession deviceSession = null;
Position position = new Position(getProtocolName());
while (buf.readerIndex() < length) {
int tag = buf.readUnsignedByte();
if (tags.contains(tag)) {
if (hasLocation && position.getFixTime() != null) {
positions.add(position);
}
tags.clear();
hasLocation = false;
// new position starts
position = new Position(getProtocolName());
}
tags.add(tag);
if (tag == 0x03) {
deviceSession = getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII));
} else if (tag == 0x30) {
hasLocation = true;
position.setValid((buf.readUnsignedByte() & 0xf0) == 0x00);
position.setLatitude(buf.readInt() / 1000000.0);
position.setLongitude(buf.readInt() / 1000000.0);
} else {
decodeTag(position, buf, tag);
}
}
if (deviceSession == null) {
deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
}
if (hasLocation && position.getFixTime() != null) {
positions.add(position);
} else if (position.getAttributes().containsKey(Position.KEY_RESULT)) {
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, null);
positions.add(position);
}
sendReply(channel, buf.readUnsignedShort());
for (Position p : positions) {
p.setDeviceId(deviceSession.getDeviceId());
}
return positions.isEmpty() ? null : positions;
}
Aggregations