use of org.jboss.netty.handler.codec.http.QueryStringDecoder in project traccar by tananaev.
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.QueryStringDecoder in project traccar by tananaev.
the class PathAwayProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
HttpRequest request = (HttpRequest) msg;
QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, decoder.getParameters().get("UserName").get(0));
if (deviceSession == null) {
return null;
}
Parser parser = new Parser(PATTERN, decoder.getParameters().get("LOC").get(0));
if (!parser.matches()) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
position.setValid(true);
position.setLatitude(parser.nextDouble(0));
position.setLongitude(parser.nextDouble(0));
position.setAltitude(parser.nextDouble(0));
position.setSpeed(parser.nextDouble(0));
position.setCourse(parser.nextDouble(0));
if (channel != null) {
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
channel.write(response).addListener(ChannelFutureListener.CLOSE);
}
return position;
}
use of org.jboss.netty.handler.codec.http.QueryStringDecoder in project traccar by tananaev.
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.QueryStringDecoder in project traccar by tananaev.
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;
}
}
Aggregations