MyDaemonInterface.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package uraxis;
  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import java.util.concurrent.CompletableFuture;
  6. import org.apache.xmlrpc.XmlRpcException;
  7. import org.apache.xmlrpc.XmlRpcRequest;
  8. import org.apache.xmlrpc.client.AsyncCallback;
  9. import org.apache.xmlrpc.client.XmlRpcClient;
  10. import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
  11. public class MyDaemonInterface {
  12. XmlRpcClient client;
  13. public MyDaemonInterface(String host, int port) {
  14. XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  15. config.setEnabledForExtensions(true);
  16. try {
  17. config.setServerURL(new URL("http://" + host + ":" + port + "/RPC2"));
  18. } catch (MalformedURLException e) {
  19. e.printStackTrace();
  20. }
  21. config.setConnectionTimeout(1000); //1s
  22. client = new XmlRpcClient();
  23. client.setConfig(config);
  24. }
  25. public boolean isReachable() {
  26. try {
  27. client.execute("get_title", new ArrayList<String>());
  28. return true;
  29. } catch (XmlRpcException e) {
  30. return false;
  31. }
  32. }
  33. public void setTitle(String title) {
  34. }
  35. public String getTitle() {
  36. return "";
  37. }
  38. public String getMessage(String name) {
  39. return "";
  40. }
  41. public CompletableFuture<Integer> getpos() {
  42. CompletableFuture<Integer> cf = new CompletableFuture();
  43. try {
  44. client.executeAsync("getpos", new Object[]{}, new AsyncCallback() {
  45. @Override
  46. public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
  47. cf.complete((Integer) o);
  48. }
  49. @Override
  50. public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
  51. cf.completeExceptionally(throwable);
  52. }
  53. });
  54. } catch (XmlRpcException e) {
  55. cf.completeExceptionally(e);
  56. }
  57. return cf;
  58. }
  59. public CompletableFuture<String> getstatus() {
  60. CompletableFuture<String> cf = new CompletableFuture();
  61. try {
  62. client.executeAsync("status", new Object[]{}, new AsyncCallback() {
  63. @Override
  64. public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
  65. cf.complete((String) o);
  66. }
  67. @Override
  68. public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
  69. cf.completeExceptionally(throwable);
  70. }
  71. });
  72. } catch (XmlRpcException e) {
  73. cf.completeExceptionally(e);
  74. }
  75. return cf;
  76. }
  77. public CompletableFuture<Boolean> rel(int pos, int speed) {
  78. CompletableFuture<Boolean> cf = new CompletableFuture();
  79. try {
  80. client.executeAsync("rel", new Object[]{pos, speed}, new AsyncCallback() {
  81. @Override
  82. public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
  83. cf.complete((Boolean) o);
  84. }
  85. @Override
  86. public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
  87. cf.completeExceptionally(throwable);
  88. }
  89. });
  90. } catch (XmlRpcException e) {
  91. cf.completeExceptionally(e);
  92. }
  93. return cf;
  94. }
  95. public CompletableFuture<Boolean> abs(int pos, int speed) {
  96. CompletableFuture<Boolean> cf = new CompletableFuture();
  97. try {
  98. client.executeAsync("abs", new Object[]{pos, speed}, new AsyncCallback() {
  99. @Override
  100. public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
  101. cf.complete((Boolean) o);
  102. }
  103. @Override
  104. public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
  105. cf.completeExceptionally(throwable);
  106. }
  107. });
  108. } catch (XmlRpcException e) {
  109. cf.completeExceptionally(e);
  110. }
  111. return cf;
  112. }
  113. }