use of org.traccar.model.Network in project traccar by tananaev.
the class Gl200BinaryProtocolDecoder method decodeInformation.
private Position decodeInformation(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
Position position = new Position(getProtocolName());
int type = buf.readUnsignedByte();
// mask
buf.readUnsignedInt();
// length
buf.readUnsignedShort();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.format("%015d", buf.readLong()));
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
// device type
buf.readUnsignedByte();
// protocol version
buf.readUnsignedShort();
position.set(Position.KEY_VERSION_FW, String.valueOf(buf.readUnsignedShort()));
if (type == MSG_INF_VER) {
// hardware version
buf.readUnsignedShort();
// mcu version
buf.readUnsignedShort();
// reserved
buf.readUnsignedShort();
}
// motion status
buf.readUnsignedByte();
// reserved
buf.readUnsignedByte();
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
// mode
buf.readUnsignedByte();
// last fix time
buf.skipBytes(7);
// reserved
buf.readUnsignedByte();
buf.readUnsignedByte();
// response report mask
buf.readUnsignedShort();
// ign interval
buf.readUnsignedShort();
// igf interval
buf.readUnsignedShort();
// reserved
buf.readUnsignedInt();
// reserved
buf.readUnsignedByte();
if (type == MSG_INF_BAT) {
position.set(Position.KEY_CHARGE, buf.readUnsignedByte() != 0);
position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte());
}
// iccid
buf.skipBytes(10);
if (type == MSG_INF_CSQ) {
position.set(Position.KEY_RSSI, buf.readUnsignedByte());
buf.readUnsignedByte();
}
// time zone flags
buf.readUnsignedByte();
// time zone offset
buf.readUnsignedShort();
if (type == MSG_INF_GIR) {
// gir trigger
buf.readUnsignedByte();
// cell number
buf.readUnsignedByte();
position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort())));
// ta
buf.readUnsignedByte();
// rx level
buf.readUnsignedByte();
}
getLastLocation(position, decodeTime(buf));
return position;
}
use of org.traccar.model.Network in project traccar by tananaev.
the class Gps103ProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = (String) msg;
// Send response #1
if (sentence.contains("##")) {
if (channel != null) {
channel.write("LOAD", remoteAddress);
Parser handshakeParser = new Parser(PATTERN_HANDSHAKE, sentence);
if (handshakeParser.matches()) {
getDeviceSession(channel, remoteAddress, handshakeParser.next());
}
}
return null;
}
// Send response #2
if (!sentence.isEmpty() && Character.isDigit(sentence.charAt(0))) {
if (channel != null) {
channel.write("ON", remoteAddress);
}
int start = sentence.indexOf("imei:");
if (start >= 0) {
sentence = sentence.substring(start);
} else {
return null;
}
}
Position position = new Position(getProtocolName());
Parser parser = new Parser(PATTERN_NETWORK, sentence);
if (parser.matches()) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, null);
position.setNetwork(new Network(CellTower.fromLacCid(parser.nextHexInt(0), parser.nextHexInt(0))));
return position;
}
parser = new Parser(PATTERN_OBD, sentence);
if (parser.matches()) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, parser.nextDateTime());
position.set(Position.KEY_ODOMETER, parser.nextInt(0));
// instant fuel consumption
parser.nextDouble(0);
position.set(Position.KEY_FUEL_CONSUMPTION, parser.nextDouble(0));
position.set(Position.KEY_HOURS, parser.nextInt());
position.set(Position.KEY_OBD_SPEED, parser.nextInt(0));
position.set(Position.KEY_ENGINE_LOAD, parser.next());
position.set(Position.KEY_COOLANT_TEMP, parser.nextInt());
position.set(Position.KEY_THROTTLE, parser.next());
position.set(Position.KEY_RPM, parser.nextInt(0));
position.set(Position.KEY_BATTERY, parser.nextDouble(0));
position.set(Position.KEY_DTCS, parser.next().replace(',', ' ').trim());
return position;
}
parser = new Parser(PATTERN, sentence);
if (!parser.matches()) {
return null;
}
String imei = parser.next();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
String alarm = parser.next();
position.set(Position.KEY_ALARM, decodeAlarm(alarm));
if (alarm.equals("help me")) {
if (channel != null) {
channel.write("**,imei:" + imei + ",E;", remoteAddress);
}
} else if (alarm.equals("acc on")) {
position.set(Position.KEY_IGNITION, true);
} else if (alarm.equals("acc off")) {
position.set(Position.KEY_IGNITION, false);
} else if (alarm.startsWith("T:")) {
position.set(Position.PREFIX_TEMP + 1, alarm.substring(2));
} else if (alarm.startsWith("oil ")) {
position.set("oil", alarm.substring(4));
} else if (!position.getAttributes().containsKey(Position.KEY_ALARM) && !alarm.equals("tracker")) {
position.set(Position.KEY_EVENT, alarm);
}
DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
int localHours = parser.nextInt(0);
int localMinutes = parser.nextInt(0);
String rfid = parser.next();
if (alarm.equals("rfid")) {
position.set(Position.KEY_DRIVER_UNIQUE_ID, rfid);
}
String utcHours = parser.next();
String utcMinutes = parser.next();
dateBuilder.setTime(localHours, localMinutes, parser.nextInt(0));
// Timezone calculation
if (utcHours != null && utcMinutes != null) {
int deltaMinutes = (localHours - Integer.parseInt(utcHours)) * 60;
deltaMinutes += localMinutes - Integer.parseInt(utcMinutes);
if (deltaMinutes <= -12 * 60) {
deltaMinutes += 24 * 60;
} else if (deltaMinutes > 12 * 60) {
deltaMinutes -= 24 * 60;
}
dateBuilder.addMinute(-deltaMinutes);
}
position.setTime(dateBuilder.getDate());
position.setValid(parser.next().equals("A"));
position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_HEM));
position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_HEM));
position.setSpeed(parser.nextDouble(0));
position.setCourse(parser.nextDouble(0));
position.setAltitude(parser.nextDouble(0));
for (int i = 1; i <= 5; i++) {
position.set(Position.PREFIX_IO + i, parser.next());
}
return position;
}
use of org.traccar.model.Network in project traccar by tananaev.
the class StarLinkProtocolDecoder 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;
}
int type = parser.nextInt(0);
if (type != MSG_EVENT_REPORT) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setValid(true);
position.set(Position.KEY_INDEX, parser.nextInt(0));
String[] data = parser.next().split(",");
Integer lac = null, cid = null;
int event = 0;
for (int i = 0; i < Math.min(data.length, dataTags.length); i++) {
if (data[i].isEmpty()) {
continue;
}
switch(dataTags[i]) {
case "#EDT#":
position.setDeviceTime(dateFormat.parse(data[i]));
break;
case "#EID#":
event = Integer.parseInt(data[i]);
position.set(Position.KEY_ALARM, decodeAlarm(event));
position.set(Position.KEY_EVENT, event);
break;
case "#PDT#":
position.setFixTime(dateFormat.parse(data[i]));
break;
case "#LAT#":
position.setLatitude(parseCoordinate(data[i]));
break;
case "#LONG#":
position.setLongitude(parseCoordinate(data[i]));
break;
case "#SPD#":
position.setSpeed(Double.parseDouble(data[i]));
break;
case "#HEAD#":
position.setCourse(Integer.parseInt(data[i]));
break;
case "#ODO#":
position.set(Position.KEY_ODOMETER, Long.parseLong(data[i]) * 1000);
break;
case "#IN1#":
position.set(Position.PREFIX_IN + 1, Integer.parseInt(data[i]));
break;
case "#IN2#":
position.set(Position.PREFIX_IN + 2, Integer.parseInt(data[i]));
break;
case "#IN3#":
position.set(Position.PREFIX_IN + 3, Integer.parseInt(data[i]));
break;
case "#IN4#":
position.set(Position.PREFIX_IN + 4, Integer.parseInt(data[i]));
break;
case "#OUT1#":
position.set(Position.PREFIX_OUT + 1, Integer.parseInt(data[i]));
break;
case "#OUT2#":
position.set(Position.PREFIX_OUT + 2, Integer.parseInt(data[i]));
break;
case "#OUT3#":
position.set(Position.PREFIX_OUT + 3, Integer.parseInt(data[i]));
break;
case "#OUT4#":
position.set(Position.PREFIX_OUT + 4, Integer.parseInt(data[i]));
break;
case "#LAC#":
if (!data[i].isEmpty()) {
lac = Integer.parseInt(data[i]);
}
break;
case "#CID#":
if (!data[i].isEmpty()) {
cid = Integer.parseInt(data[i]);
}
break;
case "#VIN#":
position.set(Position.KEY_POWER, Double.parseDouble(data[i]));
break;
case "#VBAT#":
position.set(Position.KEY_BATTERY, Double.parseDouble(data[i]));
break;
case "#DEST#":
position.set("destination", data[i]);
break;
case "#IGN#":
position.set(Position.KEY_IGNITION, data[i].equals("1"));
break;
case "#ENG#":
position.set("engine", data[i].equals("1"));
break;
default:
break;
}
}
if (position.getFixTime() == null) {
getLastLocation(position, null);
}
if (lac != null && cid != null) {
position.setNetwork(new Network(CellTower.fromLacCid(lac, cid)));
}
if (event == 20) {
String rfid = data[data.length - 1];
if (rfid.matches("0+")) {
rfid = data[data.length - 2];
}
position.set(Position.KEY_DRIVER_UNIQUE_ID, rfid);
}
return position;
}
use of org.traccar.model.Network in project traccar by tananaev.
the class SuntechProtocolDecoder method decode2356.
private Position decode2356(Channel channel, SocketAddress remoteAddress, String protocol, String[] values) throws ParseException {
int index = 0;
String type = values[index++].substring(5);
if (!type.equals("STT") && !type.equals("EMG") && !type.equals("EVT") && !type.equals("ALT")) {
return null;
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, values[index++]);
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_TYPE, type);
if (protocol.equals("ST300") || protocol.equals("ST500") || protocol.equals("ST600")) {
// model
index += 1;
}
position.set(Position.KEY_VERSION_FW, values[index++]);
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
position.setTime(dateFormat.parse(values[index++] + values[index++]));
if (!protocol.equals("ST500")) {
int cid = Integer.parseInt(values[index++], 16);
if (protocol.equals("ST600")) {
position.setNetwork(new Network(CellTower.from(Integer.parseInt(values[index++]), Integer.parseInt(values[index++]), Integer.parseInt(values[index++], 16), cid, Integer.parseInt(values[index++]))));
}
}
position.setLatitude(Double.parseDouble(values[index++]));
position.setLongitude(Double.parseDouble(values[index++]));
position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(values[index++])));
position.setCourse(Double.parseDouble(values[index++]));
position.set(Position.KEY_SATELLITES, Integer.parseInt(values[index++]));
position.setValid(values[index++].equals("1"));
position.set(Position.KEY_ODOMETER, Integer.parseInt(values[index++]));
position.set(Position.KEY_POWER, Double.parseDouble(values[index++]));
String io = values[index++];
if (io.length() == 6) {
position.set(Position.KEY_IGNITION, io.charAt(0) == '1');
position.set(Position.PREFIX_IN + 1, io.charAt(1) == '1');
position.set(Position.PREFIX_IN + 2, io.charAt(2) == '1');
position.set(Position.PREFIX_IN + 3, io.charAt(3) == '1');
position.set(Position.PREFIX_OUT + 1, io.charAt(4) == '1');
position.set(Position.PREFIX_OUT + 2, io.charAt(5) == '1');
}
switch(type) {
case "STT":
// mode
index += 1;
position.set(Position.KEY_INDEX, Integer.parseInt(values[index++]));
break;
case "EMG":
position.set(Position.KEY_ALARM, decodeEmergency(Integer.parseInt(values[index++])));
break;
case "EVT":
position.set(Position.KEY_EVENT, Integer.parseInt(values[index++]));
break;
case "ALT":
position.set(Position.KEY_ALARM, decodeAlert(Integer.parseInt(values[index++])));
break;
default:
break;
}
if (hbm) {
if (index < values.length) {
position.set(Position.KEY_HOURS, Integer.parseInt(values[index++]));
}
if (index < values.length) {
position.set(Position.KEY_BATTERY, Double.parseDouble(values[index++]));
}
if (index < values.length && values[index++].equals("0")) {
position.set(Position.KEY_ARCHIVE, true);
}
if (includeAdc) {
position.set(Position.PREFIX_ADC + 1, Double.parseDouble(values[index++]));
position.set(Position.PREFIX_ADC + 2, Double.parseDouble(values[index++]));
position.set(Position.PREFIX_ADC + 3, Double.parseDouble(values[index++]));
}
if (values.length - index >= 2) {
String driverUniqueId = values[index++];
if (values[index++].equals("1") && !driverUniqueId.isEmpty()) {
position.set(Position.KEY_DRIVER_UNIQUE_ID, driverUniqueId);
}
}
if (includeTemp) {
for (int i = 1; i <= 3; i++) {
String temperature = values[index++];
String value = temperature.substring(temperature.indexOf(':') + 1);
if (!value.isEmpty()) {
position.set(Position.PREFIX_TEMP + i, Double.parseDouble(value));
}
}
}
}
return position;
}
use of org.traccar.model.Network in project traccar by tananaev.
the class T800xProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
buf.skipBytes(2);
int type = buf.readUnsignedByte();
// length
buf.readUnsignedShort();
int index = buf.readUnsignedShort();
ChannelBuffer imei = buf.readBytes(8);
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ChannelBuffers.hexDump(imei).substring(1));
if (deviceSession == null) {
return null;
}
if (type == MSG_LOGIN || type == MSG_ALARM || type == MSG_HEARTBEAT) {
sendResponse(channel, type, imei);
}
if (type == MSG_GPS || type == MSG_ALARM) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_INDEX, index);
// acc on interval
buf.readUnsignedShort();
// acc off interval
buf.readUnsignedShort();
// angle compensation
buf.readUnsignedByte();
// distance compensation
buf.readUnsignedShort();
// speed alarm
buf.readUnsignedShort();
int locationStatus = buf.readUnsignedByte();
// gsensor manager status
buf.readUnsignedByte();
// other flags
buf.readUnsignedByte();
// heartbeat
buf.readUnsignedByte();
// relay status
buf.readUnsignedByte();
// drag alarm setting
buf.readUnsignedShort();
int io = buf.readUnsignedShort();
position.set(Position.KEY_IGNITION, BitUtil.check(io, 14));
position.set("ac", BitUtil.check(io, 13));
position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte()));
// reserved
buf.readUnsignedByte();
position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
int battery = BcdUtil.readInteger(buf, 2);
if (battery == 0) {
battery = 100;
}
position.set(Position.KEY_BATTERY, battery);
DateBuilder dateBuilder = new DateBuilder().setYear(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)).setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2));
if (BitUtil.check(locationStatus, 6)) {
position.setValid(!BitUtil.check(locationStatus, 7));
position.setTime(dateBuilder.getDate());
position.setAltitude(readSwappedFloat(buf));
position.setLongitude(readSwappedFloat(buf));
position.setLatitude(readSwappedFloat(buf));
position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4) * 0.1));
position.setCourse(buf.readUnsignedShort());
} else {
getLastLocation(position, dateBuilder.getDate());
byte[] array = new byte[16];
buf.readBytes(array);
ChannelBuffer swapped = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, array);
position.setNetwork(new Network(CellTower.from(swapped.readUnsignedShort(), swapped.readUnsignedShort(), swapped.readUnsignedShort(), swapped.readUnsignedShort())));
// two more cell towers
}
return position;
}
return null;
}
Aggregations