MyDaemonInterface.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package uraxis;
  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import org.apache.xmlrpc.XmlRpcException;
  6. import org.apache.xmlrpc.client.XmlRpcClient;
  7. import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
  8. public class MyDaemonInterface {
  9. XmlRpcClient client;
  10. public MyDaemonInterface(String host, int port) {
  11. XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  12. config.setEnabledForExtensions(true);
  13. try {
  14. config.setServerURL(new URL("http://" + host + ":" + port + "/RPC2"));
  15. } catch (MalformedURLException e) {
  16. e.printStackTrace();
  17. }
  18. config.setConnectionTimeout(1000); //1s
  19. client = new XmlRpcClient();
  20. client.setConfig(config);
  21. }
  22. public boolean isReachable() {
  23. try {
  24. client.execute("get_title", new ArrayList<String>());
  25. return true;
  26. } catch (XmlRpcException e) {
  27. return false;
  28. }
  29. }
  30. public void setTitle(String title) {
  31. }
  32. public String getTitle() {
  33. return "";
  34. }
  35. public String getMessage(String name) {
  36. return "";
  37. }
  38. public int getpos() {
  39. try {
  40. Object res= client.execute("getpos", new Object[]{});
  41. return (int) res;
  42. } catch (XmlRpcException e) {
  43. e.printStackTrace();
  44. return -1;
  45. }
  46. }
  47. public String getstatus() {
  48. try {
  49. String res= (String) client.execute("status", new Object[]{});
  50. return res;
  51. } catch (XmlRpcException e) {
  52. e.printStackTrace();
  53. return "";
  54. }
  55. }
  56. public boolean rel(int pos, int speed) {
  57. try {
  58. boolean success= (boolean) client.execute("rel", new Object[]{String.valueOf(pos), String.valueOf(speed)});
  59. return success;
  60. } catch (XmlRpcException e) {
  61. e.printStackTrace();
  62. return false;
  63. }
  64. }
  65. public boolean abs(int pos, int speed) {
  66. try {
  67. boolean success= (boolean) client.execute("abs", new Object[]{String.valueOf(pos), String.valueOf(speed)});
  68. return success;
  69. } catch (XmlRpcException e) {
  70. e.printStackTrace();
  71. return false;
  72. }
  73. }
  74. }