Skip to content

Commit

Permalink
added notification service + update mock. Currently notifications are…
Browse files Browse the repository at this point in the history
… working only in mock mode
  • Loading branch information
dkijania committed Nov 4, 2018
1 parent a666734 commit 4dd9e86
Show file tree
Hide file tree
Showing 25 changed files with 608 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.chariot.backend.model;

import java.util.HashMap;

public class ChannelNotification {
private final String title;
private final String body;
private final HashMap<String, Object> data;

public ChannelNotification(String title, String body, HashMap<String, Object> data) {
this.title = title;
this.body = body;
this.data = data;
}

public ChannelNotification() {
this("", "", new HashMap<>());
}

public String getTitle() {
return title;
}

public String getBody() {
return body;
}

public HashMap<String, Object> getData() {
return data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.chariot.backend.schema.notification;

public class NotificationRegistrationToken {
private String token;
private String username;

public NotificationRegistrationToken(String token, String username) {
this.token = token;
this.username = username;
}

public NotificationRegistrationToken() {
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.chariot.backend.utils.data;

public class FloatingPointComparator {
public static double PRECISION_LEVEL = 0.00000000000001;

public boolean isWithinBounds(double value, double upperBound, double lowerBound) {
return leftIsGreaterThanRight(upperBound, value) &&
leftIsGreaterThanRight(value, lowerBound);
}

public boolean leftIsGreaterThanRight(double left, double right) {
return Math.abs(left - right) > PRECISION_LEVEL;
}

}
20 changes: 9 additions & 11 deletions gateway-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<artifactId>gateway-webapp</artifactId>
<properties>
<start-class>com.chariot.backend.gateway.webapp.Application</start-class>
</properties>
</properties>

<profiles>
<profile>
Expand All @@ -21,7 +21,7 @@
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<actAsMock>false</actAsMock>
<actAsMock>true</actAsMock>
</properties>
</profile>
<profile>
Expand Down Expand Up @@ -51,12 +51,7 @@
<artifactId>backend-utils</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.chariot.backend</groupId>
<artifactId>backend-model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
Expand All @@ -65,7 +60,7 @@
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
Expand Down Expand Up @@ -115,7 +110,7 @@
<dependency>
<groupId>info.batey.kafka</groupId>
<artifactId>kafka-unit</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
Expand All @@ -125,7 +120,10 @@
<artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.kinoroy.expo.push</groupId>
<artifactId>expo-push-sdk</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.chariot.backend.gateway.webapp.rest.IWebAppRestClient;
import com.chariot.backend.gateway.webapp.rest.impl.WebAppRestClient;
import com.chariot.backend.gateway.webapp.rest.impl.WebAppRestClientMock;
import com.chariot.backend.gateway.webapp.rest.impl.mock.WebAppRestClientMock;
import com.chariot.backend.utils.http.RestClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -28,15 +28,15 @@ public void setUserLicenseServiceUrl(String userLicenseServiceUrl) {


@Bean
@ConditionalOnProperty(name = "actAsMock",
@ConditionalOnProperty(name = "shouldActAsMock",
havingValue = "false")
@Primary
public IWebAppRestClient remoteServiceCaller() {
return new WebAppRestClient();
}

@Bean
@ConditionalOnProperty(name = "actAsMock",
@ConditionalOnProperty(name = "shouldActAsMock",
havingValue = "true")
@Primary
public IWebAppRestClient remoteServiceCallerMock() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.chariot.backend.gateway.webapp.rest;

import com.chariot.backend.model.*;
import com.chariot.backend.schema.login.LoginResponse;
import com.chariot.backend.schema.notification.NotificationRegistrationToken;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -34,5 +36,13 @@ public interface IWebAppRestClient {

ResponseEntity<License> createNewLicense(int requestsPerSeconds, LicenseType newLicenseType);

ResponseEntity<String> authenticateUser(String username, String password);
ResponseEntity<LoginResponse> authenticateUser(String username, String password);

void putNewMeasurement(Double value, String channelId) throws Throwable;

void registerNotificationToken(NotificationRegistrationToken notificationRegistrationToken);

void setNewMeasurementNotificationRule(String channelId);

void setOutOfBoundNotificationRule(String channelId, double upperBound, double lowerBound);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.chariot.backend.gateway.webapp.config.Configuration;
import com.chariot.backend.gateway.webapp.rest.IWebAppRestClient;
import com.chariot.backend.model.*;
import com.chariot.backend.schema.login.LoginResponse;
import com.chariot.backend.schema.notification.NotificationRegistrationToken;
import com.chariot.backend.utils.http.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -66,12 +68,12 @@ public Collection<Channel> listChannels(String userName) {
}

@Override
public ResponseEntity<String> authenticateUser(String username, String password) {
public ResponseEntity<LoginResponse> authenticateUser(String username, String password) {
MultiValueMap<String, String> uriParams = new LinkedMultiValueMap<>();
uriParams.add("userName", username);
uriParams.add("password", password);
String url = configuration.getUserLicenseServiceUrl() + "/chariot/user/authenticate";
return restClient.get(uriParams, url, String.class);
return restClient.get(uriParams, url, LoginResponse.class);
}

@Override
Expand Down Expand Up @@ -113,5 +115,23 @@ public ResponseEntity<List<Measurement>> getMeasurementForChannelId(String chann
throw new NotImplementedException();
}

@Override
public void putNewMeasurement(Double value, String channelId) {
throw new NotImplementedException();
}

@Override
public void registerNotificationToken(NotificationRegistrationToken notificationRegistrationToken) {

}

}
@Override
public void setNewMeasurementNotificationRule(String channelId) {

}

@Override
public void setOutOfBoundNotificationRule(String channelId, double upperBound, double lowerBound) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.chariot.backend.gateway.webapp.rest.impl.mock;

import com.chariot.backend.model.ChannelNotification;
import com.chariot.backend.schema.notification.NotificationRegistrationToken;
import com.kinoroy.expo.push.*;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class NotificationMockApi {
private Map<String, String> tokens = new HashMap<>();

public void registerToken(NotificationRegistrationToken notificationRegistrationToken) {
tokens.put(notificationRegistrationToken.getUsername(), notificationRegistrationToken.getToken());
}

public void pushNotification(ChannelNotification channelNotification) throws Throwable {
Collection<String> somePushTokens = tokens.values();
List<Message> messages = new ArrayList<>();
for (String token : somePushTokens) {
if (!ExpoPushClient.isExpoPushToken(token)) {
System.out.println(token + " is not a valid Expo Push Token!");
}
}
for (String token : somePushTokens) {

HashMap<String, Object> data = new HashMap<>();
data.put("key", "value");


Message message = new Message.Builder()
.to(token)
.title(channelNotification.getTitle())
.body(channelNotification.getBody())
.data(channelNotification.getData())
.build();
messages.add(message);
}

List<List<Message>> chunks = ExpoPushClient.chunkItems(messages);

for (List<Message> chunk : chunks) {
try {
PushTicketResponse response = ExpoPushClient.sendPushNotifications(messages);
List<ExpoError> errors = response.getErrors();
if (errors != null) {
for (ExpoError error : errors) {
// Handle the errors
}
}
List<PushTicket> tickets = response.getTickets();
if (tickets != null) {
for (PushTicket ticket : tickets) {
if (ticket.getStatus() == Status.OK) {
String id = ticket.getId();
} else {
PushError e = ticket.getDetails().getError();
switch (e) {
case MESSAGE_TOO_BIG:
case INVALID_CREDENTIALS:
case DEVICE_NOT_REGISTERED:
case MESSAGE_RATE_EXCEEDED:
}

}
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}

}


}
Loading

0 comments on commit 4dd9e86

Please sign in to comment.