More features for Android app access to device installation, etc.
[iot2.git] / iotjava / iotinstaller / IoTInstaller.java
index e281c9e9ec111a7d2f517708840923e2e56a0570..a9e5d889a29ee50f1194ac7e95c2c9ee60216494 100644 (file)
@@ -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<kctruon1@uci.edu>  
+     * */
+
+    public static void formatWrite(String string, HashMap<String, ArrayList<HashSet<String>>> hashMap, HashMap<String, HashSet<String>> hashMap2, HashMap<String, String> 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<String[][]> 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<String[][]> arrayList, HashMap<String, HashSet<String>> 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<String, Integer> getNumAddresses(HashSet<String> hashSet) {
+        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
+        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<String, Integer> getZBAddresses(HashSet<String> hashSet) {
+        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
+        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<String, ArrayList<HashSet<String>>> 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<String, ArrayList<HashSet<String>>> hashMap, HashMap<String, String> hashMap2, HashMap<String, HashSet<String>> 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<String, HashSet<String>> 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<String> arrayList = new ArrayList<String>();
+        ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
+        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<String, HashSet<String>> hashMap = new HashMap<String, HashSet<String>>();
+        HashMap<String, ArrayList<HashSet<String>>> hashMap2 = new HashMap<String, ArrayList<HashSet<String>>>();
+        HashMap<String, String> hashMap3 = new HashMap<String, String>();
+        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<String> object = new HashSet<String>();
+            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<String, Integer> hashMap4 = IoTInstaller.getNumAddresses(object);
+            HashMap<String, Integer> hashMap5 = IoTInstaller.getZBAddresses(object);
+           for(String x : object){
+                ArrayList<HashSet<String>> arrayList3 = new ArrayList<HashSet<String>>();
+                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<String>());
+                    }
+                    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<String>());
+                    }
+                }
+                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<String>());
+                }
+                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<String>());
+                    }
+                } else {
+                    arrayList3.add(new HashSet<String>());
+                }
+                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<String> arrayList = new ArrayList<String>();
+        ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
+        HashMap<String, HashSet<String>> hashMap = new HashMap<String, HashSet<String>>();
+        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<String> object = new HashSet<String>();
+            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<String> arrayList = new ArrayList<String>(); // Holds types
+        ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
+        HashMap<String, HashSet<String>> hashMap = new HashMap<String, HashSet<String>>();
+        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<String> object = new HashSet<String>();
+            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<String> arrayList = new ArrayList<String>();
+        ArrayList<String[][]> arrayList2 = new ArrayList<String[][]>();
+        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();
                }
        }
-
-
 }