use of org.traccar.model.Position in project traccar by tananaev.
the class MainEventHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
if (e.getMessage() != null && e.getMessage() instanceof Position) {
Position position = (Position) e.getMessage();
try {
Context.getDeviceManager().updateLatestPosition(position);
} catch (SQLException error) {
Log.warning(error);
}
String uniqueId = Context.getIdentityManager().getById(position.getDeviceId()).getUniqueId();
// Log position
StringBuilder s = new StringBuilder();
s.append(formatChannel(e.getChannel())).append(" ");
s.append("id: ").append(uniqueId).append(", ");
s.append("time: ").append(new SimpleDateFormat(Log.DATE_FORMAT).format(position.getFixTime())).append(", ");
s.append("lat: ").append(String.format("%.5f", position.getLatitude())).append(", ");
s.append("lon: ").append(String.format("%.5f", position.getLongitude())).append(", ");
s.append("speed: ").append(String.format("%.1f", position.getSpeed())).append(", ");
s.append("course: ").append(String.format("%.1f", position.getCourse()));
Object cmdResult = position.getAttributes().get(Position.KEY_RESULT);
if (cmdResult != null) {
s.append(", result: ").append(cmdResult);
}
Log.info(s.toString());
Context.getStatisticsManager().registerMessageStored(position.getDeviceId());
}
}
use of org.traccar.model.Position in project traccar by tananaev.
the class PositionResource method getCsv.
@GET
@Produces(TEXT_CSV)
public Response getCsv(@QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
CsvBuilder csv = new CsvBuilder();
csv.addHeaderLine(new Position());
csv.addArray(Context.getDataManager().getPositions(deviceId, DateUtil.parseDate(from), DateUtil.parseDate(to)));
return Response.ok(csv.build()).header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_CSV).build();
}
use of org.traccar.model.Position in project traccar by tananaev.
the class CommandsManager method getCommandTypes.
public Collection<Typed> getCommandTypes(long deviceId, boolean textChannel) {
List<Typed> result = new ArrayList<>();
Position lastPosition = Context.getIdentityManager().getLastPosition(deviceId);
if (lastPosition != null) {
BaseProtocol protocol = Context.getServerManager().getProtocol(lastPosition.getProtocol());
Collection<String> commands;
commands = textChannel ? protocol.getSupportedTextCommands() : protocol.getSupportedDataCommands();
for (String commandKey : commands) {
result.add(new Typed(commandKey));
}
} else {
result.add(new Typed(Command.TYPE_CUSTOM));
}
return result;
}
use of org.traccar.model.Position in project traccar by tananaev.
the class FuelDropEventHandler method analyzePosition.
@Override
protected Map<Event, Position> analyzePosition(Position position) {
Device device = Context.getIdentityManager().getById(position.getDeviceId());
if (device == null) {
return null;
}
if (!Context.getIdentityManager().isLatestPosition(position)) {
return null;
}
double fuelDropThreshold = Context.getDeviceManager().lookupAttributeDouble(device.getId(), ATTRIBUTE_FUEL_DROP_THRESHOLD, 0, false);
if (fuelDropThreshold > 0) {
Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId());
if (position.getAttributes().containsKey(Position.KEY_FUEL_LEVEL) && lastPosition != null && lastPosition.getAttributes().containsKey(Position.KEY_FUEL_LEVEL)) {
double drop = lastPosition.getDouble(Position.KEY_FUEL_LEVEL) - position.getDouble(Position.KEY_FUEL_LEVEL);
if (drop >= fuelDropThreshold) {
Event event = new Event(Event.TYPE_DEVICE_FUEL_DROP, position.getDeviceId(), position.getId());
event.set(ATTRIBUTE_FUEL_DROP_THRESHOLD, fuelDropThreshold);
return Collections.singletonMap(event, position);
}
}
}
return null;
}
use of org.traccar.model.Position in project traccar by tananaev.
the class IgnitionEventHandler method analyzePosition.
@Override
protected Map<Event, Position> analyzePosition(Position position) {
Device device = Context.getIdentityManager().getById(position.getDeviceId());
if (device == null || !Context.getIdentityManager().isLatestPosition(position)) {
return null;
}
Map<Event, Position> result = null;
if (position.getAttributes().containsKey(Position.KEY_IGNITION)) {
boolean ignition = position.getBoolean(Position.KEY_IGNITION);
Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId());
if (lastPosition != null && lastPosition.getAttributes().containsKey(Position.KEY_IGNITION)) {
boolean oldIgnition = lastPosition.getBoolean(Position.KEY_IGNITION);
if (ignition && !oldIgnition) {
result = Collections.singletonMap(new Event(Event.TYPE_IGNITION_ON, position.getDeviceId(), position.getId()), position);
} else if (!ignition && oldIgnition) {
result = Collections.singletonMap(new Event(Event.TYPE_IGNITION_OFF, position.getDeviceId(), position.getId()), position);
}
}
}
return result;
}
Aggregations