3 import iotinstaller.MySQLInterface;
4 import iotinstaller.TableProperty;
5 import iotinstaller.Table;
9 import java.util.Scanner;
10 import java.util.Properties;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
16 /** A class that creates an object for IoT device/entity installation into database
18 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
22 public final class IoTInstaller {
25 * IoTInstaller class properties
30 * IoTInstaller class constants
32 private static final String STR_MAIN_TABLE_NAME = "IoTMain";
33 private static final String STR_COMM_TABLE_NAME = "IoTComm";
34 private static final String STR_HOST_TABLE_NAME = "IoTComputeNode";
35 private static final String STR_ADDRESS_TABLE_NAME = "IoTAddress";
36 private static final String STR_DEV_ADD_TABLE_NAME = "IoTDeviceAddress";
37 private static final String STR_ZB_ADD_TABLE_NAME = "IoTZigbeeAddress";
38 private static final int INT_NUM_COMM_FIELDS = 5;
39 private static final int INT_NUM_HOST_FIELDS = 3;
41 private static final String STR_INSTALL_ENTITY_CMD = "-install_ent";
42 private static final String STR_INSTALL_COMMUNICATION_CMD = "-install_comm";
43 private static final String STR_INSTALL_COMPLETE_CMD = "-install_comp";
44 private static final String STR_INSTALL_ADDRESS_CMD = "-install_add";
45 private static final String STR_INSTALL_DEV_ADDRESS_CMD = "-install_dev_add";
46 private static final String STR_INSTALL_ZB_ADDRESS_CMD = "-install_zb_add";
47 private static final String STR_INSTALL_HOST_CMD = "-install_host";
48 private static final String STR_DELETE_COMMUNICATION_CMD = "-delete_comm";
49 private static final String STR_DELETE_ENTITY_CMD = "-delete_ent";
50 private static final String STR_DELETE_ADDRESS_CMD = "-delete_add";
51 private static final String STR_DELETE_DEV_ADD_CMD = "-delete_dev_add";
52 private static final String STR_DELETE_ZB_ADD_CMD = "-delete_zb_add";
53 private static final String STR_DELETE_HOST_CMD = "-delete_host";
54 private static final String STR_HELP_CMD = "-help";
55 private static final String DATABASE_GET = "-get";
56 private static final String APPEND_TYPE = "-appendType";
57 private static final String UPDATE_SETLIST = "-updateSetList";
58 private static final String UPDATE_IOTDEVICEADDRESS = "-updateIoTDeviceAddress";
59 private static final String UPDATE_IDSUBTYPE = "-updateIDSubtype";
64 public IoTInstaller() {
66 // Make this not verbose by default
67 tbl = new Table(false);
68 System.out.println("IoTInstaller: Initializing installation..");
72 * A method to insert a new entry to the main table (IoTMain)
74 * This entry can be a new device or a new entity that we should keep track about
75 * A new entry will need a unique ID and a type name from the driver
77 * @param strID string ID to insert device into the main table
78 * @param strType string type to insert device into the main table
81 private void insertMainDBEntry(String strID, String strType) {
83 // Creating String array
84 String[] strFlds = new String[2];
85 for(int i=0; i<2; i++) {
86 strFlds[i] = new String();
91 // Insert entry through Table object
92 tbl.setTableName(STR_MAIN_TABLE_NAME);
93 tbl.insertEntry(strFlds);
94 System.out.println("IoTInstaller: Inserting a new entry into main table");
98 * A method to extract device/entity information from the user
100 * Users are supposed to supply the information needed for installation
101 * This is the core of the operation after getting the Scanner object.
103 * @param scanFile Scanner object of source
106 public void extractTableAndInstallCore(Scanner scanFile) {
108 // Parse configuration file
109 // Assumption here is that .config file is written with the correct syntax (need typechecking)
110 // Initialize String for ID and TYPE
113 String strTypeSpecific = "";
115 // Initialize TableProperty for devices and specific devices
117 // e.g. ProximitySensor - table of many ProximitySensor devices
118 // ProximitySensorBrandA - table that contains the constructor
119 // information for a specific device
120 TableProperty[] tpDevice = new TableProperty[1];
121 TableProperty[] tpDeviceSpecific = new TableProperty[1];
123 // Initialize array of string
124 String[] strFields = new String[1];
125 String[] strFieldsSpecific = new String[1];
127 // String for scanning the file
130 // Store number of fields here
132 while (scanFile.hasNext()) {
134 strScan = scanFile.next();
135 if (strScan.equals("IoTMain")) {
137 while (scanFile.hasNext()) {
138 strScan = scanFile.next();
141 if (strScan.equals("ID")) {
142 strID = scanFile.next();
145 else if (strScan.equals("TYPE")) {
146 strType = scanFile.next();
149 else if (strScan.equals("TYPESPECIFIC")) {
150 strTypeSpecific = scanFile.next();
151 } else if (strScan.equals("END")) {
156 } else if (strScan.equals("Table")) {
158 // Get number of fields, e.g. Table 3
159 iFields = scanFile.nextInt();
161 // We have device ID and device specific names
162 // e.g. ID = PS1; TYPE
163 tpDevice = new TableProperty[2];
164 tpDevice[0] = new TableProperty();
165 tpDevice[0].setField("ID");
166 tpDevice[0].setType("VARCHAR");
167 tpDevice[0].setLength("5");
168 tpDevice[1] = new TableProperty();
169 tpDevice[1].setField("TYPE");
170 tpDevice[1].setType("VARCHAR");
171 tpDevice[1].setLength("30");
173 // Prepare properties for a specific device
174 tpDeviceSpecific = new TableProperty[iFields];
175 for (int i=0; i<iFields; i++) {
176 tpDeviceSpecific[i] = new TableProperty();
178 // Looping over the fields
179 strScan = scanFile.next();
180 tpDeviceSpecific[i].setField(strScan);
181 strScan = scanFile.next();
182 tpDeviceSpecific[i].setType(strScan);
183 strScan = scanFile.next();
184 tpDeviceSpecific[i].setLength(strScan);
186 } else if (strScan.equals("Data")) {
188 // Get the device information
189 strFields = new String[2];
190 strFields[0] = strID;
191 strFields[1] = strTypeSpecific;
193 if ((tpDeviceSpecific.length == 1) &&
194 (tpDeviceSpecific[0].getField().equals("EMPTY"))) {
196 // Get the fields for specific device
197 strFieldsSpecific = null;
198 System.out.println("IoTInstaller: Empty constructor for: " + strTypeSpecific);
202 // Get the fields for specific device
203 strFieldsSpecific = new String[iFields];
204 for (int i=0; i<iFields; i++) {
205 strScan = scanFile.next();
206 strFieldsSpecific[i] = strScan;
212 installNewEntity(strType, strTypeSpecific, strID, tpDevice,
213 tpDeviceSpecific, strFields, strFieldsSpecific);
214 System.out.println("IoTInstaller: Installing a new entity/device into the system");
218 * A method to extract device/entity information from the user
220 * Users are supposed to supply the information needed for installation
222 * @param strCfgFileName String config file name for device/entity
225 public void extractTableAndInstall(String strCfgFileName) {
226 // TO DO: WE PROBABLY NEED TO IMPROVE THE FILE PARSING BUT FOR NOW KEEP IT MINIMUM
229 // Parse configuration file
230 // Assumption here is that .config file is written with the correct syntax (need typechecking)
231 File file = new File(strCfgFileName);
232 Scanner scanFile = new Scanner(new FileReader(file));
233 System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
235 extractTableAndInstallCore(scanFile);
237 } catch (FileNotFoundException ex) {
239 System.out.println("IoTInstaller: Exception: ");
240 ex.printStackTrace();
246 * A method to install a new entity/device into the database
248 * 1) Insert this device/entity into the main table IoTMain
249 * 2) Create a new device/entity table if it doesn't exist yet
250 * 3) Insert this entry into the specific device/entity table
252 * @param strType String device type
253 * @param strTypeSpecific String device specific type
254 * @param strID String unique device/entity ID
255 * @param tpDevice array of TableProperty to construct the new table
256 * @param tpDeviceSpecific array of TableProperty to construct the new table
257 * @param strFields field values of device table
258 * @param strFieldsSpecific field values of device specific table
260 private void installNewEntity(String strType, String strTypeSpecific, String strID,
261 TableProperty[] tpDevice, TableProperty[] tpDeviceSpecific, String[] strFields, String[] strFieldsSpecific) {
263 // Create a new IoTInstaller object
264 System.out.println("IoTInstaller: Installing device " + strType + " with specific type " + strTypeSpecific);
265 tbl.setTableName(strType);
267 // 1) Insert this device/entity into the main table IoTMain
268 insertMainDBEntry(strID, strType);
271 // 2) Create a new device/entity table if it doesn't exist yet
272 tbl.setTableName(strType);
273 if (tbl.isTableExisting()) {
275 System.out.println("IoTInstaller: Table " + strType + " exists.. just insert new entry!");
277 // table does not exist yet
278 tbl.createTable(tpDevice, "ID");
281 // 3) Insert this entry into the device/entity table
282 tbl.insertEntry(strFields);
284 // Device specific table
285 // 2) Create a new device/entity table if it doesn't exist yet
286 // P.S. We should assume that table doesn't exist yet, and we throw error otherwise!
287 tbl.setTableName(strTypeSpecific + strID);
288 tbl.createTable(tpDeviceSpecific, null);
290 // 3) Insert this entry into the device/entity table
291 if (strFieldsSpecific != null) {
292 tbl.insertEntry(strFieldsSpecific);
297 * A method to extract device/entity communication configuration from the user
299 * Users are supposed to supply the information needed for installation
301 * @param strCfgFileName String config file name for device/entity
304 public void extractCommAndInstall(String strCfgFileName) {
305 // TODO: WE PROBABLY NEED TO IMPROVE THE FILE PARSING BUT FOR NOW KEEP IT MINIMUM
309 // Parse configuration file
310 // Assumption here is that .config file is written with the correct syntax (need typechecking)
311 File file = new File(strCfgFileName);
312 Scanner scanFile = new Scanner(new FileReader(file));
314 System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
319 // Initialize array of string
320 String[] strFields = new String[INT_NUM_COMM_FIELDS];
321 for(int i=0; i<INT_NUM_COMM_FIELDS; i++) {
322 strFields[i] = new String();
324 while (scanFile.hasNext() && (iFieldCnt < INT_NUM_COMM_FIELDS)) {
326 strFields[iFieldCnt++] = scanFile.next();
328 // Create a new installer object
329 tbl.setTableName(STR_COMM_TABLE_NAME);
330 tbl.insertEntry(strFields);
332 System.out.println("IoTInstaller: Installing a new communication pattern into the system");
334 } catch (FileNotFoundException ex) {
336 System.out.println("IoTInstaller: Exception: ");
337 ex.printStackTrace();
343 * A method to delete relation/communication information from database
345 * @param strEntIDSource String for entity ID source
346 * @param strEntIDDest String for entity ID destination
349 public void deleteComm(String strEntIDSource, String strEntIDDest) {
351 // Delete from table IoTMain
352 tbl.setTableName(STR_COMM_TABLE_NAME);
353 String strWhere = "ID_SOURCE='" + strEntIDSource + "' AND ID_DESTINATION='" + strEntIDDest + "';";
354 tbl.deleteEntry(strWhere);
355 System.out.println("IoTInstaller: Removing relation/communication from table " + STR_COMM_TABLE_NAME);
359 * A method to extract device/entity addresses information
361 * Users are supposed to supply the information needed for installation
363 * @param is InputStream of the input source
366 public void installDeviceAddressCore(InputStream is) {
368 Properties prop = new Properties();
371 } catch (IOException ex) {
372 ex.printStackTrace();
375 // We can only install one device address per one time with the following sequence
376 String[] strFields = new String[2];
377 String[] strFieldsAddress = null;
378 // Check for wildcard feature
379 if ((prop.getProperty("SOURCEWILDCARD", null) != null) &&
380 (prop.getProperty("DESTWILDCARD", null) != null)) {
381 strFieldsAddress = new String[5];
382 strFieldsAddress[3] = prop.getProperty("SOURCEWILDCARD");
383 strFieldsAddress[4] = prop.getProperty("DESTWILDCARD");
385 strFieldsAddress = new String[3];
387 strFields[0] = prop.getProperty("ID");
388 strFields[1] = prop.getProperty("ADDRESSFOR");
389 strFieldsAddress[0] = prop.getProperty("DEVICEADDRESS");
390 strFieldsAddress[1] = prop.getProperty("PORTNUMBER");
391 strFieldsAddress[2] = prop.getProperty("PROTOCOL");
393 // Insert this entry into the main device address table
394 tbl.setTableName(STR_DEV_ADD_TABLE_NAME);
395 tbl.insertEntry(strFields);
397 // Create a new table for a specific device address
398 // e.g. AmcrestCameraAdd + CM1 = AmcrestCameraAddCM1
399 tbl.setTableName(strFields[1] + strFields[0]);
401 // Table does not exist yet
402 // Set TableProperty for device address (MAC address)
403 TableProperty[] tp = null;
404 // Check for wildcard feature
405 if (strFieldsAddress.length == 5) {
406 tp = new TableProperty[5];
407 tp[3] = new TableProperty();
408 tp[3].setField("SOURCEWILDCARD");
409 tp[3].setType("VARCHAR");
410 tp[3].setLength("5");
411 tp[4] = new TableProperty();
412 tp[4].setField("DESTWILDCARD");
413 tp[4].setType("VARCHAR");
414 tp[4].setLength("5");
416 tp = new TableProperty[3];
418 tp[0] = new TableProperty();
419 tp[0].setField("DEVICEADDRESS");
420 tp[0].setType("VARCHAR");
421 tp[0].setLength("20");
422 tp[1] = new TableProperty();
423 tp[1].setField("PORTNUMBER");
424 tp[1].setType("INT");
425 tp[1].setLength("11");
426 tp[2] = new TableProperty();
427 tp[2].setField("PROTOCOL");
428 tp[2].setType("VARCHAR");
429 tp[2].setLength("5");
430 tbl.createTable(tp, "DEVICEADDRESS");
432 // Insert new address entry
433 tbl.insertEntry(strFieldsAddress);
434 System.out.println("IoTInstaller: Installing a new device/entity address into the system");
438 * A method to extract device/entity addresses information
440 * Users are supposed to supply the information needed for installation
442 * @param strCfgFileName String config file name for device/entity
445 public void installDeviceAddress(String strCfgFileName) {
449 // Parse configuration file
450 File file = new File(strCfgFileName);
451 FileInputStream fis = new FileInputStream(file);
452 System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
453 installDeviceAddressCore(fis);
455 } catch (FileNotFoundException ex) {
457 System.out.println("IoTInstaller: Exception: ");
458 ex.printStackTrace();
464 * A method to extract Zigbee device addresses information
466 * Users are supposed to supply the information needed for installation
468 * @param strCfgFileName String config file name for device/entity
471 public void installZigbeeAddress(String strCfgFileName) {
475 // Parse configuration file
476 Properties prop = new Properties();
477 File file = new File(strCfgFileName);
478 FileInputStream fis = new FileInputStream(file);
481 } catch (IOException ex) {
482 ex.printStackTrace();
484 System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
486 // We can only install one device address per one time with the following sequence
487 String[] strFields = new String[2];
488 String[] strFieldsAddress = new String[1];
489 strFields[0] = prop.getProperty("ID");
490 strFields[1] = prop.getProperty("ADDRESSFOR");
491 strFieldsAddress[0] = prop.getProperty("DEVICEADDRESS");
493 // Insert this entry into the main device address table
494 tbl.setTableName(STR_ZB_ADD_TABLE_NAME);
495 tbl.insertEntry(strFields);
497 // Create a new table for a specific device address
498 // e.g. AmcrestCameraZBAdd + CM1 = AmcrestCameraZBAddCM1
499 tbl.setTableName(strFields[1] + strFields[0]);
501 // Table does not exist yet
502 // Set TableProperty for device address (MAC address)
503 TableProperty[] tp = new TableProperty[1];
504 tp[0] = new TableProperty();
505 tp[0].setField("DEVICEADDRESS");
506 tp[0].setType("VARCHAR");
507 tp[0].setLength("25");
508 tbl.createTable(tp, "DEVICEADDRESS");
510 // Insert new address entry
511 tbl.insertEntry(strFieldsAddress);
512 System.out.println("IoTInstaller: Installing a new device/entity address into the system");
514 } catch (FileNotFoundException ex) {
516 System.out.println("IoTInstaller: Exception: ");
517 ex.printStackTrace();
523 * A method to extract simple addresses information, e.g. www.google.com
525 * Users are supposed to supply the information needed for installation
527 * @param strCfgFileName String config file name for device/entity
530 public void installAddress(String strCfgFileName) {
534 // Parse configuration file
535 Properties prop = new Properties();
536 File file = new File(strCfgFileName);
537 FileInputStream fis = new FileInputStream(file);
540 } catch (IOException ex) {
541 ex.printStackTrace();
543 System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
545 // We can only install one device address per one time with the following sequence
546 String[] strFields = new String[2];
547 String[] strFieldsAddress = new String[1];
548 strFields[0] = prop.getProperty("ID");
549 strFields[1] = prop.getProperty("ADDRESSFOR");
550 strFieldsAddress[0] = prop.getProperty("ADDRESS");
552 // Insert this entry into the main device address table
553 tbl.setTableName(STR_ADDRESS_TABLE_NAME);
554 tbl.insertEntry(strFields);
556 // Create a new table for a specific device address
557 // e.g. WeatherForecastAdd + WF1 = WeatherForecastAddCM1
558 tbl.setTableName(strFields[1] + strFields[0]);
560 // Table does not exist yet
561 // Set TableProperty for device address (MAC address)
562 TableProperty[] tp = new TableProperty[1];
563 tp[0] = new TableProperty();
564 tp[0].setField("ADDRESS");
565 tp[0].setType("VARCHAR");
566 tp[0].setLength("50");
567 tbl.createTable(tp, "ADDRESS");
569 // Insert new address entry
570 tbl.insertEntry(strFieldsAddress);
571 System.out.println("IoTInstaller: Installing a new device/entity address into the system");
573 } catch (FileNotFoundException ex) {
575 System.out.println("IoTInstaller: Exception: ");
576 ex.printStackTrace();
582 * A method to extract host information for host installation
584 * Users are supposed to supply the information needed for installation
586 * @param strCfgFileName String config file name for device/entity
589 public void installHost(String strCfgFileName) {
591 // Parse configuration file
592 Properties prop = new Properties();
593 File file = new File(strCfgFileName);
594 FileInputStream fis = new FileInputStream(file);
597 } catch (IOException ex) {
598 ex.printStackTrace();
600 System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
601 // Initialize array of string
602 String[] strFields = new String[3];
603 strFields[0] = prop.getProperty("HOSTADDRESS");
604 strFields[1] = prop.getProperty("PROCESSOR");
605 strFields[2] = prop.getProperty("MEMORY");
606 // Create a new installer object
607 tbl.setTableName(STR_HOST_TABLE_NAME);
608 tbl.insertEntry(strFields);
609 System.out.println("IoTInstaller: Installing a new host into the system");
611 } catch (FileNotFoundException ex) {
613 System.out.println("IoTInstaller: Exception: ");
614 ex.printStackTrace();
620 * A method to delete host information from database by putting in host address
622 * @param strHostAddress String for host address
625 public void deleteHost(String strHostAddress) {
627 tbl.setTableName(STR_HOST_TABLE_NAME);
628 String strWhere = "HOSTADDRESS='" + strHostAddress + "';";
629 tbl.deleteEntry(strWhere);
630 System.out.println("IoTInstaller: Deleting a host from the system");
634 * A method to delete entity information from database
636 * @param strEntID String for entity ID
637 * @param strEntType String for entity type
638 * @param strEntName String for entity name
641 public void deleteEntity(String strEntID, String strEntType, String strEntName) {
643 // Delete from table IoTMain
644 tbl.setTableName(STR_MAIN_TABLE_NAME);
645 String strWhere = "ID='" + strEntID + "';";
646 tbl.deleteEntry(strWhere);
647 System.out.println("IoTInstaller: Removing entity from table " + STR_MAIN_TABLE_NAME);
648 // Delete from table with type name, e.g. Camera
649 tbl.setTableName(strEntType);
650 strWhere = "ID='" + strEntID + "';";
651 tbl.deleteEntry(strWhere);
652 System.out.println("IoTInstaller: Removing entity from table type: " + strEntType);
653 // Drop table if this was the last entry
654 if (tbl.isTableEmpty()) {
656 System.out.println("IoTInstaller: Dropping the table.. It was the last entry!");
659 // Drop the table that contains constructor information
660 tbl.setTableName(strEntName + strEntID);
662 System.out.println("IoTInstaller: Dropping class constructor table...");
664 System.out.println("IoTInstaller: Deleting an entry from the system...");
668 * A method to delete address information from database
670 * @param strTableName String for main table, i.e. IoTAddress, IoTDeviceAddress, or IoTZigbeeAddress
671 * @param strEntID String for entity ID, e.g. CM1
672 * @param strEntAddType String for entity address type, e.g. AmcrestCameraAdd
675 public void deleteAddress(String strTableName, String strEntID, String strEntAddType) {
677 // Delete from main table, e.g. IoTAddress, IoTDeviceAddress, or IoTZigbeeAddress
678 tbl.setTableName(strTableName);
679 String strWhere = "ID='" + strEntID + "';";
680 tbl.deleteEntry(strWhere);
681 System.out.println("IoTInstaller: Removing entity from table " + strTableName);
683 // Drop the table that contains constructor information
684 tbl.setTableName(strEntAddType + strEntID);
686 System.out.println("IoTInstaller: Dropping class constructor table...");
687 System.out.println("IoTInstaller: Deleting an entry from the system...");
692 * A method to install a pair of new devices with their communication pattern
694 * @param strFirstDeviceFile String that contains the file name of the fist device
695 * @param strSecondDeviceFile String that contains the file name of the second device
696 * @param strCommFile String that contains the file name of the communication file
699 public void installPairOfEntities(String strFirstEntityFile,
700 String strSecondEntityFile, String strCommFile) {
701 // TODO: NEED TO DO THE INPUT FAILURE CHECKING HERE
702 // NOW JUST ASSUME THAT THE INPUT FILES ARE GOOD
704 extractTableAndInstall(strFirstEntityFile);
705 extractTableAndInstall(strSecondEntityFile);
706 extractCommAndInstall(strCommFile);
710 * A method to output help messages
714 public void helpMessages() {
715 System.out.println();
716 System.out.println("IoTInstaller: Command line options:");
717 System.out.println("IoTInstaller: 1) Install one device, e.g. java iotinstaller.IoTInstaller -install_ent <filename>");
718 System.out.println("IoTInstaller: 2) Install comm pattern, e.g. java iotinstaller.IoTInstaller -install_comm <filename>");
719 System.out.print("IoTInstaller: 3) Install two devices and comm pattern, e.g. java iotinstaller.IoTInstaller ");
720 System.out.println("-install_comp <first_entity_filename> <second_entity_filename> <communication_filename>");
721 System.out.println("IoTInstaller: 4) Install address, e.g. java iotinstaller.IoTInstaller -install_add <filename>");
722 System.out.println("IoTInstaller: 5) Install device address, e.g. java iotinstaller.IoTInstaller -install_dev_add <filename>");
723 System.out.println("IoTInstaller: 6) Install zigbee device address, e.g. java iotinstaller.IoTInstaller -install_zb_add <filename>");
724 System.out.println("IoTInstaller: 7) Install host, e.g. java iotinstaller.IoTInstaller -install_host <filename>");
725 System.out.println("IoTInstaller: 8) Delete entity, e.g. java iotinstaller.IoTInstaller -delete_ent <ent_id> <ent_type> <ent_name>");
726 System.out.println("IoTInstaller: 9) Delete comm pattern, e.g. java iotinstaller.IoTInstaller -delete_comm <ent_id_source> <ent_id_dest>");
727 System.out.println("IoTInstaller: 10) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add <ent_id> <ent_type>");
728 System.out.println("IoTInstaller: 11) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add <ent_id> <ent_type>");
729 System.out.println("IoTInstaller: 12) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add <ent_id> <ent_type>");
730 System.out.println("IoTInstaller: 13) Delete host, e.g. java iotinstaller.IoTInstaller -delete_host <host_address>");
731 System.out.println("IoTInstaller: Type 'java iotinstaller.IoTInstaller -help' to display this help.");
732 System.out.println();
736 * A method to output help messages
740 public String helpMessagesString() {
741 String helpTxt = "\n";
742 helpTxt = helpTxt + "IoTInstaller: Command line options:\n";
743 helpTxt = helpTxt + "IoTInstaller: 1) Install one device, e.g. java iotinstaller.IoTInstaller -install_ent <filename>\n";
744 helpTxt = helpTxt + "IoTInstaller: 2) Install comm pattern, e.g. java iotinstaller.IoTInstaller -install_comm <filename>\n";
745 helpTxt = helpTxt + "IoTInstaller: 3) Install two devices and comm pattern, e.g. java iotinstaller.IoTInstaller ";
746 helpTxt = helpTxt + "-install_comp <first_entity_filename> <second_entity_filename> <communication_filename>\n";
747 helpTxt = helpTxt + "IoTInstaller: 4) Install address, e.g. java iotinstaller.IoTInstaller -install_add <filename>\n";
748 helpTxt = helpTxt + "IoTInstaller: 5) Install device address, e.g. java iotinstaller.IoTInstaller -install_dev_add <filename>\n";
749 helpTxt = helpTxt + "IoTInstaller: 6) Install zigbee device address, e.g. java iotinstaller.IoTInstaller -install_zb_add <filename>\n";
750 helpTxt = helpTxt + "IoTInstaller: 7) Install host, e.g. java iotinstaller.IoTInstaller -install_host <filename>\n";
751 helpTxt = helpTxt + "IoTInstaller: 8) Delete entity, e.g. java iotinstaller.IoTInstaller -delete_ent <ent_id> <ent_type> <ent_name>\n";
752 helpTxt = helpTxt + "IoTInstaller: 9) Delete comm pattern, e.g. java iotinstaller.IoTInstaller -delete_comm <ent_id_source> <ent_id_dest>\n";
753 helpTxt = helpTxt + "IoTInstaller: 10) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add <ent_id>\n";
754 helpTxt = helpTxt + "IoTInstaller: 11) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add <ent_id>\n";
755 helpTxt = helpTxt + "IoTInstaller: 12) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add <ent_id>\n";
756 helpTxt = helpTxt + "IoTInstaller: 13) Delete host, e.g. java iotinstaller.IoTInstaller -delete_host <host_address>\n";
757 helpTxt = helpTxt + "IoTInstaller: Type 'java iotinstaller.IoTInstaller -help' to display this help.\n";
758 helpTxt = helpTxt + "\n";
765 * @author Kevin Truong<kctruon1@uci.edu>
768 public static void formatWrite(String string, HashMap<String, ArrayList<HashSet<String>>> hashMap, HashMap<String, HashSet<String>> hashMap2, HashMap<String, String> hashMap3) throws IOException {
769 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string));
771 for (String string2 : hashMap2.keySet()) {
772 bufferedWriter.write("Type: \n");
773 bufferedWriter.write(string2 + '\n');
774 bufferedWriter.write("TAG: \n");
776 for (String string3 : hashMap2.get(string2)) {
777 String string4 = hashMap3.get(string3);
778 if (string4 != null && n == 0) {
779 string4 = string4.substring(0, 2);
780 bufferedWriter.write(string4 + '\n');
781 } else if (string4 == null && n == 0) {
782 bufferedWriter.write(string4 + '\n');
785 bufferedWriter.write("Subtype: \n");
786 bufferedWriter.write(string3 + '\n');
787 for (int i = 0; i < hashMap.get(string3).size(); ++i) {
789 bufferedWriter.write("AddressInfo:\n");
790 for (String string5 : hashMap.get(string3).get(0)) {
791 bufferedWriter.write(string5 + '\n');
795 bufferedWriter.write("DeviceInfo:\n");
796 for (String string5 : hashMap.get(string3).get(1)) {
797 bufferedWriter.write(string5 + '\n');
801 bufferedWriter.write("endType \n\n");
803 bufferedWriter.close();
806 public static void actualDeviceInfoWrite(ArrayList<String[][]> arrayList, String string) throws IOException {
807 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string));
808 for (String[][] arrstring : arrayList) {
809 for (int i = 0; i < arrstring.length; ++i) {
810 bufferedWriter.write(arrstring[i][1] + " " + arrstring[i][0] + '\n');
812 bufferedWriter.newLine();
814 bufferedWriter.close();
817 public static void configSetWrite(String string, ArrayList<String[][]> arrayList, HashMap<String, HashSet<String>> hashMap) throws IOException {
818 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string));
819 for (String string2 : hashMap.keySet()) {
821 bufferedWriter.write("START\n" + string2 + "\n");
822 block1 : for (String string3 : hashMap.get(string2)) {
823 for (String[][] arrstring : arrayList) {
824 if (!string3.equals(arrstring[0][1])) continue;
825 for (int i = 0; i < arrstring.length; ++i) {
826 bufferedWriter.write(arrstring[i][1] + " " + arrstring[i][0] + '\n');
831 bufferedWriter.write("END\n\n");
833 bufferedWriter.close();
836 public static HashMap<String, Integer> getNumAddresses(HashSet<String> hashSet) {
837 HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
838 Properties properties = new Properties();
839 for (String string : hashSet) {
840 String string2 = "../SupportedDevices/AddressInformation/" + string + ".config";
842 properties.load(new FileInputStream(string2));
844 catch (IOException iOException) {
845 iOException.printStackTrace();
847 int n = Integer.parseInt(properties.getProperty("ADDRESSES"));
848 hashMap.put(string, n);
853 public static HashMap<String, Integer> getZBAddresses(HashSet<String> hashSet) {
854 HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
855 Properties properties = new Properties();
856 for (String string : hashSet) {
857 String string2 = "../SupportedDevices/AddressInformation/" + string + ".config";
859 properties.load(new FileInputStream(string2));
861 catch (IOException iOException) {
862 iOException.printStackTrace();
864 int n = Integer.parseInt(properties.getProperty("ZBADDRESSES"));
865 hashMap.put(string, n);
870 public static void appendToFile(String string, String string2) {
872 FileWriter fileWriter = new FileWriter(string2, true);
873 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
874 PrintWriter printWriter = new PrintWriter(bufferedWriter);
875 printWriter.print(string);
877 bufferedWriter.close();
880 catch (IOException iOException) {
881 iOException.printStackTrace();
885 public static void overrideFileWrite(String string, String string2) {
887 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string2));
888 bufferedWriter.write(string);
889 bufferedWriter.close();
891 catch (IOException iOException) {
892 iOException.printStackTrace();
896 public static void formatForMultipleAddresses(String string, int n, int n2, int n3, HashMap<String, ArrayList<HashSet<String>>> hashMap, Properties properties, int n4, String string2) {
897 string = string + "TYPE_" + n + "_" + n2 + "_ADDRESS_FIELDS=";
899 for (int i = 0; i < n3; ++i) {
900 String string3 = new String();
901 for (String string4 : hashMap.get(string2).get(0)) {
903 string = string + string4 + " ";
905 string3 = string3 + string4 + "_" + n + "_" + n2 + "_ADD_" + i + "=" + properties.getProperty(new StringBuilder().append(string4).append("_").append(i + 1).toString()) + "\n";
908 string = string + "\n";
910 string = string + string3;
915 public static void getAddressInfo(String string, HashMap<String, ArrayList<HashSet<String>>> hashMap, HashMap<String, String> hashMap2, HashMap<String, HashSet<String>> hashMap3) {
916 Properties properties = new Properties();
918 Set set = new HashSet();
919 set = hashMap3.keySet();
920 IoTInstaller.overrideFileWrite((String)("NUM_OF_TYPES=" + set.size() + "\n\n"), (String)string);
921 for (String string2 : hashMap3.keySet()) {
924 String string3 = new String();
925 string3 = string3 + "TYPE_" + n + "=" + string2 + "\n";
926 for (String string4 : hashMap3.get(string2)) {
929 String string6 = "../SupportedDevices/AddressInformation/" + string4 + ".config";
931 properties.load(new FileInputStream(string6));
933 catch (IOException iOException) {
934 iOException.printStackTrace();
937 string3 = string3 + "TAG_" + n + "=" + hashMap2.get(string4).substring(0, 2) + "\n";
938 string3 = string3 + "SUBTYPE_" + n + "=" + hashMap3.get(string2).size() + "\n";
941 string3 = string3 + "TYPE_" + n + "_" + n2 + "=" + string4 + "\n";
942 int n5 = Integer.parseInt(properties.getProperty("ADDRESSES"));
943 int n6 = Integer.parseInt(properties.getProperty("ZBADDRESSES"));
944 string3 = string3 + "TYPE_" + n + "_" + n2 + "_NUM_OF_ZBADDRESSES=" + n6 + "\n";
946 string3 = string3 + "TYPE_" + n + "_" + n2 + "_ZBADDRESS_FIELDS=";
948 for (n4 = 0; n4 < n6; ++n4) {
949 string5 = new String();
950 for (String string7 : hashMap.get(string4).get(2)) {
952 string3 = string3 + string7 + " ";
954 string5 = string5 + "ZB" + string7 + "_" + n + "_" + n2 + "_ZBADD_" + n4 + "=" + properties.getProperty(new StringBuilder().append(string7).append("_").append(n4 + 1).toString()) + "\n";
957 string3 = string3 + "\n";
959 string3 = string3 + string5;
963 string3 = string3 + "TYPE_" + n + "_" + n2 + "_NUM_OF_ADDRESSES=" + n5 + "\n";
965 string3 = string3 + "TYPE_" + n + "_" + n2 + "_ADDRESS_FIELDS=";
967 for (n4 = 0; n4 < n5; ++n4) {
968 string5 = new String();
969 for (String string7 : hashMap.get(string4).get(0)) {
971 string3 = string3 + string7 + " ";
973 string5 = string5 + string7 + "_" + n + "_" + n2 + "_ADD_" + n4 + "=" + properties.getProperty(new StringBuilder().append(string7).append("_").append(n4 + 1).toString()) + "\n";
976 string3 = string3 + "\n";
978 string3 = string3 + string5;
982 n4 = Integer.parseInt(properties.getProperty("DEVICEINFO"));
983 string3 = string3 + "TYPE_" + n + "_" + n2 + "_NUM_OF_DEVICE_INFO=" + n4 + "\n";
985 string5 = new String();
987 for (String string7 : hashMap.get(string4).get(1)) {
988 if (string7.length() > 0) {
990 string3 = string3 + "TYPE_" + n + "_" + n2 + "_DEVICE_FIELDS=";
992 string3 = string3 + string7 + " ";
993 string5 = string5 + string7 + "_" + n + "_" + n2 + "=USER\n";
997 string3 = string3 + "\n";
999 string3 = string3 + string5;
1003 string3 = string3 + "\n";
1005 IoTInstaller.appendToFile((String)string3, (String)string);
1009 void getIoTDeviceAddressInfoAndOutput(HashMap<String, HashSet<String>> hashMap) {
1011 Table table = new Table("IoTDeviceAddress", false);
1012 String[][] arrstring = table.getGeneralDBTable();
1013 String string = new String();
1014 string = string + "START\n";
1015 string = string + "IoTDeviceAddress\n";
1016 for (n = 0; n < arrstring.length; ++n) {
1017 string = string + arrstring[n][1] + " " + arrstring[n][0] + "\n";
1019 string = string + "END\n\n";
1020 IoTInstaller.overrideFileWrite((String)string, (String)"IoTDeviceAddress.config");
1021 table = new Table("IoTZigbeeAddress", false);
1022 arrstring = table.getGeneralDBTable();
1023 string = new String();
1024 string = string + "START\n";
1025 string = string + "IoTZigbeeAddress\n";
1026 for (n = 0; n < arrstring.length; ++n) {
1027 string = string + arrstring[n][1] + " " + arrstring[n][0] + "\n";
1029 string = string + "END\n\n";
1030 IoTInstaller.appendToFile((String)string, (String)"IoTDeviceAddress.config");
1033 public void getInformationFromDatabase(String string) {
1035 System.out.println("Initializing -get...");
1036 ArrayList<String> arrayList = new ArrayList<String>();
1037 ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
1038 String string2 = new String("AddressInformation.config");
1039 String string3 = new String("SetList.config");
1040 String string4 = new String("DatabaseInformation.config");
1041 String string5 = new String("IDSubtypeInformation.config");
1042 HashMap<String, HashSet<String>> hashMap = new HashMap<String, HashSet<String>>();
1043 HashMap<String, ArrayList<HashSet<String>>> hashMap2 = new HashMap<String, ArrayList<HashSet<String>>>();
1044 HashMap<String, String> hashMap3 = new HashMap<String, String>();
1046 File file = new File(string);
1047 InputStream IS = new FileInputStream(file);
1048 scanner = new Scanner(IS);
1049 while (scanner.hasNext()) {
1050 arrayList.add(scanner.next());
1053 catch (FileNotFoundException fileNotFoundException) {
1054 System.out.print("File Error: ");
1055 fileNotFoundException.printStackTrace();
1058 for (int i = 0; i < arrayList.size(); ++i) {
1059 HashSet<String> object = new HashSet<String>();
1060 Table tbl = new Table(arrayList.get(i), false);
1061 String[][] arrstring = tbl.getGeneralDBTable();
1062 arrayList2.add(arrstring);
1064 for (int j = 0; j < arrstring.length; ++j) {
1065 object.add(arrstring[j][1]);
1067 hashMap.putIfAbsent(arrayList.get(i), object);
1068 HashMap<String, Integer> hashMap4 = IoTInstaller.getNumAddresses(object);
1069 HashMap<String, Integer> hashMap5 = IoTInstaller.getZBAddresses(object);
1070 for(String x : object){
1071 ArrayList<HashSet<String>> arrayList3 = new ArrayList<HashSet<String>>();
1072 for (int j = 0; j < arrstring.length; ++j) {
1073 if (!x.equals(arrstring[j][1])) continue;
1074 hashMap3.putIfAbsent(x, arrstring[j][0]);
1077 if (hashMap4.get(x) > 1) {
1078 tbl.setTableName(x + "Add1" + hashMap3.get(x));
1079 if (tbl.isTableExisting()) {
1080 tbl = new Table(x + "Add1" + hashMap3.get(x), false);
1081 arrayList3.add(tbl.getColumnNames());
1083 arrayList3.add(new HashSet<String>());
1085 hashMap2.putIfAbsent(x, arrayList3);
1087 tbl.setTableName(x + "Add" + hashMap3.get(x));
1088 if (tbl.isTableExisting()) {
1089 tbl = new Table(x + "Add" + hashMap3.get(x), false);
1090 arrayList3.add(tbl.getColumnNames());
1092 arrayList3.add(new HashSet<String>());
1095 tbl.setTableName(x + hashMap3.get(x));
1097 if (tbl.isTableExisting()) {
1098 tbl = new Table(x + hashMap3.get(x), false);
1099 arrayList3.add(tbl.getColumnNames());
1101 arrayList3.add(new HashSet<String>());
1103 if (hashMap5.get(x) != 0) {
1104 tbl.setTableName(x + "ZBAdd" + hashMap3.get(x));
1105 if (tbl.isTableExisting()) {
1106 tbl = new Table(x + "ZBAdd" + hashMap3.get(x), false);
1107 arrayList3.add(tbl.getColumnNames());
1109 arrayList3.add(new HashSet<String>());
1112 arrayList3.add(new HashSet<String>());
1114 hashMap2.putIfAbsent(x, arrayList3);
1117 IoTInstaller.getAddressInfo((String)string2, hashMap2, hashMap3, hashMap);
1118 this.getIoTDeviceAddressInfoAndOutput(hashMap);
1120 IoTInstaller.actualDeviceInfoWrite(arrayList2, (String)string5);
1121 IoTInstaller.configSetWrite((String)string3, arrayList2, hashMap);
1123 catch (IOException iOException) {
1124 iOException.printStackTrace();
1126 System.out.println("Done Executing -get");
1129 public void addNewType(String string) {
1130 System.out.println("Initializing -appendType...");
1131 IoTInstaller.appendToFile((String)string, (String)"AllDevices.config");
1132 System.out.println("Done Executing -appendType");
1135 public void updateSetList(String string) {
1138 System.out.println("Updating SetList...");
1139 String string2 = new String("SetList.config");
1140 ArrayList<String> arrayList = new ArrayList<String>();
1141 ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
1142 HashMap<String, HashSet<String>> hashMap = new HashMap<String, HashSet<String>>();
1144 File file = new File(string);
1145 InputStream IS = new FileInputStream(file);
1146 scanner = new Scanner(IS);
1147 while (scanner.hasNext()) {
1148 arrayList.add(scanner.next());
1151 catch (FileNotFoundException fileNotFoundException) {
1152 System.out.print("File Error: ");
1153 fileNotFoundException.printStackTrace();
1155 for (int i = 0; i < arrayList.size(); ++i) {
1156 HashSet<String> object = new HashSet<String>();
1157 tbl = new Table(arrayList.get(i), false);
1158 String[][] arrstring = tbl.getGeneralDBTable();
1159 arrayList2.add(arrstring);
1161 for (int j = 0; j < arrstring.length; ++j) {
1162 object.add(arrstring[j][1]);
1164 hashMap.putIfAbsent(arrayList.get(i), object);
1168 IoTInstaller.configSetWrite((String)string2, arrayList2, hashMap);
1170 catch (IOException iOException) {
1171 iOException.printStackTrace();
1173 System.out.println("Done Updating Setlist!");
1176 public void updateIoTDeviceAddress(String string) {
1179 System.out.println("Updating IoTDeviceAddress...");
1180 String string2 = new String("IoTDeviceAddress.config");
1181 ArrayList<String> arrayList = new ArrayList<String>(); // Holds types
1182 ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
1183 HashMap<String, HashSet<String>> hashMap = new HashMap<String, HashSet<String>>();
1185 File file = new File(string);
1186 InputStream IS = new FileInputStream(file);
1187 scanner = new Scanner(IS);
1188 while (scanner.hasNext()) {
1189 arrayList.add(scanner.next());
1192 catch (FileNotFoundException fileNotFoundException) {
1193 System.out.print("File Error: ");
1194 fileNotFoundException.printStackTrace();
1196 for (int i = 0; i < arrayList.size(); ++i) {
1197 HashSet<String> object = new HashSet<String>();
1198 tbl = new Table(arrayList.get(i), false);
1199 String[][] arrstring = tbl.getGeneralDBTable();
1200 arrayList2.add(arrstring);
1202 for (int j = 0; j < arrstring.length; ++j) {
1203 object.add(arrstring[j][1]);
1205 hashMap.putIfAbsent(arrayList.get(i), object);
1208 this.getIoTDeviceAddressInfoAndOutput(hashMap);
1210 System.out.println("Done Updating IoTDeviceAddress!");
1213 public void updateIDSub(String string) {
1214 FileInputStream fileInputStream;
1215 String[][] arrstring;
1218 System.out.println("Updating IDSubtype.config...");
1219 ArrayList<String> arrayList = new ArrayList<String>();
1220 ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
1222 File file = new File(string);
1223 InputStream IS = new FileInputStream(file);
1224 scanner = new Scanner(IS);
1225 while (scanner.hasNext()) {
1226 arrayList.add(scanner.next());
1229 catch (FileNotFoundException fileNotFoundException) {
1230 System.out.print("File Error: ");
1231 fileNotFoundException.printStackTrace();
1233 for (int i = 0; i < arrayList.size(); ++i) {
1234 tbl = new Table((String)arrayList.get(i), false);
1235 arrstring = tbl.getGeneralDBTable();
1236 arrayList2.add(arrstring);
1240 IoTInstaller.actualDeviceInfoWrite(arrayList2, (String)"IDSubtypeInformation.config");
1242 catch (IOException iOException) {
1243 iOException.printStackTrace();
1245 System.out.println("Done Updating!");
1248 // End of Kevin's code
1251 * Main method that accepts inputs for installation
1253 * @param args[0] String that contains the command line parameter
1254 * @param args[1] String that contains the first file name / entity ID / host address
1255 * @param args[2] String that contains the second file name
1256 * @param args[3] String that contains the third file name
1259 public static void main(String[] args) {
1261 // Testing IoTInstaller object
1262 IoTInstaller iotinst = new IoTInstaller();
1264 // TODO: PROBABLY NEED A BETTER ERROR HANDLING FOR INPUTS HERE
1265 // NOW ASSUME MINIMAL ERROR FOR INPUTS
1266 if (args.length > 0) {
1267 // Check for input parameters
1268 if (args[0].equals(STR_INSTALL_ENTITY_CMD)) {
1269 iotinst.extractTableAndInstall(args[1]);
1271 } else if (args[0].equals(STR_INSTALL_COMMUNICATION_CMD)) {
1272 iotinst.extractCommAndInstall(args[1]);
1274 } else if (args[0].equals(STR_INSTALL_COMPLETE_CMD)) {
1275 iotinst.installPairOfEntities(args[1], args[2], args[3]);
1277 } else if (args[0].equals(STR_INSTALL_ADDRESS_CMD)) {
1278 iotinst.installAddress(args[1]);
1280 } else if (args[0].equals(STR_INSTALL_DEV_ADDRESS_CMD)) {
1281 iotinst.installDeviceAddress(args[1]);
1283 } else if (args[0].equals(STR_INSTALL_ZB_ADDRESS_CMD)) {
1284 iotinst.installZigbeeAddress(args[1]);
1286 } else if (args[0].equals(STR_INSTALL_HOST_CMD)) {
1287 iotinst.installHost(args[1]);
1289 } else if (args[0].equals(STR_DELETE_ENTITY_CMD)) {
1290 iotinst.deleteEntity(args[1], args[2], args[3]);
1292 } else if (args[0].equals(STR_DELETE_COMMUNICATION_CMD)) {
1293 iotinst.deleteComm(args[1], args[2]);
1295 } else if (args[0].equals(STR_DELETE_ADDRESS_CMD)) {
1296 iotinst.deleteAddress(STR_ADDRESS_TABLE_NAME, args[1], args[2]);
1298 } else if (args[0].equals(STR_DELETE_DEV_ADD_CMD)) {
1299 iotinst.deleteAddress(STR_DEV_ADD_TABLE_NAME, args[1], args[2]);
1301 } else if (args[0].equals(STR_DELETE_ZB_ADD_CMD)) {
1302 iotinst.deleteAddress(STR_ZB_ADD_TABLE_NAME, args[1], args[2]);
1304 } else if (args[0].equals(STR_DELETE_HOST_CMD)) {
1305 iotinst.deleteHost(args[1]);
1307 } else if (args[0].equals(STR_HELP_CMD)) {
1308 iotinst.helpMessages();
1310 } else if (args[0].equals("-get")) {
1311 iotinst.getInformationFromDatabase("../SupportedDevices/"+args[1]);
1313 } else if (args[0].equals("-appendType")) {
1314 iotinst.addNewType("../SupportedDevices/"+args[1]);
1316 } else if (args[0].equals("-updateIoTDeviceAddress")) {
1317 iotinst.updateIoTDeviceAddress("../SupportedDevices/"+args[1]);
1319 } else if (args[0].equals("-updateSetList")) {
1320 iotinst.updateSetList("../SupportedDevices/"+args[1]);
1322 } else if (args[0].equals("-updateIDSubtype")) {
1323 iotinst.updateIDSub("../SupportedDevices/"+args[1]);
1326 System.out.println("IoTInstaller: ERROR: Wrong input parameters!");
1327 iotinst.helpMessages();
1331 System.out.println("IoTInstaller: ERROR: No input parameters detected!");
1332 iotinst.helpMessages();