GRID.java 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package eu.tankernn.grid.model;
  2. import java.util.Arrays;
  3. import java.util.stream.IntStream;
  4. import java.util.stream.Stream;
  5. import eu.tankernn.grid.Fan;
  6. /**
  7. *
  8. * This is a model for the GRID controller. This class uses the communicator
  9. * class to communicate to the GRID+ controller. To update their values they
  10. * each have poll functions instead of setters which send a command to the GRID
  11. * and read the response.
  12. *
  13. * This class also has a boolean check that checks if the voltage command is the
  14. * same as the previous voltage command, this is to prevent pointless serial
  15. * communication.
  16. *
  17. * @author Roel
  18. */
  19. public class GRID {
  20. private Communicator communicator;
  21. private Fan[] fans;
  22. /**
  23. * This constructor initiates all members afterwards it opens a communicator
  24. * at the selected port
  25. */
  26. public GRID() {
  27. fans = IntStream.range(0, 5).mapToObj(i -> new Fan(this, i)).toArray(Fan[]::new);
  28. communicator = new Communicator();
  29. communicator.searchForPorts();
  30. }
  31. /**
  32. * This method simply runs the disconnect method of the communicator.
  33. */
  34. public void disconnect() {
  35. getCommunicator().disconnect();
  36. }
  37. public Fan getFan(int index) {
  38. return fans[index];
  39. }
  40. public Communicator getCommunicator() {
  41. return communicator;
  42. }
  43. public double getTotalWattage() {
  44. return Arrays.stream(fans).mapToDouble(Fan::getWattage).sum();
  45. }
  46. public void pollFans() {
  47. fanStream().forEach(Fan::poll);
  48. }
  49. public void updateFanSpeeds(double temp) {
  50. fanStream().forEach(f -> f.update(temp));
  51. }
  52. public Stream<Fan> fanStream() {
  53. return Arrays.stream(fans);
  54. }
  55. }