From 23505018bebe2120c4ceac219f0d22a0646200ce Mon Sep 17 00:00:00 2001 From: Marcos Dallagnelo Date: Wed, 15 Aug 2018 16:39:07 -0300 Subject: Implementado integração com serviço envio SMS SmsGateway.me MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/traccar/sms/SmsGatewayClient.java | 72 +++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/org/traccar/sms/SmsGatewayClient.java diff --git a/src/org/traccar/sms/SmsGatewayClient.java b/src/org/traccar/sms/SmsGatewayClient.java new file mode 100644 index 000000000..b1683783d --- /dev/null +++ b/src/org/traccar/sms/SmsGatewayClient.java @@ -0,0 +1,72 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.traccar.sms; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.Response; +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.notification.MessageException; + +/** + * + * @author marcos + */ +public class SmsGatewayClient implements SmsManager { + + private static final String URL = "https://smsgateway.me/api/v4/message/send"; + private static final String KEY_PHONE_NUMBER = "phone_number"; + private static final String KEY_MESSAGE = "message"; + private static final String KEY_DEVICE_ID = "device_id"; + private static final String KEY_AUTHORIZATION = "Authorization"; + + private final String token; + private final int deviceId; + + public SmsGatewayClient() { + token = Context.getConfig().getString("sms.smsgateway.token"); + deviceId = Context.getConfig().getInteger("sms.smsgateway.deviceId"); + } + + protected JsonArray preparePayload(String destAddress, String message) { + return Json.createArrayBuilder() + .add(Json.createObjectBuilder() + .add(KEY_PHONE_NUMBER, destAddress) + .add(KEY_MESSAGE, message) + .add(KEY_DEVICE_ID, deviceId)) + .build(); + } + + @Override + public void sendMessageSync(String destAddress, String message, boolean command) + throws InterruptedException, MessageException { + Invocation.Builder requestBuilder = Context.getClient().target(URL).request(); + + requestBuilder = requestBuilder.header(KEY_AUTHORIZATION, token); + + Response response = requestBuilder.post(Entity.json(preparePayload(destAddress, message))); + if (!response.getStatusInfo().equals(Response.Status.OK)) { + String output = response.readEntity(String.class); + Log.error(output); + } + } + + @Override + public void sendMessageAsync(final String destAddress, final String message, final boolean command) { + new Thread(new Runnable() { + public void run() { + try { + sendMessageSync(destAddress, message, command); + } catch (MessageException | InterruptedException error) { + Log.warning(error); + } + } + }).start(); + } +} -- cgit v1.2.3 From 6d6a8949a598ff9aac9370655e2e0bd759391368 Mon Sep 17 00:00:00 2001 From: Marcos Dallagnelo Date: Thu, 16 Aug 2018 14:23:35 -0300 Subject: Have improved code, without create new thread and throw a MessageException --- src/org/traccar/sms/SmsGatewayClient.java | 47 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/org/traccar/sms/SmsGatewayClient.java b/src/org/traccar/sms/SmsGatewayClient.java index b1683783d..da0b59ec1 100644 --- a/src/org/traccar/sms/SmsGatewayClient.java +++ b/src/org/traccar/sms/SmsGatewayClient.java @@ -1,7 +1,18 @@ /* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2018 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.sms; @@ -11,13 +22,8 @@ import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.core.Response; import org.traccar.Context; -import org.traccar.helper.Log; import org.traccar.notification.MessageException; -/** - * - * @author marcos - */ public class SmsGatewayClient implements SmsManager { private static final String URL = "https://smsgateway.me/api/v4/message/send"; @@ -34,7 +40,7 @@ public class SmsGatewayClient implements SmsManager { deviceId = Context.getConfig().getInteger("sms.smsgateway.deviceId"); } - protected JsonArray preparePayload(String destAddress, String message) { + private JsonArray preparePayload(String destAddress, String message) { return Json.createArrayBuilder() .add(Json.createObjectBuilder() .add(KEY_PHONE_NUMBER, destAddress) @@ -43,30 +49,23 @@ public class SmsGatewayClient implements SmsManager { .build(); } + private Invocation.Builder getRequestBuilder() { + return Context.getClient().target(URL).request() + .header(KEY_AUTHORIZATION, token); + } + @Override public void sendMessageSync(String destAddress, String message, boolean command) throws InterruptedException, MessageException { - Invocation.Builder requestBuilder = Context.getClient().target(URL).request(); - - requestBuilder = requestBuilder.header(KEY_AUTHORIZATION, token); - - Response response = requestBuilder.post(Entity.json(preparePayload(destAddress, message))); + Response response = getRequestBuilder().post(Entity.json(preparePayload(destAddress, message))); if (!response.getStatusInfo().equals(Response.Status.OK)) { String output = response.readEntity(String.class); - Log.error(output); + throw new MessageException(new Exception(output)); } } @Override public void sendMessageAsync(final String destAddress, final String message, final boolean command) { - new Thread(new Runnable() { - public void run() { - try { - sendMessageSync(destAddress, message, command); - } catch (MessageException | InterruptedException error) { - Log.warning(error); - } - } - }).start(); + getRequestBuilder().async().post(Entity.json(preparePayload(destAddress, message))); } } -- cgit v1.2.3 From 0ec4e06e7319afc8aa01f38dbe26ad31aac84776 Mon Sep 17 00:00:00 2001 From: Marcos Dallagnelo Date: Fri, 17 Aug 2018 09:02:21 -0300 Subject: Add constructor to MessageException class --- src/org/traccar/notification/MessageException.java | 4 ++++ src/org/traccar/sms/SmsGatewayClient.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/notification/MessageException.java b/src/org/traccar/notification/MessageException.java index ce4b9f6ee..710b927b0 100644 --- a/src/org/traccar/notification/MessageException.java +++ b/src/org/traccar/notification/MessageException.java @@ -22,4 +22,8 @@ public class MessageException extends Exception { super(cause); } + public MessageException(String message) { + super(message); + } + } diff --git a/src/org/traccar/sms/SmsGatewayClient.java b/src/org/traccar/sms/SmsGatewayClient.java index da0b59ec1..ea2df6881 100644 --- a/src/org/traccar/sms/SmsGatewayClient.java +++ b/src/org/traccar/sms/SmsGatewayClient.java @@ -60,7 +60,7 @@ public class SmsGatewayClient implements SmsManager { Response response = getRequestBuilder().post(Entity.json(preparePayload(destAddress, message))); if (!response.getStatusInfo().equals(Response.Status.OK)) { String output = response.readEntity(String.class); - throw new MessageException(new Exception(output)); + throw new MessageException(output); } } -- cgit v1.2.3