From 79298f7386842b764e79f806a56bf67c7b88aaaa Mon Sep 17 00:00:00 2001 From: rtrimana Date: Tue, 26 Sep 2017 12:43:02 -0700 Subject: [PATCH] Minor changes to tables for device registration phone app integration; these changes make methods more generic to call --- iotjava/iotinstaller/IoTInstaller.java | 414 ++++++++++++++----------- iotjava/iotinstaller/Table.java | 13 +- 2 files changed, 247 insertions(+), 180 deletions(-) diff --git a/iotjava/iotinstaller/IoTInstaller.java b/iotjava/iotinstaller/IoTInstaller.java index 82748e8..a0c8f4d 100644 --- a/iotjava/iotinstaller/IoTInstaller.java +++ b/iotjava/iotinstaller/IoTInstaller.java @@ -83,6 +83,130 @@ public final 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 + *

+ * 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 + * 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 final 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 final class IoTInstaller { } } - /** * A method to extract simple addresses information, e.g. www.google.com *

@@ -666,7 +699,7 @@ public final 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 "); @@ -678,14 +711,41 @@ public final class IoTInstaller { System.out.println("IoTInstaller: 6) Install zigbee device address, e.g. java iotinstaller.IoTInstaller -install_zb_add "); System.out.println("IoTInstaller: 7) Install host, e.g. java iotinstaller.IoTInstaller -install_host "); System.out.println("IoTInstaller: 8) Delete entity, e.g. java iotinstaller.IoTInstaller -delete_ent "); - System.out.println("IoTInstaller: 9) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add "); - System.out.println("IoTInstaller: 10) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add "); - System.out.println("IoTInstaller: 11) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add "); + System.out.println("IoTInstaller: 9) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add "); + System.out.println("IoTInstaller: 10) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add "); + System.out.println("IoTInstaller: 11) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add "); System.out.println("IoTInstaller: 12) Delete host, e.g. java iotinstaller.IoTInstaller -delete_host "); 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 \n"; + helpTxt = helpTxt + "IoTInstaller: 2) Install comm pattern, e.g. java iotinstaller.IoTInstaller -install_comm \n"; + helpTxt = helpTxt + "IoTInstaller: 3) Install two devices and comm pattern, e.g. java iotinstaller.IoTInstaller "; + helpTxt = helpTxt + "-install_comp \n"; + helpTxt = helpTxt + "IoTInstaller: 4) Install address, e.g. java iotinstaller.IoTInstaller -install_add \n"; + helpTxt = helpTxt + "IoTInstaller: 5) Install device address, e.g. java iotinstaller.IoTInstaller -install_dev_add \n"; + helpTxt = helpTxt + "IoTInstaller: 6) Install zigbee device address, e.g. java iotinstaller.IoTInstaller -install_zb_add \n"; + helpTxt = helpTxt + "IoTInstaller: 7) Install host, e.g. java iotinstaller.IoTInstaller -install_host \n"; + helpTxt = helpTxt + "IoTInstaller: 8) Delete entity, e.g. java iotinstaller.IoTInstaller -delete_ent \n"; + helpTxt = helpTxt + "IoTInstaller: 9) Delete address, e.g. java iotinstaller.IoTInstaller -delete_add \n"; + helpTxt = helpTxt + "IoTInstaller: 10) Delete device address, e.g. java iotinstaller.IoTInstaller -delete_dev_add \n"; + helpTxt = helpTxt + "IoTInstaller: 11) Delete zigbee device address, e.g. java iotinstaller.IoTInstaller -delete_zb_add \n"; + helpTxt = helpTxt + "IoTInstaller: 12) Delete host, e.g. java iotinstaller.IoTInstaller -delete_host \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 * diff --git a/iotjava/iotinstaller/Table.java b/iotjava/iotinstaller/Table.java index 8e514d6..c3fa5ab 100644 --- a/iotjava/iotinstaller/Table.java +++ b/iotjava/iotinstaller/Table.java @@ -234,11 +234,18 @@ public class Table { */ public boolean isTableEmpty() { - if (rs == null) { - return true; + // Check if this table has any entries + String strCommand = "SELECT * FROM " + strTableName; + rs = sqlInterface.sqlCommandQuery(strCommand); + try { + if (!rs.first()) { + return true; + } + } catch(SQLException ex) { + System.out.println("Table: Exception: "); + ex.printStackTrace(); } return false; - } /** -- 2.34.1