Search in sources :

Example 1 with Device

use of org.traccar.model.Device in project traccar by tananaev.

the class BaseProtocolDecoder method addUnknownDevice.

public long addUnknownDevice(String uniqueId) {
    Device device = new Device();
    device.setName(uniqueId);
    device.setUniqueId(uniqueId);
    device.setCategory(Context.getConfig().getString("database.registerUnknown.defaultCategory"));
    long defaultGroupId = Context.getConfig().getLong("database.registerUnknown.defaultGroupId");
    if (defaultGroupId != 0) {
        device.setGroupId(defaultGroupId);
    }
    try {
        Context.getDeviceManager().addItem(device);
        Log.info("Automatically registered device " + uniqueId);
        if (defaultGroupId != 0) {
            Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
            Context.getPermissionsManager().refreshAllExtendedPermissions();
        }
        return device.getId();
    } catch (SQLException e) {
        Log.warning(e);
        return 0;
    }
}
Also used : SQLException(java.sql.SQLException) Device(org.traccar.model.Device)

Example 2 with Device

use of org.traccar.model.Device in project traccar by tananaev.

the class WebDataHandler method formatRequest.

public String formatRequest(Position position) {
    Device device = Context.getIdentityManager().getById(position.getDeviceId());
    String request = url.replace("{name}", device.getName()).replace("{uniqueId}", device.getUniqueId()).replace("{status}", device.getStatus()).replace("{deviceId}", String.valueOf(position.getDeviceId())).replace("{protocol}", String.valueOf(position.getProtocol())).replace("{deviceTime}", String.valueOf(position.getDeviceTime().getTime())).replace("{fixTime}", String.valueOf(position.getFixTime().getTime())).replace("{valid}", String.valueOf(position.getValid())).replace("{latitude}", String.valueOf(position.getLatitude())).replace("{longitude}", String.valueOf(position.getLongitude())).replace("{altitude}", String.valueOf(position.getAltitude())).replace("{speed}", String.valueOf(position.getSpeed())).replace("{course}", String.valueOf(position.getCourse())).replace("{statusCode}", calculateStatus(position));
    if (position.getAddress() != null) {
        try {
            request = request.replace("{address}", URLEncoder.encode(position.getAddress(), StandardCharsets.UTF_8.name()));
        } catch (UnsupportedEncodingException error) {
            Log.warning(error);
        }
    }
    if (request.contains("{attributes}")) {
        try {
            String attributes = Context.getObjectMapper().writeValueAsString(position.getAttributes());
            request = request.replace("{attributes}", URLEncoder.encode(attributes, StandardCharsets.UTF_8.name()));
        } catch (UnsupportedEncodingException | JsonProcessingException error) {
            Log.warning(error);
        }
    }
    if (request.contains("{gprmc}")) {
        request = request.replace("{gprmc}", formatSentence(position));
    }
    return request;
}
Also used : Device(org.traccar.model.Device) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with Device

use of org.traccar.model.Device in project traccar by tananaev.

the class GeofenceManager method recalculateDevicesGeofences.

public void recalculateDevicesGeofences() {
    for (Device device : Context.getDeviceManager().getAllDevices()) {
        List<Long> deviceGeofenceIds = device.getGeofenceIds();
        if (deviceGeofenceIds == null) {
            deviceGeofenceIds = new ArrayList<>();
        } else {
            deviceGeofenceIds.clear();
        }
        Position lastPosition = Context.getIdentityManager().getLastPosition(device.getId());
        if (lastPosition != null && getAllDeviceItems(device.getId()) != null) {
            deviceGeofenceIds.addAll(getCurrentDeviceGeofences(lastPosition));
        }
        device.setGeofenceIds(deviceGeofenceIds);
    }
}
Also used : Position(org.traccar.model.Position) Device(org.traccar.model.Device)

Example 4 with Device

use of org.traccar.model.Device in project traccar by tananaev.

the class PermissionsManager method refreshDeviceAndGroupPermissions.

public final void refreshDeviceAndGroupPermissions() {
    groupPermissions.clear();
    devicePermissions.clear();
    try {
        GroupTree groupTree = new GroupTree(Context.getGroupsManager().getItems(Context.getGroupsManager().getAllItems()), Context.getDeviceManager().getAllDevices());
        for (Permission groupPermission : dataManager.getPermissions(User.class, Group.class)) {
            Set<Long> userGroupPermissions = getGroupPermissions(groupPermission.getOwnerId());
            Set<Long> userDevicePermissions = getDevicePermissions(groupPermission.getOwnerId());
            userGroupPermissions.add(groupPermission.getPropertyId());
            for (Group group : groupTree.getGroups(groupPermission.getPropertyId())) {
                userGroupPermissions.add(group.getId());
            }
            for (Device device : groupTree.getDevices(groupPermission.getPropertyId())) {
                userDevicePermissions.add(device.getId());
            }
        }
        for (Permission devicePermission : dataManager.getPermissions(User.class, Device.class)) {
            getDevicePermissions(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
        }
        groupDevices.clear();
        for (long groupId : Context.getGroupsManager().getAllItems()) {
            for (Device device : groupTree.getDevices(groupId)) {
                getGroupDevices(groupId).add(device.getId());
            }
        }
    } catch (SQLException | ClassNotFoundException error) {
        Log.warning(error);
    }
    deviceUsers.clear();
    for (Map.Entry<Long, Set<Long>> entry : devicePermissions.entrySet()) {
        for (long deviceId : entry.getValue()) {
            getAllDeviceUsers(deviceId).add(entry.getKey());
        }
    }
}
Also used : Group(org.traccar.model.Group) Set(java.util.Set) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) Device(org.traccar.model.Device) Permission(org.traccar.model.Permission) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with Device

use of org.traccar.model.Device 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;
}
Also used : Position(org.traccar.model.Position) Device(org.traccar.model.Device) Event(org.traccar.model.Event)

Aggregations

Device (org.traccar.model.Device)33 Position (org.traccar.model.Position)10 Event (org.traccar.model.Event)9 Group (org.traccar.model.Group)7 SQLException (java.sql.SQLException)6 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 DeviceReport (org.traccar.reports.model.DeviceReport)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 HashSet (java.util.HashSet)3 Map (java.util.Map)2 DeviceState (org.traccar.model.DeviceState)2 Geofence (org.traccar.model.Geofence)2 Permission (org.traccar.model.Permission)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 InetSocketAddress (java.net.InetSocketAddress)1