IOHandler.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package uraxis.toolbar;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import com.ur.urcap.api.domain.io.*;
  7. public class IOHandler {
  8. private final IOModel ioModel;
  9. public IOHandler(IOModel ioModel) {
  10. this.ioModel = ioModel;
  11. }
  12. /*Returns a DigitalIO object found by its default name
  13. * Default names are:
  14. * digital_in[0]
  15. * digital_in[1]
  16. * ...
  17. * digital_in[7]
  18. * digital_out[0]
  19. * digital_out[1]
  20. * ...
  21. * digital_out[7]
  22. * tool_in[0]
  23. * tool_in[1]
  24. * tool_out[0]
  25. * tool_out[1]
  26. * config_in[0]
  27. * config_in[1]
  28. * ...
  29. * config_in[7]
  30. * config_out[0]
  31. * config_out[1]
  32. * ...
  33. * config_out[7]
  34. *
  35. */
  36. public DigitalIO getDigitalIO(String defaultName){
  37. Collection<DigitalIO> IOcollection = ioModel.getIOs(DigitalIO.class);
  38. int IO_count = IOcollection.size();
  39. if(IO_count > 0){
  40. Iterator<DigitalIO> IO_itr = IOcollection.iterator();
  41. while(IO_itr.hasNext()){
  42. DigitalIO thisIO = IO_itr.next();
  43. String thisDefaultName = thisIO.getDefaultName();
  44. // System.out.println("Found an IO named "+thisDefaultName);
  45. if(thisDefaultName.equals(defaultName)){
  46. return thisIO;
  47. }
  48. }
  49. }
  50. return null;
  51. }
  52. public Register getRegisterIO(String defaultName){
  53. Collection<Register> IOcollection = ioModel.getIOs(Register.class);
  54. int IO_count = IOcollection.size();
  55. if(IO_count > 0){
  56. Iterator<Register> IO_itr = IOcollection.iterator();
  57. while(IO_itr.hasNext()){
  58. Register thisIO = IO_itr.next();
  59. String thisDefaultName = thisIO.getDefaultName();
  60. if(thisDefaultName.equals(defaultName)){
  61. return thisIO;
  62. }
  63. }
  64. }
  65. return null;
  66. }
  67. public Register getRegisterIOCustom(String customName){
  68. Collection<Register> IOcollection = ioModel.getIOs(Register.class);
  69. int IO_count = IOcollection.size();
  70. if(IO_count > 0){
  71. Iterator<Register> IO_itr = IOcollection.iterator();
  72. while(IO_itr.hasNext()){
  73. Register thisIO = IO_itr.next();
  74. String thisCustomName = thisIO.getName();
  75. if(thisCustomName.equals(customName)){
  76. return thisIO;
  77. }
  78. }
  79. }
  80. return null;
  81. }
  82. private final static Pattern ioPattern = Pattern.compile("\\[(.*?)\\]");
  83. public static int getIOIndex(IO io) {
  84. Matcher m = ioPattern.matcher(io.getDefaultName());
  85. if(m.find()) {
  86. return Integer.parseInt(m.group(1));
  87. }
  88. return -1;
  89. }
  90. /*Returns an AnalogIO object found by its default name
  91. * Default names are:
  92. * analog_in[0]
  93. * analog_in[1]
  94. * analog_in[2] (Tool analog in 0)
  95. * analog_in[3] (Tool analog in 1)
  96. * analog_out[0]
  97. * analog_out[1]
  98. *
  99. */
  100. public AnalogIO getAnalogIO(String defaultName){
  101. Collection<AnalogIO> IOcollection = ioModel.getIOs(AnalogIO.class);
  102. int IO_count = IOcollection.size();
  103. if(IO_count > 0){
  104. Iterator<AnalogIO> IO_itr = IOcollection.iterator();
  105. while(IO_itr.hasNext()){
  106. AnalogIO thisIO = IO_itr.next();
  107. String thisDefaultName = thisIO.getDefaultName();
  108. // System.out.println("Found an IO named "+thisDefaultName);
  109. if(thisDefaultName.equals(defaultName)){
  110. return thisIO;
  111. }
  112. }
  113. }
  114. return null;
  115. }
  116. }