From e8f257f4ed273d38e6d35a9126854f361b4c3f22 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Tue, 26 Jun 2018 16:26:37 -0700 Subject: [PATCH] More features for Android app access to device installation, etc. --- iotjava/iotinstaller/IoTInstaller.java | 516 ++++++++++++++++++++++++- iotjava/iotinstaller/Table.java | 29 ++ 2 files changed, 542 insertions(+), 3 deletions(-) diff --git a/iotjava/iotinstaller/IoTInstaller.java b/iotjava/iotinstaller/IoTInstaller.java index e281c9e..a9e5d88 100644 --- a/iotjava/iotinstaller/IoTInstaller.java +++ b/iotjava/iotinstaller/IoTInstaller.java @@ -8,6 +8,10 @@ import java.io.*; import java.sql.*; import java.util.Scanner; import java.util.Properties; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; /** A class that creates an object for IoT device/entity installation into database * @@ -48,6 +52,11 @@ public final class IoTInstaller { private static final String STR_DELETE_ZB_ADD_CMD = "-delete_zb_add"; private static final String STR_DELETE_HOST_CMD = "-delete_host"; private static final String STR_HELP_CMD = "-help"; + private static final String DATABASE_GET = "-get"; + private static final String APPEND_TYPE = "-appendType"; + private static final String UPDATE_SETLIST = "-updateSetList"; + private static final String UPDATE_IOTDEVICEADDRESS = "-updateIoTDeviceAddress"; + private static final String UPDATE_IDSUBTYPE = "-updateIDSubtype"; /** * Class constructor @@ -654,7 +663,7 @@ public final class IoTInstaller { System.out.println("IoTInstaller: Deleting an entry from the system..."); } - + /** * A method to delete address information from database * @@ -750,6 +759,493 @@ public final class IoTInstaller { return helpTxt; } + + /** + * Kevin's Code + * @author Kevin Truong + * */ + + public static void formatWrite(String string, HashMap>> hashMap, HashMap> hashMap2, HashMap hashMap3) throws IOException { + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string)); + + for (String string2 : hashMap2.keySet()) { + bufferedWriter.write("Type: \n"); + bufferedWriter.write(string2 + '\n'); + bufferedWriter.write("TAG: \n"); + int n = 0; + for (String string3 : hashMap2.get(string2)) { + String string4 = hashMap3.get(string3); + if (string4 != null && n == 0) { + string4 = string4.substring(0, 2); + bufferedWriter.write(string4 + '\n'); + } else if (string4 == null && n == 0) { + bufferedWriter.write(string4 + '\n'); + } + ++n; + bufferedWriter.write("Subtype: \n"); + bufferedWriter.write(string3 + '\n'); + for (int i = 0; i < hashMap.get(string3).size(); ++i) { + if (i == 0) { + bufferedWriter.write("AddressInfo:\n"); + for (String string5 : hashMap.get(string3).get(0)) { + bufferedWriter.write(string5 + '\n'); + } + continue; + } + bufferedWriter.write("DeviceInfo:\n"); + for (String string5 : hashMap.get(string3).get(1)) { + bufferedWriter.write(string5 + '\n'); + } + } + } + bufferedWriter.write("endType \n\n"); + } + bufferedWriter.close(); + } + + public static void actualDeviceInfoWrite(ArrayList arrayList, String string) throws IOException { + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string)); + for (String[][] arrstring : arrayList) { + for (int i = 0; i < arrstring.length; ++i) { + bufferedWriter.write(arrstring[i][1] + " " + arrstring[i][0] + '\n'); + } + bufferedWriter.newLine(); + } + bufferedWriter.close(); + } + + public static void configSetWrite(String string, ArrayList arrayList, HashMap> hashMap) throws IOException { + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string)); + for (String string2 : hashMap.keySet()) { + boolean bl = false; + bufferedWriter.write("START\n" + string2 + "\n"); + block1 : for (String string3 : hashMap.get(string2)) { + for (String[][] arrstring : arrayList) { + if (!string3.equals(arrstring[0][1])) continue; + for (int i = 0; i < arrstring.length; ++i) { + bufferedWriter.write(arrstring[i][1] + " " + arrstring[i][0] + '\n'); + } + continue block1; + } + } + bufferedWriter.write("END\n\n"); + } + bufferedWriter.close(); + } + + public static HashMap getNumAddresses(HashSet hashSet) { + HashMap hashMap = new HashMap(); + Properties properties = new Properties(); + for (String string : hashSet) { + String string2 = "../SupportedDevices/AddressInformation/" + string + ".config"; + try { + properties.load(new FileInputStream(string2)); + } + catch (IOException iOException) { + iOException.printStackTrace(); + } + int n = Integer.parseInt(properties.getProperty("ADDRESSES")); + hashMap.put(string, n); + } + return hashMap; + } + + public static HashMap getZBAddresses(HashSet hashSet) { + HashMap hashMap = new HashMap(); + Properties properties = new Properties(); + for (String string : hashSet) { + String string2 = "../SupportedDevices/AddressInformation/" + string + ".config"; + try { + properties.load(new FileInputStream(string2)); + } + catch (IOException iOException) { + iOException.printStackTrace(); + } + int n = Integer.parseInt(properties.getProperty("ZBADDRESSES")); + hashMap.put(string, n); + } + return hashMap; + } + + public static void appendToFile(String string, String string2) { + try { + FileWriter fileWriter = new FileWriter(string2, true); + BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); + PrintWriter printWriter = new PrintWriter(bufferedWriter); + printWriter.print(string); + printWriter.close(); + bufferedWriter.close(); + fileWriter.close(); + } + catch (IOException iOException) { + iOException.printStackTrace(); + } + } + + public static void overrideFileWrite(String string, String string2) { + try { + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(string2)); + bufferedWriter.write(string); + bufferedWriter.close(); + } + catch (IOException iOException) { + iOException.printStackTrace(); + } + } + + public static void formatForMultipleAddresses(String string, int n, int n2, int n3, HashMap>> hashMap, Properties properties, int n4, String string2) { + string = string + "TYPE_" + n + "_" + n2 + "_ADDRESS_FIELDS="; + n4 = 0; + for (int i = 0; i < n3; ++i) { + String string3 = new String(); + for (String string4 : hashMap.get(string2).get(0)) { + if (n4 == 0) { + string = string + string4 + " "; + } + string3 = string3 + string4 + "_" + n + "_" + n2 + "_ADD_" + i + "=" + properties.getProperty(new StringBuilder().append(string4).append("_").append(i + 1).toString()) + "\n"; + } + if (n4 == 0) { + string = string + "\n"; + } + string = string + string3; + ++n4; + } + } + + public static void getAddressInfo(String string, HashMap>> hashMap, HashMap hashMap2, HashMap> hashMap3) { + Properties properties = new Properties(); + int n = 0; + Set set = new HashSet(); + set = hashMap3.keySet(); + IoTInstaller.overrideFileWrite((String)("NUM_OF_TYPES=" + set.size() + "\n\n"), (String)string); + for (String string2 : hashMap3.keySet()) { + int n2 = 0; + int n3 = 0; + String string3 = new String(); + string3 = string3 + "TYPE_" + n + "=" + string2 + "\n"; + for (String string4 : hashMap3.get(string2)) { + int n4; + String string5; + String string6 = "../SupportedDevices/AddressInformation/" + string4 + ".config"; + try { + properties.load(new FileInputStream(string6)); + } + catch (IOException iOException) { + iOException.printStackTrace(); + } + if (n3 == 0) { + string3 = string3 + "TAG_" + n + "=" + hashMap2.get(string4).substring(0, 2) + "\n"; + string3 = string3 + "SUBTYPE_" + n + "=" + hashMap3.get(string2).size() + "\n"; + ++n3; + } + string3 = string3 + "TYPE_" + n + "_" + n2 + "=" + string4 + "\n"; + int n5 = Integer.parseInt(properties.getProperty("ADDRESSES")); + int n6 = Integer.parseInt(properties.getProperty("ZBADDRESSES")); + string3 = string3 + "TYPE_" + n + "_" + n2 + "_NUM_OF_ZBADDRESSES=" + n6 + "\n"; + if (n6 != 0) { + string3 = string3 + "TYPE_" + n + "_" + n2 + "_ZBADDRESS_FIELDS="; + n3 = 0; + for (n4 = 0; n4 < n6; ++n4) { + string5 = new String(); + for (String string7 : hashMap.get(string4).get(2)) { + if (n3 == 0) { + string3 = string3 + string7 + " "; + } + string5 = string5 + "ZB" + string7 + "_" + n + "_" + n2 + "_ZBADD_" + n4 + "=" + properties.getProperty(new StringBuilder().append(string7).append("_").append(n4 + 1).toString()) + "\n"; + } + if (n3 == 0) { + string3 = string3 + "\n"; + } + string3 = string3 + string5; + ++n3; + } + } + string3 = string3 + "TYPE_" + n + "_" + n2 + "_NUM_OF_ADDRESSES=" + n5 + "\n"; + if (n5 != 0) { + string3 = string3 + "TYPE_" + n + "_" + n2 + "_ADDRESS_FIELDS="; + n3 = 0; + for (n4 = 0; n4 < n5; ++n4) { + string5 = new String(); + for (String string7 : hashMap.get(string4).get(0)) { + if (n3 == 0) { + string3 = string3 + string7 + " "; + } + string5 = string5 + string7 + "_" + n + "_" + n2 + "_ADD_" + n4 + "=" + properties.getProperty(new StringBuilder().append(string7).append("_").append(n4 + 1).toString()) + "\n"; + } + if (n3 == 0) { + string3 = string3 + "\n"; + } + string3 = string3 + string5; + ++n3; + } + } + n4 = Integer.parseInt(properties.getProperty("DEVICEINFO")); + string3 = string3 + "TYPE_" + n + "_" + n2 + "_NUM_OF_DEVICE_INFO=" + n4 + "\n"; + if (n4 > 0) { + string5 = new String(); + n3 = 0; + for (String string7 : hashMap.get(string4).get(1)) { + if (string7.length() > 0) { + if (n3 == 0) { + string3 = string3 + "TYPE_" + n + "_" + n2 + "_DEVICE_FIELDS="; + } + string3 = string3 + string7 + " "; + string5 = string5 + string7 + "_" + n + "_" + n2 + "=USER\n"; + } + ++n3; + } + string3 = string3 + "\n"; + ++n3; + string3 = string3 + string5; + } + ++n2; + } + string3 = string3 + "\n"; + ++n; + IoTInstaller.appendToFile((String)string3, (String)string); + } + } + + void getIoTDeviceAddressInfoAndOutput(HashMap> hashMap) { + int n; + Table table = new Table("IoTDeviceAddress", false); + String[][] arrstring = table.getGeneralDBTable(); + String string = new String(); + string = string + "START\n"; + string = string + "IoTDeviceAddress\n"; + for (n = 0; n < arrstring.length; ++n) { + string = string + arrstring[n][1] + " " + arrstring[n][0] + "\n"; + } + string = string + "END\n\n"; + IoTInstaller.overrideFileWrite((String)string, (String)"IoTDeviceAddress.config"); + table = new Table("IoTZigbeeAddress", false); + arrstring = table.getGeneralDBTable(); + string = new String(); + string = string + "START\n"; + string = string + "IoTZigbeeAddress\n"; + for (n = 0; n < arrstring.length; ++n) { + string = string + arrstring[n][1] + " " + arrstring[n][0] + "\n"; + } + string = string + "END\n\n"; + IoTInstaller.appendToFile((String)string, (String)"IoTDeviceAddress.config"); + } + + public void getInformationFromDatabase(String string) { + Scanner scanner; + System.out.println("Initializing -get..."); + ArrayList arrayList = new ArrayList(); + ArrayList arrayList2 = new ArrayList(); + String string2 = new String("AddressInformation.config"); + String string3 = new String("SetList.config"); + String string4 = new String("DatabaseInformation.config"); + String string5 = new String("IDSubtypeInformation.config"); + HashMap> hashMap = new HashMap>(); + HashMap>> hashMap2 = new HashMap>>(); + HashMap hashMap3 = new HashMap(); + try { + File file = new File(string); + InputStream IS = new FileInputStream(file); + scanner = new Scanner(IS); + while (scanner.hasNext()) { + arrayList.add(scanner.next()); + } + } + catch (FileNotFoundException fileNotFoundException) { + System.out.print("File Error: "); + fileNotFoundException.printStackTrace(); + } + + for (int i = 0; i < arrayList.size(); ++i) { + HashSet object = new HashSet(); + Table tbl = new Table(arrayList.get(i), false); + String[][] arrstring = tbl.getGeneralDBTable(); + arrayList2.add(arrstring); + object.clear(); + for (int j = 0; j < arrstring.length; ++j) { + object.add(arrstring[j][1]); + } + hashMap.putIfAbsent(arrayList.get(i), object); + HashMap hashMap4 = IoTInstaller.getNumAddresses(object); + HashMap hashMap5 = IoTInstaller.getZBAddresses(object); + for(String x : object){ + ArrayList> arrayList3 = new ArrayList>(); + for (int j = 0; j < arrstring.length; ++j) { + if (!x.equals(arrstring[j][1])) continue; + hashMap3.putIfAbsent(x, arrstring[j][0]); + } + arrayList3.clear(); + if (hashMap4.get(x) > 1) { + tbl.setTableName(x + "Add1" + hashMap3.get(x)); + if (tbl.isTableExisting()) { + tbl = new Table(x + "Add1" + hashMap3.get(x), false); + arrayList3.add(tbl.getColumnNames()); + } else { + arrayList3.add(new HashSet()); + } + hashMap2.putIfAbsent(x, arrayList3); + } else { + tbl.setTableName(x + "Add" + hashMap3.get(x)); + if (tbl.isTableExisting()) { + tbl = new Table(x + "Add" + hashMap3.get(x), false); + arrayList3.add(tbl.getColumnNames()); + } else { + arrayList3.add(new HashSet()); + } + } + tbl.setTableName(x + hashMap3.get(x)); + + if (tbl.isTableExisting()) { + tbl = new Table(x + hashMap3.get(x), false); + arrayList3.add(tbl.getColumnNames()); + } else { + arrayList3.add(new HashSet()); + } + if (hashMap5.get(x) != 0) { + tbl.setTableName(x + "ZBAdd" + hashMap3.get(x)); + if (tbl.isTableExisting()) { + tbl = new Table(x + "ZBAdd" + hashMap3.get(x), false); + arrayList3.add(tbl.getColumnNames()); + } else { + arrayList3.add(new HashSet()); + } + } else { + arrayList3.add(new HashSet()); + } + hashMap2.putIfAbsent(x, arrayList3); + } + } + IoTInstaller.getAddressInfo((String)string2, hashMap2, hashMap3, hashMap); + this.getIoTDeviceAddressInfoAndOutput(hashMap); + try { + IoTInstaller.actualDeviceInfoWrite(arrayList2, (String)string5); + IoTInstaller.configSetWrite((String)string3, arrayList2, hashMap); + } + catch (IOException iOException) { + iOException.printStackTrace(); + } + System.out.println("Done Executing -get"); + } + + public void addNewType(String string) { + System.out.println("Initializing -appendType..."); + IoTInstaller.appendToFile((String)string, (String)"AllDevices.config"); + System.out.println("Done Executing -appendType"); + } + + public void updateSetList(String string) { + Scanner scanner; + Table tbl; + System.out.println("Updating SetList..."); + String string2 = new String("SetList.config"); + ArrayList arrayList = new ArrayList(); + ArrayList arrayList2 = new ArrayList(); + HashMap> hashMap = new HashMap>(); + try { + File file = new File(string); + InputStream IS = new FileInputStream(file); + scanner = new Scanner(IS); + while (scanner.hasNext()) { + arrayList.add(scanner.next()); + } + } + catch (FileNotFoundException fileNotFoundException) { + System.out.print("File Error: "); + fileNotFoundException.printStackTrace(); + } + for (int i = 0; i < arrayList.size(); ++i) { + HashSet object = new HashSet(); + tbl = new Table(arrayList.get(i), false); + String[][] arrstring = tbl.getGeneralDBTable(); + arrayList2.add(arrstring); + object.clear(); + for (int j = 0; j < arrstring.length; ++j) { + object.add(arrstring[j][1]); + } + hashMap.putIfAbsent(arrayList.get(i), object); + } + + try { + IoTInstaller.configSetWrite((String)string2, arrayList2, hashMap); + } + catch (IOException iOException) { + iOException.printStackTrace(); + } + System.out.println("Done Updating Setlist!"); + } + + public void updateIoTDeviceAddress(String string) { + Scanner scanner; + Table tbl; + System.out.println("Updating IoTDeviceAddress..."); + String string2 = new String("IoTDeviceAddress.config"); + ArrayList arrayList = new ArrayList(); // Holds types + ArrayList arrayList2 = new ArrayList(); + HashMap> hashMap = new HashMap>(); + try { + File file = new File(string); + InputStream IS = new FileInputStream(file); + scanner = new Scanner(IS); + while (scanner.hasNext()) { + arrayList.add(scanner.next()); + } + } + catch (FileNotFoundException fileNotFoundException) { + System.out.print("File Error: "); + fileNotFoundException.printStackTrace(); + } + for (int i = 0; i < arrayList.size(); ++i) { + HashSet object = new HashSet(); + tbl = new Table(arrayList.get(i), false); + String[][] arrstring = tbl.getGeneralDBTable(); + arrayList2.add(arrstring); + object.clear(); + for (int j = 0; j < arrstring.length; ++j) { + object.add(arrstring[j][1]); + } + hashMap.putIfAbsent(arrayList.get(i), object); + } + + this.getIoTDeviceAddressInfoAndOutput(hashMap); + + System.out.println("Done Updating IoTDeviceAddress!"); + } + + public void updateIDSub(String string) { + FileInputStream fileInputStream; + String[][] arrstring; + Scanner scanner; + Table tbl; + System.out.println("Updating IDSubtype.config..."); + ArrayList arrayList = new ArrayList(); + ArrayList arrayList2 = new ArrayList(); + try { + File file = new File(string); + InputStream IS = new FileInputStream(file); + scanner = new Scanner(IS); + while (scanner.hasNext()) { + arrayList.add(scanner.next()); + } + } + catch (FileNotFoundException fileNotFoundException) { + System.out.print("File Error: "); + fileNotFoundException.printStackTrace(); + } + for (int i = 0; i < arrayList.size(); ++i) { + tbl = new Table((String)arrayList.get(i), false); + arrstring = tbl.getGeneralDBTable(); + arrayList2.add(arrstring); + } + + try { + IoTInstaller.actualDeviceInfoWrite(arrayList2, (String)"IDSubtypeInformation.config"); + } + catch (IOException iOException) { + iOException.printStackTrace(); + } + System.out.println("Done Updating!"); + } + + // End of Kevin's code /** * Main method that accepts inputs for installation @@ -811,15 +1307,29 @@ public final class IoTInstaller { } else if (args[0].equals(STR_HELP_CMD)) { iotinst.helpMessages(); + } else if (args[0].equals("-get")) { + iotinst.getInformationFromDatabase("../SupportedDevices/"+args[1]); + + } else if (args[0].equals("-appendType")) { + iotinst.addNewType("../SupportedDevices/"+args[1]); + + } else if (args[0].equals("-updateIoTDeviceAddress")) { + iotinst.updateIoTDeviceAddress("../SupportedDevices/"+args[1]); + + } else if (args[0].equals("-updateSetList")) { + iotinst.updateSetList("../SupportedDevices/"+args[1]); + + } else if (args[0].equals("-updateIDSubtype")) { + iotinst.updateIDSub("../SupportedDevices/"+args[1]); + } else { System.out.println("IoTInstaller: ERROR: Wrong input parameters!"); iotinst.helpMessages(); + } } else { System.out.println("IoTInstaller: ERROR: No input parameters detected!"); iotinst.helpMessages(); } } - - } diff --git a/iotjava/iotinstaller/Table.java b/iotjava/iotinstaller/Table.java index c3fa5ab..3b292b7 100644 --- a/iotjava/iotinstaller/Table.java +++ b/iotjava/iotinstaller/Table.java @@ -9,6 +9,7 @@ import java.io.*; import java.sql.*; import java.util.Map; import java.util.HashMap; +import java.util.HashSet; import java.util.Scanner; import java.util.Properties; @@ -324,6 +325,34 @@ public class Table { return arrTbl; } + + /** + * A method to get the column names of a specified table + * + * @return HashSet : holds all column information for specific tables + * @author Kevin Truong + */ + public HashSet getColumnNames(){ + int iCols = getGeneralNumOfCols(); + String[] arrTbleColNames = new String[iCols]; + HashSet arrTblColNames = new HashSet(); + try{ + rs.beforeFirst(); + rsmd = rs.getMetaData(); + while(rs.next()){ + for(int i = 0;i < iCols; i++){ + arrTblColNames.add(rsmd.getColumnName(i+1)); + } + } + rs.beforeFirst(); + } catch (SQLException ex){ + System.out.println("Table: Exception: "); + ex.printStackTrace(); + } + + return arrTblColNames; + } + /** * A method to close statement manually -- 2.34.1