Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.18 KB | None | 0 0
  1. /*
  2. * Copyright 2016 CodeHigh
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * Copyright (C) 2016 CodeHigh.
  17. * Permission is granted to copy, distribute and/or modify this document
  18. * under the terms of the GNU Free Documentation License, Version 1.3
  19. * or any later version published by the Free Software Foundation;
  20. * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
  21. * A copy of the license is included in the section entitled "GNU
  22. * Free Documentation License".
  23. */
  24.  
  25. package smartMirror.controllers.interfaceControllers.mainController;
  26.  
  27. import javafx.animation.FadeTransition;
  28. import javafx.application.Platform;
  29. import javafx.fxml.FXMLLoader;
  30. import javafx.scene.Parent;
  31. import javafx.scene.input.KeyEvent;
  32. import javafx.scene.layout.AnchorPane;
  33. import javafx.scene.layout.BorderPane;
  34. import javafx.scene.layout.GridPane;
  35. import javafx.scene.layout.StackPane;
  36. import javafx.util.Duration;
  37. import org.eclipse.paho.client.mqttv3.MqttMessage;
  38. import smartMirror.controllers.communications.CommunicationManager;
  39. import smartMirror.controllers.dataHandlers.dataHandlersCommons.JsonMessageParser;
  40. import smartMirror.controllers.dataHandlers.widgetsDataHandlers.timeDate.TimeDateManager;
  41. import smartMirror.controllers.interfaceControllers.widgetsControllers.busTimetableController.BusTimetableController;
  42. import smartMirror.controllers.interfaceControllers.widgetsControllers.devicesController.DeviceController;
  43. import smartMirror.controllers.interfaceControllers.widgetsControllers.feedController.FeedController;
  44. import smartMirror.controllers.interfaceControllers.widgetsControllers.greetingsController.GreetingsController;
  45. import smartMirror.controllers.interfaceControllers.widgetsControllers.postItsController.PostItViewController;
  46. import smartMirror.controllers.interfaceControllers.widgetsControllers.qrCodeController.QRCodeController;
  47. import smartMirror.controllers.interfaceControllers.widgetsControllers.shoppingListController.ShoppingListViewController;
  48. import smartMirror.controllers.interfaceControllers.widgetsControllers.timeDateController.TimeDateController;
  49. import smartMirror.controllers.interfaceControllers.widgetsControllers.weatherController.WeatherController;
  50. import smartMirror.dataModels.applicationModels.UUID_Generator;
  51. import smartMirror.dataModels.widgetsModels.qrCodeModels.QRCode;
  52. import smartMirror.dataModels.widgetsModels.weatherModels.Weather;
  53.  
  54.  
  55. import java.awt.*;
  56. import java.io.IOException;
  57. import java.util.Observable;
  58. import java.util.Observer;
  59.  
  60. /**
  61. * @author Codehigh @copyright on 06/12/2016.
  62. * Class responsible for loading each application component and starting the CommunicationManager
  63. */
  64. public class MainController extends Observable implements Observer
  65. {
  66. public BorderPane pairingPane;
  67. public GridPane pairingDateTimeContainer;
  68. public GridPane qrPairingContainer;
  69.  
  70. public AnchorPane mainPane;
  71. public GridPane widget1;
  72. public GridPane widget7;
  73. public GridPane widget6;
  74. public GridPane widget3;
  75. public GridPane gridMainQR;
  76. public GridPane widget2;
  77. public GridPane widget5;
  78. public GridPane widget4;
  79.  
  80. public StackPane stackPaneWidget1;
  81. public StackPane stackPaneWidget7;
  82. public StackPane stackPaneWidget6;
  83. public StackPane stackPaneWidget3;
  84. public StackPane stackPaneWidgetQR;
  85. public StackPane stackPaneWidget2;
  86. public StackPane stackPaneWidget5;
  87. public StackPane stackPaneWidget4;
  88.  
  89. private boolean systemRunning;
  90. private boolean paired;
  91. private UUID_Generator uuid;
  92.  
  93. private TimeDateManager timeDateManager;
  94. private CommunicationManager communicationManager;
  95.  
  96. /**
  97. * Constructor which generates a UUID (client ID), starts the CommunicationManger with this UUID and waits for
  98. * updates from the CommunicationManager
  99. */
  100. public MainController()
  101. {
  102. this.systemRunning = true;
  103. this.paired = false;
  104. keepScreenAlive();
  105. this.uuid = new UUID_Generator();
  106. this.communicationManager = new CommunicationManager(this.uuid.getUUID());
  107. this.communicationManager.addObserver(this);
  108. this.addObserver(this.communicationManager);
  109. startUp();
  110. }
  111.  
  112. /**
  113. * This method makes the call to load every component into the Interface
  114. */
  115. private void startUp()
  116. {
  117. Platform.runLater(() ->
  118. {
  119. setUpDateTimeView();
  120. setUpQRCodeView();
  121. setUpWeatherView();
  122. setUpDevicesView();
  123. setUpFeedView();
  124. setUpBusTimetableView();
  125. setUpPostItView();
  126. setUpGreetingsView();
  127. setUpShoppingListView();
  128.  
  129. this.pairingPane.setOpacity(1);
  130.  
  131. setChanged();
  132. notifyObservers(this);
  133. });
  134. }
  135.  
  136. /**
  137. * This method loads the DevicesView in the interface and adds its controller as observer to the CommunicationManager
  138. *
  139. * @see CommunicationManager
  140. * @see DeviceController
  141. */
  142. private void setUpDevicesView()
  143. {
  144. DeviceController deviceController = loadViewMainScreen(this.stackPaneWidget7, "/smartMirror/Views/widgetsViews/deviceStatusWidget/DeviceView.fxml").getController();
  145. this.communicationManager.addObserver(deviceController);
  146. }
  147.  
  148. /**
  149. * This method loads the ShoppingListView in the interface and adds its controller as observer to the CommunicationManager
  150. *
  151. * @see CommunicationManager
  152. * @see ShoppingListViewController
  153. */
  154. private void setUpShoppingListView()
  155. {
  156. ShoppingListViewController shoppingListController = loadViewMainScreen(this.stackPaneWidget3, "/smartMirror/Views/widgetsViews/shoppingListWidget/ShoppingListView.fxml").getController();
  157. this.communicationManager.addObserver(shoppingListController);
  158. }
  159.  
  160. /**
  161. * This method loads the PostItView in the interface and adds its controller as observer to the CommunicationManager
  162. *
  163. * @see CommunicationManager
  164. * @see PostItViewController
  165. */
  166. private void setUpPostItView()
  167. {
  168. PostItViewController postItViewController = loadViewMainScreen(this.stackPaneWidget6, "/smartMirror/Views/widgetsViews/postItWidget/PostitView.fxml").getController();
  169. this.communicationManager.addObserver(postItViewController);
  170.  
  171. }
  172.  
  173. /**
  174. * This method loads the FeedView in the interface and adds its controller as observer to the CommunicationManager
  175. *
  176. * @see CommunicationManager
  177. * @see FeedController
  178. */
  179. private void setUpFeedView()
  180. {
  181. FeedController feedController = loadViewMainScreen(this.stackPaneWidget2, "/smartMirror/Views/widgetsViews/feedsWidget/FeedsViews.fxml").getController();
  182. this.communicationManager.addObserver(feedController);
  183.  
  184. }
  185.  
  186. /**
  187. * This method loads the WeatherView in the interface and adds its controller as observer to the CommunicationManager
  188. *
  189. * @see CommunicationManager
  190. * @see WeatherController
  191. */
  192. private void setUpWeatherView()
  193. {
  194. WeatherController temperatureController = loadViewMainScreen(this.stackPaneWidget4, "/smartMirror/Views/widgetsViews/weatherWidget/WeatherView.fxml").getController();
  195. this.communicationManager.addObserver(temperatureController);
  196. temperatureController.addObserver(this);
  197. }
  198.  
  199. /**
  200. * This method loads the BusTimetableView in the interface and adds its controller as observer to the CommunicationManager
  201. *
  202. * @see CommunicationManager
  203. * @see BusTimetableController
  204. */
  205. private void setUpBusTimetableView()
  206. {
  207. BusTimetableController busTimetableController = loadViewMainScreen(this.stackPaneWidget3, "/smartMirror/Views/widgetsViews/busTimetableWidget/BusTimetable.fxml").getController();
  208. this.communicationManager.addObserver(busTimetableController);
  209. }
  210.  
  211. /**
  212. * This method loads the QRCodeView in the pairing interface and main interface then adds its controller as observer to the QRCode
  213. *
  214. * @see QRCode
  215. * @see QRCodeController
  216. */
  217. private void setUpQRCodeView()
  218. {
  219. QRCode qrCode = new QRCode(this.uuid.getUUID());
  220.  
  221. qrCode.addObserver(loadViewPairingScreen(this.qrPairingContainer, "/smartMirror/Views/widgetsViews/qrCode/QRCodeView.fxml",
  222. 1, 0).getController());
  223. setComponentVisible(this.qrPairingContainer);
  224. qrCode.addObserver(loadViewMainScreen(this.stackPaneWidgetQR, "/smartMirror/Views/widgetsViews/qrCode/QRCodeView.fxml").getController());
  225. setComponentVisible(this.gridMainQR);
  226. qrCode.getQRCode();
  227.  
  228.  
  229. }
  230.  
  231. /**
  232. * This method loads the DateTimeView in the pairing interface and main interface then adds its controller as observer to the TimeDateManager
  233. *
  234. * @see TimeDateManager
  235. * @see TimeDateController
  236. */
  237. private void setUpDateTimeView()
  238. {
  239. this.timeDateManager = new TimeDateManager();
  240. this.timeDateManager.bindToTime();
  241. this.timeDateManager.bindToDate();
  242. this.timeDateManager.bindToDay();
  243. this.timeDateManager.bindGreetings();
  244.  
  245. this.timeDateManager.addObserver(loadViewPairingScreen(this.pairingDateTimeContainer, "/smartMirror/Views/widgetsViews/timeDateWidget/TimeDate.fxml",
  246. 0, 0).getController());
  247. setComponentVisible(this.pairingDateTimeContainer);
  248. TimeDateController timeDateController = loadViewMainScreen(this.stackPaneWidget1, "/smartMirror/Views/widgetsViews/timeDateWidget/TimeDate.fxml").getController();
  249. this.timeDateManager.addObserver(timeDateController);
  250. this.communicationManager.addObserver(timeDateController);
  251. }
  252.  
  253. /**
  254. * This method loads the GreetingsView in the interface then adds its controller as observer to the TimeDateManager
  255. *
  256. * @see TimeDateManager
  257. * @see GreetingsController
  258. */
  259. private void setUpGreetingsView()
  260. {
  261. GreetingsController greetingsController = loadViewMainScreen(stackPaneWidget5, "/smartMirror/Views/widgetsViews/greetingsWidget/GreetingsView.fxml").getController();
  262. timeDateManager.addObserver(greetingsController);
  263. this.communicationManager.addObserver(greetingsController);
  264. this.addObserver(greetingsController);
  265. }
  266.  
  267. /**
  268. * This method makes the transition between the paring interface and the main interface
  269. */
  270. private void changeScene()
  271. {
  272. FadeTransition fadeOut = new FadeTransition(Duration.seconds(1), this.pairingPane);
  273.  
  274. fadeOut.setFromValue(1);
  275. fadeOut.setToValue(0);
  276.  
  277. FadeTransition fadeIn = new FadeTransition(Duration.seconds(2), this.mainPane);
  278.  
  279. fadeIn.setFromValue(0);
  280. fadeIn.setToValue(1);
  281.  
  282. fadeOut.setOnFinished(event -> fadeIn.play());
  283. fadeOut.play();
  284.  
  285. }
  286.  
  287. /**
  288. * This method sets a component to visible by changing its opacity value from 0 to 1. This process is made as an animation
  289. * to give a nice effect in the interface
  290. *
  291. * @param gridPane component to be set to visible
  292. */
  293. private synchronized void setComponentVisible(GridPane gridPane)
  294. {
  295. Platform.runLater(() ->
  296. {
  297. gridPane.setVisible(true);
  298. FadeTransition fadeIn = new FadeTransition(Duration.seconds(2), gridPane);
  299.  
  300. fadeIn.setFromValue(0);
  301. fadeIn.setToValue(1);
  302. fadeIn.setOnFinished(event ->
  303. {
  304. if (gridPane.getOpacity() != 1) fadeIn.playFromStart();
  305. });
  306. fadeIn.play();
  307. });
  308. }
  309.  
  310. /**
  311. * This method is responsible to keep the screen alive. The screen saver from the running machine can switch off the display in
  312. * case of inactivate, with this method the pointer will be moved every 80000 milliseconds to avoid screen blackout
  313. */
  314. private void keepScreenAlive()
  315. {
  316. Thread thread = new Thread(() ->
  317. {
  318. try
  319. {
  320. while (systemRunning)
  321. {
  322.  
  323. Thread.sleep(80000);//this is how long before it moves
  324. Point point = MouseInfo.getPointerInfo().getLocation();
  325. Robot robot = new Robot();
  326. robot.mouseMove(point.x, point.y);
  327. }
  328. }
  329. catch (InterruptedException | AWTException e)
  330. {
  331. e.printStackTrace();
  332. }
  333. });
  334. thread.start();
  335. }
  336.  
  337. /**
  338. * Method responsible for loading components in the pairing interface. It loads the FXML file and set in the specified
  339. * GridPane in the specified position
  340. *
  341. * @param parent parent component
  342. * @param resource FXML path resource
  343. * @param c column number
  344. * @param r row number
  345. * @return FXMLLoader
  346. */
  347. private FXMLLoader loadViewPairingScreen(GridPane parent, String resource, int c, int r)
  348. {
  349. FXMLLoader myLoader = null;
  350. try
  351. {
  352. myLoader = new FXMLLoader(getClass().getResource(resource));
  353. Parent loadScreen = myLoader.load();
  354. parent.add(loadScreen, c, r);
  355. }
  356. catch (IOException e)
  357. {
  358. System.err.println(e.getMessage());
  359. }
  360. return myLoader;
  361. }
  362.  
  363.  
  364. /**
  365. * Method responsible for loading components in the main interface. It loads the FXML file in the specified StackPane
  366. *
  367. * @param parent parent component
  368. * @param resource FXML path resource
  369. * @return FXMLLoader
  370. */
  371. private FXMLLoader loadViewMainScreen(StackPane parent, String resource)
  372. {
  373. FXMLLoader myLoader = null;
  374. try
  375. {
  376. myLoader = new FXMLLoader(getClass().getResource(resource));
  377. Parent loadScreen = myLoader.load();
  378. parent.getChildren().add(loadScreen);
  379. }
  380. catch (IOException e)
  381. {
  382. System.err.println(e.getMessage());
  383. }
  384. return myLoader;
  385. }
  386.  
  387. /**
  388. * Method responsible to force the application to exit
  389. *
  390. * @param event key listener event that listen to the key X
  391. */
  392. public void forceShutdown(KeyEvent event)
  393. {
  394. System.out.println(event.getCode());
  395. switch (event.getCode())
  396. {
  397. case X:
  398. System.exit(0);
  399. break;
  400. default:
  401. break;
  402. }
  403. }
  404.  
  405. /**
  406. * Update method where the observable classes sends notifications messages
  407. *
  408. * @param o observable object
  409. * @param arg object arg
  410. */
  411. @Override
  412. public void update(Observable o, Object arg)
  413. {
  414. if (arg instanceof MqttMessage)
  415. {
  416. if (!paired)
  417. {
  418. Thread thread = new Thread(() ->
  419. {
  420. JsonMessageParser parser = new JsonMessageParser();
  421. parser.parseMessage(arg.toString());
  422. if (parser.parsePairing())
  423. {
  424. setComponentVisible(this.widget1);
  425. setComponentVisible(this.widget5);
  426. changeScene();
  427. this.paired = true;
  428. }
  429. });
  430. thread.start();
  431. }
  432. }
  433. else if (arg instanceof Weather)
  434. {
  435. Thread thread = new Thread(() ->
  436. {
  437. setChanged();
  438. notifyObservers(arg);
  439. });
  440. thread.start();
  441. }
  442. }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement