aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2017-03-21 00:09:22 +1300
committerGitHub <noreply@github.com>2017-03-21 00:09:22 +1300
commit07c7d5c08583734ae70c5c9e6cec3332726b4200 (patch)
tree7c87839c1793a5c21a868b32f31e02b2d161625f
parent7fc40b50d77b5724473ffd91e8d1b00418c27cc2 (diff)
parent696ac56a6174a5ebfec939871a579ba54ee96963 (diff)
downloadtraccar-server-07c7d5c08583734ae70c5c9e6cec3332726b4200.tar.gz
traccar-server-07c7d5c08583734ae70c5c9e6cec3332726b4200.tar.bz2
traccar-server-07c7d5c08583734ae70c5c9e6cec3332726b4200.zip
Merge pull request #3022 from Abyss777/pnone_device
Handle incoming SMS from devices as Event
-rw-r--r--src/org/traccar/database/DeviceManager.java52
-rw-r--r--src/org/traccar/events/TextMessageEventHandler.java37
-rw-r--r--src/org/traccar/model/Event.java2
-rw-r--r--src/org/traccar/smpp/ClientSmppSessionHandler.java11
-rw-r--r--templates/mail/textMessage.vm9
-rw-r--r--templates/sms/textMessage.vm1
6 files changed, 98 insertions, 14 deletions
diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java
index f5fde9e1f..ae5784b01 100644
--- a/src/org/traccar/database/DeviceManager.java
+++ b/src/org/traccar/database/DeviceManager.java
@@ -19,6 +19,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -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;
@@ -90,24 +92,39 @@ public class DeviceManager implements IdentityManager {
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) {
@@ -117,18 +134,21 @@ public class DeviceManager implements IdentityManager {
device.setStatus(Device.STATUS_OFFLINE);
}
}
- for (Long cachedDeviceId : devicesById.keySet()) {
- if (!databaseDevicesIds.contains(cachedDeviceId)) {
- devicesById.remove(cachedDeviceId);
+ for (Iterator<Long> iterator = devicesById.keySet().iterator(); iterator.hasNext();) {
+ if (!databaseDevicesIds.contains(iterator.next())) {
+ iterator.remove();
+ }
+ }
+ for (Iterator<String> iterator = devicesByUniqueId.keySet().iterator(); iterator.hasNext();) {
+ if (!databaseDevicesUniqueIds.contains(iterator.next())) {
+ iterator.remove();
}
}
- for (String cachedDeviceUniqId : devicesByUniqueId.keySet()) {
- if (!databaseDevicesUniqueIds.contains(cachedDeviceUniqId)) {
- devicesByUniqueId.remove(cachedDeviceUniqId);
+ for (Iterator<String> iterator = devicesByPhone.keySet().iterator(); iterator.hasNext();) {
+ if (!databaseDevicesPhones.contains(iterator.next())) {
+ iterator.remove();
}
}
- databaseDevicesIds.clear();
- databaseDevicesUniqueIds.clear();
}
}
@@ -146,6 +166,10 @@ public class DeviceManager implements IdentityManager {
return devicesByUniqueId.get(uniqueId);
}
+ public Device getDeviceByPhone(String phone) {
+ return devicesByPhone.get(phone);
+ }
+
public Collection<Device> getAllDevices() {
boolean forceUpdate = devicesById.isEmpty();
@@ -180,6 +204,9 @@ public class DeviceManager implements IdentityManager {
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 {
@@ -187,6 +214,9 @@ public class DeviceManager implements IdentityManager {
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 {
@@ -202,8 +232,12 @@ public class DeviceManager implements IdentityManager {
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);
}
diff --git a/src/org/traccar/events/TextMessageEventHandler.java b/src/org/traccar/events/TextMessageEventHandler.java
new file mode 100644
index 000000000..be4a193a7
--- /dev/null
+++ b/src/org/traccar/events/TextMessageEventHandler.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.events;
+
+import org.traccar.Context;
+import org.traccar.model.Device;
+import org.traccar.model.Event;
+
+public final class TextMessageEventHandler {
+
+ private TextMessageEventHandler() {
+ }
+
+ 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);
+ }
+ }
+
+}
diff --git a/src/org/traccar/model/Event.java b/src/org/traccar/model/Event.java
index ee62f9776..96b371896 100644
--- a/src/org/traccar/model/Event.java
+++ b/src/org/traccar/model/Event.java
@@ -58,6 +58,8 @@ public class Event extends Message {
public static final String TYPE_MAINTENANCE = "maintenance";
+ public static final String TYPE_TEXT_MESSAGE = "textMessage";
+
private Date serverTime;
public Date getServerTime() {
diff --git a/src/org/traccar/smpp/ClientSmppSessionHandler.java b/src/org/traccar/smpp/ClientSmppSessionHandler.java
index 2a538a40f..77f75273e 100644
--- a/src/org/traccar/smpp/ClientSmppSessionHandler.java
+++ b/src/org/traccar/smpp/ClientSmppSessionHandler.java
@@ -16,6 +16,7 @@
*/
package org.traccar.smpp;
+import org.traccar.events.TextMessageEventHandler;
import org.traccar.helper.Log;
import com.cloudhopper.commons.charset.CharsetUtil;
@@ -49,11 +50,11 @@ public class ClientSmppSessionHandler extends DefaultSmppSessionHandler {
+ ", 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);
+ TextMessageEventHandler.handleTextMessage(sourceAddress, message);
}
}
response = request.createResponse();
diff --git a/templates/mail/textMessage.vm b/templates/mail/textMessage.vm
new file mode 100644
index 000000000..174173c85
--- /dev/null
+++ b/templates/mail/textMessage.vm
@@ -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>
diff --git a/templates/sms/textMessage.vm b/templates/sms/textMessage.vm
new file mode 100644
index 000000000..dc42f29f1
--- /dev/null
+++ b/templates/sms/textMessage.vm
@@ -0,0 +1 @@
+Text message received from $device.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.serverTime, $locale, $timezone)