Created
June 21, 2018 08:49
-
-
Save nkenna/a82d1e674bede07169a3160c615cf808 to your computer and use it in GitHub Desktop.
Pubnub Publish and Subscribe thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
................. | |
PubConSubThread pubConSubThread;//connect and subscribe | |
PubPublishThread pubPublishThread;//Publish | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//subscribe to pubnub channels | |
pubnubSubscribe(); | |
} | |
void pubnubSubscribe(){ | |
pubPublishThread = new PubPublishThread(); | |
pubConSubThread = new PubConSubThread(); | |
try { | |
pubConSubThread.start(); | |
pubPublishThread.start(); | |
}catch (Exception e){ | |
statusTxtview.setText(e.getMessage()); | |
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
//subcribe class outside UI thread | |
public class PubConSubThread extends Thread { | |
public static final String Tag = "Pubnub Connect Subscribe Thread"; | |
private static final int DELAY = 5000; | |
PubPublishThread pp = new PubPublishThread(); | |
@Override | |
public void run() { | |
try { | |
pubnubConSubscribe(); | |
}catch (Exception e){ | |
final String err = e.getMessage(); | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Toast.makeText(MainActivity.this, err, Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
} | |
//pubnub connect and subscribe | |
private void pubnubConSubscribe(){ | |
connectProgress(Color.RED, "Connecting"); | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Toast.makeText(MainActivity.this, "inside pubnubConSubscribe method", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
//final String direction = "direction"; | |
pubnub.addListener(new SubscribeCallback() { | |
@Override | |
public void status(PubNub pubnub, PNStatus status) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Toast.makeText(MainActivity.this, "inside status pubnub addlistener", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
if (status.getCategory() == PNStatusCategory.PNUnexpectedDisconnectCategory){ | |
connectProgress(Color.RED, "Disconnected"); | |
pubnub.reconnect(); | |
}else if (status.getCategory() == PNStatusCategory.PNConnectedCategory){ | |
pp.pubnubPublish(sensors_Data, SENSORS); | |
if (status.getCategory() == PNStatusCategory.PNConnectedCategory){ | |
connectProgress(Color.GREEN, "Connected"); | |
//pubnub.subscribe().channels(Arrays.asList(FEEDBACKS, SENSOR_DATA, SENSORS_HUMID, SENSORS_SOIL, SENSORS_TEMP, CAMERA, IRRIGATE )).execute(); | |
}else if (status.getCategory() == PNStatusCategory.PNReconnectedCategory){ | |
pp.pubnubPublish(sensors_Data, SENSORS); | |
connectProgress(Color.GREEN, "Reconnected"); | |
// pubnub.subscribe().channels(Arrays.asList(FEEDBACKS, SENSOR_DATA, SENSORS_HUMID, SENSORS_SOIL, SENSORS_TEMP, CAMERA, IRRIGATE )).execute(); | |
}else if (status.getCategory() == PNStatusCategory.PNDecryptionErrorCategory){ | |
pubnub.reconnect(); | |
}else if (status.getCategory() == PNStatusCategory.PNTimeoutCategory){ | |
connectProgress(Color.RED, "Network Timeout"); | |
pubnub.reconnect(); | |
}else { | |
pubnub.reconnect(); | |
connectProgress(Color.RED, "No Connection"); | |
} | |
} | |
} | |
@Override | |
public void message(PubNub pubnub, final PNMessageResult message) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
if (message.getChannel().equalsIgnoreCase(FEEDBACKS)) { | |
if (message.getMessage().getAsString().equalsIgnoreCase("BEDROOM MOTION") || | |
message.getMessage().getAsString().equalsIgnoreCase("GARAGE MOTION") || | |
message.getMessage().getAsString().equalsIgnoreCase("SITTINGROOM MOTION")){ | |
plsyTone(); | |
} | |
updateStatusTxtView(message.getMessage().getAsString()); | |
}else if (message.getChannel().equalsIgnoreCase(SENSORS_TEMP)){ | |
updateTempTextView(message.getMessage().getAsString()); | |
}else if (message.getChannel().equalsIgnoreCase(SENSORS)){ | |
updateStatusTxtView(message.getMessage().getAsString()); | |
}else if (message.getChannel().equalsIgnoreCase(SENSORS_HUMID)){ | |
updateHumdityTextView(message.getMessage().getAsString()); | |
}else if (message.getChannel().equalsIgnoreCase(PHOTO)){ | |
updateStatusTxtView(message.getMessage().getAsString()); | |
} | |
} | |
}); | |
} | |
@Override | |
public void presence(PubNub pubnub, PNPresenceEventResult presence) { | |
} | |
}); | |
pubnub.subscribe().channels(Arrays.asList(FEEDBACKS, SENSORS, CAMERA, SENSORS_HUMID, SENSORS_TEMP, MODE, PHOTO, | |
ELECTRICAL, GARAGE_DOOR, FRONT_DOOR, FAN, BEDROOM_LIGHT, SITTINGROOM_LIGHT, GARAGE_LIGHT, SECURITY_LIGHT )).execute(); | |
} | |
private void connectProgress(int colour, String connectStatus){ | |
final int col = colour; | |
final String cStatus = connectStatus; | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
updateConnectTextView(col, cStatus); | |
} | |
}); | |
} | |
} | |
//publish class thread | |
public class PubPublishThread extends Thread { | |
public static final String Tag = "Pubnub Publish Thread"; | |
private static final int DELAY = 5000; | |
MainActivity mainActivity = new MainActivity(); | |
@Override | |
public void run() { | |
super.run(); | |
} | |
public void pubnubPublish(String payload, String channel){ | |
try { | |
pubnub.publish().message(payload).channel(channel).async(new PNCallback<PNPublishResult>() { | |
@Override | |
public void onResponse(PNPublishResult result, PNStatus status) { | |
if (!status.isError()) { | |
publishProgress(Color.GREEN, "publish success"); | |
} else { | |
publishProgress(Color.RED, status.getCategory().toString()); | |
status.retry(); | |
} | |
} | |
}); | |
}catch (Exception e){ | |
final String err = e.getMessage(); | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Toast.makeText(MainActivity.this, err, Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
} | |
private void publishProgress(final int colour, final String publishStatus){ | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
updatePubTextView(colour, publishStatus); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment