Minor changes to tables for device registration phone app integration; these changes...
[iot2.git] / iotjava / iotinstaller / IoTInstaller.java
index 4d3dcb6c409f910dc214b115cf5a32d802cd1e33..a0c8f4d710740b6875f1f3c9cc1c9ed8afc11a4a 100644 (file)
@@ -15,7 +15,7 @@ import java.util.Properties;
  * @version     1.0
  * @since       2015-12-01
  */
-public class IoTInstaller {
+public final class IoTInstaller {
 
        /**
         * IoTInstaller class properties
@@ -83,6 +83,130 @@ public class IoTInstaller {
                tbl.insertEntry(strFlds);
                System.out.println("IoTInstaller: Inserting a new entry into main table");
        }
+       
+       /**
+        * A method to extract device/entity information from the user
+        * <p>
+        * Users are supposed to supply the information needed for installation
+        * This is the core of the operation after getting the Scanner object.
+        *
+        * @param  scanFile     Scanner object of source
+        * @return              void
+        */
+       public void extractTableAndInstallCore(Scanner scanFile) {
+
+               // Parse configuration file
+               // Assumption here is that .config file is written with the correct syntax (need typechecking)
+               //File file = new File(strCfgFileName);
+               //Scanner scanFile = new Scanner(new FileReader(file));
+               //System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
+
+               // Initialize String for ID and TYPE
+               String strID = "";
+               String strType = "";
+               String strTypeSpecific = "";
+
+               // Initialize TableProperty for devices and specific devices
+               // We have 2 tables,
+               // e.g. ProximitySensor - table of many ProximitySensor devices
+               //      ProximitySensorBrandA - table that contains the constructor
+               //                              information for a specific device
+               TableProperty[] tpDevice = new TableProperty[1];
+               TableProperty[] tpDeviceSpecific = new TableProperty[1];
+
+               // Initialize array of string
+               String[] strFields = new String[1];
+               String[] strFieldsSpecific = new String[1];
+
+               // String for scanning the file
+               String strScan = "";
+
+               // Store number of fields here
+               int iFields = 0;
+               while (scanFile.hasNext()) {
+
+                       strScan = scanFile.next();
+                       if (strScan.equals("IoTMain")) {
+
+                               while (scanFile.hasNext()) {
+                                       strScan = scanFile.next();
+
+                                       // Get ID
+                                       if (strScan.equals("ID")) {
+                                               strID = scanFile.next();
+                                       }
+                                       // Get TYPE
+                                       else if (strScan.equals("TYPE")) {
+                                               strType = scanFile.next();
+                                       }
+                                       // Get TYPE
+                                       else if (strScan.equals("TYPESPECIFIC")) {
+                                               strTypeSpecific = scanFile.next();
+                                       } else if (strScan.equals("END")) {
+                                               // Break out of loop
+                                               break;
+                                       }
+                               }
+                       } else if (strScan.equals("Table")) {
+
+                               // Get number of fields, e.g. Table 3
+                               iFields = scanFile.nextInt();
+
+                               // We have device ID and device specific names
+                               // e.g. ID = PS1; TYPE
+                               tpDevice = new TableProperty[2];
+                               tpDevice[0] = new TableProperty();
+                               tpDevice[0].setField("ID");
+                               tpDevice[0].setType("VARCHAR");
+                               tpDevice[0].setLength("5");
+                               tpDevice[1] = new TableProperty();
+                               tpDevice[1].setField("TYPE");
+                               tpDevice[1].setType("VARCHAR");
+                               tpDevice[1].setLength("30");
+
+                               // Prepare properties for a specific device
+                               tpDeviceSpecific = new TableProperty[iFields];
+                               for (int i=0; i<iFields; i++) {
+                                       tpDeviceSpecific[i] = new TableProperty();
+
+                                       // Looping over the fields
+                                       strScan = scanFile.next();
+                                       tpDeviceSpecific[i].setField(strScan);
+                                       strScan = scanFile.next();
+                                       tpDeviceSpecific[i].setType(strScan);
+                                       strScan = scanFile.next();
+                                       tpDeviceSpecific[i].setLength(strScan);
+                               }
+                       } else if (strScan.equals("Data")) {
+
+                               // Get the device information
+                               strFields = new String[2];
+                               strFields[0] = strID;
+                               strFields[1] = strTypeSpecific;
+
+                               if ((tpDeviceSpecific.length == 1) &&
+                                               (tpDeviceSpecific[0].getField().equals("EMPTY"))) {
+
+                                       // Get the fields for specific device
+                                       strFieldsSpecific = null;
+                                       System.out.println("IoTInstaller: Empty constructor for: " + strTypeSpecific);
+
+                               } else {
+
+                                       // Get the fields for specific device
+                                       strFieldsSpecific = new String[iFields];
+                                       for (int i=0; i<iFields; i++) {
+                                               strScan = scanFile.next();
+                                               strFieldsSpecific[i] = strScan;
+                                       }
+                               }
+                       }
+               }
+
+               installNewEntity(strType, strTypeSpecific, strID, tpDevice,
+                                                                                tpDeviceSpecific, strFields, strFieldsSpecific);
+               System.out.println("IoTInstaller: Installing a new entity/device into the system");
+       }
 
        /**
         * A method to extract device/entity information from the user
@@ -103,111 +227,7 @@ public class IoTInstaller {
                        Scanner scanFile = new Scanner(new FileReader(file));
                        System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
 
-                       // Initialize String for ID and TYPE
-                       String strID = "";
-                       String strType = "";
-                       String strTypeSpecific = "";
-
-                       // Initialize TableProperty for devices and specific devices
-                       // We have 2 tables,
-                       // e.g. ProximitySensor - table of many ProximitySensor devices
-                       //      ProximitySensorBrandA - table that contains the constructor
-                       //                              information for a specific device
-                       TableProperty[] tpDevice = new TableProperty[1];
-                       TableProperty[] tpDeviceSpecific = new TableProperty[1];
-
-                       // Initialize array of string
-                       String[] strFields = new String[1];
-                       String[] strFieldsSpecific = new String[1];
-
-                       // String for scanning the file
-                       String strScan = "";
-
-                       // Store number of fields here
-                       int iFields = 0;
-                       while (scanFile.hasNext()) {
-
-                               strScan = scanFile.next();
-                               if (strScan.equals("IoTMain")) {
-
-                                       while (scanFile.hasNext()) {
-                                               strScan = scanFile.next();
-
-                                               // Get ID
-                                               if (strScan.equals("ID")) {
-                                                       strID = scanFile.next();
-                                               }
-                                               // Get TYPE
-                                               else if (strScan.equals("TYPE")) {
-                                                       strType = scanFile.next();
-                                               }
-                                               // Get TYPE
-                                               else if (strScan.equals("TYPESPECIFIC")) {
-                                                       strTypeSpecific = scanFile.next();
-                                               } else if (strScan.equals("END")) {
-                                                       // Break out of loop
-                                                       break;
-                                               }
-                                       }
-                               } else if (strScan.equals("Table")) {
-
-                                       // Get number of fields, e.g. Table 3
-                                       iFields = scanFile.nextInt();
-
-                                       // We have device ID and device specific names
-                                       // e.g. ID = PS1; TYPE
-                                       tpDevice = new TableProperty[2];
-                                       tpDevice[0] = new TableProperty();
-                                       tpDevice[0].setField("ID");
-                                       tpDevice[0].setType("VARCHAR");
-                                       tpDevice[0].setLength("5");
-                                       tpDevice[1] = new TableProperty();
-                                       tpDevice[1].setField("TYPE");
-                                       tpDevice[1].setType("VARCHAR");
-                                       tpDevice[1].setLength("30");
-
-                                       // Prepare properties for a specific device
-                                       tpDeviceSpecific = new TableProperty[iFields];
-                                       for (int i=0; i<iFields; i++) {
-                                               tpDeviceSpecific[i] = new TableProperty();
-
-                                               // Looping over the fields
-                                               strScan = scanFile.next();
-                                               tpDeviceSpecific[i].setField(strScan);
-                                               strScan = scanFile.next();
-                                               tpDeviceSpecific[i].setType(strScan);
-                                               strScan = scanFile.next();
-                                               tpDeviceSpecific[i].setLength(strScan);
-                                       }
-                               } else if (strScan.equals("Data")) {
-
-                                       // Get the device information
-                                       strFields = new String[2];
-                                       strFields[0] = strID;
-                                       strFields[1] = strTypeSpecific;
-
-                                       if ((tpDeviceSpecific.length == 1) &&
-                                                       (tpDeviceSpecific[0].getField().equals("EMPTY"))) {
-
-                                               // Get the fields for specific device
-                                               strFieldsSpecific = null;
-                                               System.out.println("IoTInstaller: Empty constructor for: " + strTypeSpecific);
-
-                                       } else {
-
-                                               // Get the fields for specific device
-                                               strFieldsSpecific = new String[iFields];
-                                               for (int i=0; i<iFields; i++) {
-                                                       strScan = scanFile.next();
-                                                       strFieldsSpecific[i] = strScan;
-                                               }
-                                       }
-                               }
-                       }
-
-                       installNewEntity(strType, strTypeSpecific, strID, tpDevice,
-                                                                                        tpDeviceSpecific, strFields, strFieldsSpecific);
-                       System.out.println("IoTInstaller: Installing a new entity/device into the system");
+            extractTableAndInstallCore(scanFile);
 
                } catch (FileNotFoundException ex) {
 
@@ -314,6 +334,86 @@ public class IoTInstaller {
 
                }
        }
+       
+       /**
+        * A method to extract device/entity addresses information
+        * <p>
+        * Users are supposed to supply the information needed for installation
+        *
+        * @param  is   InputStream of the input source
+        * @return      void
+        */
+       public void installDeviceAddressCore(InputStream is) {
+
+               Properties prop = new Properties();
+               try {
+                       prop.load(is);
+               } catch (IOException ex) {
+                       ex.printStackTrace();
+               }
+               // Initialize string
+               // We can only install one device address per one time with the following sequence
+               String[] strFields = new String[2];
+               String[] strFieldsAddress = null;
+               // Check for wildcard feature
+               if ((prop.getProperty("SOURCEWILDCARD", null) != null) &&
+                       (prop.getProperty("DESTWILDCARD", null) != null)) {
+                       strFieldsAddress = new String[5];
+                       strFieldsAddress[3] = prop.getProperty("SOURCEWILDCARD");
+                       strFieldsAddress[4] = prop.getProperty("DESTWILDCARD");
+               } else {
+                       strFieldsAddress = new String[3];
+               }
+               strFields[0] = prop.getProperty("ID");
+               strFields[1] = prop.getProperty("ADDRESSFOR");
+               strFieldsAddress[0] = prop.getProperty("DEVICEADDRESS");
+               strFieldsAddress[1] = prop.getProperty("PORTNUMBER");
+               strFieldsAddress[2] = prop.getProperty("PROTOCOL");
+
+               // Insert this entry into the main device address table
+               tbl.setTableName(STR_DEV_ADD_TABLE_NAME);
+               tbl.insertEntry(strFields);
+
+               // Create a new table for a specific device address
+               // e.g. AmcrestCameraAdd + CM1 = AmcrestCameraAddCM1
+               tbl.setTableName(strFields[1] + strFields[0]);
+
+               // Table does not exist yet
+               // Set TableProperty for device address (MAC address)
+               TableProperty[] tp = null;
+               // Check for wildcard feature
+               if (strFieldsAddress.length == 5) {
+                       tp = new TableProperty[5];
+                       tp[3] = new TableProperty();
+                       tp[3].setField("SOURCEWILDCARD");
+                       tp[3].setType("VARCHAR");
+                       tp[3].setLength("5");
+                       tp[4] = new TableProperty();
+                       tp[4].setField("DESTWILDCARD");
+                       tp[4].setType("VARCHAR");
+                       tp[4].setLength("5");
+               } else {
+                       tp = new TableProperty[3];
+               }
+               tp[0] = new TableProperty();
+               tp[0].setField("DEVICEADDRESS");
+               tp[0].setType("VARCHAR");
+               tp[0].setLength("20");
+               tp[1] = new TableProperty();
+               tp[1].setField("PORTNUMBER");
+               tp[1].setType("INT");
+               tp[1].setLength("11");
+               tp[2] = new TableProperty();
+               tp[2].setField("PROTOCOL");
+               tp[2].setType("VARCHAR");
+               tp[2].setLength("5");
+               tbl.createTable(tp, "DEVICEADDRESS");
+
+               // Insert new address entry
+               tbl.insertEntry(strFieldsAddress);
+
+               System.out.println("IoTInstaller: Installing a new device/entity address into the system");
+       }
 
        /**
         * A method to extract device/entity addresses information
@@ -328,77 +428,11 @@ public class IoTInstaller {
                try {
 
                        // Parse configuration file
-                       Properties prop = new Properties();
                        File file = new File(strCfgFileName);
                        FileInputStream fis = new FileInputStream(file);
-                       try {
-                               prop.load(fis);
-                       } catch (IOException ex) {
-                               ex.printStackTrace();
-                       }
                        System.out.println("IoTInstaller: Extracting information from config file: " + strCfgFileName);
-                       // Initialize string
-                       // We can only install one device address per one time with the following sequence
-                       String[] strFields = new String[2];
-                       String[] strFieldsAddress = null;
-                       // Check for wildcard feature
-                       if ((prop.getProperty("SOURCEWILDCARD", null) != null) &&
-                               (prop.getProperty("DESTWILDCARD", null) != null)) {
-                               strFieldsAddress = new String[5];
-                               strFieldsAddress[3] = prop.getProperty("SOURCEWILDCARD");
-                               strFieldsAddress[4] = prop.getProperty("DESTWILDCARD");
-                       } else {
-                               strFieldsAddress = new String[3];
-                       }
-                       strFields[0] = prop.getProperty("ID");
-                       strFields[1] = prop.getProperty("ADDRESSFOR");
-                       strFieldsAddress[0] = prop.getProperty("DEVICEADDRESS");
-                       strFieldsAddress[1] = prop.getProperty("PORTNUMBER");
-                       strFieldsAddress[2] = prop.getProperty("PROTOCOL");
 
-                       // Insert this entry into the main device address table
-                       tbl.setTableName(STR_DEV_ADD_TABLE_NAME);
-                       tbl.insertEntry(strFields);
-
-                       // Create a new table for a specific device address
-                       // e.g. AmcrestCameraAdd + CM1 = AmcrestCameraAddCM1
-                       tbl.setTableName(strFields[1] + strFields[0]);
-
-                       // Table does not exist yet
-                       // Set TableProperty for device address (MAC address)
-                       TableProperty[] tp = null;
-                       // Check for wildcard feature
-                       if (strFieldsAddress.length == 5) {
-                               tp = new TableProperty[5];
-                               tp[3] = new TableProperty();
-                               tp[3].setField("SOURCEWILDCARD");
-                               tp[3].setType("VARCHAR");
-                               tp[3].setLength("5");
-                               tp[4] = new TableProperty();
-                               tp[4].setField("DESTWILDCARD");
-                               tp[4].setType("VARCHAR");
-                               tp[4].setLength("5");
-                       } else {
-                               tp = new TableProperty[3];
-                       }
-                       tp[0] = new TableProperty();
-                       tp[0].setField("DEVICEADDRESS");
-                       tp[0].setType("VARCHAR");
-                       tp[0].setLength("20");
-                       tp[1] = new TableProperty();
-                       tp[1].setField("PORTNUMBER");
-                       tp[1].setType("INT");
-                       tp[1].setLength("11");
-                       tp[2] = new TableProperty();
-                       tp[2].setField("PROTOCOL");
-                       tp[2].setType("VARCHAR");
-                       tp[2].setLength("5");
-                       tbl.createTable(tp, "DEVICEADDRESS");
-
-                       // Insert new address entry
-                       tbl.insertEntry(strFieldsAddress);
-
-                       System.out.println("IoTInstaller: Installing a new device/entity address into the system");
+                       installDeviceAddressCore(fis);
 
                } catch (FileNotFoundException ex) {
 
@@ -468,7 +502,6 @@ public class IoTInstaller {
                }
        }
 
-
        /**
         * A method to extract simple addresses information, e.g. www.google.com
         * <p>
@@ -666,7 +699,7 @@ public class IoTInstaller {
         *
         * @return void
         */
-       private void helpMessages() {
+       public void helpMessages() {
                System.out.println();
                System.out.println("IoTInstaller: Command line options:");
                System.out.println("IoTInstaller: 1) Install one device, e.g. java iotinstaller.IoTInstaller -install_ent <filename>");
@@ -678,14 +711,41 @@ public class IoTInstaller {
                System.out.println("IoTInstaller: 6) Install zigbee device address, e.g. java iotinstaller.IoTInstaller -install_zb_add <filename>");
                System.out.println("IoTInstaller: 7) Install host, e.g. java iotinstaller.IoTInstaller -install_host <filename>");
                System.out.println("IoTInstaller: 8) Delete entity, e.g. java iotinstaller.IoTInstaller -delete_ent <ent_id> <ent_type> <ent_name>");
-               System.out.println("IoTInstaller: 9) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add <ent_id>");
-               System.out.println("IoTInstaller: 10) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add <ent_id>");
-               System.out.println("IoTInstaller: 11) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add <ent_id>");
+               System.out.println("IoTInstaller: 9) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add <ent_id> <ent_type>");
+               System.out.println("IoTInstaller: 10) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add <ent_id> <ent_type>");
+               System.out.println("IoTInstaller: 11) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add <ent_id> <ent_type>");
                System.out.println("IoTInstaller: 12) Delete host, e.g. java iotinstaller.IoTInstaller -delete_host <host_address>");
                System.out.println("IoTInstaller: Type 'java iotinstaller.IoTInstaller -help' to display this help.");
                System.out.println();
        }
 
+       /**
+        * A method to output help messages
+        *
+        * @return void
+        */
+       public String helpMessagesString() {
+               String helpTxt = "\n";
+               helpTxt = helpTxt + "IoTInstaller: Command line options:\n";
+               helpTxt = helpTxt + "IoTInstaller: 1) Install one device, e.g. java iotinstaller.IoTInstaller -install_ent <filename>\n";
+               helpTxt = helpTxt + "IoTInstaller: 2) Install comm pattern, e.g. java iotinstaller.IoTInstaller -install_comm <filename>\n";
+               helpTxt = helpTxt + "IoTInstaller: 3) Install two devices and comm pattern, e.g. java iotinstaller.IoTInstaller ";
+               helpTxt = helpTxt + "-install_comp <first_entity_filename> <second_entity_filename> <communication_filename>\n";
+               helpTxt = helpTxt + "IoTInstaller: 4) Install address, e.g. java iotinstaller.IoTInstaller -install_add <filename>\n";
+               helpTxt = helpTxt + "IoTInstaller: 5) Install device address, e.g. java iotinstaller.IoTInstaller -install_dev_add <filename>\n";
+               helpTxt = helpTxt + "IoTInstaller: 6) Install zigbee device address, e.g. java iotinstaller.IoTInstaller -install_zb_add <filename>\n";
+               helpTxt = helpTxt + "IoTInstaller: 7) Install host, e.g. java iotinstaller.IoTInstaller -install_host <filename>\n";
+               helpTxt = helpTxt + "IoTInstaller: 8) Delete entity, e.g. java iotinstaller.IoTInstaller -delete_ent <ent_id> <ent_type> <ent_name>\n";
+               helpTxt = helpTxt + "IoTInstaller: 9) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add <ent_id>\n";
+               helpTxt = helpTxt + "IoTInstaller: 10) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add <ent_id>\n";
+               helpTxt = helpTxt + "IoTInstaller: 11) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add <ent_id>\n";
+               helpTxt = helpTxt + "IoTInstaller: 12) Delete host, e.g. java iotinstaller.IoTInstaller -delete_host <host_address>\n";
+               helpTxt = helpTxt + "IoTInstaller: Type 'java iotinstaller.IoTInstaller -help' to display this help.\n";
+               helpTxt = helpTxt + "\n";
+
+               return helpTxt;
+       }
+
        /**
         * Main method that accepts inputs for installation
         *