Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanfmartinez committed Feb 24, 2022
1 parent b266215 commit 0947650
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
18 changes: 11 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
<groupId>com.ivanfm</groupId>
<artifactId>ivanfm-traccar-mqtt</artifactId>
<packaging>jar</packaging>
<version>2.2.3</version>
<version>2.2.7</version>
<name>ivanfm-traccar-mqtt</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.7.26</slf4j.version>
<junit.version>4.13.1</junit.version>
<slf4j.version>1.7.31</slf4j.version>
<junit.version>4.13.2</junit.version>
<velocity.version>1.7</velocity.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<paho-mqttv3.version>1.2.1</paho-mqttv3.version>
<paho-mqttv3.version>1.2.5</paho-mqttv3.version>
<traccar.version>4.15-ifm</traccar.version>

<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
</properties>


Expand All @@ -34,12 +36,14 @@

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<version>3.2.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>

<finalName>${project.artifactId}-${project.version}-traccar-${traccar.version}</finalName>

<archive>
<manifest>
</manifest>
Expand Down Expand Up @@ -93,7 +97,7 @@
<dependency>
<groupId>org.traccar</groupId>
<artifactId>traccar</artifactId>
<version>4.5-SNAPSHOT</version>
<version>${traccar.version}</version>
<scope>provided</scope>
</dependency>

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/ivanfm/mqtt/MQTTPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public void messageArrived(String arg0, MqttMessage arg1) throws Exception {



public void publish(String topic, String msg) {
publishOnRoot(rootTopic + topic, msg.getBytes(), AT_LEAST_ONCE, false);
}

public void publish(String topic, byte[] msg) {
publishOnRoot(rootTopic + topic, msg, AT_LEAST_ONCE, false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ivanfm/traccar/mqtt/MQTTHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private synchronized MQTTPublisher getPublisher() {
config.getString("mqtt.clientid", "traccar.mqtt.handler"),
config.getString("mqtt.topicRoot", "/traccar/"));

globalPublisher.publish("start", (new Date()).toString().getBytes());
globalPublisher.publish("start", (new Date()).toString());
} catch (Exception e) {
log.error("", e);
}
Expand Down
46 changes: 28 additions & 18 deletions src/main/java/com/ivanfm/traccar/mqtt/ProcessPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.VelocityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -27,6 +28,8 @@
import org.traccar.model.Position;
import org.traccar.model.User;
import org.traccar.notification.NotificationFormatter;
import org.traccar.notification.NotificationMessage;
import org.traccar.notification.TextTemplateFormatter;

import com.ivanfm.mqtt.MQTTPublisher;

Expand Down Expand Up @@ -56,11 +59,11 @@ public ProcessPosition(MQTTPublisher publisher, Position position) {
final Set<Long> users = dm.getUserItems(device.getId());
user = Context.getPermissionsManager().getUser( users.isEmpty() ? 1 : users.iterator().next() );

devAlias = nameCleanUp(dm.lookupAttributeString(position.getDeviceId(), "mqtt.alias", device.getName(), false));
devAlias = nameCleanUp(dm.lookupAttributeString(position.getDeviceId(), "mqtt.alias", device.getName(), true, false));
// Current configuration should use alarmTopics instead of alarmTopic
alarmTopics = splitTopics(dm.lookupAttributeString(position.getDeviceId(), "mqtt.alarmTopics", dm.lookupAttributeString(position.getDeviceId(), "mqtt.alarmTopic", "", true), true));
alarmTopics = splitTopics(dm.lookupAttributeString(position.getDeviceId(), "mqtt.alarmTopics", dm.lookupAttributeString(position.getDeviceId(), "mqtt.alarmTopic", "", true, true), true, true));

event = new Event("MQTTX", position.getDeviceId(), position.getId());
event = new Event("MQTTX", position);

}

Expand All @@ -83,18 +86,18 @@ private static List<String> splitTopics(String topics) {
}

void publish(String path, String value) {
publisher.publish("device/" + devAlias + "/" + path , value.getBytes());
publisher.publish("device/" + devAlias + "/" + path , value);
}

private void publishIfNotBlank(String path, String value) {
if (StringUtils.isNotBlank(value)) {
publisher.publish("device/" + devAlias + "/" + path , value.getBytes());
publisher.publish("device/" + devAlias + "/" + path , value);
}
}

private void publishIfNotZero(String path, long value) {
if (value > 0) {
publisher.publish("device/" + devAlias + "/" + path , Long.toString(value).getBytes());
publisher.publish("device/" + devAlias + "/" + path , Long.toString(value));
}
}

Expand All @@ -108,7 +111,7 @@ private void publishIfNotNull(String path, Object value) {

void publish() {
final Map<String,Object> attrs = position.getAttributes();
if (dm.lookupAttributeBoolean(device.getId(), "mqtt.position.process.enabled", true, true)) {
if (dm.lookupAttributeBoolean(device.getId(), "mqtt.position.process.enabled", true, true, true)) {
final SimpleDateFormat ISO_8601_Z = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
ISO_8601_Z.setTimeZone(TimeZone.getTimeZone("UTC"));
/**
Expand Down Expand Up @@ -139,7 +142,7 @@ void publish() {
processGeofences();
}

if (dm.lookupAttributeBoolean(device.getId(), "mqtt.position.process.alarms.enabled", true, true)) {
if (dm.lookupAttributeBoolean(device.getId(), "mqtt.position.process.alarms.enabled", true, true, true)) {
if (!alarmTopics.isEmpty()) {
publishAlarm(attrs);
}
Expand All @@ -153,8 +156,8 @@ private void processGeofences() {
final String geofenceAlias = nameCleanUp(g.getName());
final GeofenceGeometry geometry = g.getGeometry();
// if running with the changes to support accuracy in geometry use this line
// final boolean inside = geometry.containsPoint(position.getLatitude(), position.getLongitude(), position.getAccuracy());
final boolean inside = geometry.containsPoint(position.getLatitude(), position.getLongitude());
final boolean inside = geometry.containsPoint(position.getLatitude(), position.getLongitude(), position.getAccuracy());
// final boolean inside = geometry.containsPoint(position.getLatitude(), position.getLongitude());
stateChanged = processGeofence(g, geofenceAlias, inside) || stateChanged;
}
if (stateChanged) {
Expand All @@ -167,7 +170,7 @@ private void processGeofences() {
}

private boolean processGeofence(Geofence geofence, String geofenceAlias, boolean inside) {
final String topicsSt = dm.lookupAttributeString(position.getDeviceId(), "mqtt.geofence." + geofenceAlias + ".topics", "", false);
final String topicsSt = dm.lookupAttributeString(position.getDeviceId(), "mqtt.geofence." + geofenceAlias + ".topics", "", true, false);
final List<String> topics = splitTopics(topicsSt);
final String insideSt = inside ? "1" : "0";

Expand All @@ -181,7 +184,8 @@ private boolean processGeofence(Geofence geofence, String geofenceAlias, boolean
if (!insideSt.equalsIgnoreCase(prevInside)) {
device.set(insideKey, insideSt);
for (String topic : topics) {
final VelocityContext velocityContext = prepareContext();
try {
final VelocityContext velocityContext = prepareContext();
velocityContext.put("geofence", geofence);
velocityContext.put("areaName", geofence.getName());
velocityContext.put("in_out", inside ? "IN" : "OUT");
Expand All @@ -190,6 +194,9 @@ private boolean processGeofence(Geofence geofence, String geofenceAlias, boolean
publisher.publishOnRoot(topic,
format(velocityContext, "mqtt-moved/").getBytes(),
MQTTPublisher.AT_LEAST_ONCE, false);
} catch (org.apache.velocity.exception.ResourceNotFoundException e) {
log.debug("{}", e.getMessage());
}
}
return true;
} else {
Expand All @@ -215,12 +222,16 @@ public void publishAlarm(final Map<String, Object> attrs) {
if (alarm.length() > 0) {
if (!"tracker".equalsIgnoreCase(alarm) && !"et".equalsIgnoreCase(alarm)) {

final VelocityContext velocityContext = prepareContext();
try {
final VelocityContext velocityContext = prepareContext();

for (String alarmTopic : alarmTopics) {
publisher.publishOnRoot(alarmTopic,
for (String alarmTopic : alarmTopics) {
publisher.publishOnRoot(alarmTopic,
format(velocityContext, "mqtt-alarm/").getBytes(),
MQTTPublisher.AT_LEAST_ONCE, false);
}
} catch (ResourceNotFoundException e) {
log.debug("{}", e.getMessage());
}
}
}
Expand All @@ -234,9 +245,8 @@ VelocityContext prepareContext() {
}

String format(VelocityContext velocityContext, String path) {
final StringWriter writer = new StringWriter();
NotificationFormatter.getTemplate(event, path).merge(velocityContext, writer);
return writer.toString();
NotificationMessage message = TextTemplateFormatter.formatMessage(velocityContext, event.getType(), path);
return message.getBody();
}


Expand Down

0 comments on commit 0947650

Please sign in to comment.