MyDaemonInstallationNodeContribution.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package uraxis;
  2. import com.ur.urcap.api.contribution.DaemonContribution;
  3. import com.ur.urcap.api.contribution.InstallationNodeContribution;
  4. import com.ur.urcap.api.domain.data.DataModel;
  5. import com.ur.urcap.api.domain.script.ScriptWriter;
  6. import com.ur.urcap.api.ui.annotation.Div;
  7. import com.ur.urcap.api.ui.annotation.Input;
  8. import com.ur.urcap.api.ui.annotation.Label;
  9. import com.ur.urcap.api.ui.component.*;
  10. import org.apache.xmlrpc.XmlRpcException;
  11. import java.awt.EventQueue;
  12. import java.util.Timer;
  13. import java.util.TimerTask;
  14. public class MyDaemonInstallationNodeContribution implements InstallationNodeContribution {
  15. private static final String POPUPTITLE_KEY = "popuptitle";
  16. private static final String XMLRPC_VARIABLE = "my_daemon";
  17. private static final String ENABLED_KEY = "enabled";
  18. private static final String DEFAULT_VALUE = "HelloWorld";
  19. private DataModel model;
  20. // private final MyDaemonDaemonService daemonService;
  21. private MyDaemonInterface daemonInterface;
  22. private Timer uiTimer;
  23. private Timer statusTimer;
  24. public MyDaemonInstallationNodeContribution(MyDaemonDaemonService daemonService, DataModel model) {
  25. // this.daemonService = daemonService;
  26. this.model = model;
  27. daemonInterface = new MyDaemonInterface("10.0.31.42", 8082);
  28. // applyDesiredDaemonStatus();
  29. }
  30. @Input(id = POPUPTITLE_KEY)
  31. private InputTextField popupTitleField;
  32. @Input(id = "btnEnableDaemon")
  33. private InputButton enableDaemonButton;
  34. @Input(id = "btnRef")
  35. private InputButton refButtonRef;
  36. @Input(id = "textIP")
  37. private InputTextField refTextIP;
  38. @Input(id = "textPort")
  39. private InputTextField refTextPort;
  40. @Label(id = "lblStatus")
  41. private LabelComponent refLblStatus;
  42. @Input(id = "numberPos")
  43. private InputTextField refNumberPos;
  44. @Input(id = "btnPos")
  45. private InputButton refButtonPos;
  46. @Input(id = "btnDisableDaemon")
  47. private InputButton disableDaemonButton;
  48. @Label(id = "lblDaemonStatus")
  49. private LabelComponent daemonStatusLabel;
  50. @Input(id = POPUPTITLE_KEY)
  51. public void onMessageChange(InputEvent event) {
  52. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  53. setPopupTitle(popupTitleField.getText());
  54. }
  55. }
  56. @Input(id = "btnRef")
  57. public void onBtnRef(InputEvent event) {
  58. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  59. try {
  60. getDaemonInterface().client.execute("ref", new String[]{"foo", "bar"});
  61. } catch (XmlRpcException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }
  66. @Input(id = "btnPos")
  67. public void onBtnPos(InputEvent event) {
  68. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  69. try {
  70. String pos=refNumberPos.getText();
  71. getDaemonInterface().client.execute("pos", new String[]{pos});
  72. } catch (XmlRpcException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. }
  77. @Input(id = "btnEnableDaemon")
  78. public void onStartClick(InputEvent event) {
  79. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  80. setDaemonEnabled(true);
  81. // applyDesiredDaemonStatus();
  82. }
  83. }
  84. @Input(id = "btnDisableDaemon")
  85. public void onStopClick(InputEvent event) {
  86. if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
  87. setDaemonEnabled(false);
  88. // applyDesiredDaemonStatus();
  89. }
  90. }
  91. @Override
  92. public void openView() {
  93. enableDaemonButton.setText("Start Daemon");
  94. disableDaemonButton.setText("Stop daemon");
  95. popupTitleField.setText(getPopupTitle());
  96. //UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
  97. uiTimer = new Timer(true);
  98. uiTimer.schedule(new TimerTask() {
  99. @Override
  100. public void run() {
  101. EventQueue.invokeLater(new Runnable() {
  102. @Override
  103. public void run() {
  104. updateUI();
  105. }
  106. });
  107. }
  108. }, 0, 1000);
  109. statusTimer = new Timer(true);
  110. statusTimer.schedule(new TimerTask() {
  111. @Override
  112. public void run() {
  113. EventQueue.invokeLater(new Runnable() {
  114. @Override
  115. public void run() {
  116. try {
  117. Object res = getDaemonInterface().client.execute("status", new String[]{});
  118. refLblStatus.setText("<html>"+res.toString()+"</html>");
  119. System.out.println(res);
  120. } catch (XmlRpcException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. });
  125. }},0,1000);
  126. }
  127. private void updateUI() {
  128. DaemonContribution.State state = getDaemonState();
  129. if (state == DaemonContribution.State.RUNNING) {
  130. enableDaemonButton.setEnabled(false);
  131. disableDaemonButton.setEnabled(true);
  132. } else {
  133. enableDaemonButton.setEnabled(true);
  134. disableDaemonButton.setEnabled(false);
  135. }
  136. String text = "";
  137. switch (state) {
  138. case RUNNING:
  139. text = "My Daemon runs";
  140. break;
  141. case STOPPED:
  142. text = "My Daemon stopped";
  143. break;
  144. case ERROR:
  145. text = "My Daemon failed";
  146. break;
  147. }
  148. daemonStatusLabel.setText(text);
  149. }
  150. @Override
  151. public void closeView() {
  152. if (uiTimer != null) {
  153. uiTimer.cancel();
  154. }
  155. }
  156. @Override
  157. public void generateScript(ScriptWriter writer) {
  158. writer.globalVariable(XMLRPC_VARIABLE, "rpc_factory(\"xmlrpc\", \"http://192.168.20.111:8081/\")");
  159. // Apply the settings to the daemon on program start in the Installation pre-amble
  160. writer.appendLine(XMLRPC_VARIABLE + ".set_title(\"" + getPopupTitle() + "\")");
  161. }
  162. public String getPopupTitle() {
  163. if (!model.isSet(POPUPTITLE_KEY)) {
  164. resetToDefaultValue();
  165. }
  166. return model.get(POPUPTITLE_KEY, DEFAULT_VALUE);
  167. }
  168. private void setPopupTitle(String title) {
  169. if ("".equals(title)) {
  170. resetToDefaultValue();
  171. } else {
  172. model.set(POPUPTITLE_KEY, title);
  173. // Apply the new setting to the daemon for real-time preview purposes
  174. // Note this might influence a running program, since the actual state is stored in the daemon.
  175. setDaemonTitle(title);
  176. }
  177. }
  178. private void resetToDefaultValue() {
  179. popupTitleField.setText(DEFAULT_VALUE);
  180. model.set(POPUPTITLE_KEY, DEFAULT_VALUE);
  181. setDaemonTitle(DEFAULT_VALUE);
  182. }
  183. private void setDaemonTitle(String title) {
  184. try {
  185. daemonInterface.setTitle(title);
  186. } catch(Exception e){
  187. System.err.println("Could not set the title in the daemon process.");
  188. }
  189. }
  190. // private void applyDesiredDaemonStatus() {
  191. // new Thread(new Runnable() {
  192. // @Override
  193. // public void run() {
  194. // if (isDaemonEnabled()) {
  195. // // Download the daemon settings to the daemon process on initial start for real-time preview purposes
  196. // try {
  197. // awaitDaemonRunning(5000);
  198. // daemonInterface.setTitle(getPopupTitle());
  199. // } catch (Exception e) {
  200. // System.err.println("Could not set the title in the daemon process.");
  201. // }
  202. // } else {
  203. // daemonService.getDaemon().stop();
  204. // }
  205. // }
  206. // }).start();
  207. // }
  208. // private void awaitDaemonRunning(long timeOutMilliSeconds) throws InterruptedException {
  209. // daemonService.getDaemon().start();
  210. // long endTime = System.nanoTime() + timeOutMilliSeconds * 1000L * 1000L;
  211. // while(System.nanoTime() < endTime && (daemonService.getDaemon().getState() != DaemonContribution.State.RUNNING || !daemonInterface.isReachable())) {
  212. // Thread.sleep(100);
  213. // }
  214. // }
  215. //
  216. private DaemonContribution.State getDaemonState(){
  217. return DaemonContribution.State.RUNNING;
  218. // return daemonService.getDaemon().getState();
  219. }
  220. private Boolean isDaemonEnabled() {
  221. return model.get(ENABLED_KEY, true); //This daemon is enabled by default
  222. }
  223. private void setDaemonEnabled(Boolean enable) {
  224. model.set(ENABLED_KEY, enable);
  225. }
  226. public String getXMLRPCVariable(){
  227. return XMLRPC_VARIABLE;
  228. }
  229. public MyDaemonInterface getDaemonInterface() {return daemonInterface; }
  230. }