MenuBarNetworkExamples.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.menuBar;
  2. import java.io.File;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import java.util.Map.Entry;
  7. import javax.swing.JMenu;
  8. import javax.swing.JMenuItem;
  9. import org.apache.commons.math3.distribution.NormalDistribution;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ExampleAnomalyController;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ImportController;
  13. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.NetworkController;
  14. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.PacketCaptureController;
  15. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SettingsController;
  16. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.SimulationController;
  17. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  18. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPerformance;
  19. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
  20. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  21. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  22. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
  23. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketSniffer;
  24. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  25. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
  26. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
  27. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.RoomStatus;
  28. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
  29. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  30. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolCollectorDevice;
  31. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolSensorDevice;
  32. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatCollectorDevice;
  33. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatSensorDevice;
  34. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartLight;
  35. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartLightSensor;
  36. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartTemperatureProducer;
  37. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.SmartTemperatureSensor;
  38. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.NormalDistributionHandler;
  39. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
  40. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.AbstractEvent;
  41. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.CountingMetric;
  42. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.Manipulation_RandomMove;
  43. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
  44. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
  45. import de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation.SWCKMeansClustering;
  46. public class MenuBarNetworkExamples extends JMenu{
  47. /**
  48. *
  49. */
  50. private static final long serialVersionUID = 1L;
  51. /**
  52. * Controller controller
  53. */
  54. private Controller controller;
  55. /**
  56. * Controller for editing the network
  57. */
  58. private NetworkController networkController;
  59. /**
  60. * Controller to insert anomalies
  61. */
  62. private ExampleAnomalyController anomalyController;
  63. /**
  64. * Controller for configuration of the program
  65. */
  66. private SettingsController settingsController;
  67. /**
  68. * SimulationController for running the simulation
  69. */
  70. private SimulationController simulationController;
  71. /**
  72. * Creates the JMenu for network examples in the MenuBar
  73. * @param controller controller to create examples
  74. */
  75. public MenuBarNetworkExamples(Controller controller) {
  76. super("Create example Network");
  77. this.controller = controller;
  78. this.networkController = controller.getNetworkController();
  79. this.anomalyController = networkController.getAnomalyController();
  80. this.settingsController = controller.getSettingsController();
  81. this.simulationController = controller.getSimulationController();
  82. /**
  83. * MQTT example
  84. */
  85. JMenuItem mnMQTT = new JMenuItem("MQTT Example");
  86. mnMQTT.addActionListener(a->initializeMQTTTest());
  87. this.add(mnMQTT);
  88. /**
  89. * Small Network Test
  90. */
  91. JMenuItem mnTest = new JMenuItem("Network Example");
  92. mnTest.addActionListener(a->initializeTest());
  93. this.add(mnTest);
  94. /**
  95. * Performance Comparison Example
  96. */
  97. JMenuItem mnPerformance = new JMenuItem("Performane Evaluation Preset");
  98. mnPerformance.addActionListener(a->initializePerformanceTest());
  99. this.add(mnPerformance);
  100. /**
  101. * User Study Preset
  102. */
  103. JMenuItem mnUserStudy = new JMenuItem("User Study Preset");
  104. mnUserStudy.addActionListener(a->initializeUserStudy());
  105. this.add(mnUserStudy);
  106. /**
  107. * Packet Collection example
  108. */
  109. JMenuItem mnPacketCollectionExmample = new JMenuItem("Packet Collection Example");
  110. mnPacketCollectionExmample.addActionListener(a->testPackageCollection());
  111. this.add(mnPacketCollectionExmample);
  112. /**
  113. * Paper example
  114. */
  115. JMenuItem mnPaperExample = new JMenuItem("Paper Example");
  116. mnPaperExample.addActionListener(a->createPaperExample(false));
  117. this.add(mnPaperExample);
  118. /**
  119. * Paper example + run
  120. */
  121. JMenuItem mnRunPaperExample = new JMenuItem("Run Paper Example");
  122. mnRunPaperExample.addActionListener(a->createPaperExample(true));
  123. this.add(mnRunPaperExample);
  124. /**
  125. * SWC example
  126. */
  127. JMenuItem mnSWCExample = new JMenuItem("SWC Example");
  128. mnSWCExample.addActionListener(a->createSWCExample(false));
  129. this.add(mnSWCExample);
  130. /**
  131. * SWC example + run
  132. */
  133. JMenuItem mnRunSWCExample = new JMenuItem("Run SWC Example");
  134. mnRunSWCExample.addActionListener(a->createSWCExample(true));
  135. this.add(mnRunSWCExample);
  136. }
  137. private void initializePerformanceTest(){
  138. SmartDevice tic = new SmartDevice("Tic");
  139. tic.setX(100);
  140. tic.setY(100);
  141. SmartDevice toc = new SmartDevice("Toc");
  142. toc.setX(100);
  143. toc.setY(250);
  144. networkController.addSmartDevice(tic);
  145. networkController.addSmartDevice(toc);
  146. Link l = new PrecisionLink("Channel");
  147. networkController.addLink(l);
  148. networkController.addLinkToDevice(l, tic);
  149. networkController.addLinkToDevice(l, toc);
  150. Port ticP = new Port(tic, (short)2, 200, (short)0, 0, (short)0);
  151. Port tocP = new Port(toc, (short)3, 200, (short)0, -100, (short)0);
  152. tic.addPort(ticP);
  153. toc.addPort(tocP);
  154. Connection con = new ConnectionPrecision(l, new SimpleProtocol());
  155. networkController.addConnection(con);
  156. networkController.addConnectionToLink(con, l);
  157. networkController.addDeviceToConnectionAndProtocol(ticP, con, 0);
  158. networkController.addDeviceToConnectionAndProtocol(tocP, con, 1);
  159. simulationController.setStepDuration(100000000);
  160. simulationController.setEndTime(1000000000);
  161. controller.notifyObservers();
  162. }
  163. /**
  164. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  165. */
  166. private void initializeUserStudy(){
  167. /**
  168. * Preset one SmartDevice
  169. */
  170. SmartDevice A = new SmartDevice("Homestation");
  171. A.setX(200);
  172. A.setY(200);
  173. networkController.addSmartDevice(A);
  174. /**
  175. * Packagecollector, which is registered at the Device
  176. */
  177. PacketCollector collector = new PacketCollector(new CountingMetric());
  178. simulationController.getPacketCaptureController().addPacketCollector(collector);
  179. simulationController.getPacketCaptureController().addDeviceToCollector(collector, A);
  180. simulationController.setPrintPackets(true);
  181. controller.notifyObservers();
  182. }
  183. /**
  184. * Test package collectors
  185. */
  186. private void testPackageCollection() {
  187. if(networkController.getSmartDevices().size()<3)
  188. return;
  189. simulationController.addAlgorithm(new Manipulation_RandomMove(), controller);
  190. PacketCollector collector = new PacketCollector();
  191. simulationController.getSimulationManager().getPacketCollectionManager().addPacketCollector(collector);
  192. System.out.println("Collector 0-500 - nothing collected:");
  193. simulationController.getSimulationManager().simulateTimeIntervall(0, 500);
  194. HashMap<Link, LinkedList<Packet>> map = collector.getPackets();
  195. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  196. System.out.println("Link: "+e.getKey().getName());
  197. for(Packet p : e.getValue())
  198. System.out.println(p.getTextualRepresentation());
  199. }
  200. System.out.println("");
  201. System.out.println("");
  202. Iterator<SmartDevice> it = networkController.getSmartDevices().iterator();
  203. it.next();
  204. SmartDevice d = it.next();
  205. collector.addDevice(d);
  206. SmartDevice f = it.next();
  207. collector.addDevice(f);
  208. System.out.println("Collector 500-1000 - "+d.getName()+" & "+f.getName()+" collected:");
  209. simulationController.getSimulationManager().simulateTimeIntervall(500, 500);
  210. map = collector.getPackets();
  211. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  212. System.out.println("Link: "+e.getKey().getName());
  213. for(Packet p : e.getValue())
  214. System.out.println(p.getTextualRepresentation());
  215. }
  216. System.out.println("");
  217. System.out.println("");
  218. Link l = networkController.getLinks().iterator().next();
  219. collector.addLink(l);
  220. System.out.println("Collector 2000-3000 - "+l+" collected:");
  221. simulationController.getSimulationManager().simulateTimeIntervall(1000, 500);
  222. map = collector.getPackets();
  223. for(Entry<Link, LinkedList<Packet>> e:map.entrySet()){
  224. System.out.println("Link: "+e.getKey().getName());
  225. for(Packet p : e.getValue())
  226. System.out.println(p.getTextualRepresentation());
  227. }
  228. simulationController.resetSimulation();
  229. controller.notifyObservers();
  230. }
  231. /**
  232. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  233. */
  234. private void initializeTest(){
  235. SmartDevice A = null, B = null, C = null;
  236. for(int i = 0; i<5; i++){
  237. A = new SmartDevice("SmartTV"+i);
  238. A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  239. A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  240. B = new SmartDevice("SmartDoor"+i);
  241. B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  242. B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  243. C = new SmartDevice("SmartLight"+i);
  244. C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  245. C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  246. networkController.addSmartDevice(A);
  247. networkController.addSmartDevice(B);
  248. networkController.addSmartDevice(C);
  249. }
  250. Link link = new SimpleLink("SimpleWifi");
  251. link.addDevice(A);
  252. link.addDevice(B);
  253. link.addDevice(C);
  254. A.addLink(link);
  255. B.addLink(link);
  256. C.addLink(link);
  257. Port a = new Port(A, (short) 1, 68);
  258. a.setLastTrigger(0);
  259. a.setStatus(Port.SENDING);
  260. A.addPort(a);
  261. Port b = new Port(B, (short) 2, 102);
  262. b.setStatus(Port.SENDING);
  263. B.addPort(b);
  264. Connection s = new ConnectionPerformance(link, new SimpleProtocol(a, b));
  265. s.setPacketLossProbability(0.01);//1% Packet loss probability
  266. a.setConnection(s);
  267. b.setConnection(s);
  268. s.addSmartDevice(a);
  269. s.addSmartDevice(b);
  270. link.addConnection(s);
  271. networkController.addLink(link);
  272. networkController.addConnection(s);
  273. controller.notifyObservers();
  274. }
  275. /**
  276. * Initializes a basic test Network, which contains a few SmartDevices, one Link and one Connection
  277. */
  278. private void initializeMQTTTest(){
  279. SimpleLink link = new SimpleLink("WIFI");
  280. link.setFixedDelay(5);
  281. SmartDevice broker = new SmartDevice("MQTT-Broker");
  282. broker.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  283. broker.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  284. networkController.addSmartDevice(broker);
  285. Port brokerPort = new Port(broker, (short) 0, 987);
  286. brokerPort.setLastTrigger(0);
  287. brokerPort.setStatus(Port.OPEN);
  288. broker.addPort(brokerPort);
  289. link.addDevice(broker);
  290. broker.addLink(link);
  291. Protocol protocol = new MQTT_protocol(brokerPort);
  292. Connection con = new ConnectionPrecision(link, protocol);
  293. con.setPacketLossProbability(0.01);//1% Packet loss probability
  294. con.addSmartDevice(brokerPort);
  295. brokerPort.setConnection(con);
  296. con.setStatus(Connection.ACTIVE);
  297. networkController.addLink(link);
  298. link.addConnection(con);
  299. networkController.addConnection(con);
  300. SmartDevice A = null, A1 = null, A2 = null, B = null, C = null;
  301. Port aP,a1P,a2P,bP,cP;
  302. for(int i = 0; i<3; i++){
  303. FloatSensorDevice aS = new FloatSensorDevice("SmartThermostat"+i+"(Pub)");
  304. aS.setFSinfoName("room"+i+"/temperature");
  305. A=aS;
  306. A.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  307. A.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  308. link.addDevice(A);
  309. A.addLink(link);
  310. aP = new Port(A,((short) (3*i+1)),100+(int)(Math.random()*900));
  311. aP.setLastTrigger(0);
  312. aP.setJitter((short) (Math.random()*50));
  313. aP.setStatus(Port.SENDING);
  314. aP.setConnection(con);
  315. protocol.addDeviceOfRole(aP, 2);
  316. con.addSmartDevice(aP);
  317. A.addPort(aP);
  318. networkController.addSmartDevice(A);
  319. FloatCollectorDevice aS1 = new FloatCollectorDevice("SmartAirCondition"+i+"(Sub)");
  320. aS1.setFCinfoName("room"+i+"/temperature");
  321. A1=aS1;
  322. A1.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  323. A1.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  324. link.addDevice(A1);
  325. A1.addLink(link);
  326. a1P = new Port(A1,((short) (3*i+1)),100+(int)(Math.random()*900));
  327. a1P.setLastTrigger(0);
  328. a1P.setJitter((short) (Math.random()*50));
  329. a1P.setStatus(Port.SENDING);
  330. a1P.setConnection(con);
  331. protocol.addDeviceOfRole(a1P, 3);
  332. con.addSmartDevice(a1P);
  333. A1.addPort(a1P);
  334. networkController.addSmartDevice(A1);
  335. FloatCollectorDevice aS2 = new FloatCollectorDevice("SmartHeater"+i+"(Sub)");
  336. aS2.setFCinfoName("room"+i+"/temperature");
  337. A2=aS2;
  338. A2.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  339. A2.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  340. link.addDevice(A2);
  341. A2.addLink(link);
  342. a2P = new Port(A2,((short) (3*i+1)),100+(int)(Math.random()*900));
  343. a2P.setLastTrigger(0);
  344. a2P.setJitter((short) (Math.random()*50));
  345. a2P.setStatus(Port.SENDING);
  346. a2P.setConnection(con);
  347. protocol.addDeviceOfRole(a2P, 3);
  348. con.addSmartDevice(a2P);
  349. A2.addPort(a2P);
  350. networkController.addSmartDevice(A2);
  351. B = new BoolSensorDevice("SmartDoor"+i+"(Pub)");
  352. ((BoolSensorDevice)B).setBSinfoName("room"+i+"/DoorOpen");
  353. B.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  354. B.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  355. link.addDevice(B);
  356. B.addLink(link);
  357. bP = new Port(B,((short) (3*i+2)),10+(int)(Math.random()*190));
  358. bP.setLastTrigger(0);
  359. bP.setJitter((short) (Math.random()*50));
  360. bP.setStatus(Port.SENDING);
  361. bP.setConnection(con);
  362. protocol.addDeviceOfRole(bP, 2);
  363. con.addSmartDevice(bP);
  364. B.addPort(bP);
  365. networkController.addSmartDevice(B);
  366. C = new BoolCollectorDevice("SmartDoorStatusLight"+i+"(Sub)");
  367. ((BoolCollectorDevice)C).setBCinfoName("room"+i+"/DoorOpen");
  368. C.setX((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  369. C.setY((int)(Math.random()*settingsController.getWidth()-2*settingsController.getDeviceVisualizationRadius())+settingsController.getDeviceVisualizationRadius());
  370. link.addDevice(C);
  371. C.addLink(link);
  372. cP = new Port(C,((short) (3*i+1)), 50+(int)(Math.random()*450));
  373. cP.setLastTrigger(0);
  374. cP.setJitter((short) (Math.random()*50));
  375. cP.setStatus(Port.SENDING);
  376. cP.setConnection(con);
  377. protocol.addDeviceOfRole(cP, 3);
  378. con.addSmartDevice(cP);
  379. C.addPort(cP);
  380. networkController.addSmartDevice(C);
  381. controller.notifyObservers();
  382. }
  383. }
  384. /**
  385. * Creates the paper toy example, also simulates it, if run = true
  386. * @param run
  387. */
  388. @SuppressWarnings("unchecked")
  389. public void createPaperExample(boolean run){
  390. /**
  391. * Reset network, etc.
  392. */
  393. if(run) {
  394. controller.getSimulationController().resetSimulation();
  395. controller.getNetworkController().deleteNetworkModel();
  396. }
  397. /*
  398. * Main networking devices
  399. */
  400. SmartDevice router = networkController.createSmartDevice("Wifi-Router", 500, 100, 50);
  401. SmartDevice zigBeeRouter = networkController.createSmartDevice("ZigBee-Router", 500, 300, 50);
  402. SmartDevice broker = networkController.createSmartDevice("SmartHub", 500, 500, 50);
  403. /*
  404. * Links/Networks
  405. */
  406. Link wifi = new PrecisionLink("Wifi");
  407. networkController.addLink(wifi);
  408. Link zigbee = new PrecisionLink("ZigBee");
  409. networkController.addLink(zigbee);
  410. /*
  411. * Connect Devices to Links
  412. */
  413. networkController.addLinkToDevice(wifi, router);
  414. networkController.addLinkToDevice(wifi, zigBeeRouter);
  415. networkController.addLinkToDevice(zigbee, zigBeeRouter);
  416. networkController.addLinkToDevice(zigbee, broker);
  417. /*
  418. * Internet Access Connection
  419. */
  420. Connection inetAcces = new ConnectionPrecision();
  421. inetAcces.setName("Cloud Access");
  422. networkController.addConnectionToLink(inetAcces, wifi);
  423. inetAcces.setProtocol(new SimpleProtocol());
  424. Port pRouter = new Port(router, (short)80, 500L);
  425. pRouter.setTriggerHandler(new NormalDistributionHandler(500, 100));
  426. networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
  427. Port pZigBee = new Port(zigBeeRouter, (short)80, 1000L);
  428. pZigBee.setTriggerHandler(new NormalDistributionHandler(1000, 300));
  429. networkController.addDeviceToConnectionAndProtocol(pRouter, inetAcces, 0);
  430. networkController.addDeviceToConnectionAndProtocol(pZigBee, inetAcces, 1);
  431. networkController.addConnection(inetAcces);
  432. /*
  433. * ZigbeeRouter -> Broker ?
  434. */
  435. Connection homeAutomationInternetAccess = new ConnectionPrecision();
  436. homeAutomationInternetAccess.setName("Home Automation Webinterface");
  437. networkController.addConnectionToLink(homeAutomationInternetAccess, zigbee);
  438. homeAutomationInternetAccess.setProtocol(new SimpleProtocol());
  439. Port pBrokerWebInterface = new Port(broker, (short)80);
  440. pBrokerWebInterface.setTriggerHandler(new NormalDistributionHandler(2000, 500));
  441. pBrokerWebInterface.setStatus(Port.SENDING);
  442. pBrokerWebInterface.setResponseTime((short)2);
  443. pBrokerWebInterface.setLastTrigger(-284L);
  444. networkController.addDeviceToConnectionAndProtocol(pBrokerWebInterface, homeAutomationInternetAccess, 0);
  445. networkController.addConnection(homeAutomationInternetAccess);
  446. Port pRouterWebInterface = new Port(zigBeeRouter, (short)80);
  447. pRouterWebInterface.setTriggerHandler(new NormalDistributionHandler(5000, 3000));
  448. pRouterWebInterface.setStatus(Port.SENDING);
  449. pRouterWebInterface.setResponseTime((short)2);
  450. pRouterWebInterface.setLastTrigger(-142L);
  451. networkController.addDeviceToConnectionAndProtocol(pRouterWebInterface, homeAutomationInternetAccess, 1);
  452. networkController.addConnection(homeAutomationInternetAccess);
  453. /*
  454. * Create MQTT Connection
  455. */
  456. Connection mqtt = new ConnectionPrecision();
  457. mqtt.setName("Automation (MQTT)");
  458. networkController.addConnectionToLink(mqtt, zigbee);
  459. mqtt.setProtocol(new MQTT_protocol());
  460. Port pBroker = new Port(broker, (short)1883);
  461. pBroker.setStatus(Port.OPEN);
  462. pBroker.setResponseTime((short)2);
  463. networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
  464. networkController.addConnection(mqtt);
  465. /*
  466. * Add some MQTT Devices
  467. */
  468. /**
  469. * Kitchen Thermostat
  470. */
  471. FloatSensorDevice floatSensor = new FloatSensorDevice("Kitchen Thermostat");
  472. floatSensor.setFSinfoName("home/kitchen/temperature");
  473. networkController.addLinkToDevice(zigbee, floatSensor);
  474. networkController.moveSmartDevice(floatSensor, 300, 500, 50);
  475. floatSensor.setFSmin(15.0f);
  476. floatSensor.setFSmax(32.0f);
  477. networkController.addSmartDevice(floatSensor);
  478. Port pFloatSensor = new Port(floatSensor, (short)1883, 15000);
  479. pFloatSensor.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  480. pFloatSensor.setStatus(Port.SENDING);
  481. pFloatSensor.setLastTrigger(-357L);
  482. networkController.addDeviceToConnectionAndProtocol(pFloatSensor, mqtt,1);
  483. /*
  484. * Add Fridge
  485. */
  486. FloatSensorDevice kitchenFridge = new FloatSensorDevice("Smart Fridge");
  487. kitchenFridge.setFSinfoName("home/kitchen/fridgeTemp");
  488. networkController.addLinkToDevice(zigbee, kitchenFridge);
  489. networkController.moveSmartDevice(kitchenFridge, 100, 600, 50);
  490. kitchenFridge.setFSmin(-6.0f);
  491. kitchenFridge.setFSmax(-4.0f);
  492. networkController.addSmartDevice(kitchenFridge);
  493. Port pKitchenFridge = new Port(kitchenFridge, (short)1883, 15000);
  494. pKitchenFridge.setStatus(Port.SENDING);
  495. pKitchenFridge.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  496. pKitchenFridge.setLastTrigger(-1231L);
  497. networkController.addDeviceToConnectionAndProtocol(pKitchenFridge, mqtt,1);
  498. /*
  499. * Add some kitchen lights
  500. */
  501. BoolSensorDevice kitchenLight = new BoolSensorDevice("Kitchen Light");
  502. kitchenLight.setBSinfoName("home/kitchen/light");
  503. networkController.addLinkToDevice(zigbee, kitchenLight);
  504. networkController.moveSmartDevice(kitchenLight, 250, 400, 50);
  505. networkController.addSmartDevice(kitchenLight);
  506. Port pKitchenLight = new Port(kitchenLight, (short)1883, 15000);
  507. pKitchenLight.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  508. pKitchenLight.setStatus(Port.SENDING);
  509. pKitchenLight.setLastTrigger(-1207L);
  510. networkController.addDeviceToConnectionAndProtocol(pKitchenLight, mqtt,1);
  511. //TODO Further devices & Connections
  512. /*
  513. * Bedroom
  514. */
  515. BoolSensorDevice sleepingRoomLight = new BoolSensorDevice("Bedroom Light");
  516. sleepingRoomLight.setBSinfoName("home/bedroom/light");
  517. networkController.addLinkToDevice(zigbee, sleepingRoomLight);
  518. networkController.moveSmartDevice(sleepingRoomLight, 750, 400, 50);
  519. networkController.addSmartDevice(sleepingRoomLight);
  520. Port pBedroomLight = new Port(sleepingRoomLight, (short)1883, 15000);
  521. pBedroomLight.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  522. pBedroomLight.setStatus(Port.SENDING);
  523. pBedroomLight.setLastTrigger(-1337L);
  524. networkController.addDeviceToConnectionAndProtocol(pBedroomLight, mqtt,1);
  525. /*
  526. * Bedroom Thermostat
  527. */
  528. FloatSensorDevice bedroomThermostat = new FloatSensorDevice("Bedroom Thermostat");
  529. bedroomThermostat.setFSinfoName("home/bedroom/temperature");
  530. networkController.addLinkToDevice(zigbee, bedroomThermostat);
  531. networkController.moveSmartDevice(bedroomThermostat, 700, 500, 50);
  532. bedroomThermostat.setFSmin(15.0f);
  533. bedroomThermostat.setFSmax(32.0f);
  534. networkController.addSmartDevice(bedroomThermostat);
  535. Port pBedroomThermostat = new Port(bedroomThermostat, (short)1883, 15000);
  536. pBedroomThermostat.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  537. pBedroomThermostat.setStatus(Port.SENDING);
  538. pBedroomThermostat.setLastTrigger(-820L);
  539. networkController.addDeviceToConnectionAndProtocol(pBedroomThermostat, mqtt,1);
  540. /*
  541. * Bedroom Info Screen
  542. */
  543. FloatCollectorDevice bedroomInfoScreen = new FloatCollectorDevice("Information Panel");
  544. bedroomInfoScreen.setFCinfoName("home/kitchen/fridgeTemp");
  545. networkController.addLinkToDevice(zigbee, bedroomInfoScreen);
  546. networkController.moveSmartDevice(bedroomInfoScreen, 900, 600, 50);
  547. networkController.addSmartDevice(bedroomInfoScreen);
  548. Port pBedroomInfo = new Port(bedroomInfoScreen, (short)1883, 15000);
  549. pBedroomInfo.setStatus(Port.SENDING);
  550. pBedroomInfo.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  551. pBedroomInfo.setLastTrigger(-666L);
  552. networkController.addDeviceToConnectionAndProtocol(pBedroomInfo, mqtt,1);
  553. /*
  554. * Update visualization
  555. */
  556. controller.notifyObservers();
  557. /**
  558. * Run only if run == true
  559. */
  560. if(!run)return;
  561. try {
  562. System.out.println("Check 1");//TODO
  563. /**
  564. * Instances of the packet Sniffers
  565. */
  566. PacketSniffer snifferEM = null, snifferKNN = null, snifferHC = null;
  567. /*
  568. * Import Example PacketSniffer algorithms
  569. */
  570. Class<? extends PacketSniffer> em = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/EMClustering.java"));
  571. snifferEM = em.newInstance();
  572. Class<? extends PacketSniffer> knn = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/KMeansClustering.java"));
  573. snifferKNN = knn.newInstance();
  574. Class<? extends PacketSniffer> hc = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/HierarchicalClustering.java"));
  575. snifferHC = hc.newInstance();
  576. System.out.println("Check 2: Imported");//TODO
  577. /*
  578. * Create collectors
  579. */
  580. PacketCollector collectorKNN = new PacketCollector(snifferKNN);
  581. //PacketCollector collectorEM = new PacketCollector(snifferEM);
  582. //PacketCollector collectorHC = new PacketCollector(snifferHC);
  583. /*
  584. * Capture both links on all collectors
  585. */
  586. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  587. //captureController.addLinkToCollector(collectorEM, zigbee);
  588. //captureController.addLinkToCollector(collectorEM, wifi);
  589. captureController.addLinkToCollector(collectorKNN, zigbee);
  590. captureController.addLinkToCollector(collectorKNN, wifi);
  591. //captureController.addLinkToCollector(collectorHC, zigbee);
  592. //captureController.addLinkToCollector(collectorHC, wifi);
  593. captureController.addPacketCollector(collectorKNN);
  594. //captureController.addPacketCollector(collectorEM);
  595. //captureController.addPacketCollector(collectorHC);
  596. System.out.println("Check 3: created Controller");//TODO
  597. long currentTime = System.currentTimeMillis();
  598. long hour = 60 * 60 * 1000;
  599. /*
  600. * Simulate 24 hours
  601. */
  602. SimulationController sim = controller.getSimulationController();
  603. sim.setStartTime(0);
  604. sim.resetSimulation();
  605. sim.setStepDuration(hour);
  606. sim.setPrintPackets(false);
  607. sim.setEndTime(24*hour);
  608. kitchenFridge.setFSmin(-6f);
  609. kitchenFridge.setFSmax(-4f);
  610. kitchenFridge.setFSval(-5f);
  611. System.out.println("Training:");//TODO
  612. for(int i=0; i<24; i++)
  613. sim.getSimulationManager().simulateTimeIntervall(i*hour, hour);
  614. long new_time = System.currentTimeMillis();
  615. long elapsed_time = new_time-currentTime;
  616. System.out.println("Training data generation: "+elapsed_time+"ms");
  617. currentTime = new_time;
  618. collectorKNN.setMode(true);
  619. //collectorEM.setMode(true);
  620. //collectorHC.setMode(true);
  621. new_time = System.currentTimeMillis();
  622. elapsed_time = new_time-currentTime;
  623. System.out.println("Training of algorithm: "+elapsed_time+"ms");
  624. currentTime = new_time;
  625. /*
  626. * Simulate/Test 1 hour without anomalies
  627. */
  628. System.out.println("Test w/0 anomalies:");//TODO
  629. for(int i=24; i<25; i++)
  630. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  631. new_time = System.currentTimeMillis();
  632. elapsed_time = new_time-currentTime;
  633. System.out.println("Training data generation: "+elapsed_time+"ms");
  634. currentTime = new_time;
  635. System.out.println("DDoS:");
  636. /**
  637. * 1 hour DDoS
  638. */
  639. Connection ddos = anomalyController.openDDosCreationMenu(broker, new LinkedList<SmartDevice>(zigbee.getDevices()));
  640. for(int i=25; i<26; i++)
  641. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  642. networkController.deleteConnection(ddos);
  643. new_time = System.currentTimeMillis();
  644. elapsed_time = new_time-currentTime;
  645. System.out.println("DDoS generation & classification: "+elapsed_time+"ms");
  646. currentTime = new_time;
  647. System.out.println("DoS:");
  648. /**
  649. * 1 hour DoS
  650. */
  651. Connection dos = anomalyController.runDosAttack(kitchenLight, kitchenFridge);
  652. for(int i=26; i<27; i++)
  653. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  654. networkController.deleteConnection(dos);
  655. new_time = System.currentTimeMillis();
  656. elapsed_time = new_time-currentTime;
  657. System.out.println("DoS generation & classification: "+elapsed_time+"ms");
  658. currentTime = new_time;
  659. /**
  660. * Value Anomaly 1h
  661. */
  662. System.out.println("Value Anomalies:");
  663. float min = kitchenFridge.getFSmin();
  664. float max = kitchenFridge.getFSmax();
  665. float val = kitchenFridge.getFSval();
  666. kitchenFridge.setFSmin(18);
  667. kitchenFridge.setFSval(18.5f);
  668. kitchenFridge.setFSmax(24);
  669. kitchenFridge.setLabel((short)-1);//-1 Value anomaly
  670. for(int i=27; i<28; i++)
  671. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  672. kitchenFridge.setFSmin(min);
  673. kitchenFridge.setFSmax(max);
  674. kitchenFridge.setFSval(val);
  675. kitchenFridge.setLabel((short)0);
  676. new_time = System.currentTimeMillis();
  677. elapsed_time = new_time-currentTime;
  678. System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
  679. currentTime = new_time;
  680. System.out.println("Testing completed");
  681. } catch(Exception e) {
  682. System.out.println("WARNING: Testing failed: ");
  683. e.printStackTrace();
  684. }
  685. }
  686. /**
  687. * Creates the paper toy example, also simulates it, if run = true
  688. * @param run
  689. */
  690. @SuppressWarnings("unchecked")
  691. public void createSWCExample(boolean run){
  692. /**
  693. * Reset network, etc.
  694. */
  695. if(run) {
  696. controller.getSimulationController().resetSimulation();
  697. controller.getNetworkController().deleteNetworkModel();
  698. }
  699. /*
  700. * Main networking devices
  701. */
  702. SmartDevice smartHub = networkController.createSmartDevice("SmartHub", 400, 400, 50);
  703. /*
  704. * Links/Networks
  705. */
  706. Link zigbee = new PrecisionLink("ZigBee");
  707. networkController.addLink(zigbee);
  708. /*
  709. * Connect Devices to Links
  710. */
  711. networkController.addLinkToDevice(zigbee, smartHub);
  712. /*
  713. * Create MQTT Connection
  714. */
  715. Connection mqtt = new ConnectionPrecision();
  716. mqtt.setName("HomeAutomation (MQTT)");
  717. networkController.addConnectionToLink(mqtt, zigbee);
  718. mqtt.setProtocol(new MQTT_protocol());
  719. Port pBroker = new Port(smartHub, (short)1883);
  720. pBroker.setStatus(Port.OPEN);
  721. pBroker.setResponseTime((short)2);
  722. networkController.addDeviceToConnectionAndProtocol(pBroker, mqtt, 0);
  723. networkController.addConnection(mqtt);
  724. mqtt.setLabel((short) 0);
  725. /**
  726. * Room
  727. */
  728. String roomName = "Bedroom";
  729. RoomStatus room = new RoomStatus();
  730. room.setTemperature(19f);
  731. /*
  732. * Add some MQTT Devices
  733. */
  734. /**
  735. * Room Thermostat
  736. */
  737. SmartTemperatureSensor heatSensor = new SmartTemperatureSensor(roomName + " Thermostat", room);
  738. heatSensor.setFSinfoName("home/" + roomName + "/sensorTemp");
  739. networkController.addLinkToDevice(zigbee, heatSensor);
  740. networkController.moveSmartDevice(heatSensor, 300, 300, 50);
  741. heatSensor.setFSmin(19.0f);
  742. heatSensor.setFSmax(21.0f);
  743. heatSensor.setFSval(19.0f);
  744. networkController.addSmartDevice(heatSensor);
  745. Port pFloatSensor = new Port(heatSensor, (short)1883, 15000);
  746. pFloatSensor.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  747. pFloatSensor.setStatus(Port.SENDING);
  748. pFloatSensor.setLastTrigger(-357L);
  749. networkController.addDeviceToConnectionAndProtocol(pFloatSensor, mqtt,1);
  750. heatSensor.setLabel((short) 0);
  751. /*
  752. * Add Heater (every second update)
  753. */
  754. SmartTemperatureProducer heaterDevice = new SmartTemperatureProducer(roomName + " Heater", room, 1000, heatSensor);
  755. heaterDevice.setFSinfoName("home/" + roomName + "/heaterTemp");
  756. networkController.addLinkToDevice(zigbee, heaterDevice);
  757. networkController.moveSmartDevice(heaterDevice, 300, 500, 50);
  758. heaterDevice.setFSmin(19.0f);
  759. heaterDevice.setFSmax(21.0f);
  760. heaterDevice.setFSval(19.0f);
  761. networkController.addSmartDevice(heaterDevice);
  762. Port pHeaterDevice = new Port(heaterDevice, (short)1883, 15000);
  763. pHeaterDevice.setStatus(Port.SENDING);
  764. pHeaterDevice.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  765. pHeaterDevice.setLastTrigger(-1231L);
  766. networkController.addDeviceToConnectionAndProtocol(pHeaterDevice, mqtt,1);
  767. heaterDevice.setLabel((short) 0);
  768. /*
  769. * Add light Sensor
  770. */
  771. SmartLightSensor lightSensor = new SmartLightSensor(roomName + " LightSensor", room, 10000);
  772. lightSensor.setBSinfoName("home/" + roomName + "/light");
  773. networkController.addLinkToDevice(zigbee, lightSensor);
  774. networkController.moveSmartDevice(lightSensor, 500, 300, 50);
  775. networkController.addSmartDevice(lightSensor);
  776. Port pLightSensor = new Port(lightSensor, (short)1883, 15000);
  777. pLightSensor.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  778. pLightSensor.setStatus(Port.SENDING);
  779. pLightSensor.setLastTrigger(-1337L);
  780. networkController.addDeviceToConnectionAndProtocol(pLightSensor, mqtt,1);
  781. lightSensor.setLabel((short) 0);
  782. /*
  783. * Add a light
  784. */
  785. SmartLight smartLight = new SmartLight(roomName + " Light", room, lightSensor);
  786. smartLight.setBSinfoName("home/" + roomName + "/light");
  787. networkController.addLinkToDevice(zigbee, smartLight);
  788. networkController.moveSmartDevice(smartLight, 500, 500, 50);
  789. networkController.addSmartDevice(smartLight);
  790. Port pSmartLight = new Port(smartLight, (short)1883, 15000);
  791. pSmartLight.setTriggerHandler(new NormalDistributionHandler(15000, 500));
  792. pSmartLight.setStatus(Port.SENDING);
  793. pSmartLight.setLastTrigger(-1207L);
  794. networkController.addDeviceToConnectionAndProtocol(pSmartLight, mqtt,1);
  795. smartLight.setLabel((short) 0);
  796. /*
  797. * Update visualization
  798. */
  799. controller.notifyObservers();
  800. /**
  801. * Run only if run == true
  802. */
  803. if(!run)return;
  804. try {
  805. System.out.println("Check 1");//TODO
  806. /**
  807. * Instances of the packet Sniffers
  808. */
  809. SWCKMeansClustering snifferKNN = null; //, snifferEM = null, snifferHC = null;
  810. /*
  811. * Import Example PacketSniffer algorithms
  812. */
  813. /*
  814. Class<? extends PacketSniffer> em = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/EMClustering.java"));
  815. snifferEM = em.newInstance();
  816. Class<? extends PacketSniffer> hc = (Class<? extends PacketSniffer>) ImportController.importJavaClass(new File("examples/classifier/HierarchicalClustering.java"));
  817. snifferHC = hc.newInstance();*/
  818. snifferKNN = new SWCKMeansClustering();
  819. System.out.println("Check 2: Imported");//TODO
  820. /*
  821. * Create collectors
  822. */
  823. PacketCollector collectorKNN = new PacketCollector(snifferKNN);
  824. //PacketCollector collectorEM = new PacketCollector(snifferEM);
  825. //PacketCollector collectorHC = new PacketCollector(snifferHC);
  826. /*
  827. * Capture both links on all collectors
  828. */
  829. PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
  830. //captureController.addLinkToCollector(collectorEM, zigbee);
  831. captureController.addLinkToCollector(collectorKNN, zigbee);
  832. //captureController.addLinkToCollector(collectorHC, zigbee);
  833. captureController.addPacketCollector(collectorKNN);
  834. //captureController.addPacketCollector(collectorEM);
  835. //captureController.addPacketCollector(collectorHC);
  836. System.out.println("Check 3: created Controller");//TODO
  837. long currentTime = System.currentTimeMillis();
  838. long hour = 60 * 60 * 1000;
  839. /*
  840. * Simulate 24 hours
  841. */
  842. SimulationController sim = controller.getSimulationController();
  843. sim.setStartTime(0);
  844. sim.resetSimulation();
  845. sim.setStepDuration(hour);
  846. sim.setPrintPackets(false);
  847. long endTime = 24*hour;
  848. sim.setEndTime(endTime);
  849. /**
  850. * Training events
  851. */
  852. System.out.println("Create training events");
  853. /**
  854. * Light Events (random switch every 30min+-15min
  855. */
  856. NormalDistribution lightDist = new NormalDistribution(hour/2, hour/4);
  857. long eventTime = 0;
  858. while(eventTime < endTime + hour/2) {
  859. /**
  860. * At least 1 second between events
  861. */
  862. eventTime+=Math.max(1000, Math.round(lightDist.sample()));
  863. SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
  864. @Override
  865. public void simulateEvent(long time) {
  866. smartLight.setBSval(!smartLight.isTrueStatus());
  867. smartLight.setTrueStatus(!smartLight.isTrueStatus());
  868. }
  869. });
  870. }
  871. /**
  872. * Heater Events (random switch every 2h +- 30min
  873. */
  874. NormalDistribution tempDist = new NormalDistribution(2*hour, hour/2);
  875. eventTime = 0;
  876. while(eventTime < endTime + 1 * hour) {
  877. /**
  878. * At least 1 second between events
  879. */
  880. eventTime+=Math.max(1000, Math.round(tempDist.sample()));
  881. SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
  882. @Override
  883. public void simulateEvent(long time) {
  884. float newTemp = (float)(heaterDevice.getFSmax() + Math.random() * (heaterDevice.getFSmax()-heaterDevice.getFSmin()));
  885. heaterDevice.setFSval(newTemp);
  886. heaterDevice.setFSval(newTemp);
  887. }
  888. });
  889. }
  890. System.out.println("Training:");//TODO
  891. for(int i=0; i<24; i++)
  892. sim.getSimulationManager().simulateTimeIntervall(i*hour, hour);
  893. long new_time = System.currentTimeMillis();
  894. long elapsed_time = new_time-currentTime;
  895. System.out.println("Training data generation took: "+elapsed_time+"ms");
  896. currentTime = new_time;
  897. /*
  898. * Start Classifying
  899. */
  900. collectorKNN.setMode(true);
  901. //collectorEM.setMode(true);
  902. //collectorHC.setMode(true);
  903. new_time = System.currentTimeMillis();
  904. elapsed_time = new_time-currentTime;
  905. System.out.println("Training of algorithm took: "+elapsed_time+"ms");
  906. currentTime = new_time;
  907. /*
  908. * Simulate/Test 1 hour without anomalies
  909. */
  910. snifferKNN.setCurrentScenario("NoAnomalies");
  911. System.out.println("Test w/0 anomalies:");//TODO
  912. for(int i=24; i<26; i++)
  913. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  914. new_time = System.currentTimeMillis();
  915. elapsed_time = new_time-currentTime;
  916. System.out.println("Test witout anomalies took: "+elapsed_time+"ms");
  917. currentTime = new_time;
  918. /**
  919. * Light Anomaly 1h
  920. * Light != Sensor
  921. */
  922. snifferKNN.setCurrentScenario("LightAnomalies");
  923. System.out.println("Light Anomaly:");
  924. smartLight.setTrueStatus(smartLight.getBSval());
  925. smartLight.setLabel((short)-1);
  926. for(int i=26; i<27; i++)
  927. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  928. smartLight.setLabel((short)0);
  929. new_time = System.currentTimeMillis();
  930. elapsed_time = new_time-currentTime;
  931. System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
  932. currentTime = new_time;
  933. /**
  934. * Schedule new light events
  935. */
  936. eventTime = 27 * hour;
  937. while(eventTime < 29 * hour) {
  938. /**
  939. * At least 1 second between events
  940. */
  941. SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
  942. @Override
  943. public void simulateEvent(long time) {
  944. smartLight.setBSval(!smartLight.isTrueStatus());
  945. smartLight.setTrueStatus(!smartLight.isTrueStatus());
  946. }
  947. });
  948. eventTime+=Math.max(1000, Math.round(lightDist.sample()));
  949. }
  950. /**
  951. * Temperature Anomaly 2h
  952. */
  953. snifferKNN.setCurrentScenario("TemperatureAnomalies");
  954. System.out.println("Temperature Anomaly:");
  955. heaterDevice.setFSval(21f);
  956. heaterDevice.setTrueTemperature(34f);
  957. heaterDevice.setLabel((short)-1);//-1 Value anomaly
  958. for(int i=27; i<29; i++)
  959. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  960. heaterDevice.setFSval(21f);
  961. heaterDevice.setTrueTemperature(21f);
  962. heaterDevice.setLabel((short)0);
  963. new_time = System.currentTimeMillis();
  964. elapsed_time = new_time-currentTime;
  965. System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
  966. currentTime = new_time;
  967. System.out.println("Testing completed");
  968. } catch(Exception e) {
  969. System.out.println("WARNING: Testing failed: ");
  970. e.printStackTrace();
  971. }
  972. }
  973. }