Skip to content

Commit

Permalink
Merge pull request traccar#2116 from Abyss777/device_manager_final
Browse files Browse the repository at this point in the history
Move code related to devices to DeviceManager
  • Loading branch information
tananaev authored Jul 16, 2016
2 parents 6fefd48 + d71210f commit 6aa4a84
Show file tree
Hide file tree
Showing 19 changed files with 342 additions and 222 deletions.
6 changes: 4 additions & 2 deletions src/org/traccar/BaseProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public boolean identify(String uniqueId, Channel channel, SocketAddress remoteAd
Device device = Context.getIdentityManager().getDeviceByUniqueId(uniqueId);
if (device != null) {
deviceId = device.getId();
Context.getConnectionManager().addActiveDevice(deviceId, protocol, channel, remoteAddress);
if (Context.getConnectionManager() != null) {
Context.getConnectionManager().addActiveDevice(deviceId, protocol, channel, remoteAddress);
}
return true;
} else {
deviceId = 0;
Expand Down Expand Up @@ -78,7 +80,7 @@ public BaseProtocolDecoder(Protocol protocol) {
public void getLastLocation(Position position, Date deviceTime) {
position.setOutdated(true);

Position last = Context.getConnectionManager().getLastPosition(getDeviceId());
Position last = Context.getIdentityManager().getLastPosition(getDeviceId());
if (last != null) {
position.setFixTime(last.getFixTime());
position.setValid(last.getValid());
Expand Down
17 changes: 14 additions & 3 deletions src/org/traccar/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.ning.http.client.AsyncHttpClient;
import org.traccar.database.ConnectionManager;
import org.traccar.database.DataManager;
import org.traccar.database.DeviceManager;
import org.traccar.database.IdentityManager;
import org.traccar.database.NotificationManager;
import org.traccar.database.PermissionsManager;
Expand Down Expand Up @@ -67,6 +68,12 @@ public static DataManager getDataManager() {
return dataManager;
}

private static DeviceManager deviceManager;

public static DeviceManager getDeviceManager() {
return deviceManager;
}

private static ConnectionManager connectionManager;

public static ConnectionManager getConnectionManager() {
Expand Down Expand Up @@ -142,7 +149,12 @@ public static void init(String[] arguments) throws Exception {
if (config.hasKey("database.url")) {
dataManager = new DataManager(config);
}
identityManager = dataManager;

if (dataManager != null) {
deviceManager = new DeviceManager(dataManager);
}

identityManager = deviceManager;

if (config.getBoolean("geocoder.enable")) {
String type = config.getString("geocoder.type", "google");
Expand Down Expand Up @@ -205,7 +217,7 @@ public static void init(String[] arguments) throws Exception {

permissionsManager = new PermissionsManager(dataManager);

connectionManager = new ConnectionManager(dataManager);
connectionManager = new ConnectionManager();

if (config.getBoolean("event.geofenceHandler")) {
geofenceManager = new GeofenceManager(dataManager);
Expand All @@ -225,7 +237,6 @@ public static void init(String[] arguments) throws Exception {

public static void init(IdentityManager testIdentityManager) {
config = new Config();
connectionManager = new ConnectionManager(null);
identityManager = testIdentityManager;
}

Expand Down
5 changes: 1 addition & 4 deletions src/org/traccar/DefaultDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ protected Position handlePosition(Position position) {

try {
Context.getDataManager().addPosition(position);
Position lastPosition = Context.getConnectionManager().getLastPosition(position.getDeviceId());
if (lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) > 0) {
Context.getDataManager().updateLatestPosition(position);
}
Context.getDeviceManager().updateLatestPosition(position);
} catch (Exception error) {
Log.warning(error);
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/DistanceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
public class DistanceHandler extends BaseDataHandler {

private Position getLastPosition(long deviceId) {
if (Context.getConnectionManager() != null) {
return Context.getConnectionManager().getLastPosition(deviceId);
if (Context.getIdentityManager() != null) {
return Context.getIdentityManager().getLastPosition(deviceId);
}
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/FilterHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public FilterHandler() {
}

private Position getLastPosition(long deviceId) {
if (Context.getConnectionManager() != null) {
return Context.getConnectionManager().getLastPosition(deviceId);
if (Context.getIdentityManager() != null) {
return Context.getIdentityManager().getLastPosition(deviceId);
}
return null;
}
Expand Down
7 changes: 1 addition & 6 deletions src/org/traccar/MainEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {

Position position = (Position) e.getMessage();

String uniqueId = Context.getDataManager().getDeviceById(position.getDeviceId()).getUniqueId();
String uniqueId = Context.getIdentityManager().getDeviceById(position.getDeviceId()).getUniqueId();

// Log position
StringBuilder s = new StringBuilder();
Expand All @@ -54,11 +54,6 @@ public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
s.append(", result: ").append(cmdResult);
}
Log.info(s.toString());

Position lastPosition = Context.getConnectionManager().getLastPosition(position.getDeviceId());
if (lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) > 0) {
Context.getConnectionManager().updatePosition(position);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/traccar/api/AsyncSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void onWebSocketConnect(Session session) {
super.onWebSocketConnect(session);

Map<String, Collection<?>> data = new HashMap<>();
data.put(KEY_POSITIONS, Context.getConnectionManager().getInitialState(userId));
data.put(KEY_POSITIONS, Context.getDeviceManager().getInitialState(userId));
sendData(data);

Context.getConnectionManager().addListener(userId, this);
Expand Down
10 changes: 5 additions & 5 deletions src/org/traccar/api/resource/DeviceResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ public Collection<Device> get(
@QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException {
if (all) {
Context.getPermissionsManager().checkAdmin(getUserId());
return Context.getDataManager().getAllDevicesCached();
return Context.getDeviceManager().getAllDevices();
} else {
if (userId == 0) {
userId = getUserId();
}
Context.getPermissionsManager().checkUser(getUserId(), userId);
return Context.getDataManager().getDevices(userId);
return Context.getDeviceManager().getDevices(userId);
}
}

@POST
public Response add(Device entity) throws SQLException {
Context.getPermissionsManager().checkReadonly(getUserId());
Context.getDataManager().addDevice(entity);
Context.getDeviceManager().addDevice(entity);
Context.getDataManager().linkDevice(getUserId(), entity.getId());
Context.getPermissionsManager().refresh();
if (Context.getGeofenceManager() != null) {
Expand All @@ -71,7 +71,7 @@ public Response add(Device entity) throws SQLException {
public Response update(@PathParam("id") long id, Device entity) throws SQLException {
Context.getPermissionsManager().checkReadonly(getUserId());
Context.getPermissionsManager().checkDevice(getUserId(), id);
Context.getDataManager().updateDevice(entity);
Context.getDeviceManager().updateDevice(entity);
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refresh();
}
Expand All @@ -83,7 +83,7 @@ public Response update(@PathParam("id") long id, Device entity) throws SQLExcept
public Response remove(@PathParam("id") long id) throws SQLException {
Context.getPermissionsManager().checkReadonly(getUserId());
Context.getPermissionsManager().checkDevice(getUserId(), id);
Context.getDataManager().removeDevice(id);
Context.getDeviceManager().removeDevice(id);
Context.getPermissionsManager().refresh();
if (Context.getGeofenceManager() != null) {
Context.getGeofenceManager().refresh();
Expand Down
2 changes: 1 addition & 1 deletion src/org/traccar/api/resource/PositionResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Collection<Position> get(
@QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to)
throws SQLException {
if (deviceId == 0) {
return Context.getConnectionManager().getInitialState(getUserId());
return Context.getDeviceManager().getInitialState(getUserId());
} else {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
return Context.getDataManager().getPositions(
Expand Down
72 changes: 21 additions & 51 deletions src/org/traccar/database/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@

import java.net.SocketAddress;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
Expand All @@ -45,21 +42,11 @@ public class ConnectionManager {
private final long deviceTimeout;

private final Map<Long, ActiveDevice> activeDevices = new HashMap<>();
private final Map<Long, Position> positions = new HashMap<>();
private final Map<Long, Set<UpdateListener>> listeners = new HashMap<>();
private final Map<Long, Timeout> timeouts = new HashMap<>();

public ConnectionManager(DataManager dataManager) {
public ConnectionManager() {
deviceTimeout = Context.getConfig().getLong("status.timeout", DEFAULT_TIMEOUT) * 1000;
if (dataManager != null) {
try {
for (Position position : dataManager.getLatestPositions()) {
positions.put(position.getDeviceId(), position);
}
} catch (SQLException error) {
Log.warning(error);
}
}
}

public void addActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) {
Expand All @@ -80,31 +67,28 @@ public ActiveDevice getActiveDevice(long deviceId) {
return activeDevices.get(deviceId);
}

public synchronized void updateDevice(final long deviceId, String status, Date time) {
public void updateDevice(final long deviceId, String status, Date time) {
Device device = Context.getIdentityManager().getDeviceById(deviceId);
if (device == null) {
return;
}

if (status.equals(Device.STATUS_MOVING) || status.equals(Device.STATUS_STOPPED)) {
device.setMotion(status);
} else {
if (!status.equals(device.getStatus())) {
Event event = new Event(Event.TYPE_DEVICE_OFFLINE, deviceId);
if (status.equals(Device.STATUS_ONLINE)) {
event.setType(Event.TYPE_DEVICE_ONLINE);
}
if (Context.getNotificationManager() != null) {
Context.getNotificationManager().updateEvent(event, null);
}
if (!status.equals(device.getStatus())) {
Event event = new Event(Event.TYPE_DEVICE_OFFLINE, deviceId);
if (status.equals(Device.STATUS_ONLINE)) {
event.setType(Event.TYPE_DEVICE_ONLINE);
}
device.setStatus(status);

Timeout timeout = timeouts.remove(deviceId);
if (timeout != null) {
timeout.cancel();
if (Context.getNotificationManager() != null) {
Context.getNotificationManager().updateEvent(event, null);
}
}
device.setStatus(status);

Timeout timeout = timeouts.remove(deviceId);
if (timeout != null) {
timeout.cancel();
}


if (time != null) {
device.setLastUpdate(time);
Expand All @@ -122,12 +106,16 @@ public void run(Timeout timeout) throws Exception {
}

try {
Context.getDataManager().updateDeviceStatus(device);
Context.getDeviceManager().updateDeviceStatus(device);
} catch (SQLException error) {
Log.warning(error);
}

for (long userId : Context.getPermissionsManager().getDeviceUsers(deviceId)) {
updateDevice(device);
}

public synchronized void updateDevice(Device device) {
for (long userId : Context.getPermissionsManager().getDeviceUsers(device.getId())) {
if (listeners.containsKey(userId)) {
for (UpdateListener listener : listeners.get(userId)) {
listener.onUpdateDevice(device);
Expand All @@ -138,7 +126,6 @@ public void run(Timeout timeout) throws Exception {

public synchronized void updatePosition(Position position) {
long deviceId = position.getDeviceId();
positions.put(deviceId, position);

for (long userId : Context.getPermissionsManager().getDeviceUsers(deviceId)) {
if (listeners.containsKey(userId)) {
Expand All @@ -157,23 +144,6 @@ public synchronized void updateEvent(long userId, Event event, Position position
}
}

public Position getLastPosition(long deviceId) {
return positions.get(deviceId);
}

public synchronized Collection<Position> getInitialState(long userId) {

List<Position> result = new LinkedList<>();

for (long deviceId : Context.getPermissionsManager().getDevicePermissions(userId)) {
if (positions.containsKey(deviceId)) {
result.add(positions.get(deviceId));
}
}

return result;
}

public interface UpdateListener {
void onUpdateDevice(Device device);
void onUpdatePosition(Position position);
Expand Down
Loading

0 comments on commit 6aa4a84

Please sign in to comment.