Skip to content

Commit

Permalink
Implement phone to device comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Abyss777 committed Mar 20, 2017
1 parent 32cf37a commit 99e16ed
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
48 changes: 47 additions & 1 deletion src/org/traccar/database/DeviceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.traccar.model.CommandType;
import org.traccar.model.Device;
import org.traccar.model.DeviceTotalDistance;
import org.traccar.model.Event;
import org.traccar.model.Group;
import org.traccar.model.Position;
import org.traccar.model.Server;
Expand All @@ -49,6 +50,7 @@ public class DeviceManager implements IdentityManager {

private Map<Long, Device> devicesById;
private Map<String, Device> devicesByUniqueId;
private Map<String, Device> devicesByPhone;
private AtomicLong devicesLastUpdate = new AtomicLong();

private Map<Long, Group> groupsById;
Expand Down Expand Up @@ -90,24 +92,39 @@ private void updateDeviceCache(boolean force) throws SQLException {
if (devicesByUniqueId == null) {
devicesByUniqueId = new ConcurrentHashMap<>(databaseDevices.size());
}
if (devicesByPhone == null) {
devicesByPhone = new ConcurrentHashMap<>(databaseDevices.size());
}
Set<Long> databaseDevicesIds = new HashSet<>();
Set<String> databaseDevicesUniqueIds = new HashSet<>();
Set<String> databaseDevicesPhones = new HashSet<>();
for (Device device : databaseDevices) {
databaseDevicesIds.add(device.getId());
databaseDevicesUniqueIds.add(device.getUniqueId());
databaseDevicesPhones.add(device.getPhone());
if (devicesById.containsKey(device.getId())) {
Device cachedDevice = devicesById.get(device.getId());
cachedDevice.setName(device.getName());
cachedDevice.setGroupId(device.getGroupId());
cachedDevice.setCategory(device.getCategory());
cachedDevice.setContact(device.getContact());
cachedDevice.setModel(device.getModel());
cachedDevice.setAttributes(device.getAttributes());
if (!device.getUniqueId().equals(cachedDevice.getUniqueId())) {
devicesByUniqueId.remove(cachedDevice.getUniqueId());
devicesByUniqueId.put(device.getUniqueId(), cachedDevice);
}
cachedDevice.setUniqueId(device.getUniqueId());
if (device.getPhone() != null && !device.getPhone().isEmpty()
&& !device.getPhone().equals(cachedDevice.getPhone())) {
devicesByPhone.put(device.getPhone(), cachedDevice);
}
cachedDevice.setPhone(device.getPhone());
} else {
devicesById.put(device.getId(), device);
devicesByUniqueId.put(device.getUniqueId(), device);
if (device.getPhone() != null && !device.getPhone().isEmpty()) {
devicesByPhone.put(device.getPhone(), device);
}
if (geofenceManager != null) {
Position lastPosition = getLastPosition(device.getId());
if (lastPosition != null) {
Expand All @@ -127,8 +144,14 @@ private void updateDeviceCache(boolean force) throws SQLException {
devicesByUniqueId.remove(cachedDeviceUniqId);
}
}
for (String cachedDevicePhone : devicesByPhone.keySet()) {
if (!databaseDevicesPhones.contains(cachedDevicePhone)) {
devicesByPhone.remove(cachedDevicePhone);
}
}
databaseDevicesIds.clear();
databaseDevicesUniqueIds.clear();
databaseDevicesPhones.clear();
}
}

Expand All @@ -146,6 +169,10 @@ public Device getDeviceByUniqueId(String uniqueId) throws SQLException {
return devicesByUniqueId.get(uniqueId);
}

public Device getDeviceByPhone(String phone) {
return devicesByPhone.get(phone);
}

public Collection<Device> getAllDevices() {
boolean forceUpdate = devicesById.isEmpty();

Expand Down Expand Up @@ -180,13 +207,19 @@ public void addDevice(Device device) throws SQLException {

devicesById.put(device.getId(), device);
devicesByUniqueId.put(device.getUniqueId(), device);
if (device.getPhone() != null && !device.getPhone().isEmpty()) {
devicesByPhone.put(device.getPhone(), device);
}
}

public void updateDevice(Device device) throws SQLException {
dataManager.updateDevice(device);

devicesById.put(device.getId(), device);
devicesByUniqueId.put(device.getUniqueId(), device);
if (device.getPhone() != null && !device.getPhone().isEmpty()) {
devicesByPhone.put(device.getPhone(), device);
}
}

public void updateDeviceStatus(Device device) throws SQLException {
Expand All @@ -202,8 +235,12 @@ public void removeDevice(long deviceId) throws SQLException {

if (devicesById.containsKey(deviceId)) {
String deviceUniqueId = devicesById.get(deviceId).getUniqueId();
String phone = devicesById.get(deviceId).getPhone();
devicesById.remove(deviceId);
devicesByUniqueId.remove(deviceUniqueId);
if (phone != null && !phone.isEmpty()) {
devicesByPhone.remove(phone);
}
}
positions.remove(deviceId);
}
Expand Down Expand Up @@ -470,4 +507,13 @@ public Collection<CommandType> getCommandTypes(long deviceId, boolean textChanne
}
return result;
}

public void handleTextMessage(String phone, String message) {
Device device = devicesByPhone.get(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);
}
}
}
2 changes: 2 additions & 0 deletions src/org/traccar/model/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public Event() {

public static final String TYPE_MAINTENANCE = "maintenance";

public static final String TYPE_TEXT_MESSAGE = "textMessage";

private Date serverTime;

public Date getServerTime() {
Expand Down
11 changes: 6 additions & 5 deletions src/org/traccar/smpp/ClientSmppSessionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.traccar.smpp;

import org.traccar.Context;
import org.traccar.helper.Log;

import com.cloudhopper.commons.charset.CharsetUtil;
Expand Down Expand Up @@ -49,11 +50,11 @@ public PduResponse firePduRequestReceived(PduRequest request) {
+ ", State: "
+ request.getOptionalParameter(SmppConstants.TAG_MSG_STATE).getValueAsByte());
} else {
Log.debug("SMS Message Received: "
+ CharsetUtil.decode(((DeliverSm) request).getShortMessage(),
smppClient.mapDataCodingToCharset(((DeliverSm) request).getDataCoding())).trim()
+ ", Source Address: "
+ ((DeliverSm) request).getSourceAddress().getAddress());
String sourceAddress = ((DeliverSm) request).getSourceAddress().getAddress();
String message = CharsetUtil.decode(((DeliverSm) request).getShortMessage(),
smppClient.mapDataCodingToCharset(((DeliverSm) request).getDataCoding()));
Log.debug("SMS Message Received: " + message.trim() + ", Source Address: " + sourceAddress);
Context.getDeviceManager().handleTextMessage(sourceAddress, message);
}
}
response = request.createResponse();
Expand Down
9 changes: 9 additions & 0 deletions templates/mail/textMessage.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#set($subject = "$device.name: text message received")
<!DOCTYPE html>
<html>
<body>
Device: $device.name<br>
Message: $event.getString("message")<br>
Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)<br>
</body>
</html>
1 change: 1 addition & 0 deletions templates/sms/textMessage.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Text message received from $device.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)

0 comments on commit 99e16ed

Please sign in to comment.