개요
프로그램 오브젝트를 사용하여 히스토리를 CSV로 변환하는 방법.AX Version(getProgram() function used)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | public void onStart() throws Exception { // start up code here } // This runs under AX // This example demonstrates executing a program object task on a separate // thread. Since actions are run on the system thread by default, any long- // running task should be completed on a separate thread in order to avoid // watchdog timeout problems. // // In this example, the execute method creates a new thread, and starts it. // Since program objects implement Runnable, you can implement the run() method // and allow the program object to be the target of the new thread. // // This technique is good when the program object will not be executed at a high // rate. Since a new thread is created for each execution, it is possible this // approach could consume to many system resources. // public void onExecute() throws Exception { if (running) return; // only allow one execution at a time // Create a new thread, with the starting point set as the current program // object. Thread thread = new Thread(this, getProgram().getName()); // Start the thread thread.start(); } public void onCancel() throws Exception { running = false; } public void run() { // This task will take a long time try { BComponent base = getProgram(); running = true; BOrdList exclude = getHistoriesToExclude(); progress("Started exporting history records"); BHistoryService service = (BHistoryService)Sys.getService(BHistoryService.TYPE); BHistoryDatabase db = service.getDatabase(); if (db == null) { progress("Failed - Could not find history database"); return; } int count = 0; BHistoryDevice[] devices = db.listDevices(); for (int i = 0; i < devices.length; i++) { if (!running) { // Check if canceled progress("Operation canceled."); return; } if (contains(devices[i], exclude)) continue; // skip if in excluded list BIHistory[] histories = db.listHistories(devices[i]); //add a sub folder for storing the histories if necessary File basePath = Sys.getStationHome(); File ePath = null; File stationFolder = null; if(new File(basePath, "historyExports").exists() != true) { ePath = new File(basePath,"historyExports"); ePath.mkdir(); } else { ePath = new File(basePath,"historyExports"); } String stationName = ""; String historyName = ""; for (int j = 0; j < histories.length; j++) { if (!running) { // Check if canceled progress("Operation canceled."); return; } if (contains(histories[j], exclude)) continue; // skip if in excluded list progress(" Exporting records for "+histories[j].getId()); stationName = histories[j].getId().getDeviceName(); historyName = histories[j].getId().getHistoryName(); if(new File(ePath,stationName).exists() != true) { //need to make the folder stationFolder = new File(ePath,stationName); stationFolder.mkdir(); } else { stationFolder = new File(ePath,stationName); } //PrintStream out = new PrintStream(new FileOutputStream(baseEPath + stationName + "" + historyName + ".csv", true)); PrintStream out = new PrintStream(new FileOutputStream(new File(stationFolder,historyName + ".csv"), true)); //Changed below to only output the timestamp and value. OrdTarget table = BOrd.make("history:" + histories[j].getId().toString() + "|bql:select timestamp, value").resolve(base); out.println(exportToCsv(table)); out.close(); count++; } } progress("Completed exporting "+count+" histories "); } catch(Exception e) { progress("Error during export of histories " + e); } finally { running = false; } } /** * Run the CSV exporter against the specified table to build an * in memory representation of the table as a CSV file. */ private String exportToCsv(OrdTarget table) throws Exception { // create instance of ITableToCsv exporter BExporter exporter = (BExporter)Sys.getType("file:ITableToCsv").getInstance(); // run the CSV exporter to export to memory based byte array ByteArrayOutputStream out = new ByteArrayOutputStream(); ExportOp op = ExportOp.make(table, out); exporter.export(op); // return as string (this works because we String will use the default // encoding, which should match encoding ITableToCsv exporter used to // create a PrintWriter from a raw OutputStream) return new String(out.toByteArray()); } public static boolean contains(BINavNode obj, BOrdList list) { BOrd navOrd = obj.getNavOrd().relativizeToSession(); for (int i = 0; i < list.size(); i++) { if (navOrd.equals(list.get(i).relativizeToSession())) return true; } return false; } public void progress(String progress) { System.out.println(progress); setStatus(progress); } public static boolean running = false; //String basePath = "d:\\niagara\\Niagara-3.5.25\\stations" + (Sys.getStation().getStationName().toString()) + ""; //String baseEPath = "d:\\niagara\\Niagara-3.5.25\\stations" + (Sys.getStation().getStationName().toString()) + "\\historyExport"; public void onStop() throws Exception { // shutdown code here } | cs |
N4 version (getComponent() function used)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | public void onStart() throws Exception { // start up code here } // This example runs under N4 // This example demonstrates executing a program object task on a separate // thread. Since actions are run on the system thread by default, any long- // running task should be completed on a separate thread in order to avoid // watchdog timeout problems. // // In this example, the execute method creates a new thread, and starts it. // Since program objects implement Runnable, you can implement the run() method // and allow the program object to be the target of the new thread. // // This technique is good when the program object will not be executed at a high // rate. Since a new thread is created for each execution, it is possible this // approach could consume to many system resources. // public void onExecute() throws Exception { if (running) return; // only allow one execution at a time // Create a new thread, with the starting point set as the current program // object. Thread thread = new Thread(this, getComponent().getName()); // Start the thread thread.start(); } public void onCancel() throws Exception { running = false; } public void run() { // This task will take a long time try { BComponent base = getComponent(); running = true; BOrdList exclude = getHistoriesToExclude(); progress("Started exporting history records"); BHistoryService service = (BHistoryService)Sys.getService(BHistoryService.TYPE); BHistoryDatabase db = service.getDatabase(); if (db == null) { progress("Failed - Could not find history database"); return; } int count = 0; BHistoryDevice[] devices = db.listDevices(); for (int i = 0; i < devices.length; i++) { if (!running) { // Check if canceled progress("Operation canceled."); return; } if (contains(devices[i], exclude)) continue; // skip if in excluded list BIHistory[] histories = db.listHistories(devices[i]); //add a sub folder for storing the histories if necessary File basePath = Sys.getStationHome(); File ePath = null; File stationFolder = null; if(new File(basePath, "historyExports").exists() != true) { ePath = new File(basePath,"historyExports"); ePath.mkdir(); } else { ePath = new File(basePath,"historyExports"); } String stationName = ""; String historyName = ""; for (int j = 0; j < histories.length; j++) { if (!running) { // Check if canceled progress("Operation canceled."); return; } if (contains(histories[j], exclude)) continue; // skip if in excluded list progress(" Exporting records for "+histories[j].getId()); stationName = histories[j].getId().getDeviceName(); historyName = histories[j].getId().getHistoryName(); if(new File(ePath,stationName).exists() != true) { //need to make the folder stationFolder = new File(ePath,stationName); stationFolder.mkdir(); } else { stationFolder = new File(ePath,stationName); } //PrintStream out = new PrintStream(new FileOutputStream(baseEPath + stationName + "" + historyName + ".csv", true)); PrintStream out = new PrintStream(new FileOutputStream(new File(stationFolder,historyName + ".csv"), true)); //Changed below to only output the timestamp and value. OrdTarget table = BOrd.make("history:" + histories[j].getId().toString() + "|bql:select timestamp, value").resolve(base); out.println(exportToCsv(table)); out.close(); count++; } } progress("Completed exporting "+count+" histories "); } catch(Exception e) { progress("Error during export of histories " + e); } finally { running = false; } } /** * Run the CSV exporter against the specified table to build an * in memory representation of the table as a CSV file. */ private String exportToCsv(OrdTarget table) throws Exception { // create instance of ITableToCsv exporter BExporter exporter = (BExporter)Sys.getType("file:ITableToCsv").getInstance(); // run the CSV exporter to export to memory based byte array ByteArrayOutputStream out = new ByteArrayOutputStream(); ExportOp op = ExportOp.make(table, out); exporter.export(op); // return as string (this works because we String will use the default // encoding, which should match encoding ITableToCsv exporter used to // create a PrintWriter from a raw OutputStream) return new String(out.toByteArray()); } public static boolean contains(BINavNode obj, BOrdList list) { BOrd navOrd = obj.getNavOrd().relativizeToSession(); for (int i = 0; i < list.size(); i++) { if (navOrd.equals(list.get(i).relativizeToSession())) return true; } return false; } public void progress(String progress) { System.out.println(progress); setStatus(progress); } public static boolean running = false; //String basePath = "d:\\niagara\\Niagara-3.5.25\\stations" + (Sys.getStation().getStationName().toString()) + ""; //String baseEPath = "d:\\niagara\\Niagara-3.5.25\\stations" + (Sys.getStation().getStationName().toString()) + "\\historyExport"; public void onStop() throws Exception { // shutdown code here } | cs |
참조1 : https://buildingsys.net/export-niagara-histories/
참조2 : https://youtu.be/Gpr9Mi_glRI
댓글 없음:
댓글 쓰기