use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.
the class OpenGtsProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
HttpRequest request = (HttpRequest) msg;
QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
Map<String, List<String>> params = decoder.getParameters();
Position position = new Position();
position.setProtocol(getProtocolName());
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
String value = entry.getValue().get(0);
switch(entry.getKey()) {
case "id":
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
if (deviceSession == null) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
break;
case "gprmc":
Parser parser = new Parser(PATTERN, value);
if (!parser.matches()) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
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());
break;
case "alt":
position.setAltitude(Double.parseDouble(value));
break;
case "batt":
position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value));
break;
default:
break;
}
}
if (position.getDeviceId() != 0) {
sendResponse(channel, HttpResponseStatus.OK);
return position;
} else {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
}
use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.
the class OsmAndProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
HttpRequest request = (HttpRequest) msg;
QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
Map<String, List<String>> params = decoder.getParameters();
if (params.isEmpty()) {
decoder = new QueryStringDecoder(request.getContent().toString(StandardCharsets.US_ASCII), false);
params = decoder.getParameters();
}
Position position = new Position(getProtocolName());
position.setValid(true);
Network network = new Network();
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
for (String value : entry.getValue()) {
switch(entry.getKey()) {
case "id":
case "deviceid":
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
if (deviceSession == null) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
break;
case "valid":
position.setValid(Boolean.parseBoolean(value));
break;
case "timestamp":
try {
long timestamp = Long.parseLong(value);
if (timestamp < Integer.MAX_VALUE) {
timestamp *= 1000;
}
position.setTime(new Date(timestamp));
} catch (NumberFormatException error) {
if (value.contains("T")) {
position.setTime(new Date(ISODateTimeFormat.dateTimeParser().parseMillis(value)));
} else {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
position.setTime(dateFormat.parse(value));
}
}
break;
case "lat":
position.setLatitude(Double.parseDouble(value));
break;
case "lon":
position.setLongitude(Double.parseDouble(value));
break;
case "location":
String[] location = value.split(",");
position.setLatitude(Double.parseDouble(location[0]));
position.setLongitude(Double.parseDouble(location[1]));
break;
case "cell":
String[] cell = value.split(",");
if (cell.length > 4) {
network.addCellTower(CellTower.from(Integer.parseInt(cell[0]), Integer.parseInt(cell[1]), Integer.parseInt(cell[2]), Integer.parseInt(cell[3]), Integer.parseInt(cell[4])));
} else {
network.addCellTower(CellTower.from(Integer.parseInt(cell[0]), Integer.parseInt(cell[1]), Integer.parseInt(cell[2]), Integer.parseInt(cell[3])));
}
break;
case "wifi":
String[] wifi = value.split(",");
network.addWifiAccessPoint(WifiAccessPoint.from(wifi[0].replace('-', ':'), Integer.parseInt(wifi[1])));
break;
case "speed":
position.setSpeed(convertSpeed(Double.parseDouble(value), "kn"));
break;
case "bearing":
case "heading":
position.setCourse(Double.parseDouble(value));
break;
case "altitude":
position.setAltitude(Double.parseDouble(value));
break;
case "accuracy":
position.setAccuracy(Double.parseDouble(value));
break;
case "hdop":
position.set(Position.KEY_HDOP, Double.parseDouble(value));
break;
case "batt":
position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value));
break;
case "driverUniqueId":
position.set(Position.KEY_DRIVER_UNIQUE_ID, value);
break;
default:
try {
position.set(entry.getKey(), Double.parseDouble(value));
} catch (NumberFormatException e) {
switch(value) {
case "true":
position.set(entry.getKey(), true);
break;
case "false":
position.set(entry.getKey(), false);
break;
default:
position.set(entry.getKey(), value);
break;
}
}
break;
}
}
}
if (position.getFixTime() == null) {
position.setTime(new Date());
}
if (network.getCellTowers() != null || network.getWifiAccessPoints() != null) {
position.setNetwork(network);
}
if (position.getLatitude() == 0 && position.getLongitude() == 0) {
getLastLocation(position, position.getDeviceTime());
}
if (position.getDeviceId() != 0) {
sendResponse(channel, HttpResponseStatus.OK);
return position;
} else {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
}
use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.
the class OwnTracksProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
HttpRequest request = (HttpRequest) msg;
JsonObject root = Json.createReader(new StringReader(request.getContent().toString(StandardCharsets.US_ASCII))).readObject();
if (!root.containsKey("_type") || !root.getString("_type").equals("location")) {
sendResponse(channel, HttpResponseStatus.OK);
return null;
}
Position position = new Position(getProtocolName());
position.setValid(true);
position.setLatitude(root.getJsonNumber("lat").doubleValue());
position.setLongitude(root.getJsonNumber("lon").doubleValue());
if (root.containsKey("vel")) {
position.setSpeed(UnitsConverter.knotsFromKph(root.getInt("vel")));
}
if (root.containsKey("alt")) {
position.setAltitude(root.getInt("alt"));
}
if (root.containsKey("cog")) {
position.setCourse(root.getInt("cog"));
}
if (root.containsKey("acc")) {
position.setAccuracy(root.getInt("acc"));
}
if (root.containsKey("t")) {
position.set("t", root.getString("t"));
}
if (root.containsKey("batt")) {
position.set(Position.KEY_BATTERY, root.getInt("batt"));
}
position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000));
String uniqueId;
if (root.containsKey("topic")) {
uniqueId = root.getString("topic");
if (root.containsKey("tid")) {
position.set("tid", root.getString("tid"));
}
} else {
uniqueId = root.getString("tid");
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, uniqueId);
if (deviceSession == null) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
sendResponse(channel, HttpResponseStatus.OK);
return position;
}
use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.
the class PiligrimProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
HttpRequest request = (HttpRequest) msg;
String uri = request.getUri();
if (uri.startsWith("/config")) {
sendResponse(channel, "CONFIG: OK");
} else if (uri.startsWith("/addlog")) {
sendResponse(channel, "ADDLOG: OK");
} else if (uri.startsWith("/inform")) {
sendResponse(channel, "INFORM: OK");
} else if (uri.startsWith("/bingps")) {
sendResponse(channel, "BINGPS: OK");
QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, decoder.getParameters().get("imei").get(0));
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
ChannelBuffer buf = request.getContent();
while (buf.readableBytes() > 2) {
// header
buf.readUnsignedByte();
int type = buf.readUnsignedByte();
// length
buf.readUnsignedByte();
if (type == MSG_GPS || type == MSG_GPS_SENSORS) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
DateBuilder dateBuilder = new DateBuilder().setDay(buf.readUnsignedByte()).setMonth(buf.getByte(buf.readerIndex()) & 0x0f).setYear(2010 + (buf.readUnsignedByte() >> 4)).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
position.setTime(dateBuilder.getDate());
double latitude = buf.readUnsignedByte();
latitude += buf.readUnsignedByte() / 60.0;
latitude += buf.readUnsignedByte() / 6000.0;
latitude += buf.readUnsignedByte() / 600000.0;
double longitude = buf.readUnsignedByte();
longitude += buf.readUnsignedByte() / 60.0;
longitude += buf.readUnsignedByte() / 6000.0;
longitude += buf.readUnsignedByte() / 600000.0;
int flags = buf.readUnsignedByte();
if (BitUtil.check(flags, 0)) {
latitude = -latitude;
}
if (BitUtil.check(flags, 1)) {
longitude = -longitude;
}
position.setLatitude(latitude);
position.setLongitude(longitude);
int satellites = buf.readUnsignedByte();
position.set(Position.KEY_SATELLITES, satellites);
position.setValid(satellites >= 3);
position.setSpeed(buf.readUnsignedByte());
double course = buf.readUnsignedByte() << 1;
course += (flags >> 2) & 1;
course += buf.readUnsignedByte() / 100.0;
position.setCourse(course);
if (type == MSG_GPS_SENSORS) {
double power = buf.readUnsignedByte();
power += buf.readUnsignedByte() << 8;
position.set(Position.KEY_POWER, power * 0.01);
double battery = buf.readUnsignedByte();
battery += buf.readUnsignedByte() << 8;
position.set(Position.KEY_BATTERY, battery * 0.01);
buf.skipBytes(6);
}
positions.add(position);
} else if (type == MSG_EVENTS) {
buf.skipBytes(13);
}
}
return positions;
}
return null;
}
use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.
the class SigfoxProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
HttpRequest request = (HttpRequest) msg;
JsonObject json = Json.createReader(new StringReader(URLDecoder.decode(request.getContent().toString(StandardCharsets.UTF_8).split("=")[0], "UTF-8"))).readObject();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, json.getString("device"));
if (deviceSession == null) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(new Date(json.getInt("time") * 1000L));
ChannelBuffer buf = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, DataConverter.parseHex(json.getString("data")));
int type = buf.readUnsignedByte() >> 4;
if (type == 0) {
position.setValid(true);
position.setLatitude(buf.readInt() * 0.0000001);
position.setLongitude(buf.readInt() * 0.0000001);
position.setCourse(buf.readUnsignedByte() * 2);
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.025);
} else {
getLastLocation(position, position.getDeviceTime());
}
position.set(Position.KEY_RSSI, json.getJsonNumber("rssi").doubleValue());
position.set(Position.KEY_INDEX, json.getInt("seqNumber"));
sendResponse(channel, HttpResponseStatus.OK);
return position;
}
Aggregations