use of org.traccar.model.CellTower in project traccar by tananaev.
the class Jt600ProtocolDecoder method decodeBinary.
private List<Position> decodeBinary(ChannelBuffer buf, Channel channel, SocketAddress remoteAddress) {
List<Position> positions = new LinkedList<>();
// header
buf.readByte();
boolean longFormat = buf.getUnsignedByte(buf.readerIndex()) == 0x75;
String id = String.valueOf(Long.parseLong(ChannelBuffers.hexDump(buf.readBytes(5))));
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
if (deviceSession == null) {
return null;
}
int protocolVersion = 0;
if (longFormat) {
protocolVersion = buf.readUnsignedByte();
}
int version = BitUtil.from(buf.readUnsignedByte(), 4);
// length
buf.readUnsignedShort();
while (buf.readableBytes() > 1) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
DateBuilder dateBuilder = new DateBuilder().setDay(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setYear(BcdUtil.readInteger(buf, 2)).setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2));
position.setTime(dateBuilder.getDate());
double latitude = convertCoordinate(BcdUtil.readInteger(buf, 8));
double longitude = convertCoordinate(BcdUtil.readInteger(buf, 9));
byte flags = buf.readByte();
position.setValid((flags & 0x1) == 0x1);
if ((flags & 0x2) == 0) {
latitude = -latitude;
}
position.setLatitude(latitude);
if ((flags & 0x4) == 0) {
longitude = -longitude;
}
position.setLongitude(longitude);
position.setSpeed(BcdUtil.readInteger(buf, 2));
position.setCourse(buf.readUnsignedByte() * 2.0);
if (longFormat) {
position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
// vehicle id combined
buf.readUnsignedInt();
position.set(Position.KEY_STATUS, buf.readUnsignedShort());
int battery = buf.readUnsignedByte();
if (battery == 0xff) {
position.set(Position.KEY_CHARGE, true);
} else {
position.set(Position.KEY_BATTERY_LEVEL, battery);
}
CellTower cellTower = CellTower.fromCidLac(buf.readUnsignedShort(), buf.readUnsignedShort());
cellTower.setSignalStrength((int) buf.readUnsignedByte());
position.setNetwork(new Network(cellTower));
if (protocolVersion == 0x17) {
// geofence id
buf.readUnsignedByte();
// reserved
buf.skipBytes(3);
}
} else if (version == 1) {
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
position.set(Position.KEY_POWER, buf.readUnsignedByte());
// other flags and sensors
buf.readByte();
position.setAltitude(buf.readUnsignedShort());
int cid = buf.readUnsignedShort();
int lac = buf.readUnsignedShort();
int rssi = buf.readUnsignedByte();
if (cid != 0 && lac != 0) {
CellTower cellTower = CellTower.fromCidLac(cid, lac);
cellTower.setSignalStrength(rssi);
position.setNetwork(new Network(cellTower));
} else {
position.set(Position.KEY_RSSI, rssi);
}
} else if (version == 2) {
int fuel = buf.readUnsignedByte() << 8;
position.set(Position.KEY_STATUS, buf.readUnsignedInt());
position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
fuel += buf.readUnsignedByte();
position.set(Position.KEY_FUEL_LEVEL, fuel);
} else if (version == 3) {
BitBuffer bitBuffer = new BitBuffer(buf);
position.set("fuel1", bitBuffer.readUnsigned(12));
position.set("fuel2", bitBuffer.readUnsigned(12));
position.set("fuel3", bitBuffer.readUnsigned(12));
position.set(Position.KEY_ODOMETER, bitBuffer.readUnsigned(20) * 1000);
int status = bitBuffer.readUnsigned(24);
position.set(Position.KEY_IGNITION, BitUtil.check(status, 0));
position.set(Position.KEY_STATUS, status);
}
positions.add(position);
}
// index
buf.readUnsignedByte();
return positions;
}
use of org.traccar.model.CellTower in project traccar by tananaev.
the class ProtocolTest method verifyDecodedPosition.
private void verifyDecodedPosition(Object decodedObject, boolean checkLocation, boolean checkAttributes, Position expected) {
assertNotNull("position is null", decodedObject);
assertTrue("not a position", decodedObject instanceof Position);
Position position = (Position) decodedObject;
if (checkLocation) {
if (expected != null) {
if (expected.getFixTime() != null) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
assertEquals("time", dateFormat.format(expected.getFixTime()), dateFormat.format(position.getFixTime()));
}
assertEquals("valid", expected.getValid(), position.getValid());
assertEquals("latitude", expected.getLatitude(), position.getLatitude(), 0.00001);
assertEquals("longitude", expected.getLongitude(), position.getLongitude(), 0.00001);
} else {
assertNotNull(position.getFixTime());
assertTrue("year > 1999", position.getFixTime().after(new Date(915148800000L)));
assertTrue("time < +25 hours", position.getFixTime().getTime() < System.currentTimeMillis() + 25 * 3600000);
assertTrue("latitude >= -90", position.getLatitude() >= -90);
assertTrue("latitude <= 90", position.getLatitude() <= 90);
assertTrue("longitude >= -180", position.getLongitude() >= -180);
assertTrue("longitude <= 180", position.getLongitude() <= 180);
}
assertTrue("altitude >= -12262", position.getAltitude() >= -12262);
assertTrue("altitude <= 18000", position.getAltitude() <= 18000);
assertTrue("speed >= 0", position.getSpeed() >= 0);
assertTrue("speed <= 869", position.getSpeed() <= 869);
assertTrue("course >= 0", position.getCourse() >= 0);
assertTrue("course <= 360", position.getCourse() <= 360);
assertNotNull("protocol is null", position.getProtocol());
}
Map<String, Object> attributes = position.getAttributes();
if (checkAttributes) {
assertFalse("no attributes", attributes.isEmpty());
}
if (attributes.containsKey(Position.KEY_INDEX)) {
assertTrue(attributes.get(Position.KEY_INDEX) instanceof Number);
}
if (attributes.containsKey(Position.KEY_HDOP)) {
assertTrue(attributes.get(Position.KEY_HDOP) instanceof Number);
}
if (attributes.containsKey(Position.KEY_VDOP)) {
assertTrue(attributes.get(Position.KEY_VDOP) instanceof Number);
}
if (attributes.containsKey(Position.KEY_PDOP)) {
assertTrue(attributes.get(Position.KEY_PDOP) instanceof Number);
}
if (attributes.containsKey(Position.KEY_SATELLITES)) {
assertTrue(attributes.get(Position.KEY_SATELLITES) instanceof Number);
}
if (attributes.containsKey(Position.KEY_SATELLITES_VISIBLE)) {
assertTrue(attributes.get(Position.KEY_SATELLITES_VISIBLE) instanceof Number);
}
if (attributes.containsKey(Position.KEY_RSSI)) {
assertTrue(attributes.get(Position.KEY_RSSI) instanceof Number);
}
if (attributes.containsKey(Position.KEY_ODOMETER)) {
assertTrue(attributes.get(Position.KEY_ODOMETER) instanceof Number);
}
if (attributes.containsKey(Position.KEY_RPM)) {
assertTrue(attributes.get(Position.KEY_RPM) instanceof Number);
}
if (attributes.containsKey(Position.KEY_FUEL_LEVEL)) {
assertTrue(attributes.get(Position.KEY_FUEL_LEVEL) instanceof Number);
}
if (attributes.containsKey(Position.KEY_POWER)) {
assertTrue(attributes.get(Position.KEY_POWER) instanceof Number);
}
if (attributes.containsKey(Position.KEY_BATTERY)) {
assertTrue(attributes.get(Position.KEY_BATTERY) instanceof Number);
}
if (attributes.containsKey(Position.KEY_BATTERY_LEVEL)) {
int batteryLevel = ((Number) attributes.get(Position.KEY_BATTERY_LEVEL)).intValue();
assertTrue(batteryLevel <= 100 && batteryLevel >= 0);
}
if (attributes.containsKey(Position.KEY_CHARGE)) {
assertTrue(attributes.get(Position.KEY_CHARGE) instanceof Boolean);
}
if (attributes.containsKey(Position.KEY_IGNITION)) {
assertTrue(attributes.get(Position.KEY_IGNITION) instanceof Boolean);
}
if (attributes.containsKey(Position.KEY_MOTION)) {
assertTrue(attributes.get(Position.KEY_MOTION) instanceof Boolean);
}
if (attributes.containsKey(Position.KEY_ARCHIVE)) {
assertTrue(attributes.get(Position.KEY_ARCHIVE) instanceof Boolean);
}
if (attributes.containsKey(Position.KEY_DRIVER_UNIQUE_ID)) {
assertTrue(attributes.get(Position.KEY_DRIVER_UNIQUE_ID) instanceof String);
}
if (attributes.containsKey(Position.KEY_STEPS)) {
assertTrue(attributes.get(Position.KEY_STEPS) instanceof Number);
}
if (attributes.containsKey(Position.KEY_ROAMING)) {
assertTrue(attributes.get(Position.KEY_ROAMING) instanceof Boolean);
}
if (position.getNetwork() != null && position.getNetwork().getCellTowers() != null) {
for (CellTower cellTower : position.getNetwork().getCellTowers()) {
checkInteger(cellTower.getMobileCountryCode(), 0, 999);
checkInteger(cellTower.getMobileNetworkCode(), 0, 999);
checkInteger(cellTower.getLocationAreaCode(), 1, 65535);
checkInteger(cellTower.getCellId(), 0, 268435455);
}
}
}
use of org.traccar.model.CellTower in project traccar by tananaev.
the class Jt600ProtocolDecoder method decodeU01.
private Position decodeU01(String sentence, Channel channel, SocketAddress remoteAddress) {
Parser parser = new Parser(PATTERN_U01, sentence);
if (!parser.matches()) {
return null;
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
String type = parser.next();
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
position.setValid(parser.next().equals("T"));
position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
position.setSpeed(UnitsConverter.knotsFromMph(parser.nextDouble(0)));
position.setCourse(parser.nextDouble(0));
position.set(Position.KEY_SATELLITES, parser.nextInt(0));
position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt(0));
position.set(Position.KEY_STATUS, parser.nextBinInt(0));
CellTower cellTower = CellTower.fromCidLac(parser.nextInt(0), parser.nextInt(0));
cellTower.setSignalStrength(parser.nextInt(0));
position.setNetwork(new Network(cellTower));
position.set(Position.KEY_ODOMETER, parser.nextLong(0) * 1000);
position.set(Position.KEY_INDEX, parser.nextInt(0));
if (channel != null) {
if (type.equals("U01") || type.equals("U02") || type.equals("U03")) {
channel.write("(S39)");
} else if (type.equals("U06")) {
channel.write("(S20)");
}
}
return position;
}
use of org.traccar.model.CellTower in project traccar by tananaev.
the class AtrackProtocolDecoder method readCustomData.
private void readCustomData(Position position, ChannelBuffer buf, String form) {
CellTower cellTower = new CellTower();
String[] keys = form.substring(1).split("%");
for (String key : keys) {
switch(key) {
case "SA":
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
break;
case "MV":
position.set(Position.KEY_POWER, buf.readUnsignedShort());
break;
case "BV":
position.set(Position.KEY_BATTERY, buf.readUnsignedShort());
break;
case "GQ":
cellTower.setSignalStrength((int) buf.readUnsignedByte());
break;
case "CE":
cellTower.setCellId(buf.readUnsignedInt());
break;
case "LC":
cellTower.setLocationAreaCode(buf.readUnsignedShort());
break;
case "CN":
// cccnn
int combinedMobileCodes = (int) (buf.readUnsignedInt() % 100000);
cellTower.setMobileCountryCode(combinedMobileCodes / 100);
cellTower.setMobileNetworkCode(combinedMobileCodes % 100);
break;
case "RL":
// rxlev
buf.readUnsignedByte();
break;
case "PC":
position.set(Position.PREFIX_COUNT + 1, buf.readUnsignedInt());
break;
case "AT":
position.setAltitude(buf.readUnsignedInt());
break;
case "RP":
position.set(Position.KEY_RPM, buf.readUnsignedShort());
break;
case "GS":
position.set(Position.KEY_RSSI, buf.readUnsignedByte());
break;
case "DT":
position.set(Position.KEY_ARCHIVE, buf.readUnsignedByte() == 1);
break;
case "VN":
position.set(Position.KEY_VIN, readString(buf));
break;
case "MF":
// mass air flow rate
buf.readUnsignedShort();
break;
case "EL":
// engine load
buf.readUnsignedByte();
break;
case "TR":
position.set(Position.KEY_THROTTLE, buf.readUnsignedByte());
break;
case "ET":
position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedShort());
break;
case "FL":
position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
break;
case "ML":
// mil status
buf.readUnsignedByte();
break;
case "FC":
position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedInt());
break;
case "CI":
// format string
readString(buf);
break;
case "AV1":
position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
break;
case "NC":
// gsm neighbor cell info
readString(buf);
break;
case "SM":
// max speed between reports
buf.readUnsignedShort();
break;
case "GL":
// google link
readString(buf);
break;
case "MA":
// mac address
readString(buf);
break;
default:
break;
}
}
if (cellTower.getMobileCountryCode() != null && cellTower.getMobileNetworkCode() != null && cellTower.getCellId() != null && cellTower.getLocationAreaCode() != null) {
position.setNetwork(new Network(cellTower));
} else if (cellTower.getSignalStrength() != null) {
position.set(Position.KEY_RSSI, cellTower.getSignalStrength());
}
}
Aggregations