PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/org.eclipse.paho.jmeclient/org.eclipse.paho.jmeclient.mqttv3.GPIOSampleAsyncWait/src/org/eclipse/paho/jmeclient/mqttv3/sampleGPIO/AsyncWait/GPIOSampleAsyncWait.java

https://gitlab.com/jforge/paho.mqtt.java
Java | 296 lines | 196 code | 29 blank | 71 comment | 27 complexity | a7abc9168fc397ff59de3569257bdc1c MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2014 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Dave Locke - initial API and implementation and/or initial documentation
  15. */
  16. package org.eclipse.paho.jmeclient.mqttv3.sampleGPIO.AsyncWait;
  17. import java.io.IOException;
  18. import javax.microedition.midlet.MIDlet;
  19. import org.eclipse.paho.client.mqttv3.MqttClient;
  20. import org.eclipse.paho.client.mqttv3.MqttException;
  21. import com.oracle.deviceaccess.gpio.GPIOManager;
  22. import com.oracle.deviceaccess.gpio.GPIOPin;
  23. import com.oracle.deviceaccess.gpio.GPIOPort;
  24. import com.oracle.deviceaccess.gpio.PinEvent;
  25. import com.oracle.deviceaccess.gpio.PinListener;
  26. /**
  27. * A sample application that demonstrates how to use the Java ME MQTT v3 Client api in
  28. * non-blocking callback/notification mode.
  29. *
  30. * Button 1 Subscribes to the specified topic and waits for messages indefinitely
  31. * Button 2 Publishes to the specified topic string and disconnects
  32. * Button 3 Disconnects the Subscription and client created in Button 1
  33. *
  34. * It can be run from an MIDP Emulator or a physical device
  35. *
  36. * There are three versions of the sample that implement the same features
  37. * but do so using using different programming styles:
  38. * <ol>
  39. * <li>GPIOSample (this one) which uses the API which blocks until the operation completes</li>
  40. * <li>GPIOSampleAsyncWait shows how to use the asynchronous API with waiters that block until
  41. * an action completes</li>
  42. * <li>GPIOSampleAsyncCallBack shows how to use the asynchronous API where events are
  43. * used to notify the application when an action completes<li>
  44. * </ol>
  45. *
  46. */
  47. public class GPIOSampleAsyncWait extends MIDlet {
  48. static final int LED1_PIN_ID = 1;
  49. static final String LED1_PIN_NAME = "LED 1";
  50. static final int LED2_PIN_ID = 2;
  51. static final String LED2_PIN_NAME = "LED 2";
  52. static final int BUTTON1_PIN_ID = 5;
  53. static final String BUTTON1_PIN_NAME = "BUTTON 1";
  54. static final int BUTTON2_PIN_ID = 6;
  55. static final String BUTTON2_PIN_NAME = "BUTTON 2";
  56. static final int BUTTON3_PIN_ID = 7;
  57. static final String BUTTON3_PIN_NAME = "BUTTON 3";
  58. static final int LED_PORT_ID = 1;
  59. static final String LED_PORT_NAME = "LEDS";
  60. boolean bFirst = false;
  61. boolean loopFlag = true;
  62. private GPIOPin led1 = null;
  63. private GPIOPin led2 = null;
  64. private GPIOPort ledPort = null;
  65. private GPIOPin button1 = null;
  66. private GPIOPin button2 = null;
  67. private GPIOPin button3 = null;
  68. MqttClient client = null;
  69. MqttClient pubClinet = null;
  70. // Private instance variables
  71. private SampleAsyncWait sampleClientPub = null;
  72. private SampleAsyncWait sampleClientSub = null;
  73. private boolean quietMode = false;
  74. private String broker = "m2m.eclipse.org";
  75. private int port = 1883;
  76. private String clientIdPub = "SampleAsynCallBackPub";
  77. private String clientIdSub = "SampleAsynCallBackSub";
  78. private boolean cleanSession = true; // Non durable subscriptions
  79. private String password = null;
  80. private String userName = null;
  81. private String protocol = "tcp://";
  82. private String url = protocol + broker + ":" + port;
  83. /**
  84. * Signals the MIDlet that it has entered the Active state
  85. */
  86. public void startApp() {
  87. if(bFirst == false) {
  88. System.out.println("Starting GPIO Demo");
  89. try {
  90. led1 = GPIOManager.getPin(LED1_PIN_ID);
  91. led2 = GPIOManager.getPin(LED2_PIN_ID);
  92. ledPort = GPIOManager.getPort(LED_PORT_ID);
  93. button1 = GPIOManager.getPin(BUTTON1_PIN_ID);
  94. button2 = GPIOManager.getPin(BUTTON2_PIN_ID);
  95. button3 = GPIOManager.getPin(BUTTON3_PIN_ID);
  96. } catch (Exception ex) {
  97. ex.printStackTrace();
  98. System.out.println("Get pin and port fail");
  99. return;
  100. }
  101. System.out.println("set listener for button 1,2,3");
  102. try {
  103. button1.setInputListener(button1Listener);
  104. button2.setInputListener(button2Listener);
  105. button3.setInputListener(button3Listener);
  106. } catch (Exception ex) {
  107. ex.printStackTrace();
  108. }
  109. bFirst = true;
  110. } else {
  111. System.out.println("GPIO Demo is already started..");
  112. }
  113. }
  114. /**
  115. * Signals the MIDlet to enter the Paused state
  116. */
  117. public void pauseApp() {
  118. }
  119. /**
  120. * Signals the MIDlet to terminate and enter the Destroyed state
  121. */
  122. public void destroyApp(boolean unconditional) {
  123. bFirst = false;
  124. if(led1 != null){
  125. try {
  126. led1.close();
  127. } catch (IOException ex) {
  128. ex.printStackTrace();
  129. }
  130. led1 = null;
  131. }
  132. if(led2 != null){
  133. try {
  134. led2.close();
  135. } catch (IOException ex) {
  136. ex.printStackTrace();
  137. }
  138. led2 = null;
  139. }
  140. if(ledPort != null){
  141. try {
  142. ledPort.close();
  143. } catch (IOException ex) {
  144. ex.printStackTrace();
  145. }
  146. ledPort = null;
  147. }
  148. if(button1 != null){
  149. try {
  150. button1.close();
  151. } catch (IOException ex) {
  152. ex.printStackTrace();
  153. }
  154. button1 = null;
  155. }
  156. if(button2 != null){
  157. try {
  158. button2.close();
  159. } catch (IOException ex) {
  160. ex.printStackTrace();
  161. }
  162. button2 = null;
  163. }
  164. }
  165. /**
  166. * Detects GPIO pin value changes
  167. */
  168. PinListener button1Listener = new PinListener(){
  169. public void valueChanged(PinEvent event) {
  170. GPIOPin pin = event.getPin();
  171. System.out.println("listener1 "+ pin.getID());
  172. if(pin.getID() == BUTTON1_PIN_ID ){
  173. try {
  174. subscribe();
  175. } catch (Exception ex) {
  176. System.out.println("Failed");
  177. ex.printStackTrace();
  178. }
  179. }
  180. }
  181. };
  182. /**
  183. * Detects GPIO pin value changes
  184. */
  185. PinListener button2Listener = new PinListener(){
  186. public void valueChanged(PinEvent event) {
  187. GPIOPin pin = event.getPin();
  188. System.out.println("listener2 "+ pin.getID());
  189. if(pin.getID() == BUTTON2_PIN_ID ){
  190. try {
  191. publish();
  192. } catch (Exception ex) {
  193. ex.printStackTrace();
  194. }
  195. }
  196. }
  197. };
  198. /**
  199. * Detects GPIO pin value changes
  200. */
  201. PinListener button3Listener = new PinListener(){
  202. public void valueChanged(PinEvent event) {
  203. GPIOPin pin = event.getPin();
  204. System.out.println("listener2 "+ pin.getID());
  205. if(pin.getID() == BUTTON3_PIN_ID ){
  206. try {
  207. disconnect();
  208. } catch (Exception ex) {
  209. ex.printStackTrace();
  210. }
  211. }
  212. }
  213. };
  214. /**
  215. * Publish / send a message to an MQTT server
  216. * @throws MqttException, IOException
  217. */
  218. protected void publish() throws MqttException, IOException {
  219. sampleClientPub = new SampleAsyncWait(url,clientIdPub,cleanSession, quietMode,userName,password);
  220. if (sampleClientPub != null) {
  221. String topic = "Sample/Java/v3";
  222. int qos = 2;
  223. String message = "Message from async calback MQTTv3 Java client sample";
  224. try {
  225. sampleClientPub.publish(topic,qos,message.getBytes());
  226. } catch (Throwable e) {
  227. e.printStackTrace();
  228. }
  229. }
  230. }
  231. /**
  232. * Subscribe to a topic on an MQTT server
  233. * Once subscribed this method waits for the messages to arrive from the server
  234. * that match the subscription. It continues listening for messages until pin 3
  235. * is pressed
  236. * @throws IOException, MqttException
  237. */
  238. protected void subscribe() throws IOException {
  239. if (sampleClientSub == null) {
  240. try {
  241. sampleClientSub = new SampleAsyncWait(url,clientIdSub,cleanSession, quietMode,userName,password);
  242. } catch (MqttException e) {
  243. e.printStackTrace();
  244. }
  245. }
  246. if (sampleClientSub != null) {
  247. String topic = "Sample/#";
  248. int qos = 2;
  249. try {
  250. sampleClientSub.subscribe(topic,qos);
  251. } catch (Throwable e) {
  252. e.printStackTrace();
  253. }
  254. }
  255. }
  256. /**
  257. * Disconnect the client that has subscribed from the server
  258. * @throws MqttException
  259. */
  260. private void disconnect() throws MqttException {
  261. if (sampleClientSub != null) {
  262. // Disconnect the client from the server
  263. sampleClientSub.disconnect();
  264. sampleClientSub = null;
  265. }
  266. }
  267. }