Created
March 6, 2017 03:16
-
-
Save Hammy223/462fb7842465ce215f3878b07082a287 to your computer and use it in GitHub Desktop.
Particle Photon Android GarageClient.java
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
import android.app.Activity; | |
import java.io.IOException; | |
import io.particle.android.sdk.cloud.ParticleCloud; | |
import io.particle.android.sdk.cloud.ParticleCloudException; | |
import io.particle.android.sdk.cloud.ParticleCloudSDK; | |
import io.particle.android.sdk.cloud.ParticleDevice; | |
import io.particle.android.sdk.cloud.ParticleEvent; | |
import io.particle.android.sdk.cloud.ParticleEventHandler; | |
import io.particle.android.sdk.utils.Async; | |
//Garage Door | |
class GarageClient { | |
private ParticleDevice myDevice; | |
enum GarageStatus { | |
OPEN, | |
CLOSED, | |
UNKNOWN | |
} | |
private final String deviceId = "DEVICE_ID"; | |
private final String particleCloudUser = "CLOUD_USERNAME"; | |
private final String particleCloudPassword = "CLOUD_PASSWORD"; | |
private GarageStatusCallback onGetGarageStatusListener; | |
void setOnGetGarageStatus(GarageStatusCallback listener) { | |
onGetGarageStatusListener = listener; | |
} | |
private final ParticleEventHandler garageStatusHandler = new ParticleEventHandler() { | |
@Override | |
public void onEvent(String eventName, ParticleEvent event) { | |
System.out.println("dataPayload=" + event.dataPayload); | |
if (Integer.parseInt(event.dataPayload) == 0) { | |
onGetGarageStatusListener.OnGetGarageStatusListener(GarageStatus.CLOSED); | |
} | |
if (Integer.parseInt(event.dataPayload) == 1) { | |
onGetGarageStatusListener.OnGetGarageStatusListener(GarageStatus.OPEN); | |
} | |
} | |
@Override | |
public void onEventError(Exception e) { | |
e.printStackTrace(); | |
} | |
}; | |
GarageClient() { | |
Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, Integer>() { | |
@Override | |
public Integer callApi(ParticleCloud particleCloud) throws ParticleCloudException, IOException { | |
particleCloud.logIn(particleCloudUser, particleCloudPassword); | |
return 1; | |
} | |
@Override | |
public void onSuccess(Integer integer) { | |
getDevice(); | |
} | |
@Override | |
public void onFailure(ParticleCloudException exception) { | |
exception.printStackTrace(); | |
} | |
}); | |
} | |
private void getDevice() { | |
Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, ParticleDevice>() { | |
@Override | |
public ParticleDevice callApi(ParticleCloud particleCloud) throws ParticleCloudException, IOException { | |
ParticleDevice d = ParticleCloudSDK.getCloud().getDevice(deviceId); | |
return d; | |
} | |
@Override | |
public void onSuccess(ParticleDevice device) { | |
myDevice = device; | |
subscribeStatusEvents(); | |
onGetGarageStatusListener.OnInitCompleteListener(); | |
} | |
@Override | |
public void onFailure(ParticleCloudException exception) { | |
exception.printStackTrace(); | |
} | |
}); | |
} | |
/** | |
* Subscribe to when garage is open or closed | |
*/ | |
private void subscribeStatusEvents() { | |
Async.executeAsync(myDevice, new Async.ApiProcedure<ParticleDevice>() { | |
@Override | |
public Void callApi(ParticleDevice particleDevice) throws ParticleCloudException, IOException { | |
//particleDevice.subscribeToEvents("garage-status", garageStatusHandler); | |
ParticleCloudSDK.getCloud().subscribeToDeviceEvents(null,deviceId,garageStatusHandler); | |
return null; | |
} | |
@Override | |
public void onFailure(ParticleCloudException exception) { | |
exception.printStackTrace(); | |
} | |
}); | |
} | |
/** | |
* Check if garage door is open | |
* | |
* @return | |
*/ | |
void getGarageStatus() { | |
Async.executeAsync(myDevice, new Async.ApiWork<ParticleDevice, GarageStatus>() { | |
@Override | |
public GarageStatus callApi(ParticleDevice particleDevice) throws ParticleCloudException, IOException { | |
try { | |
int value = particleDevice.callFunction("isopen"); | |
if (value == 0) { | |
return GarageStatus.CLOSED; | |
} else if (value == 1) { | |
return GarageStatus.OPEN; | |
} else { | |
return GarageStatus.UNKNOWN; | |
} | |
} catch (ParticleDevice.FunctionDoesNotExistException e) { | |
e.printStackTrace(); | |
return GarageStatus.UNKNOWN; | |
} | |
} | |
@Override | |
public void onSuccess(GarageStatus status) { | |
onGetGarageStatusListener.OnGetGarageStatusListener(status); | |
} | |
@Override | |
public void onFailure(ParticleCloudException exception) { | |
onGetGarageStatusListener.OnGetGarageStatusListener(GarageStatus.UNKNOWN); | |
} | |
}); | |
} | |
void toggleGarage() { | |
//TODO toggleGarage | |
/*Async.executeAsync(myDevice, new Async.ApiWork<ParticleDevice, Integer>() { | |
public Integer callApi(ParticleDevice particleDevice) throws IOException, ParticleCloudException { | |
try { | |
return particleDevice.callFunction("togglegarage"); | |
} catch (ParticleDevice.FunctionDoesNotExistException e) { | |
e.printStackTrace(); | |
return 0; | |
} | |
} | |
@Override | |
public void onSuccess(Integer value) { | |
Toaster.s(context, "Room temp is " + value + " degrees."); | |
} | |
@Override | |
public void onFailure(ParticleCloudException e) { | |
Log.e("some tag", "Something went wrong making an SDK call: ", e); | |
Toaster.l(context, "Uh oh, something went wrong."); | |
} | |
});*/ | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment