Advertisement
Guest User

Untitled

a guest
Mar 5th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. import android.app.Activity;
  2.  
  3. import java.io.IOException;
  4.  
  5. import io.particle.android.sdk.cloud.ParticleCloud;
  6. import io.particle.android.sdk.cloud.ParticleCloudException;
  7. import io.particle.android.sdk.cloud.ParticleCloudSDK;
  8. import io.particle.android.sdk.cloud.ParticleDevice;
  9. import io.particle.android.sdk.cloud.ParticleEvent;
  10. import io.particle.android.sdk.cloud.ParticleEventHandler;
  11. import io.particle.android.sdk.utils.Async;
  12.  
  13. //Garage Door
  14. class GarageClient {
  15.  
  16. private ParticleDevice myDevice;
  17.  
  18. enum GarageStatus {
  19. OPEN,
  20. CLOSED,
  21. UNKNOWN
  22. }
  23.  
  24. private final String deviceId = "DEVICE_ID";
  25. private final String particleCloudUser = "CLOUD_USERNAME";
  26. private final String particleCloudPassword = "CLOUD_PASSWORD";
  27.  
  28. private GarageStatusCallback onGetGarageStatusListener;
  29.  
  30. void setOnGetGarageStatus(GarageStatusCallback listener) {
  31. onGetGarageStatusListener = listener;
  32. }
  33.  
  34.  
  35. private final ParticleEventHandler garageStatusHandler = new ParticleEventHandler() {
  36.  
  37. @Override
  38. public void onEvent(String eventName, ParticleEvent event) {
  39. System.out.println("dataPayload=" + event.dataPayload);
  40.  
  41. if (Integer.parseInt(event.dataPayload) == 0) {
  42. onGetGarageStatusListener.OnGetGarageStatusListener(GarageStatus.CLOSED);
  43. }
  44. if (Integer.parseInt(event.dataPayload) == 1) {
  45. onGetGarageStatusListener.OnGetGarageStatusListener(GarageStatus.OPEN);
  46. }
  47. }
  48.  
  49. @Override
  50. public void onEventError(Exception e) {
  51. e.printStackTrace();
  52. }
  53. };
  54.  
  55.  
  56. GarageClient() {
  57.  
  58. Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, Integer>() {
  59.  
  60.  
  61. @Override
  62. public Integer callApi(ParticleCloud particleCloud) throws ParticleCloudException, IOException {
  63. particleCloud.logIn(particleCloudUser, particleCloudPassword);
  64. return 1;
  65. }
  66.  
  67. @Override
  68. public void onSuccess(Integer integer) {
  69. getDevice();
  70. }
  71.  
  72. @Override
  73. public void onFailure(ParticleCloudException exception) {
  74. exception.printStackTrace();
  75. }
  76. });
  77. }
  78.  
  79. private void getDevice() {
  80. Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, ParticleDevice>() {
  81.  
  82.  
  83. @Override
  84. public ParticleDevice callApi(ParticleCloud particleCloud) throws ParticleCloudException, IOException {
  85. ParticleDevice d = ParticleCloudSDK.getCloud().getDevice(deviceId);
  86.  
  87. return d;
  88. }
  89.  
  90. @Override
  91. public void onSuccess(ParticleDevice device) {
  92.  
  93. myDevice = device;
  94. subscribeStatusEvents();
  95. onGetGarageStatusListener.OnInitCompleteListener();
  96. }
  97.  
  98. @Override
  99. public void onFailure(ParticleCloudException exception) {
  100. exception.printStackTrace();
  101. }
  102. });
  103.  
  104. }
  105.  
  106.  
  107. /**
  108. * Subscribe to when garage is open or closed
  109. */
  110. private void subscribeStatusEvents() {
  111. Async.executeAsync(myDevice, new Async.ApiProcedure<ParticleDevice>() {
  112. @Override
  113. public Void callApi(ParticleDevice particleDevice) throws ParticleCloudException, IOException {
  114. //particleDevice.subscribeToEvents("garage-status", garageStatusHandler);
  115. ParticleCloudSDK.getCloud().subscribeToDeviceEvents(null,deviceId,garageStatusHandler);
  116. return null;
  117. }
  118.  
  119. @Override
  120. public void onFailure(ParticleCloudException exception) {
  121. exception.printStackTrace();
  122. }
  123. });
  124.  
  125. }
  126.  
  127.  
  128. /**
  129. * Check if garage door is open
  130. *
  131. * @return
  132. */
  133. void getGarageStatus() {
  134.  
  135. Async.executeAsync(myDevice, new Async.ApiWork<ParticleDevice, GarageStatus>() {
  136. @Override
  137. public GarageStatus callApi(ParticleDevice particleDevice) throws ParticleCloudException, IOException {
  138. try {
  139. int value = particleDevice.callFunction("isopen");
  140. if (value == 0) {
  141. return GarageStatus.CLOSED;
  142. } else if (value == 1) {
  143. return GarageStatus.OPEN;
  144. } else {
  145. return GarageStatus.UNKNOWN;
  146. }
  147. } catch (ParticleDevice.FunctionDoesNotExistException e) {
  148. e.printStackTrace();
  149. return GarageStatus.UNKNOWN;
  150. }
  151. }
  152.  
  153. @Override
  154. public void onSuccess(GarageStatus status) {
  155. onGetGarageStatusListener.OnGetGarageStatusListener(status);
  156. }
  157.  
  158. @Override
  159. public void onFailure(ParticleCloudException exception) {
  160. onGetGarageStatusListener.OnGetGarageStatusListener(GarageStatus.UNKNOWN);
  161. }
  162. });
  163. }
  164.  
  165.  
  166. void toggleGarage() {
  167. //TODO toggleGarage
  168. /*Async.executeAsync(myDevice, new Async.ApiWork<ParticleDevice, Integer>() {
  169.  
  170. public Integer callApi(ParticleDevice particleDevice) throws IOException, ParticleCloudException {
  171.  
  172. try {
  173. return particleDevice.callFunction("togglegarage");
  174. } catch (ParticleDevice.FunctionDoesNotExistException e) {
  175. e.printStackTrace();
  176. return 0;
  177. }
  178.  
  179. }
  180.  
  181. @Override
  182. public void onSuccess(Integer value) {
  183. Toaster.s(context, "Room temp is " + value + " degrees.");
  184. }
  185.  
  186. @Override
  187. public void onFailure(ParticleCloudException e) {
  188. Log.e("some tag", "Something went wrong making an SDK call: ", e);
  189. Toaster.l(context, "Uh oh, something went wrong.");
  190. }
  191. });*/
  192.  
  193. }
  194.  
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement