Search in sources :

Example 26 with Device

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

the class BaseProtocolDecoder method findDeviceId.

private long findDeviceId(SocketAddress remoteAddress, String... uniqueIds) {
    if (uniqueIds.length > 0) {
        long deviceId = 0;
        Device device = null;
        try {
            for (String uniqueId : uniqueIds) {
                if (uniqueId != null) {
                    device = Context.getIdentityManager().getByUniqueId(uniqueId);
                    if (device != null) {
                        deviceId = device.getId();
                        break;
                    }
                }
            }
        } catch (Exception e) {
            Log.warning(e);
        }
        if (deviceId == 0 && Context.getConfig().getBoolean("database.registerUnknown")) {
            return addUnknownDevice(uniqueIds[0]);
        }
        if (device != null && !device.getDisabled() || Context.getConfig().getBoolean("database.storeDisabled")) {
            return deviceId;
        }
        StringBuilder message = new StringBuilder();
        if (deviceId == 0) {
            message.append("Unknown device -");
        } else {
            message.append("Disabled device -");
        }
        for (String uniqueId : uniqueIds) {
            message.append(" ").append(uniqueId);
        }
        if (remoteAddress != null) {
            message.append(" (").append(((InetSocketAddress) remoteAddress).getHostString()).append(")");
        }
        Log.warning(message.toString());
    }
    return 0;
}
Also used : Device(org.traccar.model.Device) InetSocketAddress(java.net.InetSocketAddress) SQLException(java.sql.SQLException)

Example 27 with Device

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

the class WebDataHandler method prepareJsonPayload.

protected String prepareJsonPayload(Position position) {
    Map<String, Object> data = new HashMap<>();
    Device device = Context.getIdentityManager().getById(position.getDeviceId());
    data.put(KEY_POSITION, position);
    if (device != null) {
        data.put(KEY_DEVICE, device);
    }
    try {
        return Context.getObjectMapper().writeValueAsString(data);
    } catch (JsonProcessingException e) {
        Log.warning(e);
        return null;
    }
}
Also used : HashMap(java.util.HashMap) Device(org.traccar.model.Device) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 28 with Device

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

the class MotionEventHandler method analyzePosition.

@Override
protected Map<Event, Position> analyzePosition(Position position) {
    long deviceId = position.getDeviceId();
    Device device = Context.getIdentityManager().getById(deviceId);
    if (device == null) {
        return null;
    }
    if (!Context.getIdentityManager().isLatestPosition(position) || !tripsConfig.getProcessInvalidPositions() && !position.getValid()) {
        return null;
    }
    Map<Event, Position> result = null;
    DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId);
    if (deviceState.getMotionState() == null) {
        deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION));
    } else {
        result = updateMotionState(deviceState, position);
    }
    Context.getDeviceManager().setDeviceState(deviceId, deviceState);
    return result;
}
Also used : DeviceState(org.traccar.model.DeviceState) Position(org.traccar.model.Position) Device(org.traccar.model.Device) Event(org.traccar.model.Event)

Example 29 with Device

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

the class DeviceResource method get.

@GET
public Collection<Device> get(@QueryParam("all") boolean all, @QueryParam("userId") long userId, @QueryParam("uniqueId") List<String> uniqueIds, @QueryParam("id") List<Long> deviceIds) throws SQLException {
    DeviceManager deviceManager = Context.getDeviceManager();
    Set<Long> result = null;
    if (all) {
        if (Context.getPermissionsManager().getUserAdmin(getUserId())) {
            result = deviceManager.getAllItems();
        } else {
            Context.getPermissionsManager().checkManager(getUserId());
            result = deviceManager.getManagedItems(getUserId());
        }
    } else if (uniqueIds.isEmpty() && deviceIds.isEmpty()) {
        if (userId == 0) {
            userId = getUserId();
        }
        Context.getPermissionsManager().checkUser(getUserId(), userId);
        if (Context.getPermissionsManager().getUserAdmin(getUserId())) {
            result = deviceManager.getAllUserItems(userId);
        } else {
            result = deviceManager.getUserItems(userId);
        }
    } else {
        result = new HashSet<>();
        for (String uniqueId : uniqueIds) {
            Device device = deviceManager.getByUniqueId(uniqueId);
            Context.getPermissionsManager().checkDevice(getUserId(), device.getId());
            result.add(device.getId());
        }
        for (Long deviceId : deviceIds) {
            Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
            result.add(deviceId);
        }
    }
    return deviceManager.getItems(result);
}
Also used : Device(org.traccar.model.Device) DeviceManager(org.traccar.database.DeviceManager) HashSet(java.util.HashSet) GET(javax.ws.rs.GET)

Example 30 with Device

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

the class ConnectionManager method updateDevice.

public void updateDevice(final long deviceId, String status, Date time) {
    Device device = Context.getIdentityManager().getById(deviceId);
    if (device == null) {
        return;
    }
    String oldStatus = device.getStatus();
    device.setStatus(status);
    if (enableStatusEvents && !status.equals(oldStatus)) {
        String eventType;
        Map<Event, Position> events = new HashMap<>();
        switch(status) {
            case Device.STATUS_ONLINE:
                eventType = Event.TYPE_DEVICE_ONLINE;
                break;
            case Device.STATUS_UNKNOWN:
                eventType = Event.TYPE_DEVICE_UNKNOWN;
                if (updateDeviceState) {
                    events.putAll(updateDeviceState(deviceId));
                }
                break;
            default:
                eventType = Event.TYPE_DEVICE_OFFLINE;
                if (updateDeviceState) {
                    events.putAll(updateDeviceState(deviceId));
                }
                break;
        }
        events.put(new Event(eventType, deviceId), null);
        Context.getNotificationManager().updateEvents(events);
    }
    Timeout timeout = timeouts.remove(deviceId);
    if (timeout != null) {
        timeout.cancel();
    }
    if (time != null) {
        device.setLastUpdate(time);
    }
    if (status.equals(Device.STATUS_ONLINE)) {
        timeouts.put(deviceId, GlobalTimer.getTimer().newTimeout(new TimerTask() {

            @Override
            public void run(Timeout timeout) throws Exception {
                if (!timeout.isCancelled()) {
                    updateDevice(deviceId, Device.STATUS_UNKNOWN, null);
                    activeDevices.remove(deviceId);
                }
            }
        }, deviceTimeout, TimeUnit.MILLISECONDS));
    }
    try {
        Context.getDeviceManager().updateDeviceStatus(device);
    } catch (SQLException error) {
        Log.warning(error);
    }
    updateDevice(device);
}
Also used : TimerTask(org.jboss.netty.util.TimerTask) Position(org.traccar.model.Position) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Device(org.traccar.model.Device) Timeout(org.jboss.netty.util.Timeout) 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