Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. Arduino:
  2. #include <Keypad.h>
  3.  
  4. const byte ROWS = 4;
  5. const byte COLS = 3;
  6.  
  7. char hexaKeys[ROWS][COLS] = {
  8. {'1', '2', '3'},
  9. {'4', '5', '6'},
  10. {'7', '8', '9'},
  11. {'*', '0', '#'}
  12. };
  13.  
  14. byte rowPins[ROWS] = {9, 8, 7, 6};
  15. byte colPins[COLS] = {5, 4, 3};
  16.  
  17. Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  18.  
  19. int led = 13;
  20.  
  21. int pirSensor1 = 10;
  22. int pirSensor2 = 11;
  23. int pirSensor3 = 12;
  24.  
  25. int input1 = 0;
  26. int input2 = 0;
  27. int input3 = 0;
  28.  
  29. int area1DeactivationCode = 123;
  30. int area2DeactivationCode = 456;
  31. int area3DeactivationCode = 789;
  32.  
  33.  
  34. int getCode()
  35. {
  36. int code = 0;
  37. char key = customKeypad.getKey();
  38.  
  39. while(key != '#')
  40. {
  41. switch (key)
  42. {
  43. case NO_KEY:
  44. break;
  45.  
  46. case '0': case '1': case '2': case '3': case '4':
  47. case '5': case '6': case '7': case '8': case '9':
  48. Serial.print("debug: Entered key: ");Serial.println(key);
  49. code = code * 10 + (key - '0');
  50. break;
  51.  
  52. case '*':
  53. code = 0;
  54. break;
  55. }
  56.  
  57. key = customKeypad.getKey();
  58. }
  59.  
  60. Serial.print("debug: Confirmed code: ");Serial.println(code);
  61. return code;
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. void setup(){
  71. pinMode(led, OUTPUT);
  72.  
  73. pinMode(pirSensor1, INPUT);
  74. pinMode(pirSensor2, INPUT);
  75. pinMode(pirSensor3, INPUT);
  76.  
  77. Serial.begin(9600);
  78. }
  79.  
  80. void loop(){
  81.  
  82. input1 = digitalRead(pirSensor1);
  83. input2 = digitalRead(pirSensor2);
  84. input3 = digitalRead(pirSensor3);
  85.  
  86. if(input1 == 1){
  87. digitalWrite(led, HIGH);
  88. Serial.println("Area 1: Alarm activated");
  89.  
  90. int code = 0;
  91.  
  92. do{
  93. code = getCode();
  94. } while(code != area1DeactivationCode);
  95.  
  96. digitalWrite(led, LOW);
  97. Serial.print("Area 1: Alarm deactivated with code: ");Serial.println(code);
  98. }
  99.  
  100. if(input2 == 1){
  101. digitalWrite(led, HIGH);
  102. Serial.println("Area 2: Alarm activated");
  103.  
  104. int code = 0;
  105.  
  106. do{
  107. code = getCode();
  108. } while(code != area2DeactivationCode);
  109.  
  110.  
  111. digitalWrite(led, LOW);
  112. Serial.print("Area 2: Alarm deactivated with code: ");Serial.println(code);
  113. }
  114.  
  115. if(input3 == 1){
  116. digitalWrite(led, HIGH);
  117. Serial.println("Area 3: Alarm activated");
  118.  
  119. int code = 0;
  120.  
  121. do{
  122. code = getCode();
  123. } while(code != area3DeactivationCode);
  124.  
  125. digitalWrite(led, LOW);
  126. Serial.print("Area 3: Alarm deactivated with code: ");Serial.println(code);
  127. }
  128. }
  129.  
  130. Java:
  131.  
  132. package main;
  133.  
  134. import java.time.LocalDateTime;
  135.  
  136. public class Event {
  137.  
  138. private int id;
  139. private String name;
  140. private LocalDateTime createdAt;
  141.  
  142. public Event(){
  143.  
  144. }
  145.  
  146. public int getId() {
  147. return id;
  148. }
  149. public void setId(int id) {
  150. this.id = id;
  151. }
  152.  
  153. public String getName() {
  154. return name;
  155. }
  156.  
  157. public void setName(String name) {
  158. this.name = name;
  159. }
  160.  
  161. public LocalDateTime getCreatedAt() {
  162. return createdAt;
  163. }
  164.  
  165. public void setCreatedAt(LocalDateTime createdAt) {
  166. this.createdAt = createdAt;
  167. }
  168.  
  169. @Override
  170. public String toString() {
  171. return "Event [id=" + id + ", name=" + name + ", createdAt=" + createdAt + "]";
  172. }
  173. }
  174.  
  175. package main;
  176.  
  177. import java.io.BufferedReader;
  178. import java.io.FileInputStream;
  179. import java.io.InputStreamReader;
  180. import java.nio.charset.StandardCharsets;
  181. import java.sql.Connection;
  182. import java.sql.DriverManager;
  183. import java.sql.ResultSet;
  184. import java.sql.SQLException;
  185. import java.sql.Statement;
  186. import java.time.Duration;
  187. import java.time.LocalDateTime;
  188. import java.util.concurrent.TimeUnit;
  189.  
  190. public class Main {
  191.  
  192. public static Connection getMySQLConnection() throws SQLException {
  193. String url = "jdbc:mysql://localhost:3306/alarm";
  194. String username = "root";
  195. String password = "";
  196.  
  197. return DriverManager.getConnection(url, username, password);
  198. }
  199.  
  200. public static void main(String[] args) {
  201.  
  202. new Thread()
  203. {
  204. public void run() {
  205. while(true){
  206. try {
  207. Thread.sleep(5000);
  208.  
  209. Connection connection = getMySQLConnection();
  210. Statement statement = connection.createStatement();
  211. ResultSet resultSet;
  212. Event event = null;
  213.  
  214. resultSet = statement.executeQuery("SELECT * FROM events ORDER BY created_at DESC LIMIT 1");
  215.  
  216. while(resultSet.next()) {
  217. event = new Event();
  218.  
  219. event.setId(resultSet.getInt("id"));
  220. event.setName(resultSet.getString("name"));
  221. event.setCreatedAt(resultSet.getTimestamp("created_at").toLocalDateTime());
  222. }
  223.  
  224.  
  225. if(event != null && event.getName().contains("Alarm activated") &&
  226. TimeUnit.MILLISECONDS.toSeconds(Duration.between(event.getCreatedAt(), LocalDateTime.now()).toMillis()) > 20){
  227. String area = event.getName().substring(0, 6);
  228.  
  229. statement.executeUpdate("INSERT INTO events (id, name) VALUES (0, 'SOT team dispatched to " + area + "')");
  230.  
  231. }
  232.  
  233. connection.close();
  234. } catch (Exception e) {
  235. e.printStackTrace();
  236. }
  237. }
  238. }
  239. }.start();
  240.  
  241. try (BufferedReader br = new BufferedReader(new InputStreamReader(
  242. new FileInputStream("C:\\Users\\Dobi\\Desktop\\output.txt"), StandardCharsets.UTF_8))) {
  243.  
  244. String line;
  245.  
  246. while (true) {
  247. Thread.sleep(500);
  248.  
  249. line = br.readLine();
  250.  
  251. if (line != null && !line.startsWith("debug")) {
  252.  
  253. Connection connection = getMySQLConnection();
  254. Statement statement = connection.createStatement();
  255.  
  256. statement.executeUpdate("INSERT INTO events (id, name) VALUES (0, '" + line + "')");
  257.  
  258. connection.close();
  259. }
  260. }
  261. } catch (Exception e) {
  262. e.printStackTrace();
  263. }
  264.  
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement