Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6.  
  7. public class Server {
  8.  
  9. public static void main(String[] args) throws IOException {
  10.  
  11. Gpio pins = new Gpio();
  12.  
  13. ServerSocket listener = new ServerSocket(9090);
  14. System.out.println("Starting server");
  15.  
  16. while(true) {
  17. Socket socket = listener.accept();
  18.  
  19. InputStreamReader reader = new InputStreamReader(socket.getInputStream());
  20. BufferedReader input = new BufferedReader(reader);
  21.  
  22. String signal = input.readLine();
  23.  
  24.  
  25. if (signal.equals("TOGGLE")) {
  26. String string = input.readLine();
  27. pins.toggle(string);
  28. } else if (signal.equals("TIMER")) {
  29. pins.setTimer(input);
  30. } else if (signal.equals("REMOVE")) {
  31. pins.removeTimer(input);
  32. }
  33.  
  34. socket.close();
  35. }
  36. }
  37. }
  38.  
  39. import java.io.BufferedReader;
  40. import java.io.IOException;
  41. import java.text.ParseException;
  42. import java.text.SimpleDateFormat;
  43. import java.util.ArrayList;
  44. import java.util.Date;
  45.  
  46. import com.pi4j.io.gpio.GpioController;
  47. import com.pi4j.io.gpio.GpioFactory;
  48. import com.pi4j.io.gpio.GpioPinDigitalOutput;
  49. import com.pi4j.io.gpio.RaspiPin;
  50.  
  51.  
  52. public class Gpio {
  53. private ArrayList<Timer> timers = new ArrayList<Timer>();
  54. private GpioController gpio = GpioFactory.getInstance();
  55. private GpioPinDigitalOutput unit1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01);
  56. private GpioPinDigitalOutput unit2 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02);
  57. private GpioPinDigitalOutput unit3 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_03);
  58. private GpioPinDigitalOutput unit4 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
  59. private SimpleDateFormat format = new SimpleDateFormat("HH:mm");
  60.  
  61. public Gpio () {
  62. Thread thread = new Thread(){
  63. public void run(){
  64. while(true) {
  65.  
  66. try {
  67. String currentTime = format.format(new Date());
  68. Date now = format.parse(currentTime);
  69. for (Timer i : timers) {
  70. long difference = now.getTime() - i.getTime().getTime();
  71. if(difference >= 0) {
  72. toggle(i.getString());
  73. timers.remove(i);
  74. }
  75. }
  76.  
  77. } catch (ParseException e) {
  78. e.printStackTrace();
  79. }
  80. try {
  81. Thread.sleep(60000);
  82. } catch (InterruptedException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. }
  87. };
  88. thread.start();
  89. }
  90.  
  91. public void toggle (String signal) {
  92. switch (signal) {
  93. case "ON unit 1":
  94. unit1.high();
  95. break;
  96. case "ON unit 2":
  97. unit2.high();
  98. break;
  99. case "ON unit 3":
  100. unit3.high();
  101. break;
  102. case "ON unit 4":
  103. unit4.high();
  104. break;
  105. case "OFF unit 1":
  106. unit1.low();
  107. break;
  108. case "OFF unit 2":
  109. unit2.low();
  110. break;
  111. case "OFF unit 3":
  112. unit3.low();
  113. break;
  114. case "OFF unit 4":
  115. unit4.low();
  116. break;
  117. }
  118. }
  119.  
  120. public void setTimer(BufferedReader input) {
  121. try {
  122. String time = input.readLine();
  123. String action = input.readLine();
  124.  
  125. Date time2 = format.parse(time);
  126. Timer timer = new Timer(time2, action);
  127.  
  128. timers.add(timer);
  129.  
  130. } catch (IOException | ParseException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134.  
  135. public void removeTimer(BufferedReader input) {
  136.  
  137. try {
  138. String time = input.readLine();
  139. String action = input.readLine();
  140. Date time2 = format.parse(time);
  141.  
  142. for (Timer i : timers) {
  143. if(i.getString().equals(action) || i.getTime().equals(time2)) {
  144. timers.remove(i);
  145. break;
  146. }
  147. }
  148.  
  149. } catch (ParseException | IOException e) {
  150. e.printStackTrace();
  151. }
  152. }
  153. }
  154.  
  155. import java.util.Date;
  156.  
  157.  
  158. public class Timer {
  159. private Date time;
  160. private String string;
  161.  
  162. public Timer (Date time, String string) {
  163. this.time = time;
  164. this.string = string;
  165. }
  166.  
  167. public Date getTime() {
  168. return time;
  169. }
  170.  
  171. public String getString() {
  172. return string;
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement