use of org.traccar.model.Device 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;
}
use of org.traccar.model.Device in project traccar by tananaev.
the class TextMessageEventHandler method handleTextMessage.
public static void handleTextMessage(String phone, String message) {
Device device = Context.getDeviceManager().getDeviceByPhone(phone);
if (device != null && Context.getNotificationManager() != null) {
Event event = new Event(Event.TYPE_TEXT_MESSAGE, device.getId());
event.set("message", message);
Context.getNotificationManager().updateEvent(event, null);
}
}
use of org.traccar.model.Device in project traccar by tananaev.
the class EventForwarder method prepareJsonPayload.
protected String prepareJsonPayload(Event event, Position position, Set<Long> users) {
Map<String, Object> data = new HashMap<>();
data.put(KEY_EVENT, event);
if (position != null) {
data.put(KEY_POSITION, position);
}
Device device = Context.getIdentityManager().getById(event.getDeviceId());
if (device != null) {
data.put(KEY_DEVICE, device);
}
if (event.getGeofenceId() != 0) {
Geofence geofence = Context.getGeofenceManager().getById(event.getGeofenceId());
if (geofence != null) {
data.put(KEY_GEOFENCE, geofence);
}
}
data.put(KEY_USERS, Context.getUsersManager().getItems(users));
try {
return Context.getObjectMapper().writeValueAsString(data);
} catch (JsonProcessingException e) {
Log.warning(e);
return null;
}
}
use of org.traccar.model.Device in project traccar by tananaev.
the class NotificationFormatter method prepareContext.
public static VelocityContext prepareContext(long userId, Event event, Position position) {
User user = Context.getPermissionsManager().getUser(userId);
Device device = Context.getIdentityManager().getById(event.getDeviceId());
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("user", user);
velocityContext.put("device", device);
velocityContext.put("event", event);
if (position != null) {
velocityContext.put("position", position);
velocityContext.put("speedUnit", ReportUtils.getSpeedUnit(userId));
velocityContext.put("distanceUnit", ReportUtils.getDistanceUnit(userId));
velocityContext.put("volumeUnit", ReportUtils.getVolumeUnit(userId));
}
if (event.getGeofenceId() != 0) {
velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId()));
}
String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID);
if (driverUniqueId != null) {
velocityContext.put("driver", Context.getDriversManager().getDriverByUniqueId(driverUniqueId));
}
velocityContext.put("webUrl", Context.getVelocityEngine().getProperty("web.url"));
velocityContext.put("dateTool", new DateTool());
velocityContext.put("numberTool", new NumberTool());
velocityContext.put("timezone", ReportUtils.getTimezone(userId));
velocityContext.put("locale", Locale.getDefault());
return velocityContext;
}
use of org.traccar.model.Device in project traccar by tananaev.
the class ComputedAttributesHandler method prepareContext.
private MapContext prepareContext(Position position) {
MapContext result = new MapContext();
if (mapDeviceAttributes) {
Device device = Context.getIdentityManager().getById(position.getDeviceId());
if (device != null) {
for (Object key : device.getAttributes().keySet()) {
result.set((String) key, device.getAttributes().get(key));
}
}
}
Set<Method> methods = new HashSet<>(Arrays.asList(position.getClass().getMethods()));
methods.removeAll(Arrays.asList(Object.class.getMethods()));
for (Method method : methods) {
if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
String name = Character.toLowerCase(method.getName().charAt(3)) + method.getName().substring(4);
try {
if (!method.getReturnType().equals(Map.class)) {
result.set(name, method.invoke(position));
} else {
for (Object key : ((Map) method.invoke(position)).keySet()) {
result.set((String) key, ((Map) method.invoke(position)).get(key));
}
}
} catch (IllegalAccessException | InvocationTargetException error) {
Log.warning(error);
}
}
}
return result;
}
Aggregations