123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- package uraxis;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Map;
- import java.util.concurrent.CompletableFuture;
- import org.apache.xmlrpc.XmlRpcException;
- import org.apache.xmlrpc.XmlRpcRequest;
- import org.apache.xmlrpc.client.AsyncCallback;
- import org.apache.xmlrpc.client.XmlRpcClient;
- import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
- public class MyDaemonInterface {
- XmlRpcClient client;
- public MyDaemonInterface(String host, int port) {
- XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
- config.setEnabledForExtensions(true);
- try {
- config.setServerURL(new URL("http://" + host + ":" + port + "/RPC2"));
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- config.setConnectionTimeout(1000); //1s
- client = new XmlRpcClient();
- client.setConfig(config);
- }
- public boolean isReachable() {
- try {
- client.execute("get_title", new ArrayList<String>());
- return true;
- } catch (XmlRpcException e) {
- return false;
- }
- }
- public void setTitle(String title) {
- }
- public String getTitle() {
- return "";
- }
- public String getMessage(String name) {
- return "";
- }
- public CompletableFuture<Integer> getpos() {
- CompletableFuture<Integer> cf = new CompletableFuture();
- try {
- client.executeAsync("getpos", new Object[]{}, new AsyncCallback() {
- @Override
- public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
- cf.complete((Integer) o);
- }
- @Override
- public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
- cf.completeExceptionally(throwable);
- }
- });
- } catch (XmlRpcException e) {
- cf.completeExceptionally(e);
- }
- return cf;
- }
- public CompletableFuture<Map<String, Object>> getstatus() {
- CompletableFuture<Map<String, Object>> cf = new CompletableFuture();
- try {
- client.executeAsync("status", new Object[]{}, new AsyncCallback() {
- @Override
- public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
- cf.complete((Map<String, Object>) o);
- }
- @Override
- public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
- cf.completeExceptionally(throwable);
- }
- });
- } catch (XmlRpcException e) {
- cf.completeExceptionally(e);
- }
- return cf;
- }
- public CompletableFuture<String> rel(int pos, int speed) {
- CompletableFuture<String> cf = new CompletableFuture();
- try {
- client.executeAsync("rel", new Object[]{pos, speed}, new AsyncCallback() {
- @Override
- public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
- cf.complete((String) o);
- }
- @Override
- public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
- cf.completeExceptionally(throwable);
- }
- });
- } catch (XmlRpcException e) {
- cf.completeExceptionally(e);
- }
- return cf;
- }
- public CompletableFuture<String> abs(int pos, int speed) {
- CompletableFuture<String> cf = new CompletableFuture();
- try {
- client.executeAsync("abs", new Object[]{pos, speed}, new AsyncCallback() {
- @Override
- public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
- cf.complete((String) o);
- }
- @Override
- public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
- cf.completeExceptionally(throwable);
- }
- });
- } catch (XmlRpcException e) {
- cf.completeExceptionally(e);
- }
- return cf;
- }
- }
|