From 1ab53d1f4911e349370542b23a5fa3fe69be7dcc Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 1 Mar 2020 23:18:44 +0100 Subject: add support for pushover.net --- .../traccar/notification/NotificatorManager.java | 4 + .../traccar/notificators/NotificatorPushover.java | 89 ++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/main/java/org/traccar/notificators/NotificatorPushover.java (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notification/NotificatorManager.java b/src/main/java/org/traccar/notification/NotificatorManager.java index 191748379..c5f1ad8a0 100644 --- a/src/main/java/org/traccar/notification/NotificatorManager.java +++ b/src/main/java/org/traccar/notification/NotificatorManager.java @@ -32,6 +32,7 @@ import org.traccar.notificators.Notificator; import org.traccar.notificators.NotificatorSms; import org.traccar.notificators.NotificatorWeb; import org.traccar.notificators.NotificatorTelegram; +import org.traccar.notificators.NotificatorPushover; public final class NotificatorManager { @@ -61,6 +62,9 @@ public final class NotificatorManager { case "telegram": defaultNotificator = NotificatorTelegram.class.getCanonicalName(); break; + case "pushover": + defaultNotificator = NotificatorPushover.class.getCanonicalName(); + break; default: break; } diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java new file mode 100644 index 000000000..21da1f8d5 --- /dev/null +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -0,0 +1,89 @@ +/* + * Copyright 2019 Anton Tananaev (anton@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.notificators; + +import com.fasterxml.jackson.annotation.JsonProperty; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.traccar.Context; +import org.traccar.model.Event; +import org.traccar.model.Position; +import org.traccar.notification.NotificationFormatter; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.InvocationCallback; + +public class NotificatorPushover extends Notificator { + + private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorPushover.class); + + private String url; + private String token; + private String user; + private String device; + private String title; + + public static class Message { + @JsonProperty("token") + private String token; + @JsonProperty("user") + private String user; + @JsonProperty("device") + private String device; + @JsonProperty("title") + private String title; + @JsonProperty("message") + private String message; + } + + public NotificatorPushover() { + url = "https://api.pushover.net/1/messages.json"; + // see https://pushover.net/api + token = Context.getConfig().getString("notificator.pushover.token"); // (required) token from pushover.net + user = Context.getConfig().getString("notificator.pushover.user"); // (required) user from pushover.net + device = Context.getConfig().getString("notificator.pushover.device",""); // optional: your user's device name to send the message directly to that device, rather than all of the user's devices (multiple devices may be separated by a comma) + title = Context.getConfig().getString("notificator.pushover.title",""); // optional: your message's title, otherwise your app's name is used + } + + @Override + public void sendSync(long userId, Event event, Position position) { + + Message message = new Message(); + message.token = token; + message.user = user; + message.device = device; + message.title = title; + message.message = NotificationFormatter.formatShortMessage(userId, event, position); + + Context.getClient().target(url).request() + .async().post(Entity.json(message), new InvocationCallback() { + @Override + public void completed(Object o) { + } + + @Override + public void failed(Throwable throwable) { + LOGGER.warn("Pushover API error", throwable); + } + }); + } + + @Override + public void sendAsync(long userId, Event event, Position position) { + sendSync(userId, event, position); + } + +} -- cgit v1.2.3 From 39a2742121de3e016655e9fc5d5ba886e1ab900b Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 1 Mar 2020 23:36:54 +0100 Subject: comment too long --- src/main/java/org/traccar/notificators/NotificatorPushover.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index 21da1f8d5..29455b1a6 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -54,7 +54,8 @@ public class NotificatorPushover extends Notificator { // see https://pushover.net/api token = Context.getConfig().getString("notificator.pushover.token"); // (required) token from pushover.net user = Context.getConfig().getString("notificator.pushover.user"); // (required) user from pushover.net - device = Context.getConfig().getString("notificator.pushover.device",""); // optional: your user's device name to send the message directly to that device, rather than all of the user's devices (multiple devices may be separated by a comma) + device = Context.getConfig().getString("notificator.pushover.device",""); // optional: your user's device name to send the message directly + // to that device, rather than all of the user's devices (multiple devices may be separated by a comma) title = Context.getConfig().getString("notificator.pushover.title",""); // optional: your message's title, otherwise your app's name is used } -- cgit v1.2.3 From 4a7a3b59c39bcfe9a6d7ed11b27074d7c1f6b418 Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 1 Mar 2020 23:44:23 +0100 Subject: comment too long --- src/main/java/org/traccar/notificators/NotificatorPushover.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index 29455b1a6..734e38eb9 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -54,9 +54,11 @@ public class NotificatorPushover extends Notificator { // see https://pushover.net/api token = Context.getConfig().getString("notificator.pushover.token"); // (required) token from pushover.net user = Context.getConfig().getString("notificator.pushover.user"); // (required) user from pushover.net - device = Context.getConfig().getString("notificator.pushover.device",""); // optional: your user's device name to send the message directly + device = Context.getConfig().getString("notificator.pushover.device",""); // optional: + // your user's device name to send the message directly // to that device, rather than all of the user's devices (multiple devices may be separated by a comma) - title = Context.getConfig().getString("notificator.pushover.title",""); // optional: your message's title, otherwise your app's name is used + title = Context.getConfig().getString("notificator.pushover.title",""); // optional: your message's title, + // otherwise your app's name is used } @Override -- cgit v1.2.3 From 535b0ffa9a3e435cd58d4f7ade9a4d8c6eb03623 Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 1 Mar 2020 23:47:33 +0100 Subject: add whitespace --- src/main/java/org/traccar/notificators/NotificatorPushover.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index 734e38eb9..ff992ec8d 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -54,10 +54,10 @@ public class NotificatorPushover extends Notificator { // see https://pushover.net/api token = Context.getConfig().getString("notificator.pushover.token"); // (required) token from pushover.net user = Context.getConfig().getString("notificator.pushover.user"); // (required) user from pushover.net - device = Context.getConfig().getString("notificator.pushover.device",""); // optional: + device = Context.getConfig().getString("notificator.pushover.device", ""); // optional: // your user's device name to send the message directly // to that device, rather than all of the user's devices (multiple devices may be separated by a comma) - title = Context.getConfig().getString("notificator.pushover.title",""); // optional: your message's title, + title = Context.getConfig().getString("notificator.pushover.title", ""); // optional: your message's title, // otherwise your app's name is used } -- cgit v1.2.3 From e39eaf2e9878fe8e575b525dd2ca747e3f5342fe Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 4 Mar 2020 21:40:43 +0100 Subject: add Pushover attrs in web interface --- .../traccar/notificators/NotificatorPushover.java | 95 +++++++++++++++++++--- traccar-web | 2 +- 2 files changed, 85 insertions(+), 12 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index ff992ec8d..ae48bd46a 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -23,6 +23,10 @@ import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.notification.NotificationFormatter; +import org.traccar.model.User; +import org.traccar.notification.PropertiesProvider; +import java.util.Properties; + import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; @@ -30,9 +34,36 @@ public class NotificatorPushover extends Notificator { private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorPushover.class); - private String url; + private static Properties getProperties(PropertiesProvider provider) { + Properties properties = new Properties(); + // (required) token from pushover.net + String token = provider.getString("notificator.pushover.token"); + if (token != null) { + properties.put("notificator.pushover.token", token); + } + // (required) user from pushover.net + String user = provider.getString("notificator.pushover.user"); + if (user != null) { + properties.put("notificator.pushover.user", user); + } + // optional: your user's device name to send the message directly + // to that device, rather than all of the user's devices (multiple devices may be separated by a comma) + String device = provider.getString("notificator.pushover.device"); + if (device != null) { + properties.put("notificator.pushover.device", device); + } + // optional: your message's title, otherwise your app's name is used + String title = provider.getString("notificator.pushover.title"); + if (title != null) { + properties.put("notificator.pushover.title", title); + } + return properties; + } + + + private final String url; private String token; - private String user; + private String puser; private String device; private String title; @@ -50,23 +81,65 @@ public class NotificatorPushover extends Notificator { } public NotificatorPushover() { - url = "https://api.pushover.net/1/messages.json"; // see https://pushover.net/api - token = Context.getConfig().getString("notificator.pushover.token"); // (required) token from pushover.net - user = Context.getConfig().getString("notificator.pushover.user"); // (required) user from pushover.net - device = Context.getConfig().getString("notificator.pushover.device", ""); // optional: - // your user's device name to send the message directly - // to that device, rather than all of the user's devices (multiple devices may be separated by a comma) - title = Context.getConfig().getString("notificator.pushover.title", ""); // optional: your message's title, - // otherwise your app's name is used + url = "https://api.pushover.net/1/messages.json"; } @Override public void sendSync(long userId, Event event, Position position) { + User user = Context.getPermissionsManager().getUser(userId); + + token = null; + puser = null; + device = null; + title = null; + + Properties properties = null; + + properties = getProperties(new PropertiesProvider(Context.getConfig())); + if (properties != null) { + token = properties.getProperty("notificator.pushover.token"); + puser = properties.getProperty("notificator.pushover.user"); + device = properties.getProperty("notificator.pushover.device"); + title = properties.getProperty("notificator.pushover.title"); + } + + properties = getProperties(new PropertiesProvider(user)); + if (properties != null) { + if (properties.getProperty("notificator.pushover.token") != null) { + token = properties.getProperty("notificator.pushover.token"); + } + if (properties.getProperty("notificator.pushover.user") != null) { + puser = properties.getProperty("notificator.pushover.user"); + } + if (properties.getProperty("notificator.pushover.device") != null) { + device = properties.getProperty("notificator.pushover.device"); + } + if (properties.getProperty("notificator.pushover.title") != null) { + title = properties.getProperty("notificator.pushover.title"); + } + } + + if (token == null) { + LOGGER.warn("Pushover token not found"); + return; + } + + if (puser == null) { + LOGGER.warn("Pushover user not found"); + return; + } + + if (device == null) + device = ""; + + if (title == null) + title = ""; + Message message = new Message(); message.token = token; - message.user = user; + message.user = puser; message.device = device; message.title = title; message.message = NotificationFormatter.formatShortMessage(userId, event, position); diff --git a/traccar-web b/traccar-web index e8479d77f..a587d6257 160000 --- a/traccar-web +++ b/traccar-web @@ -1 +1 @@ -Subproject commit e8479d77f13acc3b3738a180a7990b06c495f1ba +Subproject commit a587d6257dd9de70252a5b00299ccbfd7b5cb272 -- cgit v1.2.3 From b27e8213f380f3ae8c2b9503e0aaecb55c784271 Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 4 Mar 2020 21:47:19 +0100 Subject: correction for travis-ci --- .../java/org/traccar/notificators/NotificatorPushover.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index ae48bd46a..f289e464a 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -116,11 +116,11 @@ public class NotificatorPushover extends Notificator { if (properties.getProperty("notificator.pushover.device") != null) { device = properties.getProperty("notificator.pushover.device"); } - if (properties.getProperty("notificator.pushover.title") != null) { + if (properties.getProperty("notificator.pushover.title") != null) { title = properties.getProperty("notificator.pushover.title"); } - } - + } + if (token == null) { LOGGER.warn("Pushover token not found"); return; @@ -131,11 +131,13 @@ public class NotificatorPushover extends Notificator { return; } - if (device == null) + if (device == null) { device = ""; + } - if (title == null) + if (title == null) { title = ""; + } Message message = new Message(); message.token = token; -- cgit v1.2.3 From e46cb0260f224ec296feb7087cdfb05371e6f550 Mon Sep 17 00:00:00 2001 From: Andreas Date: Thu, 5 Mar 2020 21:01:51 +0100 Subject: implement device in ui --- .../traccar/notificators/NotificatorPushover.java | 45 ++-------------------- traccar-web | 2 +- 2 files changed, 5 insertions(+), 42 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index f289e464a..6cad6f766 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -36,36 +36,20 @@ public class NotificatorPushover extends Notificator { private static Properties getProperties(PropertiesProvider provider) { Properties properties = new Properties(); - // (required) token from pushover.net - String token = provider.getString("notificator.pushover.token"); - if (token != null) { - properties.put("notificator.pushover.token", token); - } - // (required) user from pushover.net - String user = provider.getString("notificator.pushover.user"); - if (user != null) { - properties.put("notificator.pushover.user", user); - } // optional: your user's device name to send the message directly // to that device, rather than all of the user's devices (multiple devices may be separated by a comma) String device = provider.getString("notificator.pushover.device"); if (device != null) { properties.put("notificator.pushover.device", device); } - // optional: your message's title, otherwise your app's name is used - String title = provider.getString("notificator.pushover.title"); - if (title != null) { - properties.put("notificator.pushover.title", title); - } return properties; } private final String url; - private String token; - private String puser; + private final String token; + private final String puser; private String device; - private String title; public static class Message { @JsonProperty("token") @@ -74,8 +58,6 @@ public class NotificatorPushover extends Notificator { private String user; @JsonProperty("device") private String device; - @JsonProperty("title") - private String title; @JsonProperty("message") private String message; } @@ -83,6 +65,8 @@ public class NotificatorPushover extends Notificator { public NotificatorPushover() { // see https://pushover.net/api url = "https://api.pushover.net/1/messages.json"; + token = Context.getConfig().getString("notificator.pushover.token"); // (required) token from pushover.net + puser = Context.getConfig().getString("notificator.pushover.user"); // (required) user from pushover.net } @Override @@ -90,35 +74,19 @@ public class NotificatorPushover extends Notificator { User user = Context.getPermissionsManager().getUser(userId); - token = null; - puser = null; device = null; - title = null; - Properties properties = null; properties = getProperties(new PropertiesProvider(Context.getConfig())); if (properties != null) { - token = properties.getProperty("notificator.pushover.token"); - puser = properties.getProperty("notificator.pushover.user"); device = properties.getProperty("notificator.pushover.device"); - title = properties.getProperty("notificator.pushover.title"); } properties = getProperties(new PropertiesProvider(user)); if (properties != null) { - if (properties.getProperty("notificator.pushover.token") != null) { - token = properties.getProperty("notificator.pushover.token"); - } - if (properties.getProperty("notificator.pushover.user") != null) { - puser = properties.getProperty("notificator.pushover.user"); - } if (properties.getProperty("notificator.pushover.device") != null) { device = properties.getProperty("notificator.pushover.device"); } - if (properties.getProperty("notificator.pushover.title") != null) { - title = properties.getProperty("notificator.pushover.title"); - } } if (token == null) { @@ -135,15 +103,10 @@ public class NotificatorPushover extends Notificator { device = ""; } - if (title == null) { - title = ""; - } - Message message = new Message(); message.token = token; message.user = puser; message.device = device; - message.title = title; message.message = NotificationFormatter.formatShortMessage(userId, event, position); Context.getClient().target(url).request() diff --git a/traccar-web b/traccar-web index a587d6257..071f08898 160000 --- a/traccar-web +++ b/traccar-web @@ -1 +1 @@ -Subproject commit a587d6257dd9de70252a5b00299ccbfd7b5cb272 +Subproject commit 071f088980c867638f1092c48c6909a4433a0755 -- cgit v1.2.3 From d2835b2c6152b448179c0ecae6c9aadc4dce60a6 Mon Sep 17 00:00:00 2001 From: Andreas Date: Fri, 6 Mar 2020 16:51:15 +0100 Subject: simplify code --- .../traccar/notificators/NotificatorPushover.java | 42 +++++----------------- 1 file changed, 8 insertions(+), 34 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index 6cad6f766..5c86503f3 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -21,11 +21,8 @@ import org.slf4j.LoggerFactory; import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.notification.NotificationFormatter; - import org.traccar.model.User; -import org.traccar.notification.PropertiesProvider; -import java.util.Properties; +import org.traccar.notification.NotificationFormatter; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; @@ -34,22 +31,9 @@ public class NotificatorPushover extends Notificator { private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorPushover.class); - private static Properties getProperties(PropertiesProvider provider) { - Properties properties = new Properties(); - // optional: your user's device name to send the message directly - // to that device, rather than all of the user's devices (multiple devices may be separated by a comma) - String device = provider.getString("notificator.pushover.device"); - if (device != null) { - properties.put("notificator.pushover.device", device); - } - return properties; - } - - private final String url; private final String token; private final String puser; - private String device; public static class Message { @JsonProperty("token") @@ -72,21 +56,15 @@ public class NotificatorPushover extends Notificator { @Override public void sendSync(long userId, Event event, Position position) { - User user = Context.getPermissionsManager().getUser(userId); - - device = null; - Properties properties = null; + final User user = Context.getPermissionsManager().getUser(userId); - properties = getProperties(new PropertiesProvider(Context.getConfig())); - if (properties != null) { - device = properties.getProperty("notificator.pushover.device"); - } + String device = ""; - properties = getProperties(new PropertiesProvider(user)); - if (properties != null) { - if (properties.getProperty("notificator.pushover.device") != null) { - device = properties.getProperty("notificator.pushover.device"); - } + if (user.getAttributes().containsKey("notificator.pushover.device")) { + // optional: your user's device name to send the message directly + // to that device, rather than all of the user's devices (multiple devices may be separated by a comma) + // i.e.: device1,device2 (no space) + device = user.getString("notificator.pushover.device").replaceAll(" *, *", ","); } if (token == null) { @@ -99,10 +77,6 @@ public class NotificatorPushover extends Notificator { return; } - if (device == null) { - device = ""; - } - Message message = new Message(); message.token = token; message.user = puser; -- cgit v1.2.3 From c1b640ef2736b5f3a16440c8fbb12db6ad3bdb56 Mon Sep 17 00:00:00 2001 From: Andreas Date: Fri, 6 Mar 2020 19:41:04 +0100 Subject: change copyright date --- src/main/java/org/traccar/notificators/NotificatorPushover.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index 5c86503f3..52646bdfb 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2020 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. -- cgit v1.2.3