Skip to content

Instantly share code, notes, and snippets.

@nkenna
Created June 21, 2018 08:40
Show Gist options
  • Save nkenna/dd027a9d8da7b49dfb5f9e371d7566fb to your computer and use it in GitHub Desktop.
Save nkenna/dd027a9d8da7b49dfb5f9e371d7566fb to your computer and use it in GitHub Desktop.
.........
//pubnub channels
String GARAGE_DOOR = "GARAGE_DOOR";
String FRONT_DOOR = "FRONT_DOOR";
String CAMERA = "CAMERA";
.........
// payloads
// operating doors
String garageDoor_OPEN = "GARAGE DOOR OPEN";
String garageDoor_CLOSE = "GARAGE DOOR CLOSE";
String frontDoor_OPEN = "FRONT DOOR OPEN";
String frontDoor_CLOSE = "FRONT DOOR CLOSE";
// operating camera
String camera_Right = "RIGHT"; // right is to sitting room
String camera_Left = "LEFT"; // left is to bedroom
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize Pubnub Configuration and PubNub
pubnubConfig = new PubnubConfig();
pubnub = new PubNub(pubnubConfig.pConfig());
ritebtn = (Button)findViewById(R.id.ritebtn);
leftbtn = (Button)findViewById(R.id.leftbtn);
frontDoor = (Switch) findViewById(R.id.frontdoorSwitch);
garageDoor = (Switch) findViewById(R.id.garagedoorswitch);
ritebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ritebtnPressed();
}
});
leftbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
leftbtnPressed();
}
});
frontDoor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
operateFrontDoor(b);
}
});
garageDoor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
operateGarageDoor(b);
}
});
}
public void operateGarageDoor(boolean b){
if (b){
pubPublishThread.pubnubPublish(garageDoor_OPEN, GARAGE_DOOR);
}else{
pubPublishThread.pubnubPublish(garageDoor_CLOSE, GARAGE_DOOR);
}
}
public void operateFrontDoor(boolean b){
if (b){
pubPublishThread.pubnubPublish(frontDoor_OPEN, FRONT_DOOR);
}else{
pubPublishThread.pubnubPublish(frontDoor_CLOSE, FRONT_DOOR);
}
}
void ritebtnPressed(){
payload = camera_Right;
channel = CAMERA;
pubPublishThread.pubnubPublish(payload, channel);
}
void leftbtnPressed(){
payload = camera_Left;
channel = CAMERA;
pubPublishThread.pubnubPublish(payload, channel);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment