MyDaemonInterface.java 3.3 KB

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