Skip to content

Commit

Permalink
Implemented test functionality from API
Browse files Browse the repository at this point in the history
  • Loading branch information
ScreamingOranges committed Jun 29, 2021
1 parent 1c67f8c commit efe5c42
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected void onCreate(Bundle savedInstanceState) {

DefaultColor = 0;
new NotificationListener().setListener(this) ;
pusherHelper = new PusherHelper(context);
pusherHelper = new PusherHelper(MainActivity.this);

Utility.assignSpinner(devices, this, binding);
requestDeviceHelper(this);
Expand Down
35 changes: 26 additions & 9 deletions app/src/main/java/com/example/icuepyphone/PusherClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.example.icuepyphone;

import android.content.Context;

import androidx.core.content.ContextCompat;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -19,19 +23,32 @@ public class PusherClient {
public Map<String,String> devices;
private Pusher pusher;
private PusherOptions options;
private Context mainContext;

public PusherClient(ArrayList<String> pusherCredentials){
public PusherClient(ArrayList<String> pusherCredentials, Context context){
mainContext = context;
options = new PusherOptions().setCluster(pusherCredentials.get(3));
pusher = new Pusher(pusherCredentials.get(1), options);
}

public void eventHandler(PusherEvent event){
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
devices = mapper.readValue(event.getData(), HashMap.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
switch (event.getEventName()){
case "test_event":
ContextCompat.getMainExecutor(mainContext).execute(() -> {
Utility.showNotice(mainContext, "Notice!", event.getData());
});
break;
case "api_event":
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
devices = mapper.readValue(event.getData(), HashMap.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
break;
default:
System.out.println("UNRECOGNISED EVENT");
}
}

Expand All @@ -56,13 +73,13 @@ public void onSubscriptionSucceeded(String channelName) {

@Override
public void onEvent(PusherEvent event) {
//System.out.println(String.format("Received event [%s]", event.toString()));
//System.out.println(String.format("Received event [%s]", event.getEventName()));
eventHandler(event);
}
};

pusher.connect(connectionEventListener);
Channel channel = pusher.subscribe("api_Callback", channelEventListener, "api_event");
Channel channel = pusher.subscribe("api_Callback", channelEventListener, "api_event", "test_event");


while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public PusherHelper(Context context){
if(!pusherCredentials.isEmpty()){
pusher = new Pusher(pusherCredentials.get(0), pusherCredentials.get(1), pusherCredentials.get(2));
pusher.setCluster(pusherCredentials.get(3));
pusherClient = new PusherClient(pusherCredentials);
pusherClient = new PusherClient(pusherCredentials, context);
Thread thread = new Thread(new Runnable() {
@Override
public void run(){
Expand Down

0 comments on commit efe5c42

Please sign in to comment.