ScReadThread.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package at.acdp.urweb.sclient.data;
  2. import at.acdp.urweb.RobotCommand;
  3. import at.acdp.urweb.URBot;
  4. import at.acdp.urweb.sclient.SecondaryClient;
  5. import at.acdp.urweb.sclient.URLog;
  6. import okhttp3.*;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import java.io.IOException;
  10. public class ScReadThread implements Runnable {
  11. private static final Logger log = LoggerFactory.getLogger(ScReadThread.class);
  12. private final String ip;
  13. private final URBot urBot;
  14. SecondaryClient rde;
  15. public ScReadThread(String ip, URBot urBot) {
  16. this.ip = ip;
  17. this.urBot=urBot;
  18. }
  19. public SecondaryClient getRde() {
  20. return rde;
  21. }
  22. @Override
  23. public void run() {
  24. while (true) {
  25. try {
  26. read();
  27. } catch (Exception e) {
  28. log.warn("read thread terminated", e);
  29. URLog.add(new Message("read thread terminated", e));
  30. }
  31. }
  32. }
  33. private void read() throws IOException {
  34. rde = new SecondaryClient(ip, 30001, urBot);
  35. VersionMessage vm = rde.connect();
  36. while (true) {
  37. rde.readPackage();
  38. }
  39. }
  40. public void writeCmd(RobotCommand r) {
  41. writeCmd(r.cmd);
  42. }
  43. private void writeCmd(String s) {
  44. if (!s.endsWith("\n"))
  45. s += "\n";
  46. log.debug("writecmd: \"" + s + "\"");
  47. rde.writeCmd(s);
  48. }
  49. }