use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class SmokeyProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// header
buf.skipBytes(2);
// protocol version
buf.readUnsignedByte();
int type = buf.readUnsignedByte();
ChannelBuffer id = buf.readBytes(8);
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ChannelBuffers.hexDump(id));
if (deviceSession == null) {
return null;
}
if (type == MSG_DATE_RECORD) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_VERSION_FW, buf.readUnsignedShort());
int status = buf.readUnsignedShort();
position.set(Position.KEY_STATUS, status);
DateBuilder dateBuilder = new DateBuilder().setDate(2000, 1, 1).addSeconds(buf.readUnsignedInt());
getLastLocation(position, dateBuilder.getDate());
int index = buf.readUnsignedByte();
position.set(Position.KEY_INDEX, index);
int report = buf.readUnsignedShort();
// length
buf.readUnsignedShort();
position.set(Position.KEY_BATTERY, buf.readUnsignedShort());
Network network = new Network();
if (report != 0x0203) {
int count = 1;
if (report != 0x0200) {
count = buf.readUnsignedByte();
}
for (int i = 0; i < count; i++) {
int mcc = buf.readUnsignedShort();
int mnc = buf.readUnsignedShort();
int lac = buf.readUnsignedShort();
int cid = buf.readUnsignedShort();
if (i == 0) {
// timing advance
buf.readByte();
}
int rssi = buf.readByte();
network.addCellTower(CellTower.from(mcc, mnc, lac, cid, rssi));
}
}
if (report == 0x0202 || report == 0x0203) {
int count = buf.readUnsignedByte();
for (int i = 0; i < count; i++) {
// ssid
buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0) + 1);
String mac = String.format("%02x:%02x:%02x:%02x:%02x:%02x", buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
network.addWifiAccessPoint(WifiAccessPoint.from(mac, buf.readByte()));
}
}
position.setNetwork(network);
sendResponse(channel, remoteAddress, id, index, report);
return position;
}
return null;
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class SupermateProtocolDecoder 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;
}
Position position = new Position(getProtocolName());
String imei = parser.next();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
position.set("commandId", parser.next());
position.set(Position.KEY_COMMAND, parser.next());
position.setValid(parser.next().equals("A"));
DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextHexInt(0), parser.nextHexInt(0), parser.nextHexInt(0)).setTime(parser.nextHexInt(0), parser.nextHexInt(0), parser.nextHexInt(0));
position.setTime(dateBuilder.getDate());
if (parser.nextHexInt(0) == 8) {
position.setLatitude(-parser.nextHexInt(0) / 600000.0);
} else {
position.setLatitude(parser.nextHexInt(0) / 600000.0);
}
if (parser.nextHexInt(0) == 8) {
position.setLongitude(-parser.nextHexInt(0) / 600000.0);
} else {
position.setLongitude(parser.nextHexInt(0) / 600000.0);
}
position.setSpeed(parser.nextHexInt(0) / 100.0);
position.setCourse(parser.nextHexInt(0) / 100.0);
position.set(Position.KEY_STATUS, parser.next());
position.set("signal", parser.next());
position.set(Position.KEY_POWER, parser.nextDouble(0));
position.set("oil", parser.nextHexInt(0));
position.set(Position.KEY_ODOMETER, parser.nextHexInt(0));
if (channel != null) {
Calendar calendar = Calendar.getInstance();
String content = String.format("#1:%s:1:*,00000000,UP,%02x%02x%02x,%02x%02x%02x#", imei, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
channel.write(ChannelBuffers.copiedBuffer(content, StandardCharsets.US_ASCII));
}
return position;
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class T55ProtocolDecoder method decodeGprmc.
private Position decodeGprmc(DeviceSession deviceSession, String sentence, SocketAddress remoteAddress, Channel channel) {
if (deviceSession != null && channel != null && !(channel instanceof DatagramChannel) && Context.getIdentityManager().lookupAttributeBoolean(deviceSession.getDeviceId(), getProtocolName() + ".ack", false, true)) {
channel.write("OK1\r\n");
}
Parser parser = new Parser(PATTERN_GPRMC, sentence);
if (!parser.matches()) {
return null;
}
Position position = new Position(getProtocolName());
if (deviceSession != null) {
position.setDeviceId(deviceSession.getDeviceId());
}
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());
if (parser.hasNext(5)) {
position.set(Position.KEY_SATELLITES, parser.nextInt());
deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_IGNITION, parser.hasNext() && parser.next().equals("1"));
position.set(Position.KEY_FUEL_LEVEL, parser.nextInt(0));
position.set(Position.KEY_BATTERY, parser.nextInt());
}
if (parser.hasNext()) {
String[] parameters = parser.next().split(",");
for (int i = 1; i < parameters.length; i++) {
position.set(Position.PREFIX_IO + i, parameters[i]);
}
}
if (deviceSession != null) {
return position;
} else {
// save position
this.position = position;
return null;
}
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class TotemProtocolDecoder method decode12.
private boolean decode12(Position position, Parser parser, Pattern pattern) {
if (parser.hasNext()) {
position.set(Position.KEY_ALARM, decodeAlarm123(Short.parseShort(parser.next(), 16)));
}
DateBuilder dateBuilder = new DateBuilder();
int year = 0, month = 0, day = 0;
if (pattern == PATTERN2) {
day = parser.nextInt(0);
month = parser.nextInt(0);
year = parser.nextInt(0);
}
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));
if (pattern == PATTERN1) {
day = parser.nextInt(0);
month = parser.nextInt(0);
year = parser.nextInt(0);
}
if (year == 0) {
// ignore invalid data
return false;
}
dateBuilder.setDate(year, month, day);
position.setTime(dateBuilder.getDate());
if (pattern == PATTERN1) {
position.set(Position.KEY_PDOP, parser.nextDouble());
position.set(Position.KEY_HDOP, parser.nextDouble());
position.set(Position.KEY_VDOP, parser.nextDouble());
} else {
position.set(Position.KEY_HDOP, parser.nextDouble());
}
position.set(Position.PREFIX_IO + 1, parser.next());
if (pattern == PATTERN1) {
position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.01);
} else {
position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.1);
}
position.set(Position.KEY_POWER, parser.nextDouble(0));
position.set(Position.PREFIX_ADC + 1, parser.next());
int lac = parser.nextHexInt(0);
int cid = parser.nextHexInt(0);
if (lac != 0 && cid != 0) {
position.setNetwork(new Network(CellTower.fromLacCid(lac, cid)));
}
position.set(Position.PREFIX_TEMP + 1, parser.next());
position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000);
return true;
}
use of org.traccar.helper.DateBuilder in project traccar by tananaev.
the class TrackboxProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = (String) msg;
if (sentence.startsWith("a=connect")) {
String id = sentence.substring(sentence.indexOf("i=") + 2);
if (getDeviceSession(channel, remoteAddress, id) != null) {
sendResponse(channel);
}
return null;
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
Parser parser = new Parser(PATTERN, sentence);
if (!parser.matches()) {
return null;
}
sendResponse(channel);
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
position.set(Position.KEY_HDOP, parser.nextDouble());
position.setAltitude(parser.nextDouble(0));
int fix = parser.nextInt(0);
position.set(Position.KEY_GPS, fix);
position.setValid(fix > 0);
position.setCourse(parser.nextDouble(0));
position.setSpeed(parser.nextDouble(0));
dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
position.setTime(dateBuilder.getDate());
position.set(Position.KEY_SATELLITES, parser.nextInt());
return position;
}
Aggregations