MenuBarNetworkExamples.java 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. PacketSniffer 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. System.out.println("Test w/0 anomalies:");//TODO
  911. for(int i=24; i<26; i++)
  912. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  913. new_time = System.currentTimeMillis();
  914. elapsed_time = new_time-currentTime;
  915. System.out.println("Test witout anomalies took: "+elapsed_time+"ms");
  916. currentTime = new_time;
  917. /**
  918. * Light Anomaly 1h
  919. * Light != Sensor
  920. */
  921. System.out.println("Light Anomaly:");
  922. smartLight.setTrueStatus(smartLight.getBSval());
  923. smartLight.setLabel((short)-1);
  924. for(int i=26; i<27; i++)
  925. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  926. smartLight.setLabel((short)0);
  927. new_time = System.currentTimeMillis();
  928. elapsed_time = new_time-currentTime;
  929. System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
  930. currentTime = new_time;
  931. /**
  932. * Schedule new light events
  933. */
  934. eventTime = 27 * hour;
  935. while(eventTime < 29 * hour) {
  936. /**
  937. * At least 1 second between events
  938. */
  939. SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
  940. @Override
  941. public void simulateEvent(long time) {
  942. smartLight.setBSval(!smartLight.isTrueStatus());
  943. smartLight.setTrueStatus(!smartLight.isTrueStatus());
  944. }
  945. });
  946. eventTime+=Math.max(1000, Math.round(lightDist.sample()));
  947. }
  948. /**
  949. * Temperature Anomaly 2h
  950. */
  951. System.out.println("Temperature Anomaly:");
  952. heaterDevice.setFSval(21f);
  953. heaterDevice.setTrueTemperature(34f);
  954. heaterDevice.setLabel((short)-1);//-1 Value anomaly
  955. for(int i=27; i<29; i++)
  956. sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
  957. heaterDevice.setFSval(21f);
  958. heaterDevice.setTrueTemperature(21f);
  959. heaterDevice.setLabel((short)0);
  960. new_time = System.currentTimeMillis();
  961. elapsed_time = new_time-currentTime;
  962. System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
  963. currentTime = new_time;
  964. System.out.println("Testing completed");
  965. } catch(Exception e) {
  966. System.out.println("WARNING: Testing failed: ");
  967. e.printStackTrace();
  968. }
  969. }
  970. }